Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Processing Code
// think of bounding boxes as a literal box
// this box stores an objects dimensions (x, y, width height) which will be used later for collision detection
class BoundingBox {
float left;
float right;
float top;
float bottom;
float width;
float height;
BoundingBox(float x, float y, float width, float height) {
// width could be negative so check what the minimum (left-most) x
this.left = Math.min(x, x + width);
this.right = Math.max(x, x + width);
// now do same for y
this.top = Math.min(y, y + height);
this.bottom = Math.max(y, y + height);
this.width = this.right - this.left;
this.height = this.bottom - this.top;
}
boolean collides(BoundingBox box) {
boolean doesXOverlap = this.left < box.right && this.right >= box.left;
boolean doesYOverlap = this.top <= box.bottom && this.bottom >= box.top;
return doesXOverlap && doesYOverlap;
}
}
class Exit
{
float x;
float y;
float width;
float height;
boolean isFake;
color blue = (#3641FF); //blue
Exit(float x, float y, float width, float height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
void display()
{
stroke(blue);
fill(blue);
strokeWeight(2);
//noFill();
rect(this.x, this.y, this.width, this.height);
}
BoundingBox getBoundingBox() {
return new BoundingBox(this.x, this.y, this.width, this.height);
}
}
/* Hailey DeMark
Maze Escape
TODO: Collision is off. Need to realign something. - April 23
*/
boolean gameOver = false;
boolean wonGame = false;
Runner runner;
ArrayList<Wall> walls = new ArrayList<Wall>();
ArrayList<Exit> exits = new ArrayList<Exit>();
ArrayList<Monster> monsters = new ArrayList<Monster>();
void setup()
{
size(1150, 700);
background(#1F1F1F);
walls.add(new Wall(width/ 2, 50, 100, 2));
walls.add(new Wall(width/3, 60, 200, 2));
walls.add(new Wall(510, 280, 142, 2));
walls.add(new Wall (120, 200, 2, 300));
walls.add(new Wall (240, 425, 200, 2));
walls.add(new Wall(320, 162, 2, 199));
walls.add(new Wall(221, 282, 100, 2));
walls.add(new Wall(472, 67, 2, 200));
walls.add(new Wall(415, 341, 100, 2));
walls.add(new Wall(473, 173, -50, 2));
walls.add(new Wall(586, 175, 130, 2));
// outer portion of screen
walls.add(new Wall(2, 2, 2, height)); // left top to left bottom
walls.add(new Wall(2, 2, width, 2)); // left top to right top
walls.add(new Wall(width - 2, 2, 2, height)); // right top to right bottom
walls.add(new Wall(2, height - 2, width, height)); // left bottom to right bottom
this.createGame();
}
void createGame() {
// reset game
gameOver = false;
wonGame = false;
this.randomizeExits();
// monsters
// starting position is scaled for monsters
monsters = new ArrayList();
monsters.add(new Monster(350, height - 100));
// add runner
runner = new Runner(width/2, height/2);
}
void randomizeExits() {
// reset the exits (since fake exits are removed)
exits = new ArrayList();
exits.add(new Exit(21, 22, 78, 14));
exits.add(new Exit(1128, 673, -83, -14));
// make all exits fake
for (Exit exit : exits) {
exit.isFake = true;
}
// choose an exit to be the actual exit
int goodExitIndex = int(random(exits.size()));
// make this exit real
exits.get(goodExitIndex).isFake = false;
}
void draw()
{
background(#1F1F1F);
if (gameOver) {
this.showGameOverScreen();
return;
}
if (wonGame) {
this.showVictoryScreen();
return;
}
// display stuff here
// show runner
runner.display();
// display each wall
for (Wall wall : walls) {
wall.display();
}
// display each exit
for (Exit exit : exits) {
exit.display();
}
// display/move each monster
for (Monster monster : monsters) {
monster.update(walls);
monster.display();
}
// check if runner collides with anything here
// where the runner is going
BoundingBox runnerBox = runner.squareOrSomething();
// check all monsters
for (Monster monster : monsters) {
BoundingBox monsterBox = monster.getBoundingBox();
// if runner hits monster
if (runnerBox.collides(monsterBox)) {
// then game over, man. game over.
gameOver = true;
return;
}
}
// check all exits
for (Exit exit : exits) {
BoundingBox exitBox = exit.getBoundingBox();
if (runnerBox.collides(exitBox)) {
// check what kind of exit it is
if (!exit.isFake) {
this.goodExit();
return;
} else {
this.badExit(exit);
return;
}
}
}
// check all walls
for (Wall wall : walls) {
BoundingBox wallBox = wall.getBoundingBox();
if (runnerBox.collides(wallBox)) {
// don't do anything if runner hits wall
return;
}
}
// keep running, runner
runner.update();
}
void goodExit() {
println("WIN!");
this.wonGame = true;
}
void badExit(Exit exit) {
// remove this exit
this.exits.remove(exit);
// add monsters
this.monsters.add(new Monster(width / 2, height / 2));
}
void showGameOverScreen() {
textSize(150);
textAlign(CENTER, CENTER);
text("Game Over", width/2, height/2-50);
}
void showVictoryScreen() {
textSize(150);
textAlign(CENTER, CENTER);
text("You Won!", width/2, height/2-50);
}
void mousePressed()
{
this.createGame();
}
void keyPressed()
{
//runner movement
if (key == 'd')
{ //going right
runner.moveX(2);
//monster.moveX(3);
} else if (key == 'a')
{ //going left
runner.moveX(-2);
// monster.moveX(-3);
} else if (key == 's')
{ //going down
runner.moveY(2);
// monster.moveY(3);
} else if (key == 'w')
{ //going up
runner.moveY(-2);
// monster.moveY(-3);
} else if (key == ' ')
{
runner.moveX(0);
}
// arrow keys move all monsters
else if (key == CODED)
{
if (keyCode == LEFT) // ask if random movement would work like this
{
for (Monster monster : monsters) {
monster.moveX(-4);
}
} else if (keyCode == RIGHT)
{
for (Monster monster : monsters) {
monster.moveX(4);
}
} else if (keyCode == UP)
{
for (Monster monster : monsters) {
monster.moveY(-4);
}
} else if (keyCode == DOWN)
{
for (Monster monster : monsters) {
monster.moveY(4);
}
}
}
}
class Monster
{
float x;
float y;
float vx = 0;
float vy = 0;
// change monster size here
float scale = 0.5;
color green = color(#4C791C);
color white = color(255);
// how fast the monster is
int speed = 3;
Monster(int x, int y)
{
this.x = x;
this.y = y;
// choose which direction monster should move
this.pickDirection();
}
void display()
{
strokeWeight(1);
stroke(0);
fill(#056F00); //green
this.scaledArc(this.x, this.y, 72, 65, radians(180), radians(360)); //top arc head
noStroke();
this.scaledArc(this.x, this.y - 2, 70, 30, 0, PI); //bottom arc stommach
fill(255);
this.scaledArc(this.x + 5, this.y - 3, 50, 50, radians(320), radians(350)); //right eye
this.scaledArc(this.x - 5, this.y - 3, 50, 50, radians(189), radians(223)); //left eye
fill(0);
this.scaledEllipse(this.x - 15, this.y - 9, 5, 7); // left eye ball
this.scaledEllipse(this.x + 16, this.y - 9, 5, 7); //right eye ball
//left legs
strokeWeight(2);
// noStroke();
fill(#056F00);
beginShape();
this.scaledVertex(this.x - 36, this.y - 3);
this.scaledBezierVertex(this.x - 49, this.y - 4, this.x - 55, this.y + 9, this.x - 56, this.y + 12);
this.scaledBezierVertex(this.x - 60, this.y + 21, this.x - 58, this.y + 24, this.x - 59, this.y + 15);
this.scaledBezierVertex(this.x - 49, this.y + 10, this.x - 51, this.y + 13, this.x - 49, this.y + 12);
this.scaledBezierVertex(this.x - 41, this.y + 5, this.x - 37, this.y + 6, this.x - 35, this.y + 6);
this.scaledBezierVertex(this.x - 42, this.y + 7, this.x - 43, this.y + 22, this.x - 44, this.y + 26);
this.scaledBezierVertex(this.x - 35, this.y + 10, this.x - 20, this.y + 28, this.x - 27, this.y + 27);
endShape();
//right legs
beginShape();
this.scaledVertex(this.x + 37, this.y - 2);
this.scaledBezierVertex(this.x + 48, this.y - 2, this.x + 53, this.y + 7, this.x + 55, this.y + 10);
this.scaledBezierVertex(this.x + 59, this.y + 19, this.x + 59, this.y + 24, this.x + 59, this.y + 25);
this.scaledBezierVertex(this.x + 51, this.y + 11, this.x + 48, this.y + 13, this.x + 46, this.y + 11);
this.scaledBezierVertex(this.x + 43, this.y + 9, this.x + 38, this.y + 8, this.x + 31, this.y + 6);
this.scaledBezierVertex(this.x + 41, this.y + 6, this.x + 39, this.y + 21, this.x + 40, this.y + 24);
this.scaledBezierVertex(this.x + 33, this.y + 10, this.x + 29, this.y + 10, this.x + 37, this.y + 8);
endShape();
// display bounding box
BoundingBox box = this.getBoundingBox();
noFill();
stroke(green);
rect(box.left, box.top, box.width, box.height);
}
// used arc but apply the scale
void scaledArc(float a, float b, float c, float d, float start, float stop) {
arc(a * this.scale, b * this.scale, c * this.scale, d * this.scale, start, stop);
}
void scaledEllipse(float a, float b, float c, float d) {
ellipse(a * this.scale, b * this.scale, c * this.scale, d * this.scale);
}
void scaledVertex(float x, float y) {
vertex(x * this.scale, y * this.scale);
}
void scaledBezierVertex(float x, float y, float x2, float y2, float x3, float y3) {
bezierVertex(x * this.scale, y * this.scale, x2 * this.scale, y2 * this.scale, x3 * this.scale, y3 * this.scale);
}
BoundingBox getBoundingBox() {
return new BoundingBox((this.x - 59) * this.scale, (this.y-34) * this.scale, 120 * this.scale, 60 * this.scale);
}
BoundingBox getNextPosition() {
return new BoundingBox((this.x - 59) * this.scale + this.vx, (this.y-34) * this.scale + this.vy, 120 * this.scale, 60 * this.scale);
}
void pickDirection() {
// will choose number between 0 - 3
// up = 0, right = 1, down = 2, left = 3
int direction = int(random(4));
// up
if (direction == 0) {
println("going up");
this.vx = 0;
this.vy = -this.speed;
}
// right
else if (direction == 1) {
println("going right");
this.vx = this.speed;
this.vy = 0;
}
// down
else if (direction == 2) {
println("going down");
this.vx = 0;
this.vy = this.speed;
}
// left
else {
println("going left");
this.vx = -this.speed;
this.vy = 0;
}
}
void update(ArrayList<Wall> walls)
{
// check if monster is moving
if (this.vx == 0 && this.vy == 0) {
// if monster is not moving, then
this.pickDirection();
return;
}
// check if that location hits any walls
BoundingBox nextPositionBox = this.getNextPosition();
// check if the position hits any walls
boolean hitsWall = false;
for (Wall wall : walls) {
BoundingBox wallBox = wall.getBoundingBox();
if (wallBox.collides(nextPositionBox)) {
hitsWall = true;
}
}
if (hitsWall) {
println("hitting wall");
// go another way
this.pickDirection();
} else {
this.x += this.vx;
this.y += this.vy;
}
}
void moveX(float vx)
{
this.vx = vx;
this.vy = 0;
}
void moveY(float vy)
{
this.vy = vy;
this.vx = 0;
}
}
class Runner {
int vx;
int vy;
float x;
float y;
int colorValue;
Runner(float startX, float startY)
{
this.x = startX;
this.y = startY;
this.colorValue = 255;
}
void display() {
stroke(this.colorValue);
smooth();
noFill();
strokeWeight(1);
// use scale of .26 to compute body location
ellipse(this.x + 2, this.y - 5, 9, 9); //head
line(this.x, this.y, this.x - 9, this.y + 19); //body
line(this.x - 3, this.y + 4, this.x - 9, this.y + 1); //upper left arm
line(this.x - 9, this.y + 1, this.x - 10, this.y + 9); // bottom part of left arm
line(this.x - 2, this.y + 7, this.x, this.y + 12); //upper part of right arm
line(this.x, this.y + 12, this.x + 6, this.y + 8); //lower/ end of right arm
line(this.x - 9, this.y + 19, this.x - 18, this.y + 23); //upper left leg
line(this.x - 18, this.y + 23, this.x - 24, this.y + 19); // left bend leg (end part)
line(this.x - 9, this.y + 19, this.x - 7, this.y + 28); //upper right leg
line(this.x - 7, this.y + 28, this.x - 11, this.y + 34); //lower right leg
}
void update() {
//println(this.x, this.y);
this.x += this.vx;
this.y += this.vy;
}
void stop() {
this.vx = 0;
this.vy = 0;
}
void teleport() {
boolean isLeft = this.x < 50;
if (isLeft) {
this.x = width - 50;
} else {
this.x = 50;
}
}
void moveX(int x) {
this.vx = x;
this.vy = 0;
}
void moveY(int y) {
this.vy = y;
this.vx = 0;
}
BoundingBox squareOrSomething() {
return new BoundingBox(this.x - 24 + this.vx, this.y - 12 + this.vy, 30, 50);
}
}
class Wall
{
float x;
float y;
float width;
float height;
color blue = (#3641FF); //blue
Wall(float x, float y, float width, float height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
void display()
{
stroke(#4C791C);
strokeWeight(2);
noFill();
rect(this.x, this.y, this.width, this.height);
}
BoundingBox getBoundingBox() {
return new BoundingBox(this.x, this.y, this.width, this.height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.