Code

//go ahead and try to beat Master Fukiko at table tennis. Watch out for her killer serve!
//need more practice? Ping Pong at Gooski's every Wednesday night!
//got the code for bouncing ball and paddle constraints from REAS (http://www.processing.org/learning/index.html)
//got my shoes from Butterfly (http://www.butterflyonline.com)


//-------------------
int ball_size = 10;       // Width of the shape 
float ball_x, ball_y;    // Starting position of shape    

float ball_xspeed = 7;  // Speed of the shape 
float ball_yspeed = 7;  // Speed of the shape 

int ball_xdirection = 1;  // Left or Right 
int ball_ydirection = 1;  // Top to Bottom

int paddle_width = 4; 
int paddle_height = 40;
float paddle_y;
float paddle_x;

int fukiscore = 0;
int youscore = 0;
PFont myFont; 

//--------------------
void setup()
{
  size(600, 400);
  smooth(); 
  noStroke();
  paddle_x = 580;

  myFont = loadFont ("CourierNew36.vlw");
  textFont(myFont, 36);


}

//--------------------
void draw(){ 

  drawTable();
  drawBall();
  drawPaddle();
  testCollision();
  drawScore();
}


//-------------------
void drawScore(){
  fill(255);
  text("fuki: " + fukiscore, 20, 40);
  fill (255, 75,75);
  text("you: 0 ", 20, 65);

  if (fukiscore > 20){
    fill (255, 255, 255);
    text("You Loose!", 20, 95);
  }

}


//--------------------
void testCollision(){

  boolean ballRightofPaddle = (ball_x+(ball_size/2) > paddle_x);
  boolean ballUnderPaddleTop = (ball_y > paddle_y);
  boolean ballAbovePaddleBottom = (ball_y < (paddle_y + paddle_height));
  if (ballRightofPaddle && ballUnderPaddleTop && ballAbovePaddleBottom){
    ball_xdirection *= -1;

  }
}


//--------------------
void drawTable(){

  background(54,79,49);
  stroke(200,200,200);
  strokeWeight(2);

  line(0,0,0,600);
  line(599,0,599,400);
  line(0,0,600,0);
  line(0,399,600,399);
  line(0,200,600,200);

  strokeWeight(6);
  line(300,0,300,400);
}


//--------------------
void drawBall(){

  stroke(255, 200, 50);
  strokeWeight(1);



  // Update the position of the shape 
  ball_x = ball_x + ( ball_xspeed * ball_xdirection ); 
  ball_y = ball_y + ( ball_yspeed * ball_ydirection ); 

  // Test to see if the shape exceeds the boundaries of the screen 
  // If it does, reverse its direction by multiplying by -1 

  boolean ballRightOfRightWall = (ball_x+ball_size/2 > width);
  boolean ballLeftOfLeftWall   = (ball_x < 0);
  boolean ballTopofTopWall = (ball_y < 0);
  boolean ballBelowBottomWall = (ball_y+ball_size/2 > height);

  if (ballLeftOfLeftWall){
    ball_xdirection *= -1; 
  } 
  if ( ballTopofTopWall || ballBelowBottomWall ) { 
    ball_ydirection *= -1; 
  } 

  if (ballRightOfRightWall){
    fukiscore=fukiscore+1;
    ball_x= 0;
    ball_y = random(ball_y);
  } 


  // Draw the shape 
  fill(255, 200, 50);
  ellipseMode(CENTER);
  ellipse(ball_x, ball_y, ball_size, ball_size); 
}

//--------------------
void drawPaddle(){

  // Constrain paddle to screen 
  paddle_y = constrain(mouseY, 0, height-paddle_height);

  stroke (255,75, 75);
  fill (255, 75,75);
  strokeWeight(1);
  rect(paddle_x, paddle_y, paddle_width, paddle_height); 

}

020 -- Motion and Mouse (Two Options): Due Tuesday, 9/20

Statement:Assignments numbered #02X are due on Tuesday, 9/20. For Exercise 020, please complete EITHER Option A (Order-Chaos Composition) OR Option B (Pong/Breakout). You are not asked to complete both.

Option A: An Order-Chaos Composition. Create a dynamic (e.g. time-based) composition whose degree of order (or chaos) is parameterized by the horizontal position of the mouse. In other words, use the mouseX as a "knob" to adjust the composition's entropy, proportional to cursor values ranging from 0 (most ordered) to width (most chaotic).

Option B: Pong/Breakout. Pong is one of the first computer games -- a fitting place to begin an examination of the relation between graphics and interaction. Create a version of the game Pong. For some help, you can refer to these examples: bouncing, collision, an example Pong system in Processing. If you wish, you can extend your Pong-system so that puck collisions or movements affect the game's environment (e.g. as in the arcade game Breakout).

You may interpret the core Pong concept freely (for example, you might prefer to make a system with simulated gravity, such as the game "Tennis for Two" created in 1958 ). While 1-player and 2-player versions are options available to you, feel free to make a 0-player system with an agent that learns how to play Pong on its own. The only requirement is that your system must keep score.

hide statement