Computer Science Canada Help Needed!!! |
Author: | Dude_man_47 [ Thu Dec 01, 2005 11:44 pm ] | ||
Post subject: | Help Needed!!! | ||
I am making a program and I want to make boxes turn red when i click on them. I have the code for one box, but i was wondering if there was an easier way then writing 20 if statements. Any help would be greatly apreciated. ^_^ Here is my code for one box.
|
Author: | Cervantes [ Fri Dec 02, 2005 8:44 am ] |
Post subject: | |
Welcome to CompSci.ca Please, use a descriptive title. Link! Now, as to your question. You'll probably want to look into creating an array of boxes. You would likely also want to create a function to check whether you clicked on a box. For tutorials on these topics, check [Turing Tutorials]. There are also links in the Turing Walkthrough. |
Author: | Dude_man_47 [ Fri Dec 02, 2005 9:10 am ] | ||
Post subject: | |||
Sorry about the title. I looked into arrays but they dont seem to be working. I tried the tutorial but the code doesn't work. It says that variable has no value.
|
Author: | Tony [ Fri Dec 02, 2005 9:47 am ] | ||
Post subject: | |||
well after initializing
what's the value of grid at (1,1)? or any other location for that matter? Well that hasn't been specified, so of course it's empty. The code is syntatically correct. |
Author: | do_pete [ Fri Dec 02, 2005 9:48 am ] | ||
Post subject: | |||
It says it does't have a value becuase it doesn't have a value; you haven't assigned it anything. Try something like this:
|
Author: | Cervantes [ Fri Dec 02, 2005 10:42 am ] | ||||
Post subject: | |||||
do_pete wrote:
That's a very inefficient way to assign values. If there's any pattern (and there usually is), try using for loops:
If there is no pattern, read the values in from a file. |
Author: | Dude_man_47 [ Fri Dec 02, 2005 11:08 am ] | ||
Post subject: | |||
Cool, I've tryed using a loop in various ways to state Buttons and none seem to work properly. And if I state them all then its too much code. It starts to look like this:
I want it to be like that but fill the entire screen using a loop. I'll continue to experiment but if anyone sees something that will work i'll be very thankfull. |
Author: | Dude_man_47 [ Fri Dec 02, 2005 11:42 am ] | ||
Post subject: | |||
I found a bit of a pattern but it is still too long and it takes forever for the comp to calculate. And it uses like 20 for loops.
There has to be an easier/shorte way!!! |
Author: | Tony [ Fri Dec 02, 2005 11:48 am ] | ||
Post subject: | |||
Dude_man_47 wrote: I found a bit of a pattern but it is still too long and it takes forever for the comp to calculate. And it uses like 20 for loops.
That's because you're redeclearing
10^10 times. That's a BIG number of times. You've got to end your forloops.. really |
Author: | do_pete [ Fri Dec 02, 2005 11:52 am ] | ||
Post subject: | |||
Youve got more than 80 lines there, wouldn't it be easier to declare the individual coordinates? Anyway i found a solution:
|
Author: | Dude_man_47 [ Fri Dec 02, 2005 1:08 pm ] | ||
Post subject: | |||
Tanks, i did some fine tuning to it and found a pattern. I ran into a problem though. Buttons is an int. my code goes
There's a for loop called i and if i mod 10 is 10 then i divid it by ten, say i is 20, 20/10 = 2. 2+ 1 = 3. 3 x 50 = 150. It says it is of the wrong type even though it doesnt get a decimal. the eror is at the *50. it says wrong type. What can i do to fix this? |
Author: | do_pete [ Fri Dec 02, 2005 1:15 pm ] |
Post subject: | |
you can't use decimals when assigning a value to an int unless you use round() and you have to replace "/" with div |
Author: | Tony [ Fri Dec 02, 2005 1:26 pm ] |
Post subject: | |
that's right. For example 4/2 returns 2.0 that is of the type real, not an int you're looking for. div drops that .0, so 5 div 2 will return 2 of type int |
Author: | Dude_man_47 [ Fri Dec 02, 2005 1:33 pm ] |
Post subject: | |
Thanks, how do you round to 10. like if you have 19, round to 20, or 14 round to 10? |
Author: | do_pete [ Fri Dec 02, 2005 1:45 pm ] | ||
Post subject: | |||
try this:
|
Author: | Dude_man_47 [ Fri Dec 02, 2005 3:28 pm ] | ||
Post subject: | |||
Thanks for the help now it works!!!! Check it out.
|
Author: | Mr. T [ Fri Dec 02, 2005 8:51 pm ] |
Post subject: | Alex's Opinion |
Dude_man_47 wrote: Thanks, how do you round to 10. like if you have 19, round to 20, or 14 round to 10?
Look up ceil and floor. |
Author: | Cervantes [ Fri Dec 02, 2005 9:48 pm ] |
Post subject: | Re: Alex's Opinion |
Pwned wrote: Dude_man_47 wrote: Thanks, how do you round to 10. like if you have 19, round to 20, or 14 round to 10?
Look up ceil and floor. Using ceil() and floor() to round will not work that way. Judging by that quote, round(), and only round(), will suit his needs. floor() would change 19 to 10, not 20. (This is all assuming some division and then later multiplication occured.) |
Author: | Mr. T [ Sat Dec 03, 2005 12:02 am ] |
Post subject: | Re: Alex's Opinion |
Cervantes wrote: Pwned wrote: Dude_man_47 wrote: Thanks, how do you round to 10. like if you have 19, round to 20, or 14 round to 10?
Look up ceil and floor. Using ceil() and floor() to round will not work that way. Judging by that quote, round(), and only round(), will suit his needs. floor() would change 19 to 10, not 20. (This is all assuming some division and then later multiplication occured.) Ok, I must have misread what he was trying to do. |
Author: | Dude_man_47 [ Fri Dec 09, 2005 12:20 pm ] |
Post subject: | |
setscreen ("graphics:max;max,nobuttonbar,offscreenonly") var mx, my, mb : int var mx1, my1 : int := 0 var com : string := "a" var yes, yes1 : boolean := false Draw.FillBox (0, 0, maxx, maxy, white) drawline (0, 50, maxx, 50, black) setscreen ("graphics:450;450,nobuttonbar") var Buttons : array 1 .. 10, 1 .. 4 of int for i : 1 .. 10 if i = 1 then Buttons (i, 1) := 0 Buttons (i, 2) := 0 Buttons (i, 3) := 50 Buttons (i, 4) := 50 elsif i < 11 and i not= 1 then Buttons (i, 1) := (i - 1) * 50 Buttons (i, 2) := 0 Buttons (i, 3) := i * 50 Buttons (i, 4) := 50 end if end for function CheckClick (x1, y1, x2, y2 : int) : boolean var MouseX, MouseY, MouseButton : int Mouse.Where (MouseX, MouseY, MouseButton) if MouseButton not= 0 and MouseX > x1 and MouseX < x2 and MouseY > y1 and MouseY < y2 then result true else result false end if end CheckClick proc bucket loop mousewhere (mx1, my1, mb) if mb = 1 then var back := Pic.New (0, 0, maxx, maxy) loop mousewhere (mx, my, mb) for i : 1 .. 5 if CheckClick (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4)) then if i = 1 then com := "line" elsif i = 2 then com := "circle" elsif i = 3 then com := "box" elsif i = 4 then com := "exit" elsif i = 5 then com := "eraser" elsif i := 6 then com := "bucket" end if yes := true else yes := false end if end for if mb not= 0 and my < 52 then yes := true end if exit when yes = true exit when mb = 0 Draw.Fill (mx, my, red, not= red) View.Update end loop Pic.Free (back) end if exit when com not= "bucket" %------------------------------------------------------- proc eraser loop mousewhere (mx1, my1, mb) if mb = 1 then var back := Pic.New (0, 0, maxx, maxy) loop mousewhere (mx, my, mb) for i : 1 .. 5 if CheckClick (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4)) then if i = 1 then com := "line" elsif i = 2 then com := "circle" elsif i = 3 then com := "box" elsif i = 4 then com := "exit" elsif i = 5 then com := "eraser" end if yes := true else yes := false end if end for if mb not= 0 and my < 52 then yes := true end if exit when yes = true exit when mb = 0 Draw.FillBox (mx + 20, my + 20, mx, my, white) View.Update end loop Pic.Free (back) end if exit when com not= "eraser" end loop end eraser %------------------------------------------------------------------------------------- proc line loop mousewhere (mx1, my1, mb) if mb = 1 then var back := Pic.New (0, 0, maxx, maxy) loop mousewhere (mx, my, mb) for i : 1 .. 5 if CheckClick (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4)) then if i = 1 then com := "line" elsif i = 2 then com := "circle" elsif i = 3 then com := "box" elsif i = 4 then com := "exit" elsif i = 5 then com := "eraser" end if yes := true else yes := false end if end for if mb not= 0 and my < 52 then yes := true end if exit when yes = true exit when mb = 0 Pic.Draw (back, 0, 0, picCopy) Draw.Line (mx1, my1, mx, my, black) View.Update end loop Pic.Free (back) end if exit when com not= "line" end loop end line %----------------------------------------------------------- proc circle loop mousewhere (mx1, my1, mb) if mb = 1 then var back := Pic.New (0, 0, maxx, maxy) loop mousewhere (mx, my, mb) for i : 1 .. 5 if CheckClick (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4)) then if i = 1 then com := "line" elsif i = 2 then com := "circle" elsif i = 3 then com := "box" elsif i = 4 then com := "exit" elsif i = 5 then com := "eraser" end if yes := true else yes := false end if end for if mb not= 0 and my < 52 then yes := true end if if mb not= 0 and my1 < 52 then yes := true end if exit when yes = true exit when mb = 0 Pic.Draw (back, 0, 0, picCopy) Draw.Oval ((mx - mx1) div 2 + mx1, (my1 - my) div 2 + my, (mx - mx1) div 2, (my1 - my) div 2, black) View.Update end loop Pic.Free (back) end if exit when com not= "circle" end loop end circle %-------------------------------------------------------------------------- proc box loop mousewhere (mx1, my1, mb) if mb = 1 then var back := Pic.New (0, 0, maxx, maxy) loop mousewhere (mx, my, mb) for i : 1 .. 5 if CheckClick (Buttons (i, 1), Buttons (i, 2), Buttons (i, 3), Buttons (i, 4)) then if i = 1 then com := "line" elsif i = 2 then com := "circle" elsif i = 3 then com := "box" elsif i = 4 then com := "exit" elsif i = 5 then com := "eraser" end if yes := true else yes := false end if end for if mb not= 0 and my < 52 then yes := true end if exit when yes = true exit when mb = 0 Pic.Draw (back, 0, 0, picCopy) Draw.Box (mx1, my1, mx, my, black) View.Update end loop Pic.Free (back) end if exit when com not= "box" end loop end box %---------------------------------------------------------------------------- /* loop loop if CheckClick (Buttons (1, 1), Buttons (1, 2), Buttons (1, 3), Buttons (1, 4)) then com := "line" else Draw.FillBox (Buttons (1, 1), Buttons (1, 2), Buttons (1, 3), Buttons (1, 4), red) drawline (0, 50, maxx, 50, black) end if if CheckClick (Buttons (2, 1), Buttons (2, 2), Buttons (2, 3), Buttons (2, 4)) then com := "circle" else Draw.FillBox (Buttons (2, 1), Buttons (2, 2), Buttons (2, 3), Buttons (2, 4), green) end if if CheckClick (Buttons (3, 1), Buttons (3, 2), Buttons (3, 3), Buttons (3, 4)) then com := "box" else Draw.FillBox (Buttons (3, 1), Buttons (3, 2), Buttons (3, 3), Buttons (3, 4), blue) end if if CheckClick (Buttons (5, 1), Buttons (5, 2), Buttons (5, 3), Buttons (5, 4)) then com := "eraser" else Draw.FillBox (Buttons (5, 1), Buttons (5, 2), Buttons (5, 3), Buttons (5, 4), purple) end if if CheckClick (Buttons (4, 1), Buttons (4, 2), Buttons (4, 3), Buttons (4, 4)) then com := "exit" else Draw.FillBox (Buttons (4, 1), Buttons (4, 2), Buttons (4, 3), Buttons (4, 4), black) end if exit when com not= "a" end loop loop if com = "line" then line elsif com = "circle" then circle elsif com = "box" then box exit when com not= "box" elsif com = "eraser" then eraser exit when com not= "eraser" elsif com = "exit" then yes1 := true end if exit when yes1 = true end loop exit when yes1 = true end loop*/ loop if CheckClick (Buttons (1, 1), Buttons (1, 2), Buttons (1, 3), Buttons (1, 4)) then com := "line" else Draw.FillBox (Buttons (1, 1), Buttons (1, 2), Buttons (1, 3), Buttons (1, 4), red) drawline (0, 50, maxx, 50, black) end if if CheckClick (Buttons (2, 1), Buttons (2, 2), Buttons (2, 3), Buttons (2, 4)) then com := "circle" else Draw.FillBox (Buttons (2, 1), Buttons (2, 2), Buttons (2, 3), Buttons (2, 4), green) end if if CheckClick (Buttons (6, 1), Buttons (6, 2), Buttons (6, 3), Buttons (6, 4)) then com := "bucket" else Draw.FillBox (Buttons (2, 1), Buttons (2, 2), Buttons (2, 3), Buttons (2, 4), yellow) end if if CheckClick (Buttons (3, 1), Buttons (3, 2), Buttons (3, 3), Buttons (3, 4)) then com := "box" else Draw.FillBox (Buttons (3, 1), Buttons (3, 2), Buttons (3, 3), Buttons (3, 4), blue) end if if CheckClick (Buttons (5, 1), Buttons (5, 2), Buttons (5, 3), Buttons (5, 4)) then com := "eraser" else Draw.FillBox (Buttons (5, 1), Buttons (5, 2), Buttons (5, 3), Buttons (5, 4), purple) end if if CheckClick (Buttons (4, 1), Buttons (4, 2), Buttons (4, 3), Buttons (4, 4)) then com := "exit" else Draw.FillBox (Buttons (4, 1), Buttons (4, 2), Buttons (4, 3), Buttons (4, 4), black) end if %if click on colours then %drawColour := whatdotcolour(mx,my) mousewhere (mx, my, mb) View.ClipSet (0, 50, maxx, maxy) if my > 51 then if com = "line" then line elsif com = "circle" then circle elsif com = "box" then box elsif com = "eraser" then eraser elsif com = "exit" then yes1 := true elsif com = "bucket" bucket end if end if exit when yes1 = true end loop end loop |