Computer Science Canada PONG HELP - Need help bouncing ball off paddle? |
Author: | ashvinthecha [ Thu Jun 04, 2009 4:38 pm ] |
Post subject: | PONG HELP - Need help bouncing ball off paddle? |
I dont know how to say if ballx hits the paddle then the dirRight causes the ball to bounce the other way setscreen ("offscreenonly") var x : int var y : int var p1 : string var p2 : string var ashvin := 100 var ashvin2 := 180 var ashvin3 := 100 var ashvin4 := 180 var ballx := maxx div 2 var bally := maxy div 2 var background := white var getKey : array char of boolean procedure Screen drawfillbox (0, 0, maxx, maxy, background) drawfillbox (20, ashvin, 30, ashvin2, black) drawfillbox (maxx - 20, ashvin3, maxx - 30, ashvin4, black) end Screen procedure MoveP1Up drawfillbox (20, ashvin2, 30, ashvin, background) ashvin += 2 ashvin2 += 2 drawfillbox (20, ashvin2, 30, ashvin, black) delay (3) end MoveP1Up procedure MoveP1Down drawfillbox (20, ashvin2, 30, ashvin, background) ashvin -= 2 ashvin2 -= 2 drawfillbox (20, ashvin2, 30, ashvin, black) delay (3) end MoveP1Down procedure MoveP2Up drawfillbox (maxx - 20, ashvin3, maxx - 30, ashvin4, background) ashvin3 += 2 ashvin4 += 2 drawfillbox (maxx - 20, ashvin3, maxx - 30, ashvin4, black) delay (3) end MoveP2Up procedure MoveP2Down drawfillbox (maxx - 20, ashvin3, maxx - 30, ashvin4, background) ashvin3 -= 2 ashvin4 -= 2 drawfillbox (maxx - 20, ashvin3, maxx - 30, ashvin4, black) delay (3) end MoveP2Down process balldir var dirUp : boolean := true var dirRight : boolean := true loop cls if dirUp then if bally >= 10 then bally := bally - 5 else dirUp := false end if else if bally <= maxy then bally := bally + 5 else dirUp := true end if end if if dirRight then if ballx >= 10 then ballx := ballx - 5 else dirRight := false end if else if ballx <= maxx then ballx := ballx + 5 else dirRight := true end if end if Draw.FillOval (ballx, bally, 10, 10, black) delay (50) end loop end balldir process movePaddels loop % draw paddle on left drawfillbox (20, ashvin, 30, ashvin2, black) Input.KeyDown (getKey) if getKey ('q') then MoveP1Up end if if getKey ('a') then MoveP1Down end if % if we are running off of the screen don't move off of the screen if (ashvin <= 0) and (ashvin2 <= 80) then ashvin := 0 ashvin2 := 80 elsif (ashvin >= maxy - 80) and (ashvin2 >= maxy - 80) then ashvin2 := maxy ashvin := maxy - 80 end if % other paddel -- on right drawfillbox (maxx - 20, ashvin3, maxx - 30, ashvin4, black) Input.KeyDown (getKey) if getKey (KEY_UP_ARROW) then MoveP2Up end if if getKey (KEY_DOWN_ARROW) then MoveP2Down end if if (ashvin3 <= 0) and (ashvin4 <= 80) then ashvin3 := 0 ashvin4 := 80 elsif (ashvin3 >= maxy - 80) and (ashvin4 >= maxy - 80) then ashvin4 := maxy ashvin3 := maxy - 80 end if View.Update end loop end movePaddels % beginning of program fork movePaddels fork balldir |
Author: | Tony [ Thu Jun 04, 2009 4:59 pm ] | ||
Post subject: | RE:PONG HELP - Need help bouncing ball off paddle? | ||
Although unless the ball never travels faster than 1 pixel per frame, the exact match might not happen. |
Author: | ashvinthecha [ Thu Jun 04, 2009 5:18 pm ] |
Post subject: | RE:PONG HELP - Need help bouncing ball off paddle? |
im sorry but that does not really help me out because i dont use ball velosity in my program and i dont have a thing for my paddle. to be honest i dont understand turing to well and i cud just use some clarification |
Author: | Tony [ Thu Jun 04, 2009 5:38 pm ] |
Post subject: | RE:PONG HELP - Need help bouncing ball off paddle? |
http://compsci.ca/blog/super-paper-programming/ - take out a piece of paper - sketch out the layout of your pong game - describe how you would know if the ball is hitting the paddle or not |
Author: | ashvinthecha [ Thu Jun 04, 2009 6:15 pm ] |
Post subject: | RE:PONG HELP - Need help bouncing ball off paddle? |
alright i have attempted that method on several occasions and it's not like i havent attempted other variations. Im sorry but i still dont understand. |
Author: | Kharybdis [ Thu Jun 04, 2009 8:22 pm ] |
Post subject: | RE:PONG HELP - Need help bouncing ball off paddle? |
try whatdotcolour collision detection. When the colour of the paddle has been detected, assign the x value and y values to be the negatives of each other. |