Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ball ball;
- Paddle p1;
- Paddle p2;
- import ddf.minim.*;
- Minim minim;
- AudioPlayer song;
- int p1Score = 0;
- int p2Score = 0;
- void setup() {
- size(1000, 700, P3D);
- background(5);
- lights();
- stroke(#FFD900);
- minim = new Minim(this);
- ball = new Ball();
- p1 = new Paddle(1);
- p2 = new Paddle(2);
- fill(255);
- rectMode(CENTER);
- textSize(80);
- }
- void draw() {
- background(5);
- if (p1Score >= 15) {
- gameover();
- textSize(48);
- text("Player 1 wins!!", width/2, height*.75);
- } else if (p2Score >= 15) {
- gameover();
- textSize(48);
- text("Player 2 wins!!", width/2, height*.75);
- }
- p1.update();
- p1.display();
- p2.update();
- p2.display();
- ball.update();
- ball.display();
- text(p1Score, width/2-100, 100);
- text(p2Score, width/2+50, 100);
- rect(width/2, height/2, 5, height);
- }
- void gameover() {
- noLoop();
- fill(0);
- rect(width/2, height/2, width, height);
- fill(255);
- textSize(128);
- textAlign(CENTER, CENTER);
- text("GAME OVER", width/2, height/2);
- }
- class Paddle {
- int player;
- int pWidth = 20;
- int pHeight = 100;
- float x;
- float y;
- Paddle(int pNum) {
- player = pNum; //setting the which player value from the constructor value
- if (player == 1) {
- x = 30; //set player 1 to left side of the screen
- } else if (player == 2) {
- x = width-30; //set player 2 to right side of the screen
- }
- y = height/2;
- }
- void update() {
- if (player == 2) {
- if (keyPressed) {
- if (key == CODED) {
- if (keyCode == UP) {
- y-=10;
- } else if (keyCode == DOWN) {
- y+=10;
- }
- }
- }
- } else if (player == 1) {
- if (keyPressed) {
- if (key == 'q' || key == 'Q') {
- y-=10;
- } else if (key == 'a' || key == 'A') {
- y+=10;
- }
- }
- }
- if (ball.posX > x - pWidth/2 && ball.posX < x + pWidth/2) {
- if (ball.posY > y - pHeight/2 && ball.posY < y + pHeight/2) {
- ball.contact();
- }
- }
- }
- void display() {
- pushMatrix();
- translate(x, y);
- fill(200);
- box(pWidth, pHeight, 20);
- x = constrain(x, width - width, width);
- y = constrain(y, height - height, height);
- popMatrix();
- }
- }
- class Ball {
- float posX;
- float posY;
- float velX;
- float velY;
- float size;
- Ball() {
- posX = width/2;
- posY = height/2;
- if (int(random(2)) == 0) {
- velX = 9;
- } else {
- velX = -9;
- }
- velY = random(-5, 5);
- size = 20;
- }
- void update() {
- if (posX < 0) { //if the ball goes off the left side of the screen
- p2Score++; //increase player 2 score
- posX = width/2;
- posY = height/2;
- velX = -9;
- velY = random(-5, 5);
- } else if (posX > width) {
- p1Score++; //increase player 1 score
- posX = width/2;
- posY = height/2;
- velX = 9;
- velY = random(-5, 5);
- }
- if (posY > height - size/2) { //bounce off the bottom
- posY = height - size/2;
- velY = -velY;
- } else if (posY < 0 + size/2) { //bounce off the top
- posY = 0 + size/2;
- velY = -velY;
- }
- posX += velX;
- posY += velY;
- }
- void display() {
- pushMatrix();
- translate(posX, posY);
- sphere(20);
- popMatrix();
- }
- void contact() { //call this function when the ball contacts the paddle
- song = minim.loadFile("pongsound.mp3");
- song.play();
- posX = posX - velX; //step the ball back one frame
- velX = -velX;
- velY = random(-5, 5);
- velX += velX * 0.01; //increase the speed of the ball by 1% every time it hits a paddle
- velY += velY * 0.01;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement