Pong Help
Author |
Message |
landfarm
|
Posted: Mon Apr 19, 2010 7:20 pm Post subject: Pong Help |
|
|
What is it you are trying to achieve?
I am currently trying to make Pong more lively.
What is the problem you are having?
I believe that I am almost done the project, but I have no clue how to add trigonometry or an alternative to add "physics" into the game of Pong. I know it sounds easy, but I have absolutely no idea how to do it. For example, when you hit the ball with the paddle to hit it further to the left, I have no clue how to do this. I'm hoping for a tutorial to help teach me the concept, rather than having someone doing it for me. Btw, I searched on the forums, but I couldn't find what I was looking for.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Here is my code so far.
Turing: |
import GUI
var x1, x2, y1, y2, xc, yc : int %Declares the coordinate variables for the game
var c, t : int %Declares the counter variables for the game
var p1, p2 : int %Declares the points counter for the game
var velx, vely, facy, facx : int %Declares the velocity variables for the game
var timeprevious, timecurrent, timedifference : int %Declares variables for holding time
var font1 : int %Declares variable for font
var keys : array char of boolean
c := 1 %Counter to see if the game just started
t := 1 %Counter to see who's starting the game
p1 := 0
p2 := 0
velx := 0
vely := 0
facy := 1
facx := 1
font1 := Font.New ("serif:40")
assert font1 > 0
procedure controls1 %Controls for player1
Input.KeyDown (keys )
if keys ('a') and x1 >= 0 then
x1 - = 4
elsif keys ('d') and x1 + 60 <= maxx then
x1 + = 4
elsif keys (KEY_SHIFT ) then
vely := 2
velx := 2
end if
end controls1
procedure controls2 %Controls for player2
Input.KeyDown (keys )
if keys (KEY_LEFT_ARROW) and x2 - 70 >= 0 then
x2 - = 4
elsif keys (KEY_RIGHT_ARROW) and x2 <= maxx then
x2 + = 4
elsif keys (KEY_ENTER ) then
vely := - 2
velx := - 2
end if
end controls2
procedure player1
if c = 1 then %Intializes the coordinates for player1
x1 := 20
y1 := 30
end if
drawfillbox (x1, y1, x1 + 60, y1 + 20, blue) %Draws player1
end player1
procedure player2
if c = 1 then %Intializes the coordinates for player2
x2 := maxx - 30
y2 := maxy - 30
end if
drawfillbox (x2, y2, x2 - 60, y2 - 20, red) %Draws player2
end player2
procedure ball
drawfilloval (xc, yc, 20, 20, brown) %Draws the ball
if yc - 20 >= 0 and yc + 20 <= maxy then
yc + = (vely * facy )
end if
if xc - 20 >= 0 and xc + 20 <= maxx then
xc + = (velx * facx )
end if
end ball
procedure map
Draw.ThickLine (0, maxy div 2, maxx, maxy div 2, 5, black)
Draw.ThickLine (0, maxy, maxx, maxy, 5, black)
Draw.ThickLine (0, 0, maxx, 0, 5, black)
Draw.ThickLine (0, 0, 0, maxy, 5, black)
Draw.ThickLine (maxx, 0, maxx, maxy, 5, black)
end map
procedure rules
if yc + 20 >= maxy then %Sets up the points system
p1 + = 1
c := 1
elsif yc - 20 <= 0 then
p2 + = 1
c := 1
end if
if (((xc >= x1 and yc - 20 >= y1 ) and (xc <= x1 + 60 and yc - 20 <= y1 + 20)) or ((xc >= x2 - 60 and yc + 20 >= y2 - 20) and (xc <= x2 and y2 - 20 <= y2 ))) and timedifference > 1000 then
facy * = - 1 %If the ball ever collides with the players, it rebounds into the opposite direction
facx * = - 1
timeprevious := timecurrent %Time is used to prevent the endless facy *= -1 and facx *= -1 if the ball were to ever get "into" the paddles
elsif (xc + 20 >= maxx) or (xc - 20 <= 0) then %The ball rebounds if it hits the walls
facx * = - 1
end if
end rules
procedure update %Update procedure
View.Update
delay (10)
cls
end update
procedure game
setscreen ("offscreenonly") %Buffers the program to prevent lag
loop
if c = 1 then
timeprevious := Time.Elapsed
xc := 300
yc := 200
vely := 0
velx := 0
end if
timecurrent := Time.Elapsed
timedifference := timecurrent - timeprevious
map
controls1
controls2
player1
player2
ball
put "Red: ", p1
put "Blue: ", p2
update
c + = 1
rules %Rules is placed after the "c" counter to prevent "c" from increasing over 1 if one of the decisions in Rules are made
end loop
end game
var button1 : int %Declares variable for Graphic User Interface (GUI) button
%%%%%%%Creates the GUI interface%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
loop
button1 := GUI.CreateButton (maxx div 2, maxy div 2, 0, "Play", game )
Font.Draw ("PONG", (maxx div 2) - 50, (maxy div 2) + 50, font1, black)
exit when GUI.ProcessEvent
View.Update
end loop
|
Please specify what version of Turing you are using
Version 4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DtY
|
Posted: Mon Apr 19, 2010 7:27 pm Post subject: RE:Pong Help |
|
|
I don't get what the problem is? Is this an assignment with the requirement of using physics or trigonometry?
If that's all you need to do, you could add a display that tells you what angle the ball is coming towards your paddle at. |
|
|
|
|
|
landfarm
|
Posted: Mon Apr 19, 2010 7:45 pm Post subject: RE:Pong Help |
|
|
Sorry for not being clear. The problem is I don't know how incorporate that into the program. Like, if the ball hits the paddle at 20 degrees, then what? Oh yea, I added an extra "facx *= -1". Also, I just noticed that the ball sometimes gets stuck in the walls and just slides up or down it. What's going on there? |
|
|
|
|
|
TerranceN
|
Posted: Mon Apr 19, 2010 8:52 pm Post subject: RE:Pong Help |
|
|
Dont worry about angles, just reverse the y-direction (this will give the same effect) when it hits the paddles (but not the x-direction too). It gets stuck on the wall because of the if statement that stops y-axis movement when the ball is out of a certain range. Make the ball always move (get rid of the if statements), and let the walls take care of keeping the ball in the game.
Hope that helps. |
|
|
|
|
|
landfarm
|
Posted: Wed Apr 21, 2010 4:19 pm Post subject: RE:Pong Help |
|
|
Oh yea, my bad. I forgot about that. K, I didn't understand what you were getting at TerranceN, but I found a work-around, so that when the ball did touch the walls, it would instantly move away 5, in addition to the negative velocities. Thanks! |
|
|
|
|
|
TerranceN
|
Posted: Wed Apr 21, 2010 6:13 pm Post subject: RE:Pong Help |
|
|
Sorry, I should have been more clear. I meant this part:
Turing: | if yc - 20 >= 0 and yc + 20 <= maxy then
yc + = (vely * facy )
end if
if xc - 20 >= 0 and xc + 20 <= maxx then
xc + = (velx * facx )
end if |
Because of these if statements, when it goes out of bounds it will stop moving, and your other out of bounds checking only changes direction so it will not be moved back inside the bounds. |
|
|
|
|
|
landfarm
|
Posted: Tue May 04, 2010 6:30 pm Post subject: RE:Pong Help |
|
|
Um, sorry for a late reply. I just checked this thread now. Anyway, thanks TerranceN for helping. Turns out that's what I fixed at the same time. |
|
|
|
|
|
USEC_OFFICER
|
Posted: Wed May 05, 2010 1:48 pm Post subject: RE:Pong Help |
|
|
Figures. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|