Now with kids mode

This commit is contained in:
Eric Ratliff
2025-06-25 23:28:10 -05:00
parent 681feab148
commit a569e6b7d2
5 changed files with 109 additions and 57 deletions

View File

@@ -10,8 +10,9 @@ class Asteroid {
this.pos = createVector(random(width), random(height)); this.pos = createVector(random(width), random(height));
} }
} }
this.vel = p5.Vector.random2D(); this.baseVel = p5.Vector.random2D();
this.vel.mult(random(1, 3)); this.speed = window.kidMode ? random(0.2, 1.5) : random(1, 3);
this.vel = this.baseVel.copy().mult(this.speed);
this.total = floor(random(5, 15)); this.total = floor(random(5, 15));
this.offset = []; this.offset = [];
for (let i = 0; i < this.total; i++) { for (let i = 0; i < this.total; i++) {
@@ -19,10 +20,12 @@ class Asteroid {
} }
this.isGolden = isGolden; this.isGolden = isGolden;
this.hitsLeft = isGolden ? 3 : 1; this.hitsLeft = isGolden ? 3 : 1;
console.log(`Spawned asteroid, isGolden: ${isGolden}, hitsLeft: ${this.hitsLeft}`); console.log(`Spawned asteroid, isGolden: ${isGolden}, hitsLeft: ${this.hitsLeft}, speed: ${this.speed}`);
} }
update() { update() {
this.speed = window.kidMode ? random(0.2, 1.5) : random(1, 3);
this.vel = this.baseVel.copy().mult(this.speed);
this.pos.add(this.vel); this.pos.add(this.vel);
} }

View File

