/*
Name: Object-orientated toy
By: Steven Kastelec
Date: October 17th, 2016
Description:
Welcome and thank you for playing my game.
the goal is to collect the three coins that are displayed on screen,
then making it to the larger coin to finish the game.
if you dont collect all three coins, you'll finish the game but
be scorned for not collecting all the coins.
use the A and D keys to move your player and W to jump.
*/
avatar myAvatar;
platformHorizontal hPlatform;
platformVerticle vPlatform;
coins myCoins;
scoreCounter scoreBoard;
gameOver gameOverScreen;
star[] stars = new star[100];
void setup() {
size(400, 400);
background(255);
ellipseMode(CORNER);
myAvatar = new avatar();
hPlatform = new platformHorizontal();
vPlatform = new platformVerticle();
myCoins = new coins();
scoreBoard = new scoreCounter();
gameOverScreen = new gameOver();
//need to create star map using a loop
for (int i = 0; i < stars.length; i++) {
stars[i] = new star(int(random(0, 400)), int(random(0, 400)));
}
}
void draw () {
background(147, 147, 255);
//draw stars
for (int i = 0; i < stars.length; i++) {
stars[i].drawStars();
}
hPlatform.callPlatformH();
vPlatform.callPlatformV();
myCoins.callCoins();
myAvatar.allFunctionsAvatar();
scoreBoard.drawScore();
gameOverScreen.drawScreen();
}
void keyPressed() {
if (key == 'a' || key == 'A') {
rightButtonPressed = false;
leftButtonPressed = true;
} else if (key == 'd' || key == 'D') {
leftButtonPressed = false;
rightButtonPressed = true;
} else if (key == 'w' || key == 'W') {
upButtonPressed = true;
}
}
void keyReleased() {
if (key == 'a' || key == 'A') {
rightButtonPressed = false;
leftButtonPressed = false;
} else if (key == 'd' || key == 'D') {
leftButtonPressed = false;
rightButtonPressed = false;
} else if (key == 'w' || key == 'W') {
upButtonPressed = false;
}
}//global variables
PVector avatarLocation = new PVector(0, 0);
PVector avatarVelocity = new PVector(0, 0);
PVector avatarAcceleration = new PVector(0, 0);
PVector avatarJump = new PVector(0, -8);
PVector gravity = new PVector (0, 0.6);
//boolean variable to check if my avatar is moving
boolean leftButtonPressed = false;
boolean rightButtonPressed = false;
boolean upButtonPressed = false;
boolean avatarOnGround = false;
//welcome to the wonderful world of classes (god help us all)
class avatar {
//constructer for avatar
avatar() {
avatarLocation.x = 340;
avatarLocation.y = 380;
}
//have all my function in one function to all later
void allFunctionsAvatar() {
avatarConstrain();
constrainVelocity();
movement();
drawAvatar();
}
//all the constrictions without any on top of canvas
void avatarConstrain() {
//collison with left side
if (avatarLocation.x < 0) {
avatarLocation.x = 0;
}
//collision with right
if (avatarLocation.x + 20 > width) {
avatarLocation.x = width - 40;
}
// collision with bottom
if (avatarLocation.y + 20 > height) {
avatarLocation.y = height - 20;
avatarVelocity.y = 0;
avatarOnGround = true;
}
//check to see if avatar is in air
if (avatarLocation.y +20 < height) {
avatarOnGround = false;
}
}
// i need to constrain the velocity of the avatar
void constrainVelocity() {
if (avatarVelocity.x > 6) {
avatarVelocity.x = 6;
} else if (avatarVelocity.x < -6) {
avatarVelocity.x = -6;
}
}
// lets make this thing move shall we...
void movement() {
if (leftButtonPressed) {
avatarAcceleration.x = -0.9;
} else if (rightButtonPressed) {
avatarAcceleration.x = 0.9;
} else {
avatarAcceleration.x = 0;
avatarVelocity.x = avatarVelocity.x/1.5;
}
if (upButtonPressed && avatarOnGround) {
avatarVelocity.add(avatarJump);
} else {
avatarVelocity.add(gravity);
}
avatarVelocity.add(avatarAcceleration);
avatarLocation.add(avatarVelocity);
}
//my little one (player)
void drawAvatar() {
noStroke();
fill(185, 0, 0);
rect(avatarLocation.x, avatarLocation.y, 20, 20);
}
}
//variables for coins
PVector coinPosition1 = new PVector(20, 360);
PVector coinPosition2 = new PVector(370, 180);
PVector coinPosition3 = new PVector(0, 30);
PVector coinPosition4 = new PVector(180, 0);
float score = 0;
//booleans for coins
boolean coin1 = true;
boolean coin2 = true;
boolean coin3 = true;
boolean coin4Collision = false;
//class coins
class coins {
coins () {
}
//i need to draw my coins with the smaller ones checking to see if they are being drawn
void drawCoins() {
//i need to make sure the coins are meant to be drawn, if so draw them
if (coin1 == true) {
noStroke();
fill(227, 182, 21);
ellipse(coinPosition1.x, coinPosition1.y, 20, 20);
noFill();
stroke(198, 160, 18);
strokeWeight(2);
ellipse(coinPosition1.x+5, coinPosition1.y+5, 10, 10);
}
if (coin2 == true) {
noStroke();
fill(227, 182, 21);
ellipse(coinPosition2.x, coinPosition2.y, 20, 20);
noFill();
stroke(198, 160, 18);
strokeWeight(2);
ellipse(coinPosition2.x + 5, coinPosition2.y + 5, 10, 10);
}
if (coin3 == true) {
noStroke();
fill(227, 182, 21);
ellipse(coinPosition3.x, coinPosition3.y, 20, 20);
noFill();
stroke(198, 160, 18);
strokeWeight(2);
ellipse(coinPosition3.x + 5, coinPosition3.y + 5, 10, 10);
}
noStroke();
fill(227, 182, 21);
ellipse(coinPosition4.x, coinPosition4.y, 40, 40);
noFill();
stroke(198, 160, 18);
strokeWeight(4);
ellipse(coinPosition4.x + 10, coinPosition4.y +10, 20, 20);
}
//if the coin is drawn then...
//find collision with player and each coin and...
//add score
void coinCollision() {
if (coin1 == true) {
if (avatarLocation.x >= coinPosition1.x && avatarLocation.x <= coinPosition1.x +20 && avatarLocation.y >= coinPosition1.y && avatarLocation.y <= coinPosition1.y +20) {
if (score == 0) {
score = 1;
} else if (score == 1) {
score = 2;
} else if (score == 2) {
score = 3;
}
coin1 = false;
}
}
if (coin2 == true) {
if (avatarLocation.x >= coinPosition2.x && avatarLocation.x <= coinPosition2.x +20 && avatarLocation.y >= coinPosition2.y && avatarLocation.y <= coinPosition2.y +20) {
if (score == 0) {
score = 1;
} else if (score == 1) {
score = 2;
} else if (score == 2) {
score = 3;
}
coin2 = false;
}
}
if (coin3 == true) {
if (avatarLocation.x >= coinPosition3.x && avatarLocation.x <= coinPosition3.x +20 && avatarLocation.y >= coinPosition3.y && avatarLocation.y <= coinPosition3.y +20) {
if (score == 0) {
score = 1;
} else if (score == 1) {
score = 2;
} else if (score == 2) {
score = 3;
}
coin3 = false;
}
}
if (avatarLocation.x >= coinPosition4.x && avatarLocation.x <= coinPosition4.x +20 && avatarLocation.y >= coinPosition4.y && avatarLocation.y <= coinPosition4.y +20 && score == 3) {
coin4Collision = true;
}
}
void callCoins() {
drawCoins();
coinCollision();
}
}//create class for game over screen
class gameOver {
gameOver() {
}
//function for the game over screen
void drawScreen() {
if (coin4Collision == true) {
noStroke();
fill(0);
rect(0, 0, 400, 400);
fill(255);
textSize(75);
text("YOU WIN", 35, 100);
fill(255);
textSize(60);
text("GAME OVER", 25, 250);
}
}
}//variables
PVector platformH1 = new PVector(200, 360);
PVector platformH2 = new PVector(100, 320);
PVector platformH3 = new PVector(0, 280);
PVector platformH4 = new PVector(140, 260);
PVector platformH5 = new PVector(260, 240);
PVector platformH6 = new PVector(200, 180);
PVector platformH7 = new PVector(100, 140);
PVector platformH8 = new PVector(200, 110);
PVector platformH9 = new PVector(170, 40);
PVector platformH10 = new PVector(0, 60);
float platformHHeight = 20;
float platformHWidth = 60;
class platformHorizontal {
//constructor
platformHorizontal() {
}
//drawing all the horizontal platforms
void drawPlatformH() {
noStroke();
fill(90, 47, 1);
rect(platformH1.x, platformH1.y, platformHWidth, platformHHeight);
rect(platformH2.x, platformH2.y, platformHWidth, platformHHeight);
rect(platformH3.x, platformH3.y, platformHWidth, platformHHeight);
rect(platformH4.x, platformH4.y, platformHWidth, platformHHeight);
rect(platformH5.x, platformH5.y, platformHWidth, platformHHeight);
rect(platformH6.x, platformH6.y, platformHWidth, platformHHeight);
rect(platformH7.x, platformH7.y, platformHWidth, platformHHeight);
rect(platformH8.x, platformH8.y, platformHWidth, platformHHeight);
rect(platformH9.x, platformH9.y, platformHWidth, platformHHeight);
rect(platformH10.x, platformH10.y, platformHWidth, platformHHeight);
noStroke();
fill(17, 126, 8);
rect(platformH1.x, platformH1.y, 60, 5);
rect(platformH2.x, platformH2.y, 60, 5);
rect(platformH3.x, platformH3.y, 60, 5);
rect(platformH4.x, platformH4.y, 60, 5);
rect(platformH5.x, platformH5.y, 60, 5);
rect(platformH6.x, platformH6.y, 60, 5);
rect(platformH7.x, platformH7.y, 60, 5);
rect(platformH8.x, platformH8.y, 60, 5);
rect(platformH9.x, platformH9.y, 60, 5);
rect(platformH10.x, platformH10.y, 60, 5);
noStroke();
fill(147, 83, 19);
rect(platformH1.x+10, platformH1.y+10, 5, 7);
rect(platformH1.x+30, platformH1.y+11, 5, 7);
rect(platformH1.x+50, platformH1.y+9, 5, 7);
rect(platformH2.x+10, platformH2.y+10, 5, 7);
rect(platformH2.x+30, platformH2.y+11, 5, 7);
rect(platformH2.x+50, platformH2.y+9, 5, 7);
rect(platformH3.x+10, platformH3.y+10, 5, 7);
rect(platformH3.x+30, platformH3.y+11, 5, 7);
rect(platformH3.x+50, platformH3.y+9, 5, 7);
rect(platformH4.x+10, platformH4.y+10, 5, 7);
rect(platformH4.x+30, platformH4.y+11, 5, 7);
rect(platformH4.x+50, platformH4.y+9, 5, 7);
rect(platformH5.x+10, platformH5.y+10, 5, 7);
rect(platformH5.x+30, platformH5.y+11, 5, 7);
rect(platformH5.x+50, platformH5.y+9, 5, 7);
rect(platformH6.x+10, platformH6.y+10, 5, 7);
rect(platformH6.x+30, platformH6.y+11, 5, 7);
rect(platformH6.x+50, platformH6.y+9, 5, 7);
rect(platformH7.x+10, platformH7.y+10, 5, 7);
rect(platformH7.x+30, platformH7.y+11, 5, 7);
rect(platformH7.x+50, platformH7.y+9, 5, 7);
rect(platformH8.x+10, platformH8.y+10, 5, 7);
rect(platformH8.x+30, platformH8.y+11, 5, 7);
rect(platformH8.x+50, platformH8.y+9, 5, 7);
rect(platformH9.x+10, platformH9.y+10, 5, 7);
rect(platformH9.x+30, platformH9.y+11, 5, 7);
rect(platformH9.x+50, platformH9.y+9, 5, 7);
rect(platformH10.x+10, platformH10.y+10, 5, 7);
rect(platformH10.x+30, platformH10.y+11, 5, 7);
rect(platformH10.x+50, platformH10.y+9, 5, 7);
}
//create a collison equation so that every platform has collison w/player
void horizontalCollision() {
//create a collision with the first horizontal plaform
if (avatarLocation.x >= platformH1.x && avatarLocation.x+5 <= platformH1.x + platformHWidth && avatarLocation.y + 20 >= platformH1.y && avatarLocation.y +20 <= platformH1.y +20
|| avatarLocation.x + 20 >= platformH1.x && avatarLocation.x +20 <= platformH1.x + platformHWidth && avatarLocation.y + 20 > platformH1.y && avatarLocation.y +20 <= platformH1.y +20) {
//stop the avatar when its coming down, otherwise it would interrupt the jump where ever.
if (avatarVelocity.y > 0) {
avatarLocation.y = platformH1.y -20;
avatarVelocity.y = 0;
avatarVelocity.add(avatarJump);
}
}
//create a collision with the second horizontal plaform
if (avatarLocation.x >= platformH2.x && avatarLocation.x+5 <= platformH2.x + platformHWidth && avatarLocation.y + 20 >= platformH2.y && avatarLocation.y +20 <= platformH2.y +20
|| avatarLocation.x + 20 >= platformH2.x && avatarLocation.x +20 <= platformH2.x + platformHWidth && avatarLocation.y + 20 > platformH2.y && avatarLocation.y +20 <= platformH2.y +20) {
//stop the avatar when its coming down, otherwise it would interrupt the jump where ever.
if (avatarVelocity.y > 0) {
avatarLocation.y = platformH2.y -20;
avatarVelocity.y = 0;
avatarVelocity.add(avatarJump);
}
}
//create a collision with the third horizontal plaform
if (avatarLocation.x >= platformH3.x && avatarLocation.x+5 <= platformH3.x + platformHWidth && avatarLocation.y + 20 >= platformH3.y && avatarLocation.y +20 <= platformH3.y +20
|| avatarLocation.x + 20 >= platformH3.x && avatarLocation.x +20 <= platformH3.x + platformHWidth && avatarLocation.y + 20 > platformH3.y && avatarLocation.y +20 <= platformH3.y +20) {
//stop the avatar when its coming down, otherwise it would interrupt the jump where ever.
if (avatarVelocity.y > 0) {
avatarLocation.y = platformH3.y -20;
avatarVelocity.y = 0;
avatarVelocity.add(avatarJump);
}
}
//create a collision with the fourth horizontal plaform
if (avatarLocation.x >= platformH4.x && avatarLocation.x+5 <= platformH4.x + platformHWidth && avatarLocation.y + 20 >= platformH4.y && avatarLocation.y +20 <= platformH4.y +20
|| avatarLocation.x + 20 >= platformH4.x && avatarLocation.x +20 <= platformH4.x + platformHWidth && avatarLocation.y + 20 > platformH4.y && avatarLocation.y +20 <= platformH4.y +20) {
//stop the avatar when its coming down, otherwise it would interrupt the jump where ever.
if (avatarVelocity.y > 0) {
avatarLocation.y = platformH4.y -20;
avatarVelocity.y = 0;
avatarVelocity.add(avatarJump);
}
}
//create a collision with the fifth horizontal plaform
if (avatarLocation.x >= platformH5.x && avatarLocation.x+5 <= platformH5.x + platformHWidth && avatarLocation.y + 20 >= platformH5.y && avatarLocation.y +20 <= platformH5.y +20
|| avatarLocation.x + 20 >= platformH5.x && avatarLocation.x +20 <= platformH5.x + platformHWidth && avatarLocation.y + 20 > platformH5.y && avatarLocation.y +20 <= platformH5.y +20) {
//stop the avatar when its coming down, otherwise it would interrupt the jump where ever.
if (avatarVelocity.y > 0) {
avatarLocation.y = platformH5.y -20;
avatarVelocity.y = 0;
avatarVelocity.add(avatarJump);
}
}
//create a collision with the sixth horizontal plaform
if (avatarLocation.x >= platformH6.x && avatarLocation.x+5 <= platformH6.x + platformHWidth && avatarLocation.y + 20 >= platformH6.y && avatarLocation.y +20 <= platformH6.y +20
|| avatarLocation.x + 20 >= platformH6.x && avatarLocation.x +20 <= platformH6.x + platformHWidth && avatarLocation.y + 20 > platformH6.y && avatarLocation.y +20 <= platformH6.y +20) {
//stop the avatar when its coming down, otherwise it would interrupt the jump where ever.
if (avatarVelocity.y > 0) {
avatarLocation.y = platformH6.y -20;
avatarVelocity.y = 0;
avatarVelocity.add(avatarJump);
}
}
//create a collision with the seventh horizontal plaform
if (avatarLocation.x >= platformH7.x && avatarLocation.x+5 <= platformH7.x + platformHWidth && avatarLocation.y + 20 >= platformH7.y && avatarLocation.y +20 <= platformH7.y +20
|| avatarLocation.x + 20 >= platformH7.x && avatarLocation.x +20 <= platformH7.x + platformHWidth && avatarLocation.y + 20 > platformH7.y && avatarLocation.y +20 <= platformH7.y +20) {
//stop the avatar when its coming down, otherwise it would interrupt the jump where ever.
if (avatarVelocity.y > 0) {
avatarLocation.y = platformH7.y -20;
avatarVelocity.y = 0;
avatarVelocity.add(avatarJump);
}
}
//create a collision with the eighth horizontal plaform
if (avatarLocation.x >= platformH8.x && avatarLocation.x+5 <= platformH8.x + platformHWidth && avatarLocation.y + 20 >= platformH8.y && avatarLocation.y +20 <= platformH8.y +20
|| avatarLocation.x + 20 >= platformH8.x && avatarLocation.x +20 <= platformH8.x + platformHWidth && avatarLocation.y + 20 > platformH8.y && avatarLocation.y +20 <= platformH8.y +20) {
//stop the avatar when its coming down, otherwise it would interrupt the jump where ever.
if (avatarVelocity.y > 0) {
avatarLocation.y = platformH8.y -20;
avatarVelocity.y = 0;
avatarVelocity.add(avatarJump);
}
}
//create a collision with the ninth horizontal plaform
if (avatarLocation.x >= platformH9.x && avatarLocation.x+5 <= platformH9.x + platformHWidth && avatarLocation.y + 20 >= platformH9.y && avatarLocation.y +20 <= platformH9.y +20
|| avatarLocation.x + 20 >= platformH9.x && avatarLocation.x +20 <= platformH9.x + platformHWidth && avatarLocation.y + 20 > platformH9.y && avatarLocation.y +20 <= platformH9.y +20) {
//stop the avatar when its coming down, otherwise it would interrupt the jump where ever.
if (avatarVelocity.y > 0) {
avatarLocation.y = platformH9.y -20;
avatarVelocity.y = 0;
avatarVelocity.add(avatarJump);
}
}
//create a collision with the ninth horizontal plaform
if (avatarLocation.x >= platformH10.x && avatarLocation.x+5 <= platformH10.x + platformHWidth && avatarLocation.y + 20 >= platformH10.y && avatarLocation.y +20 <= platformH10.y +20
|| avatarLocation.x + 20 >= platformH10.x && avatarLocation.x +20 <= platformH10.x + platformHWidth && avatarLocation.y + 20 > platformH9.y && avatarLocation.y +20 <= platformH10.y +20) {
//stop the avatar when its coming down, otherwise it would interrupt the jump where ever.
if (avatarVelocity.y > 0) {
avatarLocation.y = platformH10.y -20;
avatarVelocity.y = 0;
avatarVelocity.add(avatarJump);
}
}
}
//easy to use function to call in main code
void callPlatformH() {
drawPlatformH();
horizontalCollision();
}
}//varibles for platforms
PVector platformV1 = new PVector(380, 220);
PVector platformV2 = new PVector(60, 100);
PVector platformV3 = new PVector(280, 70);
PVector platformV4 = new PVector(300, 30);
PVector platformV5 = new PVector(150, 0);
PVector platformV6 = new PVector(230, 0);
float platformVWidth = 20;
float platformVHeight = 20;
//create class for squares
class platformVerticle {
//constructor
platformVerticle () {
}
// draw all the squares
void drawPlatformV() {
noStroke();
fill(90, 47, 1);
rect(platformV1.x, platformV1.y, platformVWidth, platformVHeight);
rect(platformV2.x, platformV2.y, platformVWidth, platformVHeight);
rect(platformV3.x, platformV3.y, platformVWidth, platformVHeight);
noStroke();
fill(17, 126, 8);
rect(platformV1.x, platformV1.y, 20, 5);
rect(platformV2.x, platformV2.y, 20, 5);
rect(platformV3.x, platformV3.y, 20, 5);
noStroke();
fill(147, 83, 19);
rect(platformV1.x+4, platformV1.y+10, 3, 5);
rect(platformV1.x+13, platformV1.y+13, 3, 5);
rect(platformV2.x+4, platformV2.y+10, 3, 5);
rect(platformV2.x+13, platformV2.y+13, 3, 5);
rect(platformV3.x+4, platformV3.y+10, 3, 5);
rect(platformV3.x+13, platformV3.y+13, 3, 5);
}
//create collision w/avatar and squares
void verticleCollision() {
//create a collision with the first verticle plaform
if (avatarLocation.x >= platformV1.x && avatarLocation.x+5 <= platformV1.x + platformVWidth && avatarLocation.y + 20 >= platformV1.y && avatarLocation.y +20 <= platformV1.y +20
|| avatarLocation.x + 20 >= platformV1.x && avatarLocation.x +20 <= platformV1.x + platformVWidth && avatarLocation.y + 20 > platformV1.y && avatarLocation.y +20 <= platformV1.y +20) {
//stop the avatar when its coming down, otherwise it would interrupt the jump where ever.
if (avatarVelocity.y > 0) {
avatarLocation.y = platformV1.y -20;
avatarVelocity.y = 0;
avatarVelocity.add(avatarJump);
}
}
//create a collision with the second verticle plaform
if (avatarLocation.x >= platformV2.x && avatarLocation.x+5 <= platformV2.x + platformVWidth && avatarLocation.y + 20 >= platformV2.y && avatarLocation.y +20 <= platformV2.y +20
|| avatarLocation.x + 20 >= platformV2.x && avatarLocation.x +20 <= platformV2.x + platformVWidth && avatarLocation.y + 20 > platformV2.y && avatarLocation.y +20 <= platformV2.y +20) {
//stop the avatar when its coming down, otherwise it would interrupt the jump where ever.
if (avatarVelocity.y > 0) {
avatarLocation.y = platformV2.y -20;
avatarVelocity.y = 0;
avatarVelocity.add(avatarJump);
}
}
//create a collision with the third verticle plaform
if (avatarLocation.x >= platformV3.x && avatarLocation.x+5 <= platformV3.x + platformVWidth && avatarLocation.y + 20 >= platformV3.y && avatarLocation.y +20 <= platformV3.y +20
|| avatarLocation.x + 20 >= platformV3.x && avatarLocation.x +20 <= platformV3.x + platformVWidth && avatarLocation.y + 20 > platformV3.y && avatarLocation.y +20 <= platformV3.y +20) {
//stop the avatar when its coming down, otherwise it would interrupt the jump where ever.
if (avatarVelocity.y > 0) {
avatarLocation.y = platformV3.y -20;
avatarVelocity.y = 0;
avatarVelocity.add(avatarJump);
}
}
}
//function to call all functions
void callPlatformV() {
verticleCollision();
drawPlatformV();
}
}//create a class for score
class scoreCounter {
//constructor
scoreCounter() {
}
//draw my scoreboard
void drawScore() {
if (score == 0) {
noStroke();
fill(100, 100, 100);
ellipse(320, 10, 10, 10);
ellipse(340, 10, 10, 10);
ellipse(360, 10, 10, 10);
} else if (score == 1) {
fill(227, 182, 21);
ellipse(320, 10, 10, 10);
noStroke();
fill(100, 100, 100);
ellipse(340, 10, 10, 10);
ellipse(360, 10, 10, 10);
} else if (score == 2) {
noStroke();
fill(227, 182, 21);
ellipse(320, 10, 10, 10);
ellipse(340, 10, 10, 10);
noStroke();
fill(100, 100, 100);
ellipse(360, 10, 10, 10);
} else if (score == 3) {
noStroke();
fill(227, 182, 21);
ellipse(320, 10, 10, 10);
ellipse(340, 10, 10, 10);
ellipse(360, 10, 10, 10);
}
}
}
//creat new class named stars
class star {
//stars variable
PVector starLocation = new PVector(0, 0);
star(float tempX, float tempY) {
starLocation.x = starLocation.x = tempX;
starLocation.y = starLocation.y = tempY;
}
void drawStars() {
noStroke();
fill(random(100, 255), random(100, 255), random(100, 255), random(100, 175));
rect(starLocation.x, starLocation.y, 5, 5);
}
}