xxxxxxxxxx
508
let numAsteroids = 5;
let asteroids;
let coin;
let globalSpeedMod; // Will adjust speed over time
let spaceship;
let score;
let highScore;
let numRocks;
let pageNum;
let imgBack;
let imgShip;
let imgRock;
let imgHelp;
let dangerZone;
let shipVariant;
function preload() {
soundFormats('mp3', 'wav');
imgBack = loadImage("/SpaceRocksBackdrop.png");
imgShip = loadImage("/AsteroidShip.png");
imgShip2 = loadImage("/AsteroidShip2.png");
imgShip3 = loadImage("/AsteroidShip3.png");
imgRock = loadImage("/Asteroid.png");
imgHelp = loadImage("/SpaceRocksMoveUI.png");
coinSound = loadSound('/coin.wav');
crashSound = loadSound('/crash.wav');
alarmSound = loadSound('/alarm.mp3');
loadStrings("/highscore.txt");
}
function setup() {
createCanvas(windowHeight, windowHeight);
pageNum = 1; //1 is menu, 2 is game, 3 is buy.
globalSpeedMod = 1;
score = 0;
numRocks = 6;
shopButton = createButton('Skins');
shopButton.position(width/4*1, height/4*3);
shopButton.mousePressed(openShop);
shopExitButton = createButton('Exit');
shopExitButton.position(width/4*1, height/4*3);
shopExitButton.mousePressed(closeShop);
spaceship = new Spaceship(width / 2, height / 2);
coin = new Coin(width / 2, height / 3);
highScore = localStorage.getItem("highscore");
shipVariant = localStorage.getItem("ship cosmetic");
if (!highScore) {
highScore = 0;
}
if (!shipVariant) {
shipVariant = 1;
}
asteroids = [];
for (let i = 0; i < numAsteroids; i++) {
const side = floor(random(4));
let position;
if (side === 0) position = createVector(random(width), -10);
else if (side === 1) position = createVector(width + 10, random(height));
else if (side === 2) position = createVector(random(width), height + 10);
else position = createVector(-10, random(height));
asteroids.push(new Asteroid(position));
}
}
function draw() {
background(0);
image(imgBack, 0, 0);
imgBack.resize(windowHeight, windowHeight);
cursor();
if (pageNum == 2) { //GAME
shopButton.hide();
shopExitButton.hide();
noCursor();
fill(255);
textAlign(LEFT);
textSize(24);
text("Score: " + floor(score), 10, 20);
textAlign(RIGHT);
textSize(24);
if (highScore == score) { //Highscore is green while being updated
fill(0, 255, 0);
}
else {
fill(255);
}
text("High Score: " + floor(highScore), width - 10, 20);
spaceship.update();
spaceship.display();
asteroids.sort((a, b) => a.position.y - b.position.y); //stolen sorting function
for (let asteroid of asteroids) {
if (spaceship.checkAlarm(asteroid)) {
dangerZone = 1; // Triggers danger box
push();
stroke(255, 0, 0, 200);
strokeWeight(2);
setLineDash([5, 10, 30, 10]); //Dashed line to ship
line(spaceship.x,spaceship.y,asteroid.position.x,asteroid.position.y);
pop();
} else {
dangerZone = 0;
}
asteroid.update();
asteroid.display();
if (spaceship.checkCollision(asteroid)) {
crashSound.play();
localStorage.setItem("highscore", highScore); //Store current high score in the browser
setup();
score = 0;
}
}
if (random(0, 10000) < 5) { // Very rarely, the speed will increase by 10%
globalSpeedMod += 0.1;
}
console.log("Space Rock Speed Mod" + globalSpeedMod);
if (spaceship.checkAlarmCoin(coin)) {
dangerZone = 1;
push();
stroke(255, 215, 0, 255);
strokeWeight(2);
setLineDash([5, 10, 30, 10]);
line(spaceship.x, spaceship.y, coin.x, coin.y);
pop();
}
coin.display();
if (spaceship.checkCollisionCoin(coin)) {
coinSound.amp(0.1);
coinSound.play();
coin.update();
score += 1 + floor(coin.size);
coin.size = 3;
}
if (asteroids.length < 6 + floor(score / 10) && random(0, 100) < 5) {
//Checks for max, increases per 10 points.
const side = floor(random(4));
//Spawns in one of four corners, offmap.
if (side === 0) {
x = random(width);
y = -10;
} else if (side === 1) {
x = width + 10;
y = random(height);
} else if (side === 2) {
x = random(width);
y = height + 10;
} else {
x = -10;
y = random(height);
}
numRocks++;
asteroids.push(new Asteroid(x, y));
console.log(numRocks + " Asteroids in Play");
}
if (score > highScore) {
highScore = score; // Update high score if current score is higher
}
}
else if (pageNum == 1) { //MENU
shopButton.show();
shopExitButton.hide();
for (let asteroid of asteroids) {
asteroid.update();
asteroid.display();
}
if (asteroids.length < 20 + floor(score / 100) && random(0, 50) < 5) {
// Choose a random side of the canvas
const side = floor(random(4));
//Spawns in one of four corners, offmap.
if (side === 0) {
x = random(width);
y = -10;
} else if (side === 1) {
x = width + 10;
y = random(height);
} else if (side === 2) {
x = random(width);
y = height + 10;
} else {
x = -10;
y = random(height);
}
numRocks++;
asteroids.push(new Asteroid(x, y));
console.log(numRocks + " Asteroids in Play");
}
fill(255);
textSize(56);
textAlign(CENTER);
text("SPACE ROCKS", width / 2, height / 2);
textSize(24);
text("Press Any Key to Start", width / 2, height / 1.75);
push();
imageMode(CENTER);
image(imgHelp, width / 3 + width / 2, height / 3 + width / 2);
pop();
}
else if (pageNum == 3) { //Shop
shopButton.hide();
shopExitButton.show();
//circle(width/2,height/2,100);
push();
imageMode(CENTER);
scale(4);
image(imgShip, width/3/4, height/2/4);
image(imgShip3,width/3*2/4,height/2/4);
image(imgShip2,width/2/4,height/2/4);
pop();
//BuyBox
push();
noFill();
stroke(255);
strokeWeight(5);
rectMode(CENTER);
rect(width/3,height/2,90,90);
if (highScore < 25) {
stroke(255,0,0);
fill(0,0,0,200)
}
rect(width/2,height/2,90,90);
if (highScore < 50) {
stroke(255,0,0);
fill(0,0,0,200)
}
rect(width/3*2,height/2,90,90);
pop();
textAlign(CENTER);
textSize(18);
text("Free!",width/3,height/2+70);
text("High Score: 25",width/2,height/2+70);
text("High Score: 50",width/3*2,height/2+70);
}
if (keyIsPressed && pageNum == 1) { //Launch game (1 menu to 2 game)
pageNum = 2;
asteroids = [];
score = 0;
globalSpeedMod = 1;
numRocks = 0;
}
if (keyIsPressed && pageNum == 2) { //Esc to menu midgame
if (keyCode === 27) {
shopButton.hide();
setup();
}
}
}
///////////////////////////////////////////////////////// Thus begins class
class Asteroid {
constructor(position) {
this.position = createVector(position.x, position.y);
this.size = random(1, 3);
this.spin = random(300, 5000);
this.velocity = createVector(random(-2, 2), random(-2, 2));
this.direction = this.velocity.copy().normalize();
}
update() {
this.position.add(p5.Vector.mult(this.velocity, globalSpeedMod));
// Wrap around the canvas
this.wrapEdges();
}
display() {
push();
translate(this.position.x, this.position.y);
scale(this.size);
rotate(millis() / this.spin);
rectMode(CENTER);
noFill();
if (dangerZone === 1) {
strokeWeight(1);
if (!alarmSound.isPlaying()){
alarmSound.rate(2.5);
alarmSound.amp(0.1);
alarmSound.play();
}
} else {
strokeWeight(0);
}
stroke(255, 0, 0, 200);
//strokeWeight(0);
setLineDash([2 * this.size, 2 * this.size]);
rect(0, 0, 20, 20);
imageMode(CENTER);
image(imgRock, 0, 0);
pop();
}
wrapEdges() {
if (this.position.x < 0) this.position.x = width;
else if (this.position.x > width) this.position.x = 0;
if (this.position.y < 0) this.position.y = height;
else if (this.position.y > height) this.position.y = 0;
}
}
class Coin {
constructor(x, y) {
this.spin = random(300, 5000);
this.x = x;
this.y = y;
this.size = 3;
}
update() {
this.x = random(50, width - 50);
this.y = random(50, height - 50);
}
display() {
push();
translate(this.x, this.y);
scale(this.size);
if (this.size > 1) {
this.size -= 0.015;
}
rotate(millis() / this.spin);
rectMode(CENTER);
noFill();
stroke(255, 215, 0, 200);
strokeWeight(1);
setLineDash([4, 4]);
circle(0, 0, 20);
pop();
push();
textAlign(CENTER);
fill(255,255,255,100);
translate(this.x, this.y);
scale(this.size/2);
text(floor(this.size)+1 + "+",0,this.size/2+5)
pop();
}
}
class Spaceship {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = 20;
this.angle = PI / 2;
this.speed = 1;
}
checkCollision(asteroid) {
const distance = dist(
this.x,
this.y,
asteroid.position.x,
asteroid.position.y
);
const minDistance = this.size / 2 + (20 * asteroid.size) / 2;
return distance < minDistance;
}
checkCollisionCoin(coin) {
const distance = dist(this.x, this.y, coin.x, coin.y);
const minDistance = this.size / 2 + (20 * coin.size) / 2;
return distance < minDistance;
}
checkAlarm(asteroid) {
const distance = dist(
this.x,
this.y,
asteroid.position.x,
asteroid.position.y
);
const minDistance = this.size * 2 + 35 * asteroid.size;
return distance < minDistance;
}
checkAlarmCoin(coin) {
const distance = dist(this.x, this.y, coin.x, coin.y);
const minDistance = this.size * 2 + 35;
return distance < minDistance;
}
update() {
if (keyIsDown(UP_ARROW)) {
// Constant acceleration when UP_ARROW is pressed
this.speed += 0.05;
} else {
// Constant deceleration when UP_ARROW is not pressed
this.speed -= 0.05; // Adjust the deceleration rate as needed
this.speed = max(0, this.speed); // Ensure speed doesn't go below zero
}
if (keyIsDown(DOWN_ARROW) && this.speed >= 0) {
this.speed -= 0.09;
}
// Move forward
//this.y -= this.speed; // Subtract speed for upward movement
this.x -= cos(this.angle) * this.speed;
this.y -= sin(this.angle) * this.speed;
if (keyIsDown(RIGHT_ARROW)) {
// Rotate clockwise
this.angle += 0.05;
}
if (keyIsDown(LEFT_ARROW)) {
// Rotate the other clockwise
this.angle -= 0.05;
}
// Wrap around the canvas
this.wrapEdges();
}
wrapEdges() {
if (this.x > width) {
this.x = 0;
} else if (this.x < 0) {
this.x = width;
}
if (this.y > height) {
this.y = 0;
} else if (this.y < 0) {
this.y = height;
}
}
display() {
push();
translate(this.x, this.y);
rotate(this.angle - PI / 2);
scale(1.8);
fill(255);
noStroke();
imageMode(CENTER);
if (shipVariant == 1){
image(imgShip, 0, 0);
}
else if (shipVariant == 2){
image(imgShip2,0,0);
}
else if (shipVariant == 3){
image(imgShip3,0,0);
}
pop();
}
}
//Ethically sourced from user SquishyNotions (sorry jeff, i saw ur guide too but theirs was less total code so easier to steal).
function setLineDash(list) {
drawingContext.setLineDash(list);
}
//Evil stinky bastard code who isn't particularly inefficient but i dislike the personality of.
function openShop() {pageNum = 3;}
function closeShop() {pageNum = 1;}
function mouseClicked() {
if (pageNum == 3 && mouseX > width/3*2 -45 && mouseX < width/3*2 + 45 && mouseY > height/2 -45 && mouseY < height/2 + 45) {
if (highScore > 49) {
console.log("boop3");
shipVariant = 3;
localStorage.setItem("ship cosmetic", shipVariant);
}
}
if (pageNum == 3 && mouseX > width/3 -45 && mouseX < width/3 + 45 && mouseY > height/2 -45 && mouseY < height/2 + 45) {
console.log("boop");
shipVariant = 1;
localStorage.setItem("ship cosmetic", shipVariant);
}
if (pageNum == 3 && mouseX > width/2 -45 && mouseX < width/2 + 45 && mouseY > height/2 -45 && mouseY < height/2 + 45) {
if (highScore > 24) {
console.log("boop2");
shipVariant = 2;
localStorage.setItem("ship cosmetic", shipVariant);
}
}
}