// Pong by Mitch Said and Jose Olivares // ICM Week 2 ( September 19, 2007) //set up some global variables int xspeed; int yspeed; int x; int y; int rectx = 0; int recty = 0; int rect2x = 400; int rect2y = 0; //our setup function void setup() { size(400,400); //define size //frameRate (50); xspeed = 3; //(int) random(-5,5); //if we want random (note "random" gives us a float yspeed = 5; //(int) random(-5,5); //so we have to "cast" it back into an "int") x = width/2; //initialize our x,y y = height/2; //rectx = 0; // recty = 0; } void draw() { background(100); //first we draw the background ellipseMode(CENTER); //set our ellipse mode noStroke(); fill (255); rectMode(CORNERS); rect (width/2, 0, width/2 + 10, height); fill(255,200,0); //set ellipse color ellipse(x,y,20,20); //draw ellipse rect (rectx, recty, rectx + 25, recty + 100); //rectMode (CORNERS); rect (rect2x - 25 , rect2y, rect2x , rect2y + 100); //adjust x,y based on speed x = x + xspeed; y = y + yspeed; //account for bouncing off left paddle if ((x <= rectx + 25) && (y < recty+100) && (y > recty) && (x < 400) && (x>0)) { xspeed = xspeed * -1; yspeed = yspeed * -1; //yspeed = yspeed + 1; } //account for bouncing off right paddle if ((x > rect2x - 25) && (y < rect2y+100) && (y > rect2y) && (x<400) && (x>0)) { xspeed = xspeed * -1; yspeed = yspeed * -1; //yspeed = yspeed + 1; } if ((y>400) && (x>0)) { //this tells it to bounce off the bottom yspeed = yspeed *-1; } if ((y<0) && (x>0)) { //this tells it to bounce off the bottom yspeed = yspeed *-1; } println (x); } void keyPressed() { if ((key == 'w' ) && (recty>0)) { recty = recty - 50; } else if ((key == 's' ) && (recty + 100 < 400)) { recty = recty +50; } else if ((keyCode == UP ) && (rect2y>0)) { rect2y = rect2y - 50; } else if ((keyCode == DOWN ) && (rect2y + 100 < 400)){ rect2y = rect2y +50; } else if (key == 'x'){ x = width/2; //initialize our x,y y = height/2; } }