@@ -2,11 +2,14 @@ class Hamburger {
constructor(pos, r) { constructor(pos, r) {
this.pos = pos.copy(); this.pos = pos.copy();
this.r = r ? r / 2 : random(20, 30); this.r = r ? r / 2 : random(20, 30);
this.vel = p5.Vector.random2D(); this.baseVel = p5.Vector.random2D();
this.vel.mult(random(1, 3)); this.speed = window.kidMode ? random(0.5, 1.5) : random(1, 3);
this.vel = this.baseVel.copy().mult(this.speed);
} }
update() { update() {
this.speed = window.kidMode ? random(0.5, 1.5) : random(1, 3);
this.vel = this.baseVel.copy().mult(this.speed);
this.pos.add(this.vel); this.pos.add(this.vel);
} }

View File

@@ -10,8 +10,9 @@ class Pizza {
this.pos = createVector(random(width), random(height)); this.pos = createVector(random(width), random(height));
} }
} }
this.vel = p5.Vector.random2D(); this.baseVel = p5.Vector.random2D();
this.vel.mult(random(1, 3)); this.speed = window.kidMode ? random(0.5, 1.5) : random(1, 3);
this.vel = this.baseVel.copy().mult(this.speed);
this.total = floor(random(8, 12)); // For potential future use this.total = floor(random(8, 12)); // For potential future use
this.offset = []; this.offset = [];
for (let i = 0; i < this.total; i++) { for (let i = 0; i < this.total; i++) {
@@ -20,6 +21,8 @@ class Pizza {
} }
update() { update() {
this.speed = window.kidMode ? random(0.5, 1.5) : random(1, 3);
this.vel = this.baseVel.copy().mult(this.speed);
this.pos.add(this.vel); this.pos.add(this.vel);
} }

117
ship.js
View File

@@ -1,15 +1,48 @@
class Ship { class Ship {
constructor() { constructor(safeSpawn = false) {
this.pos = createVector(width / 2, height / 2); if (safeSpawn) {
this.pos = createVector(random(width), random(height));
let safe = false;
while (!safe) {
safe = true;
for (let asteroid of asteroids) {
if (dist(this.pos.x, this.pos.y, asteroid.pos.x, asteroid.pos.y) < 100) {
safe = false;
this.pos = createVector(random(width), random(height));
break;
}
}
for (let pizza of pizzas) {
if (dist(this.pos.x, this.pos.y, pizza.pos.x, pizza.pos.y) < 100) {
safe = false;
this.pos = createVector(random(width), random(height));
break;
}
}
for (let hamburger of hamburgers) {
if (dist(this.pos.x, this.pos.y, hamburger.pos.x, hamburger.pos.y) < 100) {
safe = false;
this.pos = createVector(random(width), random(height));
break;
}
}
}
} else {
this.pos = createVector(width / 2, height / 2);
}
this.r = 20; this.r = 20;
this.heading = 0; this.heading = 0;
this.rotation = 0; this.rotation = 0;
this.vel = createVector(0, 0); this.vel = createVector(0, 0);
this.isBoosting = false; this.isBoosting = false;
this.shieldActive = false; this.shieldActive = false;
this.shieldEndTime = 0; this.shieldStart = 0;
this.quadShotActive = false; this.quadShotActive = false;
this.quadShotEndTime = 0; this.quadShotStart = 0;
}
boosting(b) {
this.isBoosting = b;
} }
update() { update() {
@@ -17,12 +50,16 @@ class Ship {
this.boost(); this.boost();
} }
this.pos.add(this.vel); this.pos.add(this.vel);
this.vel.mult(0.99); // Friction this.vel.mult(0.99);
this.heading += this.rotation; this.heading += this.rotation;
if (this.shieldActive && millis() > this.shieldEndTime) {
// Check shield duration
if (this.shieldActive && millis() - this.shieldStart > 10000) {
this.shieldActive = false; this.shieldActive = false;
} }
if (this.quadShotActive && millis() > this.quadShotEndTime) {
// Check quad shot duration
if (this.quadShotActive && millis() - this.quadShotStart > 10000) {
this.quadShotActive = false; this.quadShotActive = false;
} }
} }
@@ -38,54 +75,52 @@ class Ship {
return d < this.r + obj.r; return d < this.r + obj.r;
} }
edges() { activateShield() {
if (this.pos.x > width + this.r) this.pos.x = -this.r; this.shieldActive = true;
else if (this.pos.x < -this.r) this.pos.x = width + this.r; this.shieldStart = millis();
if (this.pos.y > height + this.r) this.pos.y = -this.r; }
else if (this.pos.y < -this.r) this.pos.y = height + this.r;
activateQuadShot() {
this.quadShotActive = true;
this.quadShotStart = millis();
} }
setRotation(a) { setRotation(a) {
this.rotation = a; this.rotation = a;
} }
boosting(b) {
this.isBoosting = b;
}
activateShield() {
this.shieldActive = true;
this.shieldEndTime = millis() + 10000; // 10 seconds
}
activateQuadShot() {
this.quadShotActive = true;
this.quadShotEndTime = millis() + 10000; // 10 seconds
}
show() { show() {
push(); push();
translate(this.pos.x, this.pos.y); translate(this.pos.x, this.pos.y);
rotate(this.heading + PI / 2); rotate(this.heading + PI / 2);
noFill();
stroke(255);
if (this.quadShotActive) {
fill(0, 255, 0); // Green fill for quad-shot
let quadTimeLeft = 10000 - (millis() - this.quadShotStart);
if (quadTimeLeft < 2000) {
let alpha = 255 * (1 + sin(millis() * 0.01)) / 2;
fill(0, 255, 0, alpha);
}
}
triangle(-this.r, this.r, this.r, this.r, 0, -this.r);
if (this.shieldActive) { if (this.shieldActive) {
// Pulsing shield effect in last 2 seconds
let timeLeft = (this.shieldEndTime - millis()) / 1000;
let scaleFactor = this.shieldActive && timeLeft < 2 ? 1 + 0.1 * sin(millis() / 100) : 1;
scale(scaleFactor);
noFill(); noFill();
stroke(0, 255, 255); // Cyan shield stroke(0, 255, 255);
let shieldTimeLeft = 10000 - (millis() - this.shieldStart);
if (shieldTimeLeft < 2000) {
let scaleFactor = 1 + 0.2 * sin(millis() * 0.01);
scale(scaleFactor);
}
ellipse(0, 0, this.r * 2.5); ellipse(0, 0, this.r * 2.5);
} }
// Ship fill: green if quad-shot active, else black
if (this.quadShotActive) {
let timeLeft = (this.quadShotEndTime - millis()) / 1000;
let alpha = this.quadShotActive && timeLeft < 2 ? map(sin(millis() / 100), -1, 1, 100, 255) : 255;
fill(0, 255, 0, alpha);
} else {
fill(0);
}
stroke(255);
triangle(-this.r, this.r, this.r, this.r, 0, -this.r);
pop(); pop();
} }
edges() {
if (this.pos.x > width + this.r) this.pos.x = -this.r;
else if (this.pos.x < -this.r) this.pos.x = width + this.r;
if (this.pos.y > height + this.r) this.pos.y = -this.r;
else if (this.pos.y < -this.r) this.pos.y = height + this.r;
}
} }

View File

