Computer Science Canada

Buttons

Author:  Salman [ Sat Jan 19, 2013 6:19 pm ]
Post subject:  Buttons

What is it you are trying to achieve?
<Replace all the <> with your answers/code and remove the <>>


What is the problem you are having?
<Answer Here>


Describe what you have tried to solve this problem
<Answer Here>


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


<Add your code here>



Please specify what version of Turing you are using
<Answer Here>

Author:  Panphobia [ Sat Jan 19, 2013 6:32 pm ]
Post subject:  RE:Buttons

?

Author:  Salman [ Sat Jan 19, 2013 6:53 pm ]
Post subject:  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 <= 360 - 175 - 50 then
movement1 := movement1 + 2
end if
end if

if keys ('s') 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 <= 360 - 175 - 50 then
movement2 := movement2 + 2
end if
end if

if keys (KEY_DOWN_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 <10 or 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

Author:  Tony [ Sat Jan 19, 2013 7:24 pm ]
Post subject:  RE:Buttons

looks like you are clearing the screen after you process the buttons but before you draw them to the screen.

Author:  Salman [ Sun Jan 20, 2013 1:30 pm ]
Post subject:  RE:Buttons

where should i process the buttons?

Author:  Tony [ Sun Jan 20, 2013 7:40 pm ]
Post subject:  RE:Buttons

Good question. What are your choices? It would be before and after all the other major steps that your game is taking.

Author:  Salman [ Sun Jan 20, 2013 10:16 pm ]
Post subject:  RE:Buttons

Start game and help are the options

Author:  Tony [ Mon Jan 21, 2013 1:22 am ]
Post subject:  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


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


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.

Author:  Salman [ Tue Jan 22, 2013 11:32 am ]
Post subject:  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 <= 360 - 175 - 50 then
movement1 := movement1 + 2
end if
end if

if keys ('s') 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 <= 360 - 175 - 50 then
movement2 := movement2 + 2
end if
end if

if keys (KEY_DOWN_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

Author:  Tony [ Tue Jan 22, 2013 3:35 pm ]
Post subject:  Re: RE:Buttons

Salman @ Tue Jan 22, 2013 11:32 am wrote:

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?

Author:  Salman [ Tue Jan 22, 2013 8:13 pm ]
Post subject:  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

Author:  Salman [ Tue Jan 22, 2013 8:15 pm ]
Post subject:  RE:Buttons

When, i run it just the buttons won't clear

Author:  Tony [ Tue Jan 22, 2013 8:38 pm ]
Post subject:  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)?

Author:  Salman [ Tue Jan 22, 2013 8:59 pm ]
Post subject:  RE:Buttons

It will create 2 buttons, the loop will exit when GUI.ProcessEvent

Author:  Salman [ Tue Jan 22, 2013 9:17 pm ]
Post subject:  RE:Buttons

I'm still confused with what to do

Author:  Tony [ Tue Jan 22, 2013 9:20 pm ]
Post subject:  RE:Buttons

multiple sets of "2 buttons", until GUI.ProcessEvent (which in this case is never).

Author:  Salman [ Tue Jan 22, 2013 9:49 pm ]
Post subject:  RE:Buttons

Can you please give me the code for this part, I have to have it done by tommorow

Author:  Tony [ Tue Jan 22, 2013 11:39 pm ]
Post subject:  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.

Author:  Salman [ Wed Jan 23, 2013 9:49 am ]
Post subject:  RE:Buttons

Should I make the buttons outside the loop? The help button works, and i put is outside the loop

Author:  Salman [ Wed Jan 23, 2013 10:25 am ]
Post subject:  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 <= 360 - 175 - 50 then
movement1 := movement1 + 2
end if
end if

if keys ('s') 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 <= 360 - 175 - 50 then
movement2 := movement2 + 2
end if
end if

if keys (KEY_DOWN_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

Author:  Tony [ Wed Jan 23, 2013 3:09 pm ]
Post subject:  RE:Buttons

I would guess that the startGame works, but completes fast enough for you to not notice.

Author:  Salman [ Wed Jan 23, 2013 8:56 pm ]
Post subject:  RE:Buttons

It's already figured out


: