
-----------------------------------
marshymell0
Fri Mar 18, 2005 11:27 pm

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!

-----------------------------------
Flikerator
Sat Mar 19, 2005 10:41 am


-----------------------------------
Im not 100% on what you want.

If you mean like this;

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.


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
Sat Mar 19, 2005 11:30 am


-----------------------------------
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?

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
Sat Mar 19, 2005 12:42 pm


-----------------------------------
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
Sat Mar 19, 2005 2:04 pm


-----------------------------------
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?

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
Sat Mar 19, 2005 4:15 pm


-----------------------------------
I made this, it didn't take long. Here have fun with it ;)
Sorry the only Rems I put were that which you replace :/

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]
Sat Mar 19, 2005 4:55 pm


-----------------------------------
Basically:
Cases are just like an if x = "this" and "this" and "this" then...

So instead of doing:


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:


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
Sat Mar 19, 2005 7:13 pm


-----------------------------------
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

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)
   
    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

-----------------------------------
marshymell0
Sat Mar 19, 2005 7:15 pm


-----------------------------------
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  :D 

thanks for all the help and sorry I wasn't more specific, above is the program to work with, thx!  :D

-----------------------------------
Cervantes
Sat Mar 19, 2005 8:20 pm


-----------------------------------
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.  

:eh:

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
Sat Mar 19, 2005 9:32 pm


-----------------------------------
I"m not really sure I get it, sry  :oops: 
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   :oops:  thx

-----------------------------------
Cervantes
Sun Mar 20, 2005 9:16 am


-----------------------------------
I"m not really sure I get it, sry  :oops: 
...
 sry for the noobish   :oops:  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.

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:

Draw.FillMapleLeaf (leafX, leafY, leafX + leafWidth, leafY + leafHeight, leafColour)


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:

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:

 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
Sun Mar 20, 2005 4:06 pm


-----------------------------------
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 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
Sun Mar 20, 2005 7:39 pm


-----------------------------------
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.

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

    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)
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    setscreen ("offscreenonly")
    View.Update
    cls
    Draw.FillBox (0, 0, maxx, maxy, 0)
    drawfillmapleleaf (leafx, leafy, leafx + leafwidth, leafy + leafheight, red)
    key := ""
    getch (key)
    if ord (key) = 200 then
        selection := selection + 1
    elsif ord (key) = 208 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


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 
 selection += 1 which is the same as selection := selection + 1 and var selection := 3 which is the same as var selection : int := 3.  Turing knows that 3 is an integer, so you don't have to use the : int := stuff.


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


    cls
    locate (10, 25)
    put "INFORMATION"
    locate (15, 25)
    put "QUIZ"
    locate (20, 25)
    put "HIGH SCORES"
    Font.Draw ("TABLE OF CONTENTS", 185, 355, font1, red)
    drawfillmapleleaf (130, 335, 180, 385, red)
    drawfillmapleleaf (465, 335, 515, 385, red)
    drawfillmapleleaf (leafx, leafy, leafx + leafwidth, leafy + leafheight, red)
    View.Update
    delay (10)
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    getch (key)
    if ord (key) = 200 then
        selection += 1
    elsif ord (key) = 208 then
        selection -= 1
    end if
    if selection < 1 then
        selection := 3
    elsif selection > 3 then
        selection := 1
    end if
    leafy := selection * 100 + 50

end loop
Font.Free (font1)

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
Mon Mar 21, 2005 9:31 pm


-----------------------------------
hahah I think I get it =) tnx a lot!!!!  :D  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.

-----------------------------------
Token
Mon Mar 21, 2005 9:39 pm


-----------------------------------
actually it is in the turing help file, its just hiding

 press  F10, then look for Appendices  and then below that 
Keystroke Codes - The character codes returned by getch and getchar 
good luck

-----------------------------------
marshymell0
Mon Mar 21, 2005 11:10 pm


-----------------------------------
ok, I have a new problem  :?  I'm using procedures and mousewhere. On my screen, I have a picutre of the jeopordy squares, and I have made the program so that when the user moves the mouse over one of the squares, a question appears. What I don't know what to do is restrict the user from pressing the same square again. This is sorta what my program looks like: 

procedure question1
    cls
    for count : 1 .. 3
        var question : string
        var answer : string
        question := "What is the capital of Prince Edward Island?"
        put question
        put skip
        get answer
        if answer = "Charlottetown" then
            put "Correct!"
        else
            put "You are incorrect, guess again!"
            delay (750)
            cls
        end if
        exit when answer = "Charlottetown"
    end for
end question1








loop
    var picID : int
    picID := Pic.FileNew ("%oot/Support/Help/Examples/Data Files/squares.bmp")
    Pic.Draw (picID, 150, 80, picCopy)
    var x, y, button : int
    mousewhere (x, y, button)
    locate (1, 1)
    put x : 5, y : 5, button : 5
    if x >= 150 and x = 250 and y = 150 and x = 250 and y 