@@ -5,7 +5,7 @@ let hamburgers = [];
let bullets = []; let bullets = [];
let shieldOrb = null; let shieldOrb = null;
let greenOrb = null; let greenOrb = null;
let lives = 100; let lives = 30;
let score = 0; let score = 0;
let gameOver = false; let gameOver = false;
let lastShieldSpawn = 0; let lastShieldSpawn = 0;
@@ -13,6 +13,7 @@ let lastGreenSpawn = 0;
let pizzaImg, hamburgerImg; let pizzaImg, hamburgerImg;
let level = 1; let level = 1;
let initialObjects = 5; let initialObjects = 5;
let kidMode = true;
function preload() { function preload() {
pizzaImg = loadImage('pizza.png', () => console.log('Pizza image loaded'), () => console.error('Failed to load pizza.png')); pizzaImg = loadImage('pizza.png', () => console.log('Pizza image loaded'), () => console.error('Failed to load pizza.png'));
@@ -21,6 +22,7 @@ function preload() {
function setup() { function setup() {
createCanvas(800, 600); createCanvas(800, 600);
window.kidMode = kidMode; // Set global kidMode before spawning
ship = new Ship(); ship = new Ship();
spawnObjects(initialObjects); spawnObjects(initialObjects);
} }
@@ -28,7 +30,7 @@ function setup() {
function spawnObjects(numObjects) { function spawnObjects(numObjects) {
for (let i = 0; i < numObjects; i++) { for (let i = 0; i < numObjects; i++) {
let rand = random(); let rand = random();
if (rand < 0.25) { if (rand < 0.15) {
pizzas.push(new Pizza()); pizzas.push(new Pizza());
console.log(`Spawned pizza at level ${level}, total pizzas: ${pizzas.length}`); console.log(`Spawned pizza at level ${level}, total pizzas: ${pizzas.length}`);
} else if (rand < 0.25) { } else if (rand < 0.25) {
@@ -53,12 +55,13 @@ function draw() {
return; return;
} }
// Display lives, score, and level // Display lives, score, level, and kid mode
textSize(20); textSize(20);
fill(255); fill(255);
text("Lives: " + lives, 50, 30); text("Lives: " + lives, 50, 30);
text("Score: " + score, 50, 60); text("Score: " + score, 50, 60);
text("Level: " + level, 50, 90); text("Level: " + level, 50, 90);
text("Kid Mode: " + (kidMode ? "ON" : "OFF"), 70, 120);
// Handle continuous key input (WASD and Arrow Keys) // Handle continuous key input (WASD and Arrow Keys)
if (keyIsDown(65) || keyIsDown(LEFT_ARROW)) { // A or Left Arrow if (keyIsDown(65) || keyIsDown(LEFT_ARROW)) { // A or Left Arrow
@@ -134,7 +137,7 @@ function draw() {
if (lives <= 0) { if (lives <= 0) {
gameOver = true; gameOver = true;
} else { } else {
ship = new Ship(); // Reset ship position ship = new Ship(true); // Reset ship with safe spawn
} }
} }
@@ -165,7 +168,7 @@ function draw() {
if (lives <= 0) { if (lives <= 0) {
gameOver = true; gameOver = true;
} else { } else {
ship = new Ship(); // Reset ship position ship = new Ship(true); // Reset ship with safe spawn
} }
} }
@@ -196,7 +199,7 @@ function draw() {
if (lives <= 0) { if (lives <= 0) {
gameOver = true; gameOver = true;
} else { } else {
ship = new Ship(); // Reset ship position ship = new Ship(true); // Reset ship with safe spawn
} }
} }
@@ -218,7 +221,7 @@ function draw() {
level++; level++;
initialObjects = Math.ceil(initialObjects * 1.1); // Increase by 10% initialObjects = Math.ceil(initialObjects * 1.1); // Increase by 10%
bullets = []; // Clear bullets bullets = []; // Clear bullets
ship = new Ship(); // Reset ship position ship = new Ship(true); // Reset ship with safe spawn
spawnObjects(initialObjects); spawnObjects(initialObjects);
console.log(`Starting Level ${level} with ${initialObjects} objects`); console.log(`Starting Level ${level} with ${initialObjects} objects`);
} }
@@ -237,7 +240,7 @@ function keyPressed() {
} }
} }
if (keyCode === 82 && gameOver) { // R to restart if (keyCode === 82 && gameOver) { // R to restart
lives = 100; lives = 30;
score = 0; score = 0;
level = 1; level = 1;
initialObjects = 5; initialObjects = 5;
@@ -247,12 +250,17 @@ function keyPressed() {
bullets = []; bullets = [];
shieldOrb = null; shieldOrb = null;
greenOrb = null; greenOrb = null;
ship = new Ship(); ship = new Ship(true); // Safe spawn on restart
lastShieldSpawn = millis(); lastShieldSpawn = millis();
lastGreenSpawn = millis(); lastGreenSpawn = millis();
spawnObjects(initialObjects); spawnObjects(initialObjects);
gameOver = false; gameOver = false;
} }
if (keyCode === 75) { // K to toggle Kid Mode
kidMode = !kidMode;
window.kidMode = kidMode; // Update global kidMode
console.log(`Kid Mode: ${kidMode ? 'ON' : 'OFF'}`);
}
} }
function keyReleased() { function keyReleased() {