
-----------------------------------
Salman
Sat Jan 19, 2013 6:19 pm

Buttons
-----------------------------------
What is it you are trying to achieve?



What is the problem you are having?



Describe what you have tried to solve this problem



Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)








Please specify what version of Turing you are using


-----------------------------------
Panphobia
Sat Jan 19, 2013 6:32 pm

RE:Buttons
-----------------------------------
?

-----------------------------------
Salman
Sat Jan 19, 2013 6:53 pm

RE:Buttons
-----------------------------------
For some reason the buttons aren't showing anymore, can you tell me how i can show thoses, this is my code:

import GUI
var x2 := maxx div 2
var y2 := maxy div 2
const radius := 10
var speedx : int := -1
var speedy : int := 1
var font : int
var font2 : int
var movement1 : int := 0
var movement2 : int := 0
var keys : array char of boolean
var x := 175
var y := 0
colorback (black)
for i : 1 .. 25 %Counted loop makes the background stay black
    for j : 1 .. 80
        put " " ..
    end for
end for
setscreen ("nocursor,offscreenonly")
font := Font.New ("Arial Black:18")
font2 := Font.New ("Arial Black:12")
Font.Draw ("Pong", 280, 320, font, red)

procedure startGame
    cls

    Draw.FillBox (0, 600, maxx, 360, brightred)
    Draw.FillBox (0, 00, maxx, 45, brightred)
    Draw.FillBox (0, 175 + movement1, 10, 225 + movement1, brightred)
    Draw.FillBox (630, x + movement2, maxx, 225 + movement2, brightred)
    Font.Draw ("Player 1 Score", 5, 385, font2, blue)
    Font.Draw ("Player 2 Score", 500, 385, font2, blue)
    Input.KeyDown (keys)
    if keys ('w') then
        Draw.FillBox (0, 175 + movement1, 10, 225 + movement1, brightred)
        if movement1 = 45 - 175 then
            movement1 := movement1 - 2
        end if
    end if

    if keys (KEY_UP_ARROW) then
        Draw.FillBox (630, 175 + movement2, maxx, 225 + movement2, brightred)
        if movement2 = 45 - 175 then
            movement2 := movement2 - 2
        end if
    end if

    Draw.FillOval (x2, y2, radius, radius, brightblue)
    x2 := x2 + speedx
    if y2 + speedy + radius > 360 or y2 + speedy - radius < 45 then
        speedy := speedy * -1
    end if
    y2:=y2+speedy
    if y2 + speedy + radius > 175 + movement1 and y2 + speedy + radius < 225 + movement1 and x2 + speedx - radius < 10 then
        speedx := speedx * -1
        end if
    if y2 + speedy + radius > 175 + movement2 and y2 + speedy - radius < 225 + movement2 and x2 + speedx - radius > 620 then
        speedx := speedx * -1
    end if
    if y2 + speedy + radius maxx then
    Draw.FillOval (x2, y2, radius, radius, brightblue)
    end if

end startGame
var startButton : int := GUI.CreateButton (maxx div 2 - 30, 60, 60, "Start Game", startGame)



procedure helpGame
    cls
    Font.Draw ("Refer to the instruction manual", 100, 320, font, red)
end helpGame
var helpButtom : int := GUI.CreateButton (maxx div 2 - 200, 60, 60, "Help", helpGame)

loop
    startGame
    View.Update
    exit when GUI.ProcessEvent
end loop

-----------------------------------
Tony
Sat Jan 19, 2013 7:24 pm

RE:Buttons
-----------------------------------
looks like you are clearing the screen after you process the buttons but before you draw them to the screen.

-----------------------------------
Salman
Sun Jan 20, 2013 1:30 pm

RE:Buttons
-----------------------------------
where should i process the buttons?

-----------------------------------
Tony
Sun Jan 20, 2013 7:40 pm

RE:Buttons
-----------------------------------
Good question. What are your choices? It would be before and after all the other major steps that your game is taking.

-----------------------------------
Salman
Sun Jan 20, 2013 10:16 pm

RE:Buttons
-----------------------------------
Start game and help are the options

-----------------------------------
Tony
Mon Jan 21, 2013 1:22 am

RE:Buttons
-----------------------------------
well, the help text is only drawn when you press the button, it doesn't really matter.

You game loop right now is something like
[code]
loop
   clear screen
   move stuff
   draw stuff to screen
   process buttons
