Hi all,
I got super frustrated trying to make two player pong, so this is my lonely one player pong :(.
I really struggled with the collision detection (and also trying to make it two player) so I didnt invest enough time into the cool features it should have like score, reset/start (boolean?), and phrases that pop up like ‘you lose!’
I think I need a bit more practice, but here is my lazy lonely pong!
code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
//boolean startGame = false; Pong b0; void setup () { size (600,400); background (0); b0= new Pong (); } //void startGame () { //if (key == CODED) // if (keyCode == ' '){ // startGame =!startGame; } //} // //if (startGame) { void draw () { background (0); b0.update (); b0.renderBall(); b0.checkWall(); b0.renderPaddle (); b0.intersect(); //WALL fill (203,103,103); rect (0,0,10,400); } void keyPressed () { b0.movePaddle( ); } |
class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
//VARIABLES class Pong { //BALL float xPos =20; float yPos =20; float xMove=2; float diam; float xSpeed; float ySpeed; float rect; int rectSize=100; //PADDLE float xPaddle; float yPaddle=10; float hPaddle=90; //BALL Pong () { diam = 70; noStroke (); xPaddle=width-15; //xPos = random(diam/2, width-diam/2); // yPos = random(diam/2, width-diam/2); xPos=(width/2); yPos=(height/2); // xSpeed = random(-5, 5); // ySpeed = random(-5, 5); xSpeed=3; ySpeed=3; } void update () { xPos = xPos + xSpeed; yPos = yPos +ySpeed; } void renderBall () { fill(188); ellipseMode(CENTER); ellipse (xPos, yPos, diam, diam); } void checkWall() { if (yPos >height -diam/2 || yPos <diam/2) { ySpeed = ySpeed*-1; } if (xPos <diam/2) { xSpeed = xSpeed*-1; } } //PADDLE void renderPaddle() { fill (255); rect(xPaddle, yPaddle,10,rectSize); } void intersect () { if ((xPos> xPaddle) && (yPos>= yPaddle) && (yPos<= yPaddle + rectSize)) { xSpeed = -xSpeed; diam=diam-5; if (diam<=10){ diam=10;} xSpeed=xSpeed-.5; //ySpeed=ySpeed+1; } } void movePaddle() { if (key == CODED) { if (keyCode == UP) { yPaddle =(yPaddle-50); } else if (keyCode == DOWN) { yPaddle = yPaddle + 50; } } } } //WALL void renderWall (){ fill (188); rect(100,100,100,100);} |