//Name: Alex Valickis
//Program: Interactive Toy
//Description: An interactive toy that models an infinate runner.
// The player is the circle on the left and must jump over the obstacle that aproaches from the right of the sceen.
// The obstacles approach at randome speeds and the player will loose 100 points if they touch it.
//Controls: SpaceBar - Jump
//Variable declaration and instantiation.
boolean goingDown = false;
int groundY = 275;
int spikeX1 = 400;
int spikeY1 = 300;
int spikeX2 = 450;
int spikeY2 = 300;
int spikeX3 = 425;
int spikeY3 = 250;
int score = 0;
float rand1 = random(0, 255);
float rand2 = random(0, 255);
float rand3 = random(0, 255);
float speed = random(5, 10);
//Setup function that defines the frame rate and screen size
void setup()
{
frameRate(80);
size(400, 400);
smooth();
rectMode(CORNERS);
//background
background(rand1, rand2, rand3);
//Ground
noStroke();
fill(0, 60);
rect(-5, 400, 400, 300);
}
//Main function
void draw() {
//Displays the character
displayCharacter();
//Displays the spike
displaySpike();
//Checks if the user has pressed the jump button
checkJump();
//Checks if the player has gained or lost points
checkScore();
}
//Creates the background and the ground layers
void setStage() {
//Draws the background with the predefined ranom colours
background(rand1, rand2, rand3);
noStroke();
//Draws the ground
fill(0, 60);
rect(-5, 400, 400, 300);
//Displays the score on the screen
textSize(20);
fill(0);
text("Score:", 100, 30);
text(score, 170, 30);
}
//Updates the Character
void updateCharacter() {
//Draws the circle at the predefined hight
stroke(1);
fill(255, 200);
ellipse(80, groundY, 50, 50);
}
//Updates the Spike
void updateSpike() {
//Draws the triangle at the predifned coordinates
stroke(1);
fill(255, 200);
triangle(spikeX1, spikeY1, spikeX2, spikeY2, spikeX3, spikeY3);
}
//Checks if the player has pressed the jump button
void checkJump() {
//
if (keyPressed == true) {
if (key == ' ') {
if (goingDown == false) {
//Move the character up
groundY -= 10;
//Redraw the stage
setStage();
//Redraw the charatcer
updateCharacter();
//When the player jumps change the background colour
if (groundY == 235) {
changeBackground();
goingDown = true;
}
}
}
}
//Bring the character back down
if (groundY != 275 || goingDown == true) {
if (groundY != 275) {
groundY += 5;
if (groundY < 150)
{
groundY += 10;
}
//Redraw the stage
setStage();
//Redraw the character
updateCharacter();
}
goingDown = false;
}
//move the spike left
moveSpike();
}
//Changes the background colour
void changeBackground() {
rand1 = random(0, 255);
rand2 = random(0, 255);
rand3 = random(0, 255);
}
//Moves the spike left
void moveSpike() {
//Moves the spike left at the predefined speed
if (spikeX2 > 0) {
spikeX1 -= speed;
spikeX2 -= speed;
spikeX3 -= speed;
//Redraw the stage
setStage();
//Redraw the character
updateCharacter();
//Redraw the spike
updateSpike();
}
//Reset the spike on the right side when it goes off the left side of the screen
if (spikeX2 <= 0) {
//Randomise the speed
speed = random(7, 12);
spikeX1 = 400;
spikeX2 = 450;
spikeX3 = 425;
}
}
//Check whether the player has gained or lost points
void checkScore() {
//If the player has jumped over the spike
if (groundY < 250) {
if (spikeX3 == 75 || spikeX3 == 76 || spikeX3 == 77 || spikeX3 == 78 || spikeX3 == 79 || spikeX3 == 80 || spikeX3 == 81|| spikeX3 == 82 || spikeX3 == 83 || spikeX3 == 84 || spikeX3 == 85) {
//Add points
score += 100;
//Redraw stage
setStage();
//Redraw character
updateCharacter();
//Redraw spike
updateSpike();
}
//If the player hit the spike
} else {
if (spikeX3 == 75 || spikeX3 == 76 || spikeX3 == 77 || spikeX3 == 78 || spikeX3 == 79 || spikeX3 == 80 || spikeX3 == 81|| spikeX3 == 82 || spikeX3 == 83 || spikeX3 == 84 || spikeX3 == 85) {
//Subtract points
score -= 100;
//Redraw stage
setStage();
//Redraw character
updateCharacter();
//Redraw spike
updateSpike();
}
}
}
//Displays the character at the predefined location
void displayCharacter() {
stroke(1);
fill(255, 200);
ellipse(80, groundY, 50, 50);
}
//Displays the Spike at the predefined location
void displaySpike() {
stroke(1);
fill(255, 200);
triangle(spikeX1, spikeY1, spikeX2, spikeY2, spikeX3, spikeY3);
}