end loop
[/code]

but because the loop repeats over and over, it doesn't really matter after the first frame. We can describe it starting from any other point, as long as we maintain the same order of events. It is often helpful to take "draw stuff" as the last step. So what you are doing is:

[code]
loop
  process buttons
  clear screen
  move stuff
  draw stuff
end loop
[/code]

Thus your choices are:
[1]clear screen[2]move stuff[3]draw stuff[4]

You currently decided to place the buttons into slot [1]. I'll let you think about what that means.

-----------------------------------
Salman
Tue Jan 22, 2013 11:32 am

RE:Buttons
-----------------------------------
I tried this, still doesn't work

import GUI
var x2 := maxx div 2
var y2 := maxy div 2
const radius := 10
var score1 : int := 0
var score2 : int := 0
var strScore1 : string := "0"
var strScore2 : string := "0"
var speedx : int := -1
var speedy : int := 1
var font : int
var font2 : int
var movement1 : int := 0
var movement2 : int := 0
var keys : array char of boolean
var x := 175
var y := 0
colorback (black)
for i : 1 .. 25 %Counted loop makes the background stay black
    for j : 1 .. 80
        put " " ..
    end for
end for
setscreen ("nocursor,offscreenonly")
font := Font.New ("Arial Black:18")
font2 := Font.New ("Arial Black:12")
Font.Draw ("Pong", 280, 320, font, red)
procedure startGame %This procedure runs the game
    cls
    Draw.FillBox (0, 600, maxx, 360, brightred)
    Draw.FillBox (0, 00, maxx, 45, brightred)
    Draw.FillBox (0, 175 + movement1, 10, 225 + movement1, brightred)
    Draw.FillBox (630, x + movement2, maxx, 225 + movement2, brightred)
    Font.Draw ("Player 1 Score", 5, 385, font2, blue)
    Font.Draw ("Player 2 Score", 500, 385, font2, blue)
    Input.KeyDown (keys)
    if keys ('w') then
        Draw.FillBox (0, 175 + movement1, 10, 225 + movement1, brightred)
        if movement1 = 45 - 175 then
            movement1 := movement1 - 2
        end if
    end if

    if keys (KEY_UP_ARROW) then
        Draw.FillBox (630, 175 + movement2, maxx, 225 + movement2, brightred)
        if movement2 = 45 - 175 then
            movement2 := movement2 - 2
        end if
    end if
    Font.Draw (strScore1, 5, 370, font2, blue)
    Font.Draw (strScore2, 600, 370, font2, blue)

    Draw.FillOval (x2, y2, radius, radius, brightblue)
    x2 := x2 + speedx
    y2 := y2 + speedy
    if y2 + speedy + radius > 360 or y2 + speedy - radius < 45 then
        speedy := speedy * -1
    end if
    if y2 + speedy + radius > 175 + movement1 and y2 + speedy + radius < 225 + movement1 and x2 + speedx - radius < 10 then
        speedx := speedx * -1
    end if
    if y2 + speedy + radius > 175 + movement2 and y2 + speedy - radius < 225 + movement2 and x2 + speedx - radius > 620 then
        speedx := speedx * -1
    end if
    %If statement used for scoring
    if x2 + speedx + radius > 650 then %If the x value is greater than maxx, then a point is added to player 1
        score1 := score1 + 1
        strScore1 := intstr (score1)
        speedx := 1
        x2 := maxx div 2
        y2 := maxy div 2
    elsif x2 + speedx + radius < 0 then %if the x value is less than zero, then a point is added to player 2
        score2 := score2 + 1
        strScore2 := intstr (score2) %Converts score to string value
        speedx := -1
        x2 := maxx div 2
        y2 := maxy div 2
    end if
end startGame
procedure helpGame
    Font.Draw ("Refer to the instruction manual", 100, 320, font, red)
end helpGame
loop
    exit when GUI.ProcessEvent
    var startButton : int := GUI.CreateButton (maxx div 2 - 30, 60, 60, "Start Game", startGame)
    var helpButtom : int := GUI.CreateButton (maxx div 2 - 200, 60, 60, "Help", helpGame)
    View.Update
end loop
loop
    startGame
    View.Update
end loop

-----------------------------------
Tony
Tue Jan 22, 2013 3:35 pm

Re: RE:Buttons
-----------------------------------

loop
    exit when GUI.ProcessEvent
    var startButton : int := GUI.CreateButton (maxx div 2 - 30, 60, 60, "Start Game", startGame)
    var helpButtom : int := GUI.CreateButton (maxx div 2 - 200, 60, 60, "Help", helpGame)
    View.Update
end loop

Can you explain what your code is doing here?

-----------------------------------
Salman
Tue Jan 22, 2013 8:13 pm

RE:Buttons
-----------------------------------
it creates the buttons, and the loop exits when gui.quit is called, whereupon GUI.ProcessEvent will return true and the loop will exit

-----------------------------------
Salman
Tue Jan 22, 2013 8:15 pm

RE:Buttons
-----------------------------------
When, i run it just the buttons won't clear

-----------------------------------
Tony
Tue Jan 22, 2013 8:38 pm

RE:Buttons
-----------------------------------
- how many buttons will it create (hint, it's more than 2)?
- when will the loop exit (that is, when will GUI.Quit be called)?

-----------------------------------
Salman
Tue Jan 22, 2013 8:59 pm

RE:Buttons
-----------------------------------
It will create 2 buttons, the loop will exit when GUI.ProcessEvent

-----------------------------------
Salman
Tue Jan 22, 2013 9:17 pm

RE:Buttons
-----------------------------------
I'm still confused with what to do

-----------------------------------
Tony
Tue Jan 22, 2013 9:20 pm

RE:Buttons
-----------------------------------
multiple sets of "2 buttons", until GUI.ProcessEvent (which in this case is never).

-----------------------------------
Salman
Tue Jan 22, 2013 9:49 pm

RE:Buttons
-----------------------------------
Can you please give me the code for this part, I have to have it done by tommorow

-----------------------------------
Tony
Tue Jan 22, 2013 11:39 pm

RE:Buttons
-----------------------------------
I'm trying to help you write the code that you need. You have an infinite loop making new buttons over and over again.

-----------------------------------
Salman
Wed Jan 23, 2013 9:49 am

RE:Buttons
-----------------------------------
Should I make the buttons outside the loop? The help button works, and i put is outside the loop

-----------------------------------
Salman
Wed Jan 23, 2013 10:25 am

RE:Buttons
-----------------------------------
I tried this, and the help and quit buttons function right, and the start button doesn't, where should i call the procedure startGame?

import GUI
var winID:int
winID:=Window.Open ("nocursor,offscreenonly")
var x2 := maxx div 2
var y2 := maxy div 2
const radius := 10
var score1 : int := 0
var score2 : int := 0
var strScore1 : string := "0"
var strScore2 : string := "0"
var speedx : int := -1
var speedy : int := 1
var font : int
var font2 : int
var movement1 : int := 0
var movement2 : int := 0
var keys : array char of boolean
var x := 175
var y := 0
colorback (black)
for i : 1 .. 25 %Counted loop makes the background stay black
    for j : 1 .. 80
        put " " ..
    end for
end for
setscreen ("nocursor,offscreenonly")
font := Font.New ("Impact:35")
font2 := Font.New ("Arial Black:12")
Font.Draw ("Classic Pong", 200, 320, font, red)
procedure quitGame
    Window.Close (winID)
end quitGame
procedure startGame %This procedure runs the game
    cls
    Draw.FillBox (0, 600, maxx, 360, brightred)
    Draw.FillBox (0, 00, maxx, 45, brightred)
    Draw.FillBox (0, 175 + movement1, 10, 225 + movement1, brightred)
    Draw.FillBox (630, x + movement2, maxx, 225 + movement2, brightred)
    drawline (maxx div 2, 0, maxx div 2, 400, white)
    Font.Draw ("Player 1 Score", 5, 385, font2, blue)
    Font.Draw ("Player 2 Score", 500, 385, font2, blue)
    Input.KeyDown (keys)
    if keys ('w') then
        Draw.FillBox (0, 175 + movement1, 10, 225 + movement1, brightred)
        if movement1 = 45 - 175 then
            movement1 := movement1 - 2
        end if
    end if

    if keys (KEY_UP_ARROW) then
        Draw.FillBox (630, 175 + movement2, maxx, 225 + movement2, brightred)
        if movement2 = 45 - 175 then
            movement2 := movement2 - 2
        end if
    end if
    Font.Draw (strScore1, 5, 370, font2, blue)
    Font.Draw (strScore2, 600, 370, font2, blue)

    Draw.FillOval (x2, y2, radius, radius, brightblue)
    x2 := x2 + speedx
    y2 := y2 + speedy
    if y2 + speedy + radius > 360 or y2 + speedy - radius < 45 then
        speedy := speedy * -1
    end if
    if y2 + speedy + radius > 175 + movement1 and y2 + speedy + radius < 225 + movement1 and x2 + speedx - radius < 10 then
        speedx := speedx * -1
    end if
    if y2 + speedy + radius > 175 + movement2 and y2 + speedy - radius < 225 + movement2 and x2 + speedx - radius > 620 then
        speedx := speedx * -1
    end if
    %If statement used for scoring
    if x2 + speedx + radius > 650 then %If the x value is greater than maxx, then a point is added to player 1
        score1 := score1 + 1
        strScore1 := intstr (score1)
        speedx := -1
        x2 := maxx div 2
        y2 := maxy div 2
        if strScore1 = "20" then
            Font.Draw ("Game over, player one wins", 320, 500, font, brightred)
            x2 := maxx div 2
            y2 := maxy div 2
            speedx := 0
            speedy := 0
            var quitButton : int := GUI.CreateButton (maxx div 2 - 30, 60, 60, "Quit", quitGame)
        end if
    elsif x2 + speedx + radius < 0 then %if the x value is less than zero, then a point is added to player 2
        score2 := score2 + 1
        strScore2 := intstr (score2) %Converts score to string value
        speedx := 1
        x2 := maxx div 2
        y2 := maxy div 2
        if strScore2 = "20" then
            Font.Draw ("Game Over, Player 2 wins", 100, 320, font2, red)
            x2 := maxx div 2
            y2 := maxy div 2
            speedx := 0
            speedy := 0
        end if
    end if
end startGame
var font3 : int
var font4 : int
var font5 : int
font3 := Font.New ("Impact:30")
font4 := Font.New ("Comic Sans MS:12")
font5 := Font.New ("Comic Sans MS:16")

procedure helpGame %Creates the help instructions
    cls
    Font.Draw ("Controls", 260, 350, font3, brightred)
    Font.Draw ("Player 1 Paddle:", 20, 310, font5, brightblue)
    Font.Draw ("Player 2 Paddle:", 330, 310, font5, brightblue)
    Draw.FillBox (20, 270, 70, 220, brightred)
    Draw.FillBox (20, 170, 70, 120, brightred)
    Font.Draw ("W", 25, 235, font3, brightblue)
    Font.Draw ("S", 30, 135, font3, brightblue)
    Font.Draw ("-This key moves the paddle up", 75, 235, font4, white)
    Font.Draw ("-This key moves the paddle down", 75, 135, font4, white)
    Draw.FillBox (330, 270, 380, 220, brightred)
    Draw.FillBox (330, 170, 380, 120, brightred)
    Font.Draw ("-This key moves the paddle up", 390, 235, font4, white)
    Font.Draw ("-This key moves the paddle down", 390, 135, font4, white)
    %Draws down arrow key
    drawline (355, 165, 355, 125, blue)
    drawline (355, 125, 370, 140, blue)
    drawline (355, 125, 340, 140, blue)
    %Draws up arrow key
    drawline (355,265 ,355,225,blue)
    drawline (355, 265, 340, 250,blue)
    drawline (355, 265, 370, 250, blue)
    Font.Draw ("-The match ends when a player scores 20 points", 10, 60, font5, brightred)
    Font.Draw ("-Refer to the instruction manual for more details", 10, 30, font5, brightred)
    var startButton2 : int := GUI.CreateButton (525, 35, 100, "Start Game", startGame)
end helpGame

var startButton : int := GUI.CreateButton (200, 175, 250, "Start Game", startGame)
var helpButton : int := GUI.CreateButton (200, 125, 250, "Instructions", helpGame)
var quitBotton:int:=GUI.CreateButton (200, 75, 250, "Quit Game", quitGame)
loop
    exit when GUI.ProcessEvent
    View.Update
end loop

-----------------------------------
Tony
Wed Jan 23, 2013 3:09 pm

RE:Buttons
-----------------------------------
I would guess that the startGame works, but completes fast enough for you to not notice.

-----------------------------------
Salman
Wed Jan 23, 2013 8:56 pm

RE:Buttons
-----------------------------------
It's already figured out
