Computer Science Canada

pong help plz :(

Author:  FakeSound [ Sat May 06, 2006 6:49 pm ]
Post subject:  pong help plz :(

My friend and i are making a game of pong; just a simple game for extra marks in our programming class.
However, we're facing a few problems right now...
a] i can't get the paddles to stay in the screen (I need to set boundaries, but don't know how)
b] the ball doesn't bounce off the paddles (We're not very experienced using whatdotcolor or collision)
so ... i'll post my code right here, any help is greatly appreciated.

code:
setscreen ("graphics:630;420, Title: Pong?")
var x, y, x1, y1, a, b, a1, b1,oy,ox,os: int
var chars : array char of boolean
var nx,ny:int:=1 % changes direction of ball and checks boundries
colorback (255)
x := 10
y := 10
x1 := 20
y1 := 100
a := 10
b := 10
a1 := 20
b1 := 100
oy:=200
ox:=170
os:=10
ox := Rand.Int (10, 100) %set the x quadrant for the 1st oval
oy := Rand.Int (10, 100) %set the y quadrant for the 1st oval


loop
   
   
    /* omit if ox <= 0 then %check to see if it goes off to the left
        nx := +1 %chages the direction */
    if oy <= 0 then %check to see if it goes off the top
        ny := +1 %chages the direction
    end if

    /* omitif ox >= maxx then %check to see if it goes off to the right
        nx := - 1 %chages the direction */
    if oy >= maxy then %check to see if it goes off the bottom
        ny := - 1 %chages the direction
    end if
   
        if ox <= a then
        nx := +1
    elsif oy <= b then
        nx:= +1
        end if

    Input.KeyDown (chars)
   

    if chars ('w') then
        y := y + 5
        y1 := y1 + 5
    end if
    if chars ('s') then
        y := y - 5
        y1 := y1 - 5
    end if

    drawfillbox (x, y, x1, y1, brightgreen)
    drawfillbox (a + 600, b, a1 + 600, b1, brightred)
    drawfilloval (ox,oy,os,os,brightblue)
    delay (10)
    cls


    Input.KeyDown (chars)

    if chars (KEY_UP_ARROW) then
        b := b + 5
        b1 := b1 + 5
    end if
    if chars (KEY_DOWN_ARROW) then
        b := b - 5
        b1 := b1 - 5
    end if
    ox += nx
    oy += ny
 
end loop

Author:  Clayton [ Sat May 06, 2006 7:03 pm ]
Post subject: 

hello welcome to compsci, plz dont post "I need help" or "help plz" as we know you need help, thats why you posted here (in a perfect world), now on to your problem, for the use of whatdotcolor (aka View.WhatDotColor) all you are doing is checking to see if a pixel is a color right? and if your ball has a radius, the best place to check for a collision is on the edge of the ball right? so do something like this
Turing:

if View.WhatDotColor(x,y)=black then
    bounce backwards
end if

its as simple as that, if you are having any problems with anything check out the Turing Walkthrough good luck on the project!!

Author:  FakeSound [ Sat May 06, 2006 7:11 pm ]
Post subject: 

I just tried inputting that, it didn't work
Sorry for posting the 'help plz' . . Embarassed
ummm ... yeah, that didn't work ... if it means anything, i'm using version 4.0.3
but when i tried using that, it said that bounce needs to be declared ... i just can't seem to make the ball bounce off of the paddles. I can get the ball to bouncce off of the walls, that's no problem; however, gettting the paddles to stay within bounds is another thing i can't figure out ... i know this is pretty much a repost of my first post, but .. uhhh... i've clarified as much as i can, sorry Sad

ps its making me kinda mad Mad

Author:  Cervantes [ Sat May 06, 2006 7:24 pm ]
Post subject: 



  1. code:

    if paddle.y < 0 then
        paddle.y := 0
    elsif paddle.y > maxy - paddle.height then
        paddle.y := maxy - paddle.height
    end if

    Or more simply,
    code:

    paddle.y := min (max (paddle.y, 0), maxy - paddle.height)


  2. In pseudocode (note that this code will not run, and the same is true for the pseudocode SuperFreak82 gave you that had "bounce backwards" in it)
    code:

    If there is a collision between the ball and either paddle then
        reverse ball's horizontal velocity
    end

    In Turing,
    code:

    if ball.x - ball.radius < paddle.x + paddle.width and ball.x + ball.radius > paddle.x and ball.y - ball.radius < paddle.y + paddle.height and ball.y + ball.radius > paddle.y then
        ball.vx *= -1  % Multiply the ball's horizontal velocity by -1 and reassign that value to itself
    end if


Author:  wtd [ Sat May 06, 2006 7:48 pm ]
Post subject: 

Cervantes wrote:
code:

if ball.x - ball.radius < paddle.x + paddle.width and ball.x + ball.radius > paddle.x and ball.y - ball.radius < paddle.y + paddle.height and ball.y + ball.radius > paddle.y then
    ball.vx *= -1  % Multiply the ball's horizontal velocity by -1 and reassign that value to itself
end if


That condition so very much needs to be factored out into a function.


: