From 97ecd4eb24e9a6c7333dd10997cfda324c9e532c Mon Sep 17 00:00:00 2001 From: Staz <staz@staz.io> Date: Fri, 9 Jun 2017 09:53:19 -0400 Subject: [PATCH] Initial Commit --- Intro_State.pde | 43 +++++++++++++++++ Pong_Processing.pde | 110 ++++++++++++++++++++++++++++++++++++++++++++ Position.pde | 68 +++++++++++++++++++++++++++ 3 files changed, 221 insertions(+) create mode 100644 Intro_State.pde create mode 100644 Pong_Processing.pde create mode 100644 Position.pde diff --git a/Intro_State.pde b/Intro_State.pde new file mode 100644 index 0000000..b249f8d --- /dev/null +++ b/Intro_State.pde @@ -0,0 +1,43 @@ +boolean isHoverOnPlay = false; + +void introState() { + clear(); + textAlign(CENTER, CENTER); + textSize(30); + fill(255); + text("Pong", width/2, height/3); + + rectMode(CENTER); + + if (isHoverOnPlay) + fill(134, 134, 134); + else + fill(255); + rect(width/2, height/2+5, 100, 50); + + fill(0); + text("Play", width/2, height/2); +} + +void touchStarted() { + mouseMoved(); +} +void mouseMoved() { + if (STATE == 0) + isHoverOnPlay = + mouseX >= width/2 - 50 && mouseX <= width/2+50 && + mouseY >= height/2-25 && mouseY <= height/2+30; + //else if (STATE == 4) + // isHoverOnPlay = + // mouseX >= width/2 - 100 && mouseX <= width/2+100 && + // mouseY >= height/2-25 && mouseY <= height/2+30; + else + isHoverOnPlay = false; +} + +void mouseClicked() { + if (STATE == 0) + if (isHoverOnPlay) { + STATE = 1; + } +} \ No newline at end of file diff --git a/Pong_Processing.pde b/Pong_Processing.pde new file mode 100644 index 0000000..6b2a020 --- /dev/null +++ b/Pong_Processing.pde @@ -0,0 +1,110 @@ +final int + PADDLE_WIDTH = 10, + PADDLE_HEIGHT = 75, + BALL_RADIUS = 10; + +Position posHumanPaddle; +Motion ball, posAIPaddle; + +/** + 0 - Intro + 1 - InGame + 2 - Pause + 3 - You Loose + 4 - You Win + **/ +int STATE = 0; + +void setup() { + size(600, 400); + posAIPaddle = new Motion(10, (height - PADDLE_HEIGHT)/2, 0, 0); + posHumanPaddle = new Position(width - PADDLE_WIDTH - 10, (height - PADDLE_HEIGHT)/2); + ball = new Motion(width/2, height/2, 2, 0); +} + +void draw() { + clear(); + fill(255); + noStroke(); + drawField(); + drawBall(); + drawAIPaddle(); + drawHumanPaddle(); + + if (STATE == 0) { + introState(); + } else if (STATE == 1) { + + updateAIPaddle(); + updateBall(); + } else if (STATE == 3) { + text("You Loose", width/2, height/2); + } else if (STATE == 4) { + text("You Win!", width/2, height/2); + } +} + +void drawField() { +} + +void drawBall() { + ellipse(ball.x, ball.y, BALL_RADIUS*2, BALL_RADIUS*2); +} + +void updateBall() { + ball.update(2); + if (ball.y + BALL_RADIUS >= height) + ball.dy = -ball.dy; + else if (ball.y <= BALL_RADIUS) + ball.dy = -ball.dy; + + if (ball.x - BALL_RADIUS >= posAIPaddle.x && ball.x - BALL_RADIUS <= posAIPaddle.x + PADDLE_WIDTH) { + if (ball.y + BALL_RADIUS >= posAIPaddle.y && ball.y - BALL_RADIUS <= posAIPaddle.y + PADDLE_HEIGHT) { + ball.dx = -ball.dx; + ball.dy = (ball.y - (posAIPaddle.y + PADDLE_HEIGHT/2)) * 0.05; + } + } + if (ball.x + BALL_RADIUS >= posHumanPaddle.x && ball.x + BALL_RADIUS <= posHumanPaddle.x + PADDLE_WIDTH) { + if (ball.y + BALL_RADIUS >= posHumanPaddle.y && ball.y - BALL_RADIUS <= posHumanPaddle.y + PADDLE_HEIGHT) { + ball.dx = -ball.dx; + ball.dy = (ball.y - (posHumanPaddle.y + PADDLE_HEIGHT/2)) * 0.05; + } + } + + if (ball.x - BALL_RADIUS > width) + STATE = 3; + else if (ball.x + BALL_RADIUS < 0) + STATE = 4; +} + +void drawAIPaddle() { + rectMode(CORNER); + rect(posAIPaddle.x, posAIPaddle.y, PADDLE_WIDTH, PADDLE_HEIGHT); +} + +void drawHumanPaddle() { + rectMode(CORNER); + rect(posHumanPaddle.x, posHumanPaddle.y, PADDLE_WIDTH, PADDLE_HEIGHT); +} + +void updateAIPaddle() { + posAIPaddle.update(1); + if (posAIPaddle.y + PADDLE_HEIGHT/2 < ball.y - 1) + posAIPaddle.dy = 2.5; + else if (posAIPaddle.y + PADDLE_HEIGHT/2 > ball.y + 1) + posAIPaddle.dy = -2.5; + else + posAIPaddle.dy = 0; + + if (posAIPaddle.y >= height - PADDLE_HEIGHT) + posAIPaddle.y = height - PADDLE_HEIGHT; + else if (posAIPaddle.y <= 0) + posAIPaddle.y = 0; +} + +void keyPressed() { + if (keyCode == UP) + posHumanPaddle.y -= 15; + else if (keyCode == DOWN) + posHumanPaddle.y += 15; +} \ No newline at end of file diff --git a/Position.pde b/Position.pde new file mode 100644 index 0000000..c28736a --- /dev/null +++ b/Position.pde @@ -0,0 +1,68 @@ +class Position { + + public float x, y; + + public Position(float x, float y) { + this.x = x; + this.y = y; + } + + public Position(float x) { + this(x, 0); + } + + public Position() { + this(0, 0); + } + + public int getX() {return (int)this.x;} + public int getY() {return (int)this.x;} + + /** + Moves in the direcrtion specified by the distance + **/ + void move(int direction, int distance) { + if (direction % 2 == 0) // Vert or Horiz? + this.y += (distance*(direction-1)); // dir - 1 to make it positive or negative + else + this.x += (distance*(direction-2)); // dir - 2 to make it positive or negative + } + + boolean at(Position other) { + return this.x == other.x && this.y == other.y; + } + + Position clone() { + return new Position(this.x, this.y); + } +} + +class Motion extends Position { + public float dx, dy; + public Motion(float x, float y, float dx, float dy) { + super(x,y); + this.dx = dx; + this.dy = dy; + } + public Motion(float x, float dx, float dy) { + super(x); + this.dx = dx; + this.dy = dy; + } + public Motion(float dx, float dy) { + super(); + this.dx = dx; + this.dy = dy; + } + public Motion() { + super(); + this.dx = 0; + this.dy = 0; + } + + public void update(float delta) { + this.x += delta * this.dx; + this.y += delta * this.dy; + } + +} \ No newline at end of file