So I have decided to make a ping pong game in processing. First of all it is a multiplayer game for two people. I have made two classes one of them is the paddle and the other one is the ball. The paddles are controlled by either “w” and “s” or “up” and “down”. There is a score count and a reset button.
Here is the code:
main file:
ball ball; padle padle1, padle2; color fillVal = color(126); float move = 0; float velocity = 40; float move1 = 0; PFont f; void setup() { rectMode(CENTER); size(1280, 640); f = createFont("Arial", 16, true); ball = new ball(5, 5, 20); padle1 = new padle(width*0.05, height/2, 10, height/4); padle2 = new padle(width*0.95, height/2, 10, height/4); } void keyPressed() { println(keyCode); if (keyCode == 32) { ball.velocityX = 5; ball.ballY = height/2; ball.velocityY = 5; ball.ballX = width/2; ball.counter1 = 0; ball.counter2 = 0; } if (keyCode == 83) { if (move<=200) move+= velocity; } if (keyCode == 87) { if (move>=-200) move -= velocity; } if (keyCode == 38) { if (move1>=-200) move1-= velocity; } if (keyCode == 40) { if (move1<=200) move1+= velocity; } } void draw() { background(0); textFont(f, 32); fill(255); text(ball.counter1, width*0.33, height*0.1); text(ball.counter2, width*0.66, height*0.1); textFont(f, 14); text("'SPACE' to reset score", 5, 14); text("'W' and 'S' to move left paddle", 5, height-7); text("'UP' and 'DOWN' to move right paddle", width*0.78, height-7); drawLine(); ball.run(height/2+move-height/8, height/2+move+height/8, height/2+move1-height/8, height/2+move1+height/8); padle1.display(width*0.05, height/2+move, 10, height/4); padle2.display(width*0.95, height/2+move1, 10, height/4); } void drawLine() { stroke(255); strokeWeight(4); fill(0); line(width/2, 0, width/2, height); }
ball class:
class ball { float velocityX; float velocityY; int size; float ballX = width/2; float ballY = height/2; color fillVal = color(255, 0, 0); int counter1 = 0; int counter2 = 0; ball(float _velocityX, float _velocityY, int _size) { velocityX = _velocityX; velocityY = _velocityY; size = _size; } void update(float a, float b, float c, float d) { //println("velocityX = "+velocityX); if (ballX >= width) { ballX = width/2; ballY = height/2; velocityX = -5; velocityY = 5; counter1+=1; } if (ballX <= 0 ) { ballX = width/2; ballY = height/2; velocityX = 5; velocityY = 5; counter2+=1; } if (ballX >= width*0.94 && ballX <= width*0.96 && ballY >= c && ballY <=d || ballX >= width*0.04 && ballX <= width*0.06 && ballY >= a && ballY <=b) { velocityX*=1.1; velocityY = random(-15,15); velocityX = -velocityX; } ballX += velocityX; if (ballY >= height || ballY <= 0) { velocityY = -velocityY; } ballY += velocityY; } void display() { //ellipse(width/2, height/2, size, size); fill(fillVal); noStroke(); ellipse(ballX, ballY, size, size); } void run(float a, float b, float c, float d) { update(a, b, c, d); display(); } }
paddle class:
class padle { float x; float y; float w; float h; color fillVal = color(0, 255, 0); padle(float _x, float _y, float _w, float _h) { x = _x; y = _y; w = _w; h = _h; } void update() { } void display(float x, float y, float w, float h) { fill(fillVal); rect(x, y, w, h, 5); } void run() { update(); display(x,y,w,h); } }