xxxxxxxxxx
338
// Parachute Game — iPod Nano 1st gen
const pane = new Tweakpane.Pane();
let tower = null;
const bullets = [];
const ships = [];
const parachutes = [];
let gameIsRunning = false;
let score = 0;
let lifes = 3;
let sentLasShip = 0;
const START_SHIP_DELAY = 6000;
let shipMaxDelay = START_SHIP_DELAY;
const SHIP_POINTS = 100;
const PARACHUTE_POINTS = 50;
const PARAMS = {
frameRate: 0,
potiVal: 0,
buttonPress: 0,
blokdotsIsConnected: false,
};
function preload() {}
function setup() {
createCanvas(500, 500);
imagePG = createGraphics(width, height);
drawingPG = createGraphics(width, height);
// connect to blokdots
const socket = io("http://localhost:8777/blokdots");
socket.on("connect", function () {
PARAMS.blokdotsIsConnected = true;
});
socket.on("disconnect", function () {
PARAMS.blokdotsIsConnected = false;
});
socket.on("blokdots", function (data) {
if (data.msg === "poti") {
PARAMS.potiVal = data.val;
}
if (data.msg === "buttonPress") {
PARAMS.buttonPress = 1;
if (gameIsRunning) {
tower.shoot();
} else {
startGame();
}
}
if (data.msg === "buttonRelease") {
PARAMS.buttonPress = 0;
}
});
// tweakpane
const f1 = pane.addFolder({
title: "Monitoring",
});
f1.addMonitor(PARAMS, "frameRate");
f1.addMonitor(PARAMS, "frameRate", {
view: "graph",
min: 0,
max: 65,
});
f1.addMonitor(PARAMS, "blokdotsIsConnected");
f1.addMonitor(PARAMS, "potiVal");
f1.addMonitor(PARAMS, "buttonPress");
// game
tower = new Tower();
}
function draw() {
background(20);
if (gameIsRunning) {
if (lifes <= 0) {
endGame();
}
generateShips();
for (let s = 0; s < ships.length; s++) {
ships[s].draw();
}
for (let p = 0; p < parachutes.length; p++) {
parachutes[p].draw();
}
for (let b = 0; b < bullets.length; b++) {
bullets[b].draw();
}
tower.draw();
showStats();
} else {
push();
textAlign(CENTER);
fill(255);
text("Press Button To Play", width / 2, height / 2);
text(score, width / 2, height / 2 + 40);
pop();
}
PARAMS.frameRate = frameRate();
}
class Tower {
constructor() {
this.angle = 90;
this.isShooting = false;
this.shootingTimeout = null;
}
draw() {
this.angle = map(PARAMS.potiVal, 0, 1023, 90, -90);
push();
translate(width / 2, height);
rotate(radians(this.angle));
if (this.isShooting) {
fill(0, 255, 0);
} else {
fill(100);
}
rect(-10, -100, 20, 100);
pop();
}
shoot() {
this.isShooting = true;
clearTimeout(this.shootingTimeout);
this.shootingTimeout = setTimeout(() => {
this.isShooting = false;
}, 200);
bullets.push(new Bullet(this.angle));
}
}
class Bullet {
constructor(angle) {
this.angle = radians(angle - 90);
this.speed = 5;
this.x = width / 2 + cos(this.angle) * 100;
this.y = height + sin(this.angle) * 100;
this.xMove = cos(this.angle) * this.speed;
this.yMove = sin(this.angle) * this.speed;
}
draw() {
push();
translate(this.x, this.y);
// circle( 0, 0, 6 );
rotate(this.angle - HALF_PI);
fill(255);
stroke(0, 255, 0);
rect(0, 0, 3, 10, 3, 3, 3, 3);
pop();
this.move();
}
move() {
this.x += this.xMove;
this.y += this.yMove;
if (this.x > width || this.x < 0 || this.y < 0) {
this.destroy();
}
}
destroy() {
const index = bullets.indexOf(this);
if (index > -1) {
bullets.splice(index, 1);
}
}
}
const generateShips = () => {
if (sentLasShip === 0 || millis() - sentLasShip > shipMaxDelay) {
if (random() > 0.97) {
ships.push(new Ship());
sentLasShip = millis();
}
}
// increase spawn time
shipMaxDelay = map(score, 0, 5000, START_SHIP_DELAY, 500, true);
};
class Ship {
constructor() {
this.fromRight = random([true, false]);
this.x = this.fromRight ? width : -40;
this.y = random(100, 200);
this.speed = 1;
this.sentLasParachute = 0;
}
draw() {
push();
translate(this.x, this.y);
// rect( 0, 0, 40, 30 );
textAlign(CENTER);
textSize(40);
text("🛸", 20, 30);
pop();
this.checkForCollision();
this.generateParachute();
this.move();
}
move() {
this.x += (this.fromRight ? -1 : 1) * this.speed;
if (this.x > width || this.x + 40 < 0) {
this.destroy();
}
}
checkForCollision() {
const bR = 3; // bullet hitbox
bullets.forEach((b) => {
if (
b.x + bR > this.x &&
b.x - bR < this.x + 40 &&
b.y + bR < this.y + 30 &&
b.y - bR > this.y
) {
this.destroy();
b.destroy();
score += SHIP_POINTS;
}
});
}
generateParachute() {
if (
millis() - this.sentLasParachute > 2000 &&
this.x > 50 &&
this.x < width - 50
) {
if (random() > 0.97) {
parachutes.push(new Parachute(this.x, this.y));
this.sentLasParachute = millis();
}
}
}
destroy() {
const index = ships.indexOf(this);
if (index > -1) {
ships.splice(index, 1);
}
}
}
class Parachute {
constructor(x, y) {
this.x = x;
this.y = y;
this.startTime = millis();
this.speed = 1;
}
draw() {
push();
translate(this.x, this.y);
// rect( -10, 0, 20, 20 );
textAlign(CENTER);
textSize(20);
text("👾", 0, 16);
pop();
this.checkForCollision();
this.move();
}
move() {
if (this.y > height - 20) {
lifes--;
this.destroy();
} else {
this.y += this.speed;
}
}
checkForCollision() {
const bR = 3; // bullet hitbox
bullets.forEach((b) => {
if (
b.x + bR > this.x &&
b.x - bR < this.x + 40 &&
b.y + bR < this.y + 30 &&
b.y - bR > this.y
) {
this.destroy();
b.destroy();
score += PARACHUTE_POINTS;
}
});
}
destroy() {
const index = parachutes.indexOf(this);
if (index > -1) {
parachutes.splice(index, 1);
}
}
}
const showStats = () => {
push();
textAlign(LEFT);
fill(255);
text(score, 20, 20);
for (let l = 0; l < lifes; l++) {
text("❤️", l * 20 + 20, 40);
}
pop();
};
const startGame = () => {
score = 0;
gameIsRunning = true;
lifes = 3;
sentLasShip = 0;
shipMaxDelay = START_SHIP_DELAY;
};
const endGame = () => {
console.log(score);
ships.splice(0, ships.length);
parachutes.splice(0, parachutes.length);
bullets.splice(0, bullets.length);
gameIsRunning = false;
};