Created
November 19, 2022 10:52
-
-
Save ProfAndreaPollini/e0e3b90c5c7508971f646318181d84dd to your computer and use it in GitHub Desktop.
Pong - Game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package pong; | |
| import processing.core.PGraphics; | |
| import processing.core.PVector; | |
| public class Ball { | |
| private PVector pos; | |
| private PVector vel; | |
| private int mag; | |
| public Ball(PVector pos, int mag) { | |
| this.pos = pos; | |
| this.mag = mag; | |
| this.vel = new PVector((float)Math.random()*18, (float)Math.random()*18); | |
| } | |
| public boolean checkCollision(int w, int h) { | |
| if (pos.y -mag< 0) { | |
| vel.y = -vel.y; | |
| pos.y = mag; | |
| return true; | |
| } | |
| if (pos.y+ mag >= h) { | |
| vel.y = -vel.y; | |
| pos.y = h-mag; | |
| return true; | |
| } | |
| if (pos.x -mag< 0) { | |
| vel.x = -vel.x; | |
| pos.x = mag; | |
| return true; | |
| } | |
| if (pos.x + mag >= w) { | |
| vel.x = -vel.x; | |
| pos.x = w -mag; | |
| return true; | |
| } | |
| return false; | |
| } | |
| public void draw(PGraphics gfx) { | |
| gfx.circle(pos.x, pos.y,mag); | |
| } | |
| public void move() { | |
| pos = PVector.add(pos,vel); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package pong; | |
| import processing.core.PApplet; | |
| import processing.core.PVector; | |
| import java.util.Vector; | |
| public class Pong extends PApplet { | |
| //private Ball ball; | |
| private int collisions; | |
| private Vector<Ball> balls; | |
| public Pong() { | |
| collisions = 0; | |
| balls = new Vector<>(); | |
| } | |
| @Override | |
| public void settings() { | |
| size(800,600); | |
| } | |
| @Override | |
| public void setup() { | |
| //ball = new Ball(new PVector(width/2,height/2),10); | |
| balls.add(new Ball(new PVector(width/2,height/2),10)); | |
| } | |
| @Override | |
| public void draw() { | |
| background(55,55,55); | |
| for(var i = 0; i < balls.size();i++) { | |
| var ball = balls.get(i); | |
| ball.move(); | |
| var collided = ball.checkCollision(width, height); | |
| if (collided) { | |
| collisions++; | |
| balls.add(new Ball(new PVector(width/2,height/2),10)); | |
| } | |
| ball.draw(getGraphics()); | |
| } | |
| textSize(48); | |
| text(collisions,width-200,40); | |
| } | |
| public static void main(String[] args) { | |
| PApplet.main("pong.Pong"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment