We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Questions about my first Pong program
Page Index Toggle Pages: 1
Questions about my first Pong program (Read 2110 times)
Questions about my first Pong program
Feb 12th, 2010, 3:01pm
 
So I'm pretty new at the Processing language, and have only gotten to the conditionals chapter of Daniel Shiffman's book, yet I think I've done pretty well with this program. Questions are in the comments. But I've one more question I didn't include: Sometimes when the ball is lost, I wait a moment and I see it coming back on screen. I don't know why this is, but how can I fix it? Thanks very much!  Smiley

Code:
//I set my variables
float xstart=100;
float xspeed=5;

void setup() {
size(800,600);
smooth();
}

void draw() {
background(255);

fill(0);
ellipse(xstart,300,50,50);
xstart=xstart+xspeed;

if (xstart>700&&mouseY>225&&mouseY<375) { //If mouse gets to the edge of the right rectangle while the rectangle is in the proper Y-range,
xspeed=-xspeed; //Then bounce the ball back
}
else if (xstart<100&&mouseY>225&&mouseY<375) { //If mouse gets to the edge of the left rectangle while the rectangle is in the proper Y-range,
xspeed=-xspeed; //Then bounce the ball back
}

//My rectangles/paddles
rectMode(CENTER);
fill(0);
rect(50,mouseY,50,150);
rect(750,mouseY,50,150);
}

/*When the ball is lost, I want to be able to click and have the ball reappear in the middle of the screen, heading toward the right. How would I do this?
Clearly my current code isn't accomplishing this.*/
void mousePressed() {
ellipse(xstart,300,50,50);
}
Re: Questions about my first Pong program
Reply #1 - Feb 12th, 2010, 3:49pm
 
try this:

Quote:
void mousePressed(){
 xstart=100;
 xspeed=5;
}


when you draw the ellipse on mousePressed, you're just literally drawing an ellipse at the current ball location (offscreen).  You can also create a conditional that says "if the startx is below 0 or above the width, reset the ball."  By the way, startx is a confusing variable name, since it is really the ball's location, both at the start and thereafter.  ballx or xloc might be less confusing?
Re: Questions about my first Pong program
Reply #2 - Feb 12th, 2010, 4:16pm
 
Thanks Ben, that answered my questions. I thought of one more - obviously my program isn't that good because the ball just keeps going straight. I know I need to change my Y parameter. I'll probably need to set a variable called yball.

How would I go about making it to when the ball hits the bottom of the left rectangle, it will go up at a slant, and the equivalent to the other sides of both rectangles, like in the real Pong game?

I assume lots and lots of conditional if-then statements would work, but is there an easier way?

Thanks  Smiley
Re: Questions about my first Pong program
Reply #3 - Feb 12th, 2010, 5:35pm
 
Yep, there's a simple way to do angle reflection against a vertical or horizontal surface: track x and y velocity separately, and flip one of the axis on collision.

So if your vx is 3 and your vy is -4, you're moving up and to the right (in processing's coordinate plane, anyway).  If you hit the ceiling, you continue to go to the right (vx remains 3, but vy flips (becomes 4).  Now you are headed down and to the right.  If you now hit the right-hand side wall, vx flips -- so you are still headed down, but to the left (vx = -3).
Re: Questions about my first Pong program
Reply #4 - Feb 12th, 2010, 8:38pm
 
Could you, or someone else, please let me see the code for doing that? I don't think I've quite reached the part in Shiffman's book where it discusses functions like that (or maybe I'm just having trouble understanding what needs to be done). I catch on fast though, I like to think.

From what you said, I gather I need to set a variable for my x velocity and y velocity. And maybe something like:
Code:

if {
xball>700&&yball>300} //So if the ball hits the bottom part of the right rectangle,
{
vx=-vx; //My x will go backwards (bouncing off) and
vy=vy+5; //my y will go down.
}
Re: Questions about my first Pong program
Reply #5 - Feb 12th, 2010, 9:32pm
 
It's out there -- check out the "Bounce" example, full path: Examples/Topics/Motion/Bounce

By the way, if the ball hits the bottom of your rect, vx will be unmodified, but vy will reverse.
Re: Questions about my first Pong program
Reply #6 - Feb 12th, 2010, 9:48pm
 
This is what I get when I try incorporating that Bounce example into my code:
Code:
//I set my variables
float xstart=width/2;
float ystart=height/2;

float xspeed=4;
float yspeed=3;

int xdirection=1;
int ydirection=1;

void setup() {
size(800,600);
smooth();
}

void draw() {
background(255);

xstart=xstart+(xspeed*xdirection);
ystart=ystart+(yspeed*ydirection);

fill(0);
ellipse(xstart,300,50,50);
xstart=xstart+xspeed;

if (xstart>700&&mouseY>200&&mouseY<400) { //If mouse gets to the edge of the right rectangle while the rectangle is in the proper Y-range,
xdirection*=-1; //Then bounce the ball back
}
else if (xstart<100&&mouseY>200&&mouseY<400) { //If mouse gets to the edge of the left rectangle while the rectangle is in the proper Y-range,
ydirection*=-1; //Then bounce the ball back
}
else if (xstart<0||xstart>800) {
xstart=400;
}

//My rectangles/paddles
rectMode(CENTER);
fill(0);
rect(50,mouseY,50,150);
rect(750,mouseY,50,150);
}




