Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
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.

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

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.

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!
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 10 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: