Computer Science Canada

Pong ball

Author:  Hurtful [ Mon Jun 02, 2003 9:09 pm ]
Post subject:  Pong ball

I'm working on a basic pong game but i'm having difficulty with getting the ball (moving and colliding). I got the paddles working but thats about it. How do I make it so that the ball starts it the center, begin moving on a key stroke, and collide off walls and paddles? (I couldn't find what I was looking for in previous threads, maybe I missed something?) Any help would be appreciated.

Author:  nate [ Mon Jun 02, 2003 9:10 pm ]
Post subject:  m

READ THE COLLISION DETECTING TUTORIAL!!!

Author:  Tony [ Mon Jun 02, 2003 9:48 pm ]
Post subject: 

thats under tutorials Wink

Author:  Hurtful [ Mon Jun 02, 2003 10:27 pm ]
Post subject: 

well, I read the tutorial, and managed to get it in to my program. Only details that I'm having still is the ball bouncing off the paddles and the graphics are a little choppy.

Here is what I have so far:

Quote:

var winID : int := Window.Open ("offscreenonly")
Window.Set (winID, "nocursor")
colorback (black)
color (green)
cls

var x, y : int
x := 0
y := maxy div 2
var x2, y2 : int
x2 := maxx
y2 := maxy div 2
var chars, chars2 : array char of boolean
var ballx, bally : int
var nx, ny : int := 1
var s : int := 10

ballx := Rand.Int (10, 100)
bally := Rand.Int (10, 100)
loop
View.Set ("offscreenonly")
Draw.FillOval (ballx, bally, s, s, brightgreen)
View.Update
delay (50)
Draw.FillOval (ballx, bally, s, s, black)

if ballx <= 0 then
nx := 1
elsif bally <= 0 then
ny := 1
end if

if ballx >= maxx then
nx := -1
elsif bally >= maxy then
ny := -1
end if
ballx += nx
bally += ny


Input.KeyDown (chars)

if chars (KEY_SHIFT) then
y := y + 5
end if
if chars (KEY_CTRL) then
y := y - 5
end if

Input.KeyDown (chars2)

if chars (KEY_UP_ARROW) then
y2 := y2 + 5
end if

if chars (KEY_DOWN_ARROW) then
y2 := y2 - 5
end if



View.Set ("offscreenonly")
drawfillbox (x, y, 10 + x, 100 + y, brightgreen)
View.Update
delay (25)
drawfillbox (x, y, 10 + x, 100 + y, black)

View.Set ("offscreenonly")
drawfillbox (x2, y2, x2 - 10, y2 - 100, brightgreen)
View.Update
delay (25)
drawfillbox (x2, y2, x2 - 10, y2 - 100, black)



end loop


I couldn't figure out how to apply what was in the Collision Detection tutorial to this game.

Author:  Tony [ Tue Jun 03, 2003 10:17 am ]
Post subject: 

well do you see how you use
code:

if ballx >= maxx then
nx := -1
elsif bally >= maxy then
ny := -1
end if
ballx += nx
bally += ny


to revese the direction when hiting floor and ceiling? Do the same, but replace the if statment with one from the collision detection tutorial.

Also, why do you keep on setting screen to "offscreenonly" throughout the program? 1 time is enough

Author:  Blade [ Tue Jun 03, 2003 11:11 am ]
Post subject: 

ok. first off i read the collision tutorial tony wrote, but it blew my mind too. maybe it was because it looked a hella lot harder than what it really was. all you do is check to see if the ball is touching either sides. and for the paddles check to see if its in the range of the paddle.
as an example
code:
if ballx >= maxx or ballx <= 0 then
nx:= -nx
elsif bally >= maxy or bally <= 0 then
ny:=-ny
end if

that is something like what i would use to check the sides..
as for the paddles. i'm only gonna help you with the one. if you can figure it out for yourself to do the other... then godspeed son Smile
code:
if ballx >= x & ballx <= x+10 & bally >= y & bally <= y+100 then
%do whatever you want to do when it hits the paddle
end if

Author:  Muhammad Sheraly [ Thu Jun 05, 2003 4:42 pm ]
Post subject:  ping pong

i have the pong game....but just the ball moving around the screen colliding....i need to put some paddles in...im havin trouble with that...


: