
-----------------------------------
Schaef
Wed Dec 10, 2003 11:12 pm

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.

-----------------------------------
santabruzer
Wed Dec 10, 2003 11:36 pm


-----------------------------------
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:
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
Thu Dec 11, 2003 7:56 am


-----------------------------------
What does all of this stuff mean??

drawfilloval (ball (1), ball (2), ball (3), ball (4), ball (5))

-----------------------------------
santabruzer
Thu Dec 11, 2003 8:58 am


-----------------------------------
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 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 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:
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
Thu Dec 11, 2003 6:21 pm


-----------------------------------
ok thx a lot man.  Im just starting turing and i havent learned about arrays yet so that is a big help

-----------------------------------
santabruzer
Thu Dec 11, 2003 8:14 pm


-----------------------------------
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 [url=http://www.compsci.ca/v2/viewtopic.php?t=1117]Array Tutorial

-----------------------------------
DanShadow
Fri Dec 12, 2003 4:13 pm


-----------------------------------
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.

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=maxy then
ystep:=-1
elsif bally-15