Posted: Fri Mar 18, 2005 11:27 pm Post subject: table of contents
hey, I read your tutorial at http://www.compsci.ca/v2/viewtopic.php?t=114#77826 where you move your arrow keys to control the circle. I was wondering how I could apply that in a program where there is a table of contents, and the user moves the arrow keys to an option, like high scores, games, etc. and when the user presses enter when the circle is on top of the "High score" part of the screen, it goes to that screen. Well, I'm actualyl going to use a maple leaf, not a circle, but anways lol. Or is there an easier way to do this? Because I do not want to do case construct where the user just presses a button.also, I'm thinking to move between screens, is the easiest way just to have loops and exit when? Thx!
Sponsor Sponsor
Flikerator
Posted: Sat Mar 19, 2005 10:41 am Post subject: (No subject)
Im not 100% on what you want.
If you mean like this;
code:
Start Load Instructions
Then there is many ways to do it ^^ Here is one way;
Have a var that equals 1. If its one then it shows start menu highlighted. If its 2 then it shows the Load menu highlighted. Left lowers the value 1, right raises it one. You might want to do a check so it doesnt go above three or go lower then 1 (If its one and it goes down, have it go to 3 )
Now do a check if enter is pressed.
code:
var ch : array char of boolean
var num : int
loop
Input.KeyDown (ch)
if ch (KEY_ENTER) and num = 1 then
elsif ch (KEY_ENTER) and num = 2 then
elsif ch (KEY_ENTER) and num = 3 then
end if
end loop
Note that num is the var I mentioned at the top.
Post if you need any more help
Cervantes
Posted: Sat Mar 19, 2005 11:30 am Post subject: (No subject)
Flik: take a look at that if statement you posted. See how it will check if ENTER is pressed three different times? Why not just check if it's pressed once, then check what the value of num is?
code:
if ch (KEY_ENTER) then
case num of
label 1 : %start
label 2 : %load
label 3: %instructions
end case
end if
Also, I recommend using getch() for this, rather than Input.KeyDown. Reason being, you don't want your program to be doing anything at the same time as getting Input. You wan to wait for input, then do the appropriate action. getch will probably make things easier.
Token
Posted: Sat Mar 19, 2005 12:42 pm Post subject: (No subject)
umm okay heres an idea you could make it so that each time you press the up or down arrow key it adds to a variable, so if the variable was = to 1 it would highlight the top (just by drawing a yellow box behind the text, and if the down button was pressed it would add 1 to that variable making it =2 which would then highlight the next one, then once the enter button was pressed it would look at the value of that variable and execute accordingly
Flikerator
Posted: Sat Mar 19, 2005 2:04 pm Post subject: (No subject)
Cervantes wrote:
Flik: take a look at that if statement you posted. See how it will check if ENTER is pressed three different times? Why not just check if it's pressed once, then check what the value of num is?
code:
if ch (KEY_ENTER) then
case num of
label 1 : %start
label 2 : %load
label 3: %instructions
end case
end if
Also, I recommend using getch() for this, rather than Input.KeyDown. Reason being, you don't want your program to be doing anything at the same time as getting Input. You wan to wait for input, then do the appropriate action. getch will probably make things easier.
Never heard of Case of before, I think I will learn how it works. It looks like it would be helpful for my equipting (changes the value instead of the string).
Flikerator
Posted: Sat Mar 19, 2005 4:15 pm Post subject: (No subject)
I made this, it didn't take long. Here have fun with it
Sorry the only Rems I put were that which you replace :/
code:
var ch : string (1)
var num : int := 1
var font1, font2, font3, font4 : int
var col1, col2, col3 : int := brightred
font1 := Font.New ("serif:24")
View.Set ("nocursor")
colourback (black)
cls
colour (brightgreen)
Font.Draw ("Start", 50, 30, font1, brightgreen)
Font.Draw ("Save", 150, 30, font1, col2)
Font.Draw ("Load", 250, 30, font1, col3)
loop
getch (ch)
cls
if ch = (KEY_LEFT_ARROW) then
num -= 1
end if
if ch = (KEY_RIGHT_ARROW) then
num += 1
end if
if num = 0 then
num := 3
elsif num = 4 then
num := 1
end if
if num = 1 then
col1 := brightgreen
else
col1 := brightred
end if
if num = 2 then
col2 := brightgreen
else
col2 := brightred
end if
if num = 3 then
col3 := brightgreen
else
col3 := brightred
end if
if ch = (KEY_ENTER) then
case num of
label 1 : %Proc for Start
label 2 : %Proc for Save
label 3 : %Proc for Load
end case
end if
Font.Draw ("Start", 50, 30, font1, col1)
Font.Draw ("Save", 150, 30, font1, col2)
Font.Draw ("Load", 250, 30, font1, col3)
end loop
[Gandalf]
Posted: Sat Mar 19, 2005 4:55 pm Post subject: (No subject)
Basically:
Cases are just like an if x = "this" and "this" and "this" then...
So instead of doing:
code:
if answer = "yes" then
put "agree"
elsif answer = "ya" then
put "agree"
elsif answer = "no" then
put "disagree"
else
put "maybe?"
end if
you could just do:
code:
case answer of
label "yes", "ya":
put "agree"
label "no":
put "disagree"
label :
put "maybe?"
end case
This is much shorter and you can add many more possibilites very easily.[/b]
*edit* ehh.. I see you got the hang of it, but I guess its useful for anyone else...
marshymell0
Posted: Sat Mar 19, 2005 7:13 pm Post subject: (No subject)
this is what I have so far:
var key : string (1) := ""
var leafx1 : int := 130
var leafx2 : int := 180
var leafy1 : int := 235
var leafy2 : int := 285
setscreen ("offscreenonly")
View.Update
Draw.FillBox (0, 0, maxx, maxy, 0)
drawfillmapleleaf (leafx1, leafy1, leafx2, leafy2, red)
key := ""
if hasch then
getch (key)
end if
if key = (KEY_UP_ARROW) then
leafy1 := leafy1 + 100
leafy2 := leafy2 + 100
elsif key = (KEY_DOWN_ARROW) then
leafy1 := leafy1 - 100
leafy2 := leafy2 - 100
end if
locate (10, 25)
put "INFORMATION"
locate (15, 25)
put "Quiz"
locate (20, 25)
put "High Scores"
end loop
I just don't know how to make it go to Information, quiz, high scores
Sponsor Sponsor
marshymell0
Posted: Sat Mar 19, 2005 7:15 pm Post subject: (No subject)
wow so much help lol, thx a lot, I'll try to understand it, above I put the program, there are a few extra things like the maple leaf and stuff for design, cause I'm making a quiz about Canada
thanks for all the help and sorry I wasn't more specific, above is the program to work with, thx!
Cervantes
Posted: Sat Mar 19, 2005 8:20 pm Post subject: (No subject)
Looking good. The only thing is, instead of moving the maple leaf up and down based on keyboard input, you need to move it based on a variable, which is based on keyboard input.
So, what that means is the input if statements (if keys (KEY_UP_ARROW) etc.) need to modify a variable. That variable keeps track of which menu is selected. I guess you could name the variable, "selection". selection is an int, and it will start at 1 (for the first menu). Then, if the down arrow is pressed, add or subtract one to selection. (Drawing the maple leaf will probably be easiest If you choose to subtract, and if you all your menus to be spaced an equal distance from each other. But, if you subtract, you should have selection = 1 when the bottom menu is selected and selection = number_of_menus when the top menu is selected.) Then, draw the maple leaf based on the value of selection. You can probably do the drawing in one line, using a little bit of math (if you need help, just ask.) Lastly, perform the appropriate action if the enter key is pressed. I haven't thought about it much, but I think this would be accomplished easily with procedures, though there could be a better, though more difficult, way.
marshymell0
Posted: Sat Mar 19, 2005 9:32 pm Post subject: (No subject)
I"m not really sure I get it, sry
I'm guessing that selection is like the count variable, so I would have something like selction:int:=1, and selection:=selection+1, or selection +2, and if selection is greated or less than, then I just go back to one and two. So this is what I came up with and using the case construct used by Flikerator:
var selection : int
selection := 1
setscreen ("offscreenonly")
View.Update
Draw.FillBox (0, 0, maxx, maxy, 0)
drawfillmapleleaf (leafx1, leafy1, leafx2, leafy2, red)
key := ""
if hasch then
getch (key)
end if
if key = (KEY_UP_ARROW) then
selection := selection + 1
leafy1 := leafy1 + 100
leafy2 := leafy2 + 100
elsif key = (KEY_DOWN_ARROW) then
selection := selection - 1
leafy1 := leafy1 - 100
leafy2 := leafy2 - 100
end if
if selection < 0 then
selection := 3
if selection > 3 then
selection := 1
if key = (KEY_ENTER) then
case selection
label 1 : %instructions
label 2 : %quiz
label 3 : %high score
I am not sure if this is right, since I can't check it since I haven't even made the other parts of the progam yet. Does this make sense? put I'm wondering, using the case construct, how do I move to the next menu? Cervantes, I'm trying to get what you are saying, but I'm getting confused near the middle with the maple leaf and stuff. sry for the noobish thx
Cervantes
Posted: Sun Mar 20, 2005 9:16 am Post subject: (No subject)
marshymell0 wrote:
I"m not really sure I get it, sry
...
sry for the noobish thx
No need to be sorry, everyone starts there.
Okay, you're off to a pretty good start. The biggest problem you've got, right now, is that you're moving the maple leaf down or up based on input. You need to take the coordinate movement (the leafy1 := leafy1 - 100 etc.) out of those if statements. Make a new if statement or case construct that uses the value of selection to determine where the maple leaf should go.
code:
case selection of
label 3 :
leafy1 := 300
label 2 :
leafy1 := 200
label 1 :
leafy1 := 100
label :
end case
Assuming you've got three menus, that is. Also, notice that you don't have to put leafy2 anywhere, because leafy2 is dependant upon leafy1. You want your leaf to always be the same size, so just draw it at:
Okay, next, we don't even have to use that case construct. This next part might be a little harder to understand, though, but really, it's purely simple math. Let's give it a go:
code:
leafy1 := selection * spaceBetweenMenus + extraSpaceBetweenBottomMenuAndTheFloor
%(ie. space between bottom menu and the horizontal line, y = 0 )
if selection = 0, we are at the bottom menu. Let's say the space between each of your menus is 100 pixels. So selection = 0, spaceBetweenMenus = 100. 0 * 100 = 0. So, ignoring the addition at the end, the bottom part of the leaf would be drawn at y = 0. Now, if we add the next part, we can move it up a little bit. Let's say you're bottom menu was 50 pixels off the ground (the y = 0 line). Then leafy1 := 0 * 100 + 50 = 50. If selection = 2, leafy1 := 2 * 100 + 50 = 250.
Hope you got that.
Next:
code:
if selection < 0 then
selection := 3
if selection > 3 then
selection := 1
That's what you posted. First, you need some end if's in there. Second, if selection is < 0 is a good thing, if you want to use the method for getting leafy1 described immediately above. If you use your bottom menu = 0, then you'll have four menus: 0, 1, 2, and 3. Ditch the third menu. We only want three (right?).
Lastly, how to move to the next menu? Well, you could just put the code for the next menu right inside the case construct, but that's ugly. It would be better if you learned procedures. If you dont' know them, here's the tutorial: http://www.compsci.ca/v2/viewtopic.php?t=407.
Hope that helps, let us know if you need more help.
marshymell0
Posted: Sun Mar 20, 2005 4:06 pm Post subject: (No subject)
ok, I guess this makes my program shorter/simpler. So now I have only leafx and leafy, and then I get leafx2 and y2 by subtracting my old x2 and y2 by x1 and y2 and that is now my leafwidth and leafheigh. therefore, I get leaft2-leafy1=285-180=105 and xwidth would be 55, right?
Thefore, my maple leaf is
drawfillmapleleaf (leafx, leafy,leafx+leafwidth,leafy+leafheight, red).
BUt for some reason, when I did that, my maple leaf went somewhere near the top corner, when it should remain in the same place-.-
Next, I have my selection part, which is
if key = (KEY_UP_ARROW) then
selection := selection + 1
elsif key = (KEY_DOWN_ARROW) then
selection := selection - 1
end if
if selection < 1 then
selection := 3
elsif selection > 3 then
selection := 1
end if
I understand that. Where I'm getting confused is the how to draw the maple leaf. I understand what is happening, where my leafy changes accordign to what my selection is, but when I try it, the leaf no longer even moves Another problem I have is, how do I set restrictions, because if the user keeps on pressign down, or up, the leaf would just disappear? Here is my program so far
code:
var key : string (1) := ""
var leafx : int := 130 %the X1 coordinate of the maple leaf
var leafy : int := 180 %the y1 coordinate of the maple leaf
var leafwidth : int := 55 %the x2 coorindate of the maple leaf
var leafheight : int := 105
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
loop
var font1, font2 : int
font1 := Font.New ("serif:20")
Font.Draw ("TABLE OF CONTENTS", 185, 355, font1, red)
Font.Free (font1)
drawfillmapleleaf (130, 335, 180, 385, red)
drawfillmapleleaf (465, 335, 515, 385, red)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var selection : int
selection := 0
setscreen ("offscreenonly")
View.Update
Draw.FillBox (0, 0, maxx, maxy, 0)
drawfillmapleleaf (leafx, leafy, leafx + leafwidth, leafy + leafheight, red)
key := ""
if hasch then
getch (key)
end if
if key = (KEY_UP_ARROW) then
selection := selection + 1
elsif key = (KEY_DOWN_ARROW) then
selection := selection - 1
end if
if selection < 1 then
selection := 3
elsif selection > 3 then
selection := 1
end if
leafy := selection * 100 + 50
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
locate (10, 25)
put "INFORMATION"
locate (15, 25)
put "QUIZ"
locate (20, 25)
put "HIGH SCORES"
end loop
thx again!
Cervantes
Posted: Sun Mar 20, 2005 7:39 pm Post subject: (No subject)
Height and width sound okay, though really you don't have to calculate it, you could just pick new values.
You were using KEY_UP_ARROW with getch. KEY_UP_ARROW is used with Input.KeyDown. With getch, use ord. I don't know, the code didn't crash, but it ate up ALL of my system resources!
Here's the working code.
Turing:
var selection :int
selection :=0 var key :string(1):="" var leafx :int:=130%the X1 coordinate of the maple leaf var leafy :int:=180%the y1 coordinate of the maple leaf var leafwidth :int:=55%the x2 coorindate of the maple leaf var leafheight :int:=105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% loop
And here's the code cleaned up. Don't declare variables inside the loop. Don't reset the font to the same thing every time inside the loop. Don't free the font inside the loop. Don't make the screen offscreenonly inside the loop. All that should be done OUTSIDE the loop. Also, I find it best (and a heck of a lot easier) if you group all your drawing. Lastly, I used a few features to shorten the code. Namely
code:
selection += 1
which is the same as
code:
selection := selection + 1
and
code:
var selection := 3
which is the same as
code:
var selection : int := 3
. Turing knows that 3 is an integer, so you don't have to use the : int := stuff.
Turing:
setscreen("offscreenonly") var selection :=3 var font1 :=Font.New("serif:20") var key :string(1):="" var leafx :=130%the X1 coordinate of the maple leaf var leafy :=180%the y1 coordinate of the maple leaf var leafwidth :=55%the x2 coorindate of the maple leaf var leafheight :=105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% loop
You just need to fix up the y value of the leaf. If you draw your menu's using fonts, or if you use locatexy, it should be easier.
marshymell0
Posted: Mon Mar 21, 2005 9:31 pm Post subject: (No subject)
hahah I think I get it =) tnx a lot!!!! um...do u have a list of all the ord keys? like what they equal?for example, is enter=233, 243,etc.? cause I can't find it on the net or on Turing manual.