Now my ball won't even bounce off the rectangles going straight; it just goes straight though. What am I doing wrong?
Re: Questions about my first Pong program
Reply #7 - Feb 13th, 2010, 1:02am
 
The second test is against the left side, but you invert y direction. Should be x, and should have an additional test for top/bottom hits.
Also, don't hard-code numbers, use variables and constants, it helps in understanding what these numbers mean: instead of if (xstart>700&&mouseY>200&&mouseY<400) you can write instead: if (xstart > width - PAD_WIDTH - PAD_POS && mouseY  > 200 && mouseY < 400)
I haven't converted 200 and 400 because I don't understand where these magical numbers come from...
Re: Questions about my first Pong program
Reply #8 - Feb 13th, 2010, 4:23pm
 
Well I'm slowly catching on.

Is there a Pong game already made in Processing that I could check out (source included)? I've Google searched it to no avail.

To be more specific, I'm looking for a Pong game made in Processing that uses only conditionals and variables (nothing more advanced than that), if possible.

Once again, thanks for the help you have all provided.  Smiley
Re: Questions about my first Pong program
Reply #9 - Feb 14th, 2010, 2:40am
 
> Is there a Pong game already made in Processing that I could check out (source included)? I've Google searched it to no avail.

i used the search box at the top of the page there. which gave me a forum post with a link. the link was to examples / collision (it gave me a 404 but was easy enough to find using the links, also at the top of the page)

http://processing.org/learning/topics/collision.html

(edit: i see BenHem pointed you at the examples already)
Re: Questions about my first Pong program
Reply #10 - Feb 15th, 2010, 5:44pm
 
I thank you all for your help and apologize for my ignorance. I've gotten the ball to bounce and everything seems to be working.

There's one small problem I noticed. When my mouseY is all the way up or down, my paddles half-disappear off screen. I tried fixing this with:

Code:

constrain(mouseY,0+hrect/2,height-hrect/2);


Yet that doesn't seem to fix the problem. At any rate, here is my game of Pong so far  Smiley:

Code:

//Set all variables
float xrect1=40;
float wrect=40;
float hrect=180;
float xrect2=760;
float xellipse=400;
float yellipse=200;
float wellipse=40;
float hellipse=40;
float xspeed=8;
float yspeed=4;

void setup() {
size (800,600);
smooth();
}

void draw() {
background(255);
rectMode(CENTER);

fill(0);
rect(xrect1,mouseY,wrect,hrect); //Paddle one
rect(xrect2,mouseY,wrect,hrect); //Paddle two

ellipse(xellipse,yellipse,wellipse,hellipse);

//I set my variables to increase speed so the ball has movement
xellipse=xellipse+xspeed;
yellipse=yellipse+yspeed;

if (xellipse-wellipse/2==xrect1+wrect/2&&yellipse>mouseY-hrect/2&&yellipse<mouseY+hrect/2) { //If the ball hits the left paddle, then
xspeed=-xspeed; //reverse speed away from the paddle
}
else if (xellipse+wellipse/2==xrect2-wrect/2&&yellipse>mouseY-hrect/2&&yellipse<mouseY+hrect/2) { //If the ball hits the right paddle, then
xspeed=-xspeed; //reverse speed away from the paddle
}
else if (yellipse+hellipse/2>height) { //If the ball hits the bottom of the screen, then
yspeed=-yspeed; //have the ball bounce off it
}
else if (yellipse-hellipse/2<0) { //If the ball hits the top of the screen, then
yspeed=-yspeed; //have the ball bounce off it
}
else if (xellipse>width||xellipse<0) { //If either paddles misses the ball, then the ball is reset
xellipse=400;
yellipse=200;
}
}


Re: Questions about my first Pong program
Reply #11 - Feb 16th, 2010, 3:49pm
 
Nice!  No worries about asking for help, as far as I can figure that is what the forums are good for.

constrain(mouseY,0+hrect/2,height-hrect/2);
This will return the constrained value, but this code doesn't do anything with that value, because your paddle rect code is still drawing at mouseY.  To be precise, constrain returns a new value that is the constrained version of your first parameter (mouseY) but doesn't actually change that parameter directly.  You need to save it in a variable or use it immediately:

float paddleY = constrain(mouseY,0+hrect/2,height-hrect/2);
rect(xrect1,paddleY,wrect,hrect); //Paddle one

or, you can put it into your rect code directly:

rect(xrect1,constrain(mouseY,0+hrect/2,height-hrect/2),wrect,hrect); //Paddle one
Page Index Toggle Pages: 1