xxxxxxxxxx
162
let planets = [];
let timer = 1;
let points = 0;
function setup() {
createCanvas(800, 800);
SpaceProbes = [];
player = new Player(400, 770,0, 20);
generateRandomPlanet();
}
function draw() {
background("rgb(204,242,204)");
player.draw();
player.move();
for (let i = 0; i < SpaceProbes.length; i++) {
SpaceProbes[i].draw();
}
for (let i = 0; i < planets.length; i++) {
planets[i].draw();
}
// checkHit();
// if (frameCount % 60 == 0 && timer > 0) {
// timer--;
// if (timer == 0) {
// // generateEnemy();
// timer = 1;
// }
// }
}
function keyReleased(event) {
console.log();
if (event.code === "Space") {
shoot();
}
return false;
}
function generateRandomPlanet() {
for (let i = 0; i < 1; i++) {
planets.push(
new Planet(
floor(random(50, width - 100)),
floor(random(70, height / 3 - 100)),
floor(random(100,200))
)
);
}
}
function shoot() {
let probe = new SpaceProbe(player.posX + 12, player.posY - 120, 20);
this.SpaceProbes.push(probe);
for (let i = 0; i < SpaceProbes.length; i++) {
if (this.SpaceProbes[i].posY < 0) {
SpaceProbes.splice(i, 1);
}
}
console.log(SpaceProbes);
}
// function checkHit() {
// for (let i = 0; i < SpaceProbes.length; i++) {
// for (let j = 0; j < enemies.length; j++) {
// let d = dist(
// SpaceProbes[i].posX,
// SpaceProbes[i].posY,
// enemies[j].posX,
// enemies[j].posY
// );
// if (d < enemies[j].size) {
// enemies.splice(j, 1);
// points++;
// }
// }
// }
// for (let i = 0; i < enemies.length; i++) {
// let d = dist(enemies[i].posX, enemies[i].posY, player.posX, player.posY);
// if (d < player.size) {
// points = 0;
// player.posX = 300;
// player.posY = 700;
// enemies = [];
// generateRandomEnemies();
// }
// }
// }
// function generateEnemy() {
// enemies.push(
// new Enemy(
// floor(random(20, width - 50)),
// floor(random(20, height / 3 - 50)),
// 20
// )
// );
// }
class Player {
constructor(posX, posY, size, rotation) {
this.posX = posX;
this.posY = posY;
this.rotation = rotation;
this.size = size;
}
draw() {
rect(this.posX, this.posY - 90, 30, 120);
fill("#424242");
}
move() {
console.log(this.rotation);
if (keyIsDown(LEFT_ARROW) && this.posX >= 5) {
this.posX -= 5;
}
if (keyIsDown(RIGHT_ARROW) && this.posX <= width - player.size) {
this.posX += 5;
}
// if (keyIsDown(UP_ARROW)) {
// rotate(PI / this.rotation++);
// }
// if (keyIsDown(DOWN_ARROW)) {
// this.posY += 5;
// }
}
}
class Planet{
constructor(posX, posY, radius){
this.posX = posX;
this.posY = posY;
this.radius = radius;
}
draw(){
circle(this.posX, this.posY, this.radius);
}
}
class SpaceProbe {
constructor(posX, posY, size) {
this.posX = posX;
this.posY = posY;
this.size = size;
}
draw() {
circle(this.posX, this.posY, this.size + 30);
this.posY = this.posY - 10;
}
}