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.
Page Index Toggle Pages: 1
ping pong game (Read 1833 times)
ping pong game
Jan 25th, 2010, 6:58am
 
Yesterday I started writing a simple pingpong game sketch. I have problem with the conditional part. If you uncomment it, the ball will levitate in the middle, instead of bouncing between the two pads.

Quote:
float ay;
float by;
PVector location;
PVector velocity;

void setup(){
  size(500,250);
  location=new PVector(50,100);
  velocity=new PVector(1,random(.1,2));
  smooth();
}
void draw(){
  ay=mouseY;
  by=mouseY;
  background(100);
  location.add(velocity);
  rect(0,ay,20,100);
  rect(480,by,20,100);
  ellipse(location.x,location.y,10,10);
  if((location.x>475&&location.y>by&&location.y<by+100)
  //||(location.x>30&&location.y>ay&&location.y<ay+100) // uncomment me to see the problem!
  ){
    velocity.x=velocity.x*-1;
    velocity.y=velocity.y*-1;
  }
  if(location.y>height-5||location.y<5){
    velocity.y=velocity.y*-1;
  }
}






Any help welcome! Looking forward to you answers!

Greg
Re: ping pong game
Reply #1 - Jan 25th, 2010, 7:51am
 
Wild guess (untested): change location.x>30 to location.x<30, if I understand correctly the test.
Hint: define final int PAD_HEIGHT = 100; and use it instead of hard-coding 100 everywhere. Helps in making the sketch flexible, and more readable (less "guess what this number means").
Re: ping pong game
Reply #2 - Jan 25th, 2010, 10:46am
 
You're a star! Thanks! It's working! My first ever computer game:D
Page Index Toggle Pages: 1