xxxxxxxxxx
258
// player object
let player;
// obstacle and power-up arrays
let obstacles = [];
let powerUps = [];
// game state variables
let introMenu = true;
let endGame = false;
// score and time variables
let score = 0;
let timeLeft = 30;
// sound and animation variables
let collisionSound;
let powerUpSound;
let explosionAnimation;
function preload() {
//collisionSound = loadSound('collision.mp3');
//powerUpSound = loadSound('power-up.mp3');
// explosionAnimation = loadAnimation('explosion.png', 'explosion2.png');
}
function setup() {
createCanvas(800, 600, WEBGL);
player = new Player();
// create initial obstacles
for (let i = 0; i < 5; i++) {
obstacles.push(new Obstacle());
}
// create initial power-ups
for (let i = 0; i < 3; i++) {
powerUps.push(new PowerUp());
}
}
function setupIntroMenu() {
background(0);
textSize(32);
textAlign(CENTER);
text('Press Enter to Start', 0, 0);
}
function setupEndGame() {
background(0);
textSize(32);
textAlign(CENTER);
text('Game Over', 0, -50);
text(`Score: ${score}`, 0, 0);
}
function drawGameWorld() {
// rotate the world
rotateX(-PI / 6);
rotateZ(frameCount * 0.01);
// update and display player
player.update();
player.display();
// update and display obstacles
for (let i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
obstacles[i].display();
// check for collision with player
if (player.collidesWith(obstacles[i])) {
collisionSound.play();
player.explode();
endGame = true;
}
// remove obstacles that go off-screen
if (obstacles[i].y > height) {
obstacles.splice(i, 1);
score++;
}
}
// update and display power-ups
for (let i = powerUps.length - 1; i >= 0; i--) {
powerUps[i].update();
powerUps[i].display();
// check for collision with player
if (player.collidesWith(powerUps[i])) {
powerUpSound.play();
powerUps.splice(i, 1);
obstacles.push(new Obstacle());
score += 10;
}
// remove power-ups that go off-screen
if (powerUps[i].y > height) {
powerUps.splice(i, 1);
}
}
// display score and time left
textSize(24);
textAlign(RIGHT);
text(`Score: ${score}`, width / 2 - 10, -height / 2 + 30);
textAlign(LEFT);
text(`Time Left: ${timeLeft.toFixed(0)}`, -width / 2 + 10, -height / 2 + 30);
}
function draw() {
if (introMenu) {
setupIntroMenu();
if (keyIsDown(ENTER)) {
introMenu = false;
}
} else if (endGame) {
setupEndGame();
} else {
background(0);
drawGameWorld();
timeLeft -= deltaTime / 1000;
// end game when time is up
if (timeLeft <= 0) {
endGame = true;
}
}
}
function keyPressed() {
if (keyCode === UP_ARROW) {
player.moveUp();
} else if (keyCode === DOWN_ARROW) {
player.moveDown();
} else if (keyCode === LEFT_ARROW) {
player.moveLeft();
} else if (keyCode === RIGHT_ARROW) {
player.moveRight();
}
}
// player class
class Player {
constructor() {
this.x = 0;
this.y = 0;
this.z = 0;
this.speed = 5;
this.size = 40;
this.color = color(255, 0, 0);
this.exploding = false;
this.explosionFrame = 0;
}
moveUp() {
this.y -= this.speed;
}
moveDown() {
this.y += this.speed;
}
moveLeft() {
this.x -= this.speed;
}
moveRight() {
this.x += this.speed;
}
update() {
if (this.exploding) {
this.explosionFrame++;
if (this.explosionFrame >= explosionAnimation.getLastFrame()) {
endGame = true;
}
}
}
display() {
push();
translate(this.x, this.y, this.z);
if (this.exploding) {
animation(explosionAnimation, 0, 0);
} else {
noStroke();
fill(this.color);
box(this.size);
}
pop();
}
collidesWith(obstacle) {
let d = dist(this.x, this.y, this.z, obstacle.x, obstacle.y, obstacle.z);
return d < this.size / 2 + obstacle.size / 2;
}
explode() {
this.exploding = true;
this.explosionFrame = 0;
}
}
// obstacle class
class Obstacle {
constructor() {
this.x = random(-width / 2, width / 2);
this.y = random(-height / 2, -height / 2 - 100);
this.z = random(-400, 400);
this.speed = random(2, 5);
this.size = random(20, 60);
this.color = color(0, 255, 0);
}
update() {
this.y += this.speed;
}
display() {
push();
translate(this.x, this.y, this.z);
noStroke();
fill(this.color);
box(this.size);
pop();
}
}
// power-up class
class PowerUp {
constructor() {
this.x = random(-width / 2, width / 2);
this.y = random(-height / 2, -height / 2 - 100);
this.z = random(-400, 400);
this.speed = random(2, 5);
this.size = 30;
this.color = color(0, 0, 255);
}
update() {
this.y += this.speed;
}
display() {
push();
translate(this.x, this.y, this.z);
noStroke();
fill(this.color);
sphere(this.size);
pop();
}
}