How to get Paddles to move in Pong Game
Author |
Message |
WhooliMooli
|
Posted: Sat Jan 18, 2014 11:56 pm Post subject: How to get Paddles to move in Pong Game |
|
|
What is it you are trying to achieve?
<Hey, I am creating a pong program for my grade 10 ICS class and I am trying to get my Pong Paddles to move>>
What is the problem you are having?
I do not know why but my paddles are not moving at all>
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)
<Answer Here>
Turing: |
PLEASE BE AWARE THAT CURRENTLY THE PLAYER 1 VERSION IS BEING USED TO CREATE THE PLAYER 2 VERSION (I did this by accident but will be fixing it later once I figure out this problem- the game is not close to being finished ) SO PLEASE USE PLAYER 1 MODE
<setscreen ("Graphics: 1750; 950")
colourback (black)
Text.Color (white)
%Variables, also defines the first values to start the game.
%Variables created to track the score of each player
var p1score, p2score : int := 0
%Variables created for the paddles each player is going to control in the game
%to attempt to bounce the ball onto the oppenents side, recieving a point
var p1x1 : int := 50
var p1x2 : int := 15
var p1y1 : int := 350
var p1y2 : int := 500
var p2x1 : int := maxx - 50
var p2x2 : int := maxx - 15
var p2y1 : int := 350
var p2y2 : int := 500
%Allows the user to interact with the paddles using the keyboard variables
var p1buttons : char
var p2buttons : char
%Computers Paddle
var cpaddle : int
%Variables to control ball movement
var balldirection, ballpoints : int
var randomdirection : int := 0
var ballx : int := 875
var bally : int := 475
%Variable to control the different speed levels of the ball
var ballspeed : int
%Variable for the font of the game
var font : int := Font.New ("Agency FB:180")
var font1 : int := Font.New ("Agency FB:100")
var instructionfont : int := Font.New ("Agency FB:84")
var gamefont : int := Font.New ("Agency FB:64")
var scorefont : int := Font.New ("Agency FB:45")
var ballspeedfont : int := Font.New ("Agency FB:14")
%Variable to allow the user's to choose a name
var p1name, p2name : string
%Create the variables for the x and y coordinates of the mouse pointer
var x, y : int
%Create a variable to store the state of the mouse buttons
var buttons : int
%Title screen of the game:
drawfillbox (maxx, 0, 0, maxy, black)
Font.Draw ("Welcome to Pong!", 120, maxy - 230, font, yellow)
drawfillbox (1300, 650, 300, 470, yellow)
drawfillbox (1300, 450, 300, 270, yellow)
drawfillbox (1300, 250, 300, 70, yellow)
Font.Draw ("Player 1!", 630, 520, font1, black)
Font.Draw ("Player 2!", 630, 320, font1, black)
Font.Draw ("Instructions!", 540, 120, font1, black)
%If we want to be able to tell the difference between the left and right buttons, we need this command
Mouse.ButtonChoose ("multibutton")
%Start a loop
loop
%Use the mouse.where() command to ask turing what the mouse is doing
Mouse.Where (x, y, buttons )
%This allows the user to click the option they want to go to on the program in the window.
%Player 1
if buttons = 1 and x > 300 and y > 470 and x < 1300 and y < 650 then
cls
%Allows User to choose the ball speed they would prefer
loop
Font.Draw ("Choose the preffered ball speed (1-5) : ", 80, 500, font1, yellow)
locate (32, 100)
get ballspeed
%If the user inputs a value for ballspeed that is under 1 or above 5
%the program will output that, that is an invalid value
%If the user outputs an appropriate value then
%they will be redircted to the actual game.
exit when ballspeed = 5 or ballspeed = 4 or ballspeed = 3 or ballspeed = 2 or ballspeed = 1
Font.Draw ("Invalid value. Please Enter a Ball Speed Between 1-5!", 150, 140, instructionfont, yellow)
delay (2100)
cls
end loop
%Allows the user to choose a name for there player
cls
Font.Draw ("What is player 1's name", 350, 700, font1, yellow)
locate (20, 100)
get p1name
Font.Draw ("What is player 2's name", 350, 300, font1, yellow)
locate (45, 100)
get p2name
cls
%The actual game area with game being started
%Drawing a section that is seperate from the game area.
%This space will be used to contain the players scores.
loop
Font.Draw ("Pong", 850, maxy - 50, scorefont, yellow)
Font.Draw (p1name + " :", 100, maxy - 50, scorefont, yellow)
Font.Draw (p2name + " :", maxx - 200, maxy - 50, scorefont, yellow)
drawline (0, maxy - 100, maxx, maxy - 100, yellow)
locate (5, 50)
put p1score
locate (5, 100)
put p2score
%Drawing the pieces that the users will interact with
drawfillbox (p1x1, p1y1, p1x2, p1y2, yellow)
drawfillbox (p2x1, p2y1, p2x2, p2y2, yellow)
drawfilloval (ballx, bally, 20, 20, yellow)
%The initial ball direction when the game starts
if randomdirection < 1 then
balldirection := 1
end if
end loop
%Player 2
elsif buttons = 1 and x > 300 and y > 270 and x < 1300 and y < 450 then
cls
loop
Font.Draw ("Choose the preffered ball speed (1-5) : ", 80, 500, font1, yellow)
locate (32, 100)
get ballspeed
exit when ballspeed = 5 or ballspeed = 4 or ballspeed = 3 or ballspeed = 2 or ballspeed = 1
Font.Draw ("Invalid value. Please Enter a Ball Speed Between 1-5!", 150, 140, instructionfont, yellow)
delay (2100)
cls
end loop
cls
Font.Draw ("What is player 1's name", 350, 700, font1, yellow)
locate (20, 100)
get p1name
Font.Draw ("What is player 2's name", 350, 300, font1, yellow)
locate (45, 100)
get p2name
cls
%Instructions
elsif buttons = 1 and x > 300 and y > 70 and x < 1300 and y < 250 then
cls
Font.Draw ("The objective of the game is to get the ball", 100, maxy - 100, instructionfont, yellow)
Font.Draw ("passed the opponents paddle and achieve", 100, maxy - 230, instructionfont, yellow)
Font.Draw ("10 before the opposing player", 350, maxy - 380, instructionfont, yellow)
Font.Draw ("Player 2", 250, maxy - 550, instructionfont, yellow)
Font.Draw ("Player 1", 1150, maxy - 550, instructionfont, yellow)
drawline (875, 530, 875, 0, yellow)
drawline (maxx, 530, 0, 530, yellow)
Font.Draw ("Up Arrow Key = UP", 875, maxy - 700, instructionfont, yellow)
Font.Draw ("Down Arrow Key = Down", 875, maxy - 850, instructionfont, yellow)
Font.Draw ("A = Up", 50, maxy - 700, instructionfont, yellow)
Font.Draw ("Z = Down", 50, maxy - 850, instructionfont, yellow)
end if
%This is the first players "UP and "DOWN" button, the speed at which
%the paddles can move up and down will depend on the ball speed the player chooses
%This also makes it so that if the paddle reaches somewhere it is not suppose to
%(outside of the window it cannot because the keys will not work past
%the boundry I created for it
if p1buttons = (KEY_UP_ARROW) and p1y1 >= maxx then
if ballspeed = 1 then
p1y1 := p1y1 + 1
p2y2 := p2y2 + 1
elsif ballspeed = 2 then
p1y1 := p1y1 + 2
p2y2 := p2y2 + 2
elsif ballspeed = 3 then
p1y1 := p1y1 + 3
p2y2 := p2y2 + 3
elsif ballspeed = 4 then
p1y1 := p1y1 + 4
p2y2 := p2y2 + 4
elsif ballspeed = 5 then
p1y1 := p1y1 + 5
p2y2 := p2y2 + 5
end if
end if
if p1buttons = (KEY_DOWN_ARROW) and p1y2 <= maxy - 100 then
if ballspeed = 1 then
p1y1 := p1y1 - 1
p2y2 := p2y2 - 1
elsif ballspeed = 2 then
p1y1 := p1y1 - 2
p2y2 := p2y2 - 2
elsif ballspeed = 3 then
p1y1 := p1y1 - 3
p2y2 := p2y2 - 3
elsif ballspeed = 4 then
p1y1 := p1y1 - 4
p2y2 := p2y2 - 4
elsif ballspeed = 5 then
p1y1 := p1y1 - 5
p2y2 := p2y2 - 5
end if
end if
%This is the second players "UP and "DOWN" button, the speed at which
%the paddles can move up and down will depend on the ball speed the player chooses
%This also makes it so that if the paddle reaches somewhere it is not suppose to
%(outside of the window it cannot because the keys will not work past
%the boundry I created for it
if p2buttons = ('a') and p2y1 >= maxx then
if ballspeed = 1 then
p1y1 := p1y1 + 1
p2y2 := p2y2 + 1
elsif ballspeed = 2 then
p1y1 := p1y1 + 2
p2y2 := p2y2 + 2
elsif ballspeed = 3 then
p1y1 := p1y1 + 3
p2y2 := p2y2 + 3
elsif ballspeed = 4 then
p1y1 := p1y1 + 4
p2y2 := p2y2 + 4
elsif ballspeed = 5 then
p1y1 := p1y1 + 5
p2y2 := p2y2 + 5
end if
end if
if p1buttons = ('z') and p2y2 >= maxx - 100 then
if ballspeed = 1 then
p1y1 := p1y1 - 1
p2y2 := p2y2 - 1
elsif ballspeed = 2 then
p1y1 := p1y1 - 2
p2y2 := p2y2 - 2
elsif ballspeed = 3 then
p1y1 := p1y1 - 3
p2y2 := p2y2 - 3
elsif ballspeed = 4 then
p1y1 := p1y1 - 4
p2y2 := p2y2 - 4
elsif ballspeed = 5 then
p1y1 := p1y1 - 5
p2y2 := p2y2 - 5
end if
end if
end loop>
|
Please specify what version of Turing you are using
<4.1.1> |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Dreadnought
|
|
|
|
 |
WhooliMooli
|
Posted: Sun Jan 19, 2014 9:46 pm Post subject: Re: How to get Paddles to move in Pong Game |
|
|
I did what was in the example, however my paddles are still not working :O |
|
|
|
|
 |
Tony

|
Posted: Sun Jan 19, 2014 10:14 pm Post subject: Re: How to get Paddles to move in Pong Game |
|
|
WhooliMooli @ Sun Jan 19, 2014 9:46 pm wrote: however my paddles are still not working :O
That's not very descriptive. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
|
|