xxxxxxxxxx
295
//variables
let ship;
let asteroid;
let level;
let shipLevel;
let currentMinute;
function setup() {
//setup
createCanvas(1000, 1000);
ship = new Ship();
asteroid = new Asteroid();
level = 1;
shipLevel = 1;
currentMinute = minute();
noCursor();
}
function draw() {
//draw loop
background(0);
ship.update();
ship.show();
asteroid.update();
asteroid.show();
//could use a fix... starting level could only be a couple of seconds
if (currentMinute != minute()) {
level++;
currentMinute = minute();
}
if (level > 5 && shipLevel <= 5) {
level = 1;
shipLevel++;
}
}
class Ship {
constructor() {
//constructor
this.x = 500;
this.y = 500;
this.right = 48;
this.left = 49;
this.down = 50;
this.up = 51;
this.hasntHappened = true;
this.arrows = [37, 38, 39, 40];
}
update() {
//movement system
if (shipLevel == 1) {
if (keyIsDown(RIGHT_ARROW)) {
this.x += 5;
}
if (keyIsDown(LEFT_ARROW)) {
this.x -= 5;
}
if (keyIsDown(DOWN_ARROW)) {
this.y += 5;
}
if (keyIsDown(UP_ARROW)) {
this.y -= 5;
}
}
if (shipLevel == 2) {
if (keyIsDown(37)) {
this.x += 5;
}
if (keyIsDown(39)) {
this.x -= 5;
}
if (keyIsDown(38)) {
this.y += 5;
}
if (keyIsDown(40)) {
this.y -= 5;
}
}
if (shipLevel == 3) {
if (keyIsDown(88) && keyIsDown(16)) {
this.x -= 5;
} else if (keyIsDown(88)) {
this.x += 5;
}
if (keyIsDown(89) && keyIsDown(16)) {
this.y -= 5;
} else if (keyIsDown(89)) {
this.y += 5;
}
}
if (shipLevel > 3) {
if (keyIsDown(this.right)) {
this.x += 5;
}
if (keyIsDown(this.left)) {
this.x -= 5;
}
if (keyIsDown(this.down)) {
this.y += 5;
}
if (keyIsDown(this.up)) {
this.y -= 5;
}
}
if (shipLevel == 5 && this.hasntHappened) {
let arrows = [37, 38, 39, 40];
arrows = shuffle(arrows);
ship.right = arrows[0];
ship.left = arrows[1];
ship.down = arrows[2];
ship.up = arrows[3];
this.hasntHappened = false;
}
if (shipLevel == 5) {
if (round(frameCount/200) == frameCount/200) {
this.arrows = shuffle(this.arrows);
this.right = this.arrows[0];
this.left = this.arrows[1];
this.down = this.arrows[2];
this.up = this.arrows[3];
}
}
if (shipLevel == 6) {
text("You're on your own man...", 50, 55);
}
//wrapping the edges
if (this.x > width) {
this.x = 0;
}
if (this.x < 0) {
this.x = width;
}
if (this.y > height) {
this.y = 0;
}
if (this.y < 0) {
this.y = height;
}
}
show() {
//drawing the dot of the ship
stroke(100, 255, 100);
strokeWeight(5);
noFill();
point(this.x, this.y);
if (shipLevel == 4) {
noStroke();
fill(100, 255, 100);
textSize(15);
text("Right: " + (this.right - 48) + "\nLeft: " + (this.left - 48) + "\nDown: " + (this.down - 48) + "\nUp: " + (this.up - 48), 50, 55);
}
}
}
class Asteroid {
constructor() {
//constructor
this.x = 0;
this.y = 0;
this.speed = 5;
this.rand = random(0, this.speed);
}
update() {
//movement
this.x += this.rand;
this.y += this.speed - this.rand;
//warpping the edges
if (level == 1) {
if (this.x > width + 100) {
this.x = -100;
this.rand = random(0, this.speed);
}
if (this.x < -100) {
this.x = width + 100;
this.rand = random(0, this.speed);
}
if (this.y > height + 100) {
this.y = -100;
this.rand = random(0, this.speed);
}
if (this.y < -100) {
this.y = height + 100;
this.rand = random(0, this.speed);
}
}
if (level > 1 && level < 5) {
if (this.x > width) {
this.x = 0;
this.rand = random(0, this.speed);
}
if (this.x < 0) {
this.x = width;
this.rand = random(0, this.speed);
}
if (this.y > height) {
this.y = 0;
this.rand = random(0, this.speed);
}
if (this.y < 0) {
this.y = height;
this.rand = random(0, this.speed);
}
}
if (level == 5) {
if (this.x > width) {
this.x = 0;
}
if (this.x < 0) {
this.x = width;
}
if (this.y > height) {
this.y = 0;
}
if (this.y < 0) {
this.y = height;
}
}
//death condition
if (dist(this.x, this.y, ship.x, ship.y) < 100) {
console.log("cooked!");
frameRate(0);
}
}
show() {
//drawing the asteroid
if (level == 1) {
noStroke();
fill(255, 100, 100);
circle(this.x, this.y, 200);
}
if (level == 2) {
stroke(255, 100, 100);
noFill();
point(this.x, this.y);
}
if (level == 3) {
if (floor(frameCount/70) == round(frameCount/70 + 0.4)) {
stroke(255, 100, 100);
noFill();
point(this.x, this.y);
}
}
if (level == 4) {
stroke(255, 100, 100);
line(-10, this.y, 10, this.y);
line(this.x, height + 10, this.x, height - 10);
}
if (level == 5) {
noStroke();
fill(255, 100, 100);
textSize(15);
text("COORDINATES:\nParsec X: " + round(this.x/100) + "\nParsec Y: " + round(this.y/100), 50, 905);
}
}
}
function keyPressed() {
//changing the level
if (!isNaN(key) && key > 0 && key <= 5) {
//level = key;
console.log(level);
}
}
function keyReleased() {
if (shipLevel == 4) {
ship.right = round(random(48, 54));
ship.left = ship.right + 1;
ship.down = ship.right + 2;
ship.up = ship.right + 3;
}
}