Posted: 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
Salman
Posted: 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
Tony
Posted: 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.