IAT265 Pong Example.docx

9 Pages
87 Views
Unlock Document

Department
Interactive Arts & Tech
Course
IAT 265
Professor
Matt Lockyer
Semester
Winter

Description
Pong Main /* Goal: Implement a simple PONG game. Features: - two paddles that bounce the ball - a ball that moves about the screen - If the ball goes off the end of the sketch, the opposite player gets a point Player 1 - left Player 2 - right // TODO - Add independent controls - Allow paddle to inflence the x velocity */ int leftPlayerScore, rightPlayerScore; // player scores Ball ball; Paddle leftPlayer, rightPlayer; ArrayList objects; // list of the paddles, and ball boolean serveReady; void setup() { size(400, 400); objects = new ArrayList(); // intialize the objects list serveReady = true; ball = new Ball(new PVector (width/2, height/2), new PVector(-1, 0), 30); objects.add( ball); leftPlayer = new Paddle(new PVector (30, height/2), new PVector(0, 0), 30, 100); objects.add( leftPlayer); rightPlayer = new Paddle(new PVector (width - 30, height/2), new PVector(0, 0), 30, 100); objects.add( rightPlayer); } void draw() { background(255); drawNetLine(); // draw all the objects in the scene for (MovingObject o: objects) { o.update(); o.render(); } if (rightPlayer.doesCollide(ball) || leftPlayer.doesCollide(ball)) { ball.velx(ball.velx() *-1); } // check if the ball goes off the right edge // give point to player 1 if (ball.x() > width + ball.size/2) { leftPlayerScore++; // move Ball in front of leftPlayer's paddle ball.x(leftPlayer.frontEdge() + ball.size/2); // set the velocity equal to zero ball.vel.x = 0; // set flag to indicate if ball should be released serveReady = true; } // check if the ball goes off the left edge // give point to player 2 if (ball.x() < ball.size/2) { rightPlayerScore++; rightPlayer.y(height/2); // move Ball in front of rightPlayer's paddle ball.x(rightPlayer.frontEdge() - ball.size/2); // set the velocity equal to zero ball.vel.x = 0; // set flag to indicate if ball should be released serveReady = true; } if (keyPressed) { if ( key == 'w') { leftPlayer.moveDown(); } else if ( key == 's') { leftPlayer.moveUp(); } else if ( key == 'i') { rightPlayer.moveDown(); } else if ( key == 'k') { rightPlayer.moveUp(); } } // draw the score fill(0); textSize(20); textAlign(LEFT); text(leftPlayerScore, width/4, 30); textAlign(RIGHT); text(rightPlayerScore, 3 * (width/4), 30); } void keyPressed() { if (key == ' ' && serveReady) { serveReady = !serveReady; // check which side of the screen the ball is on // serve it if (ball.onLeft) { ball.velx(2); } else { ball.velx(-2); } } } void drawNetLine() { int dashLength = 20; int numDashes = (height / dashLength) /2; for (int i = 0; i < numDashes; i++) { strokeWeight(2); // 0 0 // 1 // // 2 1 // 3 // // 4 2 // 5 line(width/2, i * 2 * dashLength, width/2, ((2 * i) + 1) * dashLength); } } Ball /* Goal: Implement a simple PONG game. Features: - two paddles that bounce the ball - a ball that moves about the screen - If the ball goes off the end of the sketch, the opposite player gets a point Player 1 - left Player 2 - right // TODO - Add independent controls - Allow paddle to inflence the x velocity */ int leftPlayerScore, rightPlayerScore; // player scores Ball ball; Paddle leftPlayer, rightPlayer; ArrayList objects; // list of the paddles, and ball boolean serveReady; void setup() { size(400, 400); objects = new ArrayList(); // intialize the objects list serveReady = true; ball =
More Less

Related notes for IAT 265

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit