2011年1月18日 星期二

Pong (Man Kam Hung, Jasper Fung, Phim Tam)

float xspeed = 0;
float yspeed = 0;
float ballSize = 10;
float x = 250;
float y = 200;
float rpy=200; float rpyspeed;
int press_mouse_msg,right_score,left_score = 0;
void setup()
{
  size(500,400);
  smooth();
}
void draw() {
 background(0);
 fill(255);
 noStroke();
 text(left_score, 15,30);
 text(right_score, 475,30);
 if (press_mouse_msg == 0) text("Press the mouse to begin.", 180+ press_mouse_msg, 175);
 // draw the ball
 ellipse(x,y,ballSize,ballSize);
 x += xspeed ;
 y += yspeed ;

 if (x < ballSize/2){
  right_score +=1;
  x = 250;
  y = 200;
  press_mouse_msg = 0;
  xspeed = 0;
  yspeed = 0; 
}
if (x > width-ballSize/2){
  left_score +=1;
  x = 250;
  y = 200;
  press_mouse_msg = 0;
  xspeed = 0;
  yspeed = 0; 
}
 if (y > height-ballSize/2 || y < ballSize/2){
   yspeed = yspeed * -1;}
 // draw the left paddle
 rect(50,mouseY,10,50);
 if (x-5 < 50+5 && y < mouseY + 50 && y > mouseY && x-5 > 50-5)
   { float paddle_yspeed = mouseY-pmouseY;
     float xc = random (0.5,1.8);
     xspeed = -xspeed* xc;
     yspeed = (5* (paddle_yspeed - yspeed) + 5 * paddle_yspeed + yspeed) / 6;
   }
  // draw the right paddle
 rect(width-50,rpy-25,10,50);
 if (xspeed >0) {
      rpy += rpyspeed;
 if (rpy < y) rpyspeed = 1 + (x-50)/100;
 if (rpy > y) rpyspeed = - (1 + (x-50)/100);
 }
 if (x+5 > width-50-5 && y < rpy+25 && y > rpy-25 && x+5 < width-50+5)
   { float xb = random (0.5,1.8);
     xspeed = -xspeed* xb;
     yspeed = yspeed * -1;
   }
}
void mousePressed(){
  if (xspeed==0) {
  xspeed= random(-5.0,5.0);
  if (xspeed < 0 && xspeed >-1.0) {xspeed = -3;}
  if (xspeed > 0 && xspeed <1.0) {xspeed = 3;}
  yspeed= random(-5.0,5.0);
  if (yspeed < 0 && yspeed >-1.0) {yspeed = -3;}
  if (yspeed > 0 && yspeed <1.0) {yspeed = 3;}
  press_mouse_msg+=100;}
}

沒有留言:

張貼留言