//Jason Pushkar's Cube Runner
//Use Spacebar to jump over the incoming cubes.
//Cubes will alternate their speeds at time goes on.
//Try and get a highscore! :)
//Vectors of characters positions
public PVector playerPosition = new PVector (200, 325);
public PVector enemyPosition = new PVector (random(800, 900), 310);
public PVector enemyPosition1 = new PVector (random(1600, 1800), 330);
//score integers
public int scorePoints;
public int highScore;
//Speed/Momentum floats
public float enemyMomentum1 = 3;
public float enemyMomentum = 3;
public float speedX = 20;
public float speedY = 20;
//Class definitions.
Rain bg = new Rain();
Cube cube = new Cube();
Ground ground = new Ground();
Score score = new Score();
Enemy1 enemy1 = new Enemy1();
Start startScreen = new Start();
Particles particles1 = new Particles();
void setup()
{
//sketch size
size (800, 400);
}
void draw()
{
//Background colours
background (0);
frameRate = (60);
noCursor();
//calling classes. Movement>Art Assets>Key Functions
//movement
cube.cubeMovement();
bg.backGroundRain();
//art assets
particles1.particleExplosion();
enemy1.enemy1();
enemy1.enemy2();
cube.drawCube();
enemy1.enemy2Behaviour();
enemy1.enemy2Collision();
ground.drawGround();
score.scoreBoard();
enemy1.enemy1Behaviour();
enemy1.enemy1Collision();
startScreen.start1();
//key functions
keyPressed();
keyReleased();
}
//key functions
public void keyPressed()
{
//space bar
if (key == ' ') {
//jump boolean gets triggered, makes cube jump
cube.jump = true;
//start screen boolean gets triggered, makes start screen dissapear, program starts
}
if (key == 'e') {
startScreen.startBool = true;
}
}
public void keyReleased()
{
//when spacebar is released, jump is false (no repetition during jump)
if (key == ' ' && playerPosition.y > 320) {
cube.jump = false;
}
}public class Cube
{
//array for colour randomisation
float[] colours = new float[3];
public boolean jump = false;
public boolean goRight = false;
;
public float playerVelocity = 15;
public float playerVelocity2 = 1.6;
public boolean backDown = false;
public boolean colourMix;
float shadowPos = playerPosition.y;
public void drawCube()
{
rectMode(CENTER);
//if player is at the highest point of the jump command, run function
if (playerPosition.y <= 186) {
//changes colours of game objects to a bluish green every time the player is to jump
colourMix=true;
fill(colours[0]=random(0, 5), colours[1]=random(250, 255), colours[2]=random(0, 255));
colourMix=false;
}
//draw player cube.
rect(playerPosition.x, playerPosition.y, 50, 50, 25);
}
public void cubeMovement()
{
//if the player jumps (spacebar) and is on the ground, run function
if (jump && playerPosition.y < 326 ) {
//simulate gravity. (essentially making the ball go from fast to slow near the top
playerVelocity = playerVelocity/1.1;
playerPosition.y -= playerVelocity;
}
//if player hits this position;
if (playerPosition.y <= 185) {
backDown = true;
}
//CONT. run this function, which sends the player back down.
if (backDown && playerPosition.y <325) {
//send player back down, going from slow to fast to simulate gravity down.
playerVelocity2 = playerVelocity2*1.06;
playerPosition.y += playerVelocity2;
}
//if player hits ground
if (playerPosition.y > 326) {
//reset positions and booleans+velocities that relate to the cube so that the code is runnable multiple times.
playerPosition.y = 324;
backDown = false;
cube.jump = false;
jump = false;
playerVelocity = 15;
playerVelocity2= 1.6;
}
}
}public class Enemy1
{
void enemy1()//(tall)
{
//draw enemy cube
rectMode(CENTER);
rect (enemyPosition.x, enemyPosition.y, 50, 100, 20);
//make the enemy move with an increasing momentum
enemyPosition.x -= enemyMomentum;
enemyMomentum = enemyMomentum + 0.01;
// if the score is to reach the value of 1000, have the enemies slow down. If it reaches past 2000, they speed up again.
if (scorePoints >= 1000 && scorePoints <= 2000) {
enemyMomentum = enemyMomentum - 0.02;
}
}
//draw enemy cube
void enemy2()//(short)
{
//draw enemy cube
rectMode(CENTER);
rect (enemyPosition1.x, enemyPosition1.y, 50, 50, 20);
//make the enemy move with an increasing momentum, slow than the first cube.
enemyPosition1.x -= enemyMomentum1;
enemyMomentum1 = enemyMomentum1 + 0.005;
// if the score is to reach the value of 1000, have the enemies slow down. If it reaches past 2000, they speed up again.
if (scorePoints >= 1000 && scorePoints <= 2000) {
enemyMomentum1 = enemyMomentum1 - 0.01;
}
}
void enemy1Behaviour() {
//if enemy reaches a boundary outside of the screen, move it back to random position of 800 to 900
if (enemyPosition.x <= -100) {
enemyPosition.x = random(800, 900);
}
}
void enemy2Behaviour()
{
//if enemy reaches a boundary outside of the screen, move it back to a random position of 1600 to 1800
if (enemyPosition1.x <= -100) {
enemyPosition1.x = random(1600, 1800);
}
}
void enemy1Collision() {
//if the enemy collides with the player model
if (enemyPosition.x >= playerPosition.x -25 && enemyPosition.x <= playerPosition.x + 25)
if (enemyPosition.y >= playerPosition.y -50 && enemyPosition.y <= playerPosition.y +25) {
//score gets reset to 0
scorePoints = 0;
//enemy momentum is returned to original value
enemyMomentum = 3;
enemyMomentum1 = 3;
//reset enemy position
enemyPosition.x = random(800, 900);
enemyPosition1.x = random(1600, 1800);
//flash red
fill(#FC0A0A);
rect (0, 0, 1000, 600);
//make background white again. I did this to signal that the player is back at square one, where they started.
fill(255);
}
}
void enemy2Collision() {
//if the enemy collides with the player model
if (enemyPosition1.x >= playerPosition.x -25 && enemyPosition1.x <= playerPosition.x + 25)
if (enemyPosition1.y >= playerPosition.y -25 && enemyPosition1.y <= playerPosition.y +25) {
//score is reset to 0
scorePoints = 0;
//enemy momentum is reset
enemyMomentum = 3;
enemyMomentum1 = 3;
//enemy position is reset
enemyPosition.x = random(800, 900);
enemyPosition1.x = random(1600, 1800);
//flash red
fill(#FC0A0A);
rect (0, 0, 10000, 6000);
//make background white again. I did this to signal that the player is back at square one, where they started.
fill(255);
}
}
}public class Ground
{
public void drawGround()
{
//create the ground platform
rectMode(CORNERS);
rect(0, 400, 800, 350);
}
}class Particles
{
float particles[] = new float[50];
float particlePlaceHolderY = random (playerPosition.y-25, playerPosition.y+25);
float particlePlaceHolderX = random (playerPosition.x, playerPosition.x-100);
void particleExplosion()
{
//a for loop to create circles
for (int i=0; i <50; i++)
{ //creates a circle at a random location attached to the player over and over. emulates the character moving fast
ellipse(particlePlaceHolderX, particlePlaceHolderY, 5, 5);
particlePlaceHolderX = random (playerPosition.x-500, playerPosition.x);
particlePlaceHolderY = random (playerPosition.y-25, playerPosition.y+25);
//playerPosition.y+25,playerPosition.y-25
}
}
}class Rain
{
float backgroundPlaceHolderX;
float backgroundPlaceHolderY;
void backGroundRain()
{
//create a circle and places it in a random location over and over to emulate speed.
ellipse(backgroundPlaceHolderX,backgroundPlaceHolderY,50,5);
backgroundPlaceHolderX = random(0,800);
backgroundPlaceHolderY = random(0,400);
}
}class Score
{
void scoreBoard()
{
//every frame add +1 point
scorePoints ++;
//if the highscore is higher than the current score, print the highscore.
if (scorePoints > highScore) {
highScore = scorePoints;
}
textSize(20);
//print score text & high score text
text("score:" + scorePoints, 20, 20);
text("high score:" + highScore, 620, 20);
}
}public class Start
{
//starts the boolean as false
boolean startBool = false;
void start1()
{
//if start screen is false, run this code. once "e" is pressed, this code no longer runs
if (startBool == false) {
//draw white background
rectMode(CORNERS);
fill(255);
rect(0, 0, 1000, 600);
fill (0);
textSize(64);
//draw text
text("CUBERUNNER", 200, 100);
textSize(20);
text("Press E to start", 350, 300);
//holds the game assets until the start menu is set to true
scorePoints = 0;
enemyMomentum = 0;
enemyMomentum1 = 0;
enemyPosition.x = random(800, 900);
enemyPosition1.x = random(1600, 1800);
fill(255);
}
}
}