Arrow keys or AD to steer. Avoid red boxes and hit blue boxes to break through them and increase your score. Good luck!
xxxxxxxxxx
p5.disableFriendlyErrors = true;
class Box {
constructor() {
this.reset();
}
show() {
push();
translate(this.boxX, this.boxY, this.boxZ);
fill(this.clr);
box(50);
pop();
}
update() {
this.boxY += this.speedY;
this.boxZ += this.speedZ;
if (this.boxZ > 500) {
this.reset();
return;
}
var playerHitPlat = rectRect(
this.boxX - 25,
height - 330,
50,
50,
-(width / 2) + playerX - 30,
-(width / 2) + playerY - 30,
60,
60
);
if (this.boxZ >= 0 && playerHitPlat) {
switch (this.boxType) {
case "bad":
mode = "loseMode";
loseCause = "You hit an obstacle.";
playerX = width / 2;
playerY = height / 1.4;
break;
case "good":
this.speedY -= 10;
score += 1;
break;
}
}
}
reset() {
this.boxX = random(-(width / 2), width / 2);
this.boxZ = -10000;
this.boxY = height - 300;
this.speedY = 0;
this.speedZ = random(50, 150);
this.boxType = random([0, 1, 2, 3, 4]) > 3 ? "good" : "bad";
this.clr = this.boxType == "good" ? "blue" : "red";
}
}
function setup() {
createCanvas(400, 400, WEBGL);
this.focus();
playerX = width / 2;
playerY = height / 1.4;
speedX = 0;
speedY = 0;
speed = 5;
mode = "gameMode";
font = loadFont("LoveDays-2v7Oe.ttf");
loseCause = null;
score = 0;
boxes = [];
highscore = 0;
for (var j = 0; j < 4; j++) {
boxes.push(new Box());
}
}
function draw() {
background(60);
textFont(font);
if (mode == "gameMode") {
player();
platforms();
for (var i = 0; i < boxes.length; i++) {
boxes[i].show();
boxes[i].update();
}
} else if (mode == "loseMode") {
loseScreen(loseCause);
}
}
function keyPressed() {
switch (key) {
case "a":
case "A":
case "ArrowLeft":
speedX = -speed;
break;
case "d":
case "D":
case "ArrowRight":
speedX = speed;
break;
}
}
function keyReleased() {
switch (key) {
case "a":
case "A":
case "ArrowLeft":
speedX = 0;
break;
case "d":
case "D":
case "ArrowRight":
speedX = 0;
break;
}
}
function platforms() {
push();
translate(0, height - 250, 0);
fill("green");
box(width, height / 6, 1000000);
pop();
push();
translate(0, 0);
fill("green");
textSize(50);
text(int(score), -10, -(height / 4));
pop();
score += 0.01;
}
function player() {
push();
translate(-(width / 2), -(height / 2));
translate(playerX, playerY);
fill("yellow");
rotateX(frameCount * 0.1);
sphere(30);
pop();
playerX += speedX;
playerY += speedY;
if (playerX < 0 || playerX > width) {
mode = "loseMode";
loseCause = "You fell off the edge.";
playerX = width / 2;
playerY = height / 1.4;
}
}
function loseScreen(label) {
if (score >= highscore) {
highscore = score;
}
var y = -(height / 2);
push();
fill("black");
rect(-(width / 2), y, width, height);
fill("red");
textSize(50);
textAlign(CENTER, CENTER);
text("You Lose!", 0, y + 100);
textSize(25);
text(label, 0, y + 150);
text("Your score was " + int(score) + ".", 0, y + 180);
text("Your highscore is " + int(highscore) + ".", 0, y + 210);
text("Click to try again", 0, y + 240);
pop();
if (mouseIsPressed) {
mode = "gameMode";
for (var i = 0; i < boxes.length; i++) {
boxes[i].reset();
}
score = 0;
}
}
function rectRect(r1x, r1y, r1w, r1h, r2x, r2y, r2w, r2h) {
return (
r1x + r1w >= r2x && r1x <= r2x + r2w && r1y + r1h >= r2y && r1y <= r2y + r2h
);
}