Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Pong - How to restart the game
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
BreeG16




PostPosted: Sat Jun 14, 2008 12:16 pm   Post subject: Pong - How to restart the game

When one of the players wins i want to give them the option to play again.


WriteScore
if player (2).score > 11 then %Declares player2's name as winner when their score is above 11.
locate (13, 65)
put "player2 Wins!"
end if
if player (1).score > 11 then %Declares player1's name as winner when the score is above 11.
locate (13, 65)
put "player1 Wins!"
end if

locate (11, 20)
if player (2).score > 11 then
put "Would you like to play again? Y/N"
if true then
?????
end if
locate (11, 20)
if player (1).score > 11 then
put "Would you like to play again? Y/N"
end if

exit when player (1).score > 11
exit when player (2).score > 11 %Stops the game when one of the players score is greater than 11 (game point at 12)

I'm almost certain this is all wrong. What do i write to get the game to go back to the beginning when they put Y?
I'm hopeless, i kno.
Sponsor
Sponsor
Sponsor
sponsor
Lithium_Debator




PostPosted: Sat Jun 14, 2008 12:28 pm   Post subject: Re: Pong - How to restart the game

Looks pretty good, what your looking for is the "hasch" command, look it up in the truing reference, it'll fully explain how to use it. then it's just a matter of writing a simple if statement to test for y or n
Tony




PostPosted: Sat Jun 14, 2008 1:22 pm   Post subject: RE:Pong - How to restart the game

Not quite. hasch is used to skip over getch, so not to halt the program.

What you need to do is wrap your game in another loop, and place this "play again" before the end of the outer loop.
Turing:

loop
   % make sure all your variables have their values reset here.
   loop
      % game code
      exit when player (1).score > 11
      exit when player (2).score > 11
   end loop
   % here the game is over, but we could loop and restart

   exit when replay_reply = "n"
end loop
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
BreeG16




PostPosted: Sat Jun 14, 2008 9:36 pm   Post subject: Re: Pong - How to restart the game

i tried that out but it keeps telling me "operands of comparison operators must be of the same type" and it highlights this part:

exit when replay_reply = "n" with the "n" in black.

I have no clue what that means.
Tony




PostPosted: Sat Jun 14, 2008 9:49 pm   Post subject: RE:Pong - How to restart the game

