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

Username:   Password: 
 RegisterRegister   
 Pong ball
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Hurtful




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
nate




PostPosted: Mon Jun 02, 2003 9:10 pm   Post subject: m

READ THE COLLISION DETECTING TUTORIAL!!!
Tony




PostPosted: Mon Jun 02, 2003 9:48 pm   Post subject: (No subject)

thats under tutorials Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Hurtful




PostPosted: Mon Jun 02, 2003 10:27 pm   Post subject: (No 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.
Tony




PostPosted: Tue Jun 03, 2003 10:17 am   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Blade




PostPosted: Tue Jun 03, 2003 11:11 am   Post subject: (No 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
Muhammad Sheraly




PostPosted: 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...
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: