Turing Pong Paddle Problem
Author |
Message |
Schaef
|
Posted: Wed Dec 10, 2003 11:12 pm Post subject: Turing Pong Paddle Problem |
|
|
I have a pong program that has paddles that are 100 pixels high and 10 wide. I cannot figure out a way to get the ball to bounce off of the paddles. Im not sure if i can set the paddle as a variable itself or something.. neways ff you know how to do this plz send an answer. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
santabruzer
|
Posted: Wed Dec 10, 2003 11:36 pm Post subject: (No subject) |
|
|
you should have an if statement that will bounce the ball away from the paddle at a certain amount..
I did a program simular to what you are doing, but i remeber it being very basic.. well.. here it is:
code: | setscreen ("graphics:320;480")
var chars : array char of boolean
var ball : array 1 .. 5 of int := init
(160, 240, 10, 10, 80)
var cx, cy := 2
var x : array 1 .. 2 of int := init (130, 190)
var y : array 1 .. 2 of int := init (20, 20)
var d : int
var yn : string
loop
put "Do you want to play? (Y/N)"
color (black)
get yn
if yn = "y" or yn = "Y" then
setscreen ("offscreenonly")
cls
loop
drawfilloval (ball (1), ball (2), ball (3), ball (4), ball (5))
if ball (1) > maxx - ball (3) or ball (1) < ball (3) then
cx := -cx
end if
if ball (2) > maxy - ball (4) or ball (2) < ball (4) then
cy := -cy
end if
ball (1) += cx
ball (2) += cy
exit when ball (2) < 15
drawline (x (1), y (1), x (2), y (2), 3)
Input.KeyDown (chars)
if chars (KEY_RIGHT_ARROW) then
for i : 1 .. 2
x (i) := x (i) + 1
drawline (x (1), y (1), x (2), y (2), 1)
end for
end if
if chars (KEY_LEFT_ARROW) then
for i : 1 .. 2
x (i) := x (i) - 1
drawline (x (1), y (1), x (2), y (2), 1)
end for
end if
if ball (2) = 30 and ball (1) > x (1) and ball (1) < x (2)
then
cy := -cy
end if
if x (2) > 319
then
x (2) := 319
x (1) := 259
elsif x (2) < 2
then
x (2) := 2
x (1) := 62
end if
exit when ball (2) < 10
View.Update
delay (8)
cls
end loop
cls
setscreen ("nooffscreenonly")
put "You loose!"
elsif yn = "n" or yn = "N"
then exit
else put "error"
end if
end loop
|
Don't Copy it.. just look at what i did... it's not that practical and compact anyways... |
|
|
|
|
|
Schaef
|
Posted: Thu Dec 11, 2003 7:56 am Post subject: (No subject) |
|
|
What does all of this stuff mean??
drawfilloval (ball (1), ball (2), ball (3), ball (4), ball (5)) |
|
|
|
|
|
santabruzer
|
Posted: Thu Dec 11, 2003 8:58 am Post subject: (No subject) |
|
|
they are arrays.. well yea.. it's just a lot easier to do it with them...
well.. anyways.. the main thing you gotta look at is code: | if ball (2) = 30 and ball (1) > x (1) and ball (1) < x (2)
then
cy := -cy
end if |
basically here, if the ball gets below 30 pixels from the bottom, it will bounce back... (ball (1 .. 5) are basically the ball's paramiters.. as seen here code: | drawfilloval (ball (1), ball (2), ball (3), ball (4), ball (5)) |
so here, i set the value for ball (1), ball (2).. etc.. in the very top:
code: | var ball : array 1 .. 5 of int := init
(160, 240, 10, 10, 80) |
so you should be able to do that with only two variables, it's just that i wanted to have control over color and radius as well... |
|
|
|
|
|
Schaef
|
Posted: Thu Dec 11, 2003 6:21 pm Post subject: (No subject) |
|
|
ok thx a lot man. Im just starting turing and i havent learned about arrays yet so that is a big help |
|
|
|
|
|
santabruzer
|
Posted: Thu Dec 11, 2003 8:14 pm Post subject: (No subject) |
|
|
just remember, if you are in grade 10 computer science... just watch out from using arrays.. my teacher for some reason, allows me to use them, but the rest of the class has no special permission.. *feels special*... depends on who your teacher is..
And if you are wondering about arrays, there's the tutorial at Array Tutorial |
|
|
|
|
|
DanShadow
|
Posted: Fri Dec 12, 2003 4:13 pm Post subject: (No subject) |
|
|
Ok Schaef...his way is a little more difficult than it has to be. There are many ways to approach this problem, but I have a good on for you. First, use whatever you have for collision detection, (like whatdotcolor), and you should also have a variable called xstep, and one called ystep. Check out this program...it is how a ball would move around the screen using those two variables.
code: |
var ballx,bally : int :=200
var xstep,ystep : int := 1
loop
ballx:=ballx+xstep
bally:=bally+ystep
Draw.FillOval(ballx, bally, 15, 15, 0)
delay(5)
Draw.FillOval(ballx, bally, 15, 15, 12)
if ballx+15>=maxx then
xstep:=-1
elsif ballx-15<=0 t hen
xstep:=+1
elsif bally+15>=maxy then
ystep:=-1
elsif bally-15<=0 then
ystep:=+1
end if
end loop
|
This makes a red ball bounce around the screen, when it hits a wall, it will not continue moving up, but go down in the same direction. |
|
|
|
|
|
Schaef
|
Posted: Fri Dec 12, 2003 4:54 pm Post subject: (No subject) |
|
|
ok thx, all you are missing is
View.Set ("offscreenonly") at the beginning of the code and View.Update and cls after the end if.
That helps a lot though.. my old bouncing code was much more complex. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DanShadow
|
Posted: Fri Dec 12, 2003 5:37 pm Post subject: (No subject) |
|
|
lol, your welcome. and I didnt add the view.update and setscreen and cls because I chose not to..heh |
|
|
|
|
|
Andy
|
Posted: Fri Dec 12, 2003 5:39 pm Post subject: (No subject) |
|
|
you should use a structure... i wrote a tutorial on it...
o and santabruzer, u get sum bits for advertising for me! |
|
|
|
|
|
|
|