Engineering Projects/Poppit/Howard Community College/Fall2011/550 prgm/sourcePong.txt
// Pong Game for the language 'Processing' //
int size = 10; float xpos, ypos, x, y;
float xspeed = 8; float yspeed = 5.2;
int xdirection = 1; // Left or Right int ydirection = 1; // Top to Bottom
void setup() {
size(600, 250); noStroke(); frameRate(30); smooth(); newStart(); // Set the starting position of the shape
}
void newStart() {
size = 10; xspeed = 8; yspeed = 5.2;
xdirection = 1; // Left or Right ydirection = 1; // Top to Bottom xpos = width/2; ypos = height/2; x = 5; y = height/2;
}
void draw() {
background(100); // Update the position of the shape xpos = xpos + ( xspeed * xdirection ); ypos = ypos + ( yspeed * ydirection ); //Update paddle position if(keyPressed) { if (keyCode == UP) {y -= 10;} else if (keyCode == DOWN) {y += 10;} } if(keyPressed && keyCode == RIGHT) {x = 10;} else {x = 5;} //Test to see if paddle reaches end if (y > height-size-30) { y = height-size-30; } else if(y < 0) { y = 0; } // Test to see if the shape exceeds the boundaries of the screen // If it does, reverse its direction by multiplying by -1 if (xpos > width-size) { xdirection *= -1; } if (ypos > height-size || ypos < 0) { ydirection *= -1; } if(xpos < x+5){ if(ypos < y+42 & ypos > y-2){ xdirection *=-1;} else{ newStart();} }
// Draw the shape ellipse(xpos+size/2, ypos+size/2, size, size); //Draw the paddle rect(x, y, 5, 40);
}