/*
Del Nordlund
PROG14998
Object-Oriented Toy
21 October 2016
Help the robot to avoid the falling ladders! Be careful, they vary in height!
A - Move Left
D - Move Right
*/
//Create a new bot object
Bot botty = new Bot();
//Create an array of 9 ladders
Ladder[] ladders = new Ladder[9];
//The game is not over yet, so the gameOver variable is false.
boolean isGameOver = false;
void setup()
{
//set window size
size (500, 400);
smooth();
noCursor();
noStroke();
//prints instructions to the bottom of the screen
println("Use A and D to move the robot and avoid the falling ladders!");
//for loop creates ladder objects and inputs their speed
for(int i = 0; i < ladders.length; i++)
{
ladders[i] = new Ladder(3);
}
}
void draw()
{
background(247, 173, 173);
//moves the bot and draws it to the screen
botty.moveBot();
botty.drawBot();
//makes each ladder fall and display it to the screen
for(int i = 0; i < ladders.length; i++)
{
ladders[i].ladderFall();
ladders[i].drawLadder();
//check if ladder has been hit
ladders[i].checkHit();
//check if the game is over
gameOver();
}
}
//for when the game ends
void gameOver()
{
//once gameOver is true
if (isGameOver == true)
{
//draws a game over screen with text "Game Over"
fill(255);
rect(250, 190, 501, 421);
fill(247, 173, 173);
rect(width/2, height/2, 300, 200);
fill(0);
textAlign(CENTER);
textSize(24);
text("G A M E\nO V E R", width/2, height/2);
}
}//BOT CLASS
//create a float variable for x position of bot
float botX;
class Bot
{
//constructor for bot
Bot()
{
//bot starts in the middle of the screen
botX = 250;
}
void drawBot()
{
//Draw the bot to the screen when called upon.
ellipseMode(CENTER);
rectMode(CENTER);
//draws hover
fill(249, 249, 77);
ellipse(botX, 350+sin(millis()/100)*2, 30, 20);
fill(255, 127);
ellipse(botX, 350+sin(millis()/100)*2, 40, 30);
fill(175, 175);
ellipse(botX+sin(millis()/100)*2, 380, 50, 10);
//draws body and neck
fill(0);
rect(botX, 330+sin(millis()/100)*2, 50, 50);
rect(botX, 305+sin(millis()/100)*2, 30, 20);
//draws head and ears
rect(botX, 285+sin(millis()/100)*2, 50, 30);
triangle(botX-25, 273+sin(millis()/100)*2, botX-15, 260+sin(millis()/100)*2, botX-5, 273+sin(millis()/100)*2);
triangle(botX+25, 273+sin(millis()/100)*2, botX+15, 260+sin(millis()/100)*2, botX+5, 273+sin(millis()/100)*2);
//draw eyes
fill(255, 127);
ellipse(botX-15, 285+sin(millis()/100)*2, 15, 15);
fill(255, 255, 150);
ellipse(botX-15, 285+sin(millis()/100)*2, 10, 10);
fill(255, 127);
ellipse(botX+15, 285+sin(millis()/100)*2, 15, 15);
fill(255, 255, 150);
ellipse(botX+15, 285+sin(millis()/100)*2, 10, 10);
}
void moveBot()
//moves the bot when called upon. if the 'a' key is pressed, move left, if 'd' key is pressed, move right.
//if the bot exceeds the screen boundaries, make it pop up on the other side.
{
if (keyPressed)
{
if (key == 'a' || key == 'A')
{
if (botX + 25 < 0)
{
botX = 510;
}
else
{
botX = botX - 5;
}
}
if (key == 'd' || key == 'D')
{
if (botX - 25 > 500)
{
botX = -10;
}
else
{
botX = botX + 5;
}
}
this.drawBot();
}
}
}//LADDER CLASS
class Ladder
{
//PVectors for ladder position and velocity, floats for length of the ladders and their speed
PVector ladderPos = new PVector();
PVector ladderVelo = new PVector();
float ladderLength;
float ladderSpeed;
//Ladder constructor
Ladder(float ladderSpeed)
{
//ladders can be a limited number of pixels long.
ladderLength = random(39, 251);
//ladders begin at random x position
ladderPos.x = random(1, 499);
//ladders spawn out of view and fall into view
ladderPos.y = 0 - ladderLength;
//ladders fall at this speed
ladderVelo.y = ladderSpeed;
}
void drawLadder()
{
fill(173, 151, 107);
//draw 2 length pieces
rect(ladderPos.x, ladderPos.y, 5, ladderLength);
rect(ladderPos.x+20, ladderPos.y, 5, ladderLength);
//draw steps based on how long the ladder is
rect(ladderPos.x+10, ladderPos.y, 20, 5);
if (ladderLength > 55)
{
rect(ladderPos.x+10, ladderPos.y-25, 20, 5);
rect(ladderPos.x+10, ladderPos.y+25, 20, 5);
if (ladderLength > 110)
{
rect(ladderPos.x+10, ladderPos.y-50, 20, 5);
rect(ladderPos.x+10, ladderPos.y+50, 20, 5);
if (ladderLength > 205)
{
rect(ladderPos.x+10, ladderPos.y-75, 20, 5);
rect(ladderPos.x+10, ladderPos.y+75, 20, 5);
if (ladderLength > 240)
{
rect(ladderPos.x+10, ladderPos.y-100, 20, 5);
rect(ladderPos.x+10, ladderPos.y+100, 20, 5);
}
}
}
}
}
//causes the ladders to fall
void ladderFall()
{
//add velocity to position of ladder
ladderPos.add(ladderVelo);
//if the ladder falls out of view of the user
if (ladderPos.y-ladderLength > 500)
{
//respawn ladder at the top with different x coordinates and a different length
ladderPos.y = 0 - ladderLength;
ladderPos.x = random (1, 499);
ladderLength = random (9, 251);
}
}
//checks if ladders have been hit
void checkHit()
{
//if ladder falls within bot boundaries
if (ladderPos.x >= (botX-25) && ladderPos.x <= (botX+25) || (ladderPos.x+20) >= (botX-25) && (ladderPos.x+20) <= (botX+25))
{
if ((ladderPos.y-(ladderLength/2)) > 260 && (ladderPos.y+(ladderLength/2)) < 355)
{
//set game over to true
isGameOver = true;
}
}
}
}