Skip to content

Instantly share code, notes, and snippets.

@niiicolai
Created May 17, 2022 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niiicolai/246eba2b9d8c76355e5e89b5d8af6d95 to your computer and use it in GitHub Desktop.
Save niiicolai/246eba2b9d8c76355e5e89b5d8af6d95 to your computer and use it in GitHub Desktop.
class Player {
// Current position
PVector position;
// Movement direction
float yDir;
// Size
float w = 10;
float h = 25;
// Boundaries
float b = 15;
// Player constructor
public Player(float x, float y, float yDir) {
this.position = new PVector(x, y);
this.yDir = yDir;
}
// Setter Methods
public void setDirection(float yDir) {
this.yDir = yDir;
}
// Getter Methods
public PVector getPosition() {
return position;
}
public float getHeight() {
return h;
}
public float getWidth() {
return w;
}
// Update behavior
public void update() {
// Update player position
position.y += yDir;
// Have the player reached
// the top?
if (position.y < b) {
// Stop movement at the top
position.y = b;
// Have the player reached
// the bottom?
} else if (position.y > height-b-h) {
// Stop the movement at the bottom
position.y = height-b-h;
}
// Set player color to white
fill(255);
// Draw rectangle
rect(position.x, position.y, w, h);
}
}
class Ball {
// Current position
PVector position;
// Current direction
PVector direction;
// Diameter of the circle
float d = 15;
// The speed of the ball
float s = 5;
// Ball constructor
public Ball() {
resetMovement();
}
// Getter Methods
public PVector getPosition() {
return position;
}
// Reset position and randomize direction.
public void resetMovement() {
this.position = new PVector(width/2, height/2);
// Get a random speed
float speed = random(-s, s);
// Set the y direction to half of the speed
// to ensure it moves faster in the left or
// right direction
direction = new PVector(speed, speed/2);
}
// Set direction on the x-axis.
public void setDirection(float x) {
direction.x = x * speed;
}
// Updates the ball's position, add boundaries
// and draw its visuals.
public void update() {
// Add velocity
position.add(direction);
// Check if the ball has reached the top
// or the bottom of the screen.
if (position.y < 0 || position.y > height) {
// Invert the direction on the y-axis.
direction.y = -direction.y;
}
// Set the fill color to white.
fill(255);
// Draw the ball's circle.
circle(position.x, position.y, d);
}
public boolean overlapsWith(Player player) {
// Get player position,
// width, and height.
var p = player.getPosition();
var w = player.getWidth();
var h = player.getHeight();
// Calculate radius.
var r = d/2;
// Loop eight points.
for (int i = 0; i < 8; i++) {
// Convert i * 45 degrees to radians.
var degree = (i * 45) * (PI/180);
// Calculate x and y points by rotating a vector
// relative to the position 45 degrees
var x = r * cos(position.x + degree) + position.x;
var y = r * sin(position.y + degree) + position.y;
// Return true if the point is within on both
// the x-axis and the y-axis.
if (p.x < x && x < p.x + w &&
p.y < y && y < p.y + h) return true;
}
// If none of the points were within
// the player, return false.
return false;
}
}
// Define movement speed
float speed = 3;
// Define scores
float p1Score = 0;
float p2Score = 0;
// Define player variables
Player p1;
Player p2;
// Define ball variables
Ball ball;
// Define the last x position
// of the ball
float lastBallPositionX = 0;
void setup() {
size(500, 500); // Set screen size
// Create the player instances
// at each side of the screen,
// with no y-direction.
p1 = new Player(10, height/2, 0);
p2 = new Player(width-20, height/2, 0);
// Create the ball instance
ball = new Ball();
}
void draw() {
// Set screen background to black.
background(0);
// Update players.
p1.update();
p2.update();
// Update ball.
ball.update();
// Get ball position
PVector ballPosition = ball.getPosition();
// Is the ball outside the
// screen on the left side?
if (ballPosition.x < 0) {
// Give P2 one point.
p2Score += 1;
// Reset ball position and direction.
ball.resetMovement();
}
// Or, is the ball outside the
// screen on the right side?
else if (ballPosition.x > width) {
// Give P1 one point.
p1Score += 1;
// Reset ball position and direction.
ball.resetMovement();
}
// Do the ball overlaps with one of the players?
if (ball.overlapsWith(p1)) {
// Invert x-direction.
ball.setDirection(1);
}
if (ball.overlapsWith(p2)) {
// Invert x-direction.
ball.setDirection(-1);
}
// Move P2 towards the ball if the ball
// is moving towards P2's position
if (lastBallPositionX < ballPosition.x) {
// Get P2's position
PVector p2Position = p2.getPosition();
// Calculate direction
float directionToBallY = ballPosition.y - p2Position.y;
// Constrain the value between -1 and 1
directionToBallY = constrain(directionToBallY, -1, 1);
// Add speed
directionToBallY *= speed;
// Set P2 direction
p2.setDirection(directionToBallY);
}
// Cache the ball's x position
// for next check
lastBallPositionX = ballPosition.x;
// Set the fill color to white.
fill(255);
// Draw the player's score on each side.
text("P1 Score: " + p1Score, 10, 20);
text("P2 Score: " + p2Score, width-80, 20);
}
void keyPressed() {
// Move up
if (key == 'w') p1.setDirection(-speed);
// Move down
else if (key == 's') p1.setDirection(speed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment