Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Staz committed Jun 9, 2017
0 parents commit 97ecd4e
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Intro_State.pde
Original file line number Diff line number Diff line change
@@ -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;
}
}
110 changes: 110 additions & 0 deletions Pong_Processing.pde
Original file line number Diff line number Diff line change
@@ -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;
}
68 changes: 68 additions & 0 deletions Position.pde
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 97ecd4e

Please sign in to comment.