it means that variable replay_reply and literal value "n" are not of the same type. "n" is a string (I think it's a string. 'n' is character?)

your replay_reply was likely declared as string(1) or char, for getch to work.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
BreeG16




PostPosted: Sat Jun 14, 2008 10:35 pm   Post subject: Re: Pong - How to restart the game

ha, oh. Mmk, so I fixed that...but now theres some new problem. This is getting frustrating. Now my variable has no value? This is my entire code.

var paddle1, paddle2, ballx, bally, introFont, reboundx, reboundy, instructFont : int := 0 %Sets the variables that are integers.
var y, n, replay_reply : string

View.Set ("graphics, offscreenonly")
type entryType : %Creates an array of records
record
name : string %Sets variables for the array of records
score : int
end record
var player : array 1 .. 2 of entryType %Declares the array of records
player (1).score := 0
player (2).score := 0
procedure DrawText
introFont := Font.New ("Snap :25:bold") %Sets the font for the welcome message
instructFont := Font.New ("Comicsans:12:italic")
for i : 0 .. 255
Draw.Text ("WELCOME TO PONG", 33, maxy div 2, introFont, 11) %Outputs a welcome message on the main menu screen.
Draw.Text ("Instructions:", 1, maxx div 4, introFont, 11)
Draw.Text ("-Player 1 controls his/her paddle by pressing the up and down arrow keys.", 1, maxx div 5, instructFont, 94)
Draw.Text ("-Player 2 controls his/her paddle by pressing the w key for up and the s key for down.", 1, maxx div 6, instructFont, 94)
Draw.Text ("-Move your paddle up and down to stop the ball from hitting your wall.", 1, maxx div 7.5, instructFont, 94)
Draw.Text ("-At the same time, attempt to hit the wall of your opponent.", 1, maxx div 10, instructFont, 94)
Draw.Text ("-Everytime the ball hits your wall your opponent gains a point.", 1, maxx div 15, instructFont, 94)
Draw.Text ("-The first player to gain 12 points is the winner.", 1, maxx div 29.5, instructFont, 94)
end for


end DrawText

%Programming the Main Menu.
procedure MainMenu
colorback (85) %Sets the main menus background colour
View.Set ("graphics, nooffscreenonly") %View.Set graphics allows for a diminished amount of glitches.
drawfillbox (0, 0, maxx, maxy, 85)
DrawText
color (11) %Sets the following text's colour.
put "Enter Player 1's Name: " ..
get player (1).name %Outputs first players name.
put "Enter Player 2's Name: " ..
get player (2).name %Outputs second players name.
View.Set ("graphics, offscreenonly")
colorback (67)
end MainMenu

procedure WriteScore
locate (2, 65)
put "Score"
locate (7, 65)
put player (1).name, " : ", player (1).score %Outputs Player1's name and then a colon for the scoreboard
locate (9, 65)
put player (2).name, " : ", player (2).score %Outputs Player2's name and then a colon for the scoreboard
end WriteScore

%Setting the starting positions of the paddles and the ball.
paddle1 := 50
paddle2 := 50
ballx := 245
bally := 150
%Setting the rebound speeds of the ball.
reboundx := 3
reboundy := 2

MainMenu

%Programming background music
process playstuff
loop
play (">4a>c1d4dc<1a4a>c1d4fec<1a4a>c1d<4f1f2g4a1>c<4a>c1d4dc1<a4a>c1d<4fg1a4ag1f4e4dcaf>1dpppp") %Code for piano notes
end loop
end playstuff
fork playstuff %Fork allows the music to play.

loop

loop

%Programming the paddles to move.
var w : array char of boolean
Input.KeyDown (w)
if w ('w') then
paddle1 := paddle1 + 2 %Programs the first paddle to move up by pushing W on the keyboard.
end if
var s : array char of boolean %Boolean variables allow for true and false values.
Input.KeyDown (s)
if s ('s') then
paddle1 := paddle1 - 2 %Programs the first paddle to move down by pushing S on the keyboard.
end if
var uparrow : array char of boolean
Input.KeyDown (uparrow)
if uparrow (KEY_UP_ARROW) then
paddle2 := paddle2 + 2 %Programs the second paddle to move up by pushing the up arrow on the keyboard.
end if
var downarrow : array char of boolean
Input.KeyDown (downarrow)
if downarrow (KEY_DOWN_ARROW) then
paddle2 := paddle2 - 2 %Programs the second paddle to move down by pushing the down arrow on the keyboard.
end if

delay (3) %Slows the ball down by a factor of 3
cls

%Background
Draw.FillBox (0, 0, maxx, maxy, 67)
Draw.Box (0, 0, maxx, maxy, 87)
Draw.Line (500, maxy, maxx, -200000, 87) %Outputs the background and background outline.

%Player1 paddle
Draw.FillBox (50, paddle1, 65, paddle1 + 150, 87) %Draws the first paddle.
if paddle1 <= 0 then
paddle1 := 0 %Sets the boundaries of the bottom of the screen for the left paddle.
end if
if paddle1 >= maxy - 150 then
paddle1 := maxy - 150 %Sets the boundaries of the top of the screen for the left paddle.
end if

%Player2 paddle
Draw.FillBox (450, paddle2, 465, paddle2 + 150, 87) %Draws the second paddle.
if paddle2 <= 0 then
paddle2 := 0 %Sets the boundaries of the bottom of the screen for the right paddle.
end if
if paddle2 >= maxy - 150 then
paddle2 := maxy - 150 %Sets the boundaries for the top of the screen for the right paddle.
end if

%Draws the Ball.
Draw.FillOval (ballx, bally, 10, 10, 0)

%Sets Note Boundries and Speed.
ballx := ballx + reboundx %Makes the ball bounce left and right on a horozontal line.
bally := bally + reboundy %Makes the ball bounce up and down on a vertical line.
if bally >= maxy - 10 then
reboundy := -2 %Sets the boundaries of the top of the screen for the ball to bounce off of it.
end if
if bally <= 10 then
reboundy := 2 %Sets the boundaries of the bottom of the screen for the ball to bounce off of it.
end if
if ballx >= maxx - 150 then
reboundx := -3 %Sets the boundaries of the right side of the screen for the ball to bounce off of it.
end if
if ballx <= 10 then
reboundx := 3 %Sets the boundaries for the left side of the screen for the ball to bounce off of it.
end if
%Making the Ball bounce off the paddles.
if bally >= paddle1 and bally <= paddle1 + 150 and ballx <= 75 then
reboundx := 3 %Sets the boundaries for the left paddle so that the ball can bounce off of it.
end if
if bally >= paddle2 and bally <= paddle2 + 150 and ballx >= 440 then
reboundx := -3 %Sets the boundaries for the right paddle so that the ball can bounce off of it.
end if

%Keeps score
if ballx <= 10 then
player (1).score := player (1).score + 1 %Outputs +1 point for everytime the ball hits Player2's wall.
end if
if ballx >= 490 then
player (2).score := player (2).score + 1 %Outputs +1 point for everytime the ball hits Player1's wall.
end if

WriteScore
if player (2).score > 11 then %Declares player2's name as winner when their score is above 11.
locate (13, 65)
put "player2 Wins!"
end if
if player (1).score > 11 then %Declares player1's name as winner when the score is above 11.
locate (13, 65)
put "player1 Wins!"
end if

exit when player (1).score > 11
exit when player (2).score > 11 %Stops the game when one of the players score is greater than 11 (game point at 12)
if player (1).score > 11 then
put "Would you like to play again? Y/N"
end if
View.Update
end loop
exit when replay_reply = "n"

end loop
Tony




PostPosted: Sat Jun 14, 2008 10:58 pm   Post subject: RE:Pong - How to restart the game

which variable has no value?

also, a couple of comments about the code:

the way Input.KeyDown works is that it captures all of the keys, so doing it once is enough

Turing:

var keys : array char of boolean
Input.KeyDown (keys)
if keys ('w') then
paddle1 := paddle1 + 2 %Programs the first paddle to move up by pushing W on the keyboard.
end if
if keys ('s') then
paddle1 := paddle1 - 2 %Programs the first paddle to move down by pushing S on the keyboard.
end if
if keys (KEY_UP_ARROW) then
paddle2 := paddle2 + 2 %Programs the second paddle to move up by pushing the up arrow on the keyboard.
end if
if keys (KEY_DOWN_ARROW) then
paddle2 := paddle2 - 2 %Programs the second paddle to move down by pushing the down arrow on the keyboard.
end if


Turing:

delay (3) %Slows the ball down by a factor of 3

It's not a "factor" of 3. It's 0.003 of a second.

Turing:

Draw.Line (500, maxy, maxx, -200000, 87) %Outputs the background and background outline.

-200000 ?


....

Ok, I've noticed the problem. Your replay_reply variable has no value. That is because you've never gotten a reply from the user.

Turing:

  exit when player (1).score > 11
     % ...
  if player (1).score > 11 then
    put "Would you like to play again? Y/N"
  end if
end loop


since the loop exists during the exit when condition, the player is never told that they could play again, that part is skipped over. And even if it wasn't, you are still not recording the user's response in any variable.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: