Best game ever

This commit is contained in:
Eric Ratliff
2025-06-25 22:39:41 -05:00
parent 694cc903c8
commit 95adad39f0
7 changed files with 217 additions and 7 deletions

View File

@@ -19,6 +19,7 @@ class Asteroid {
}
this.isGolden = isGolden;
this.hitsLeft = isGolden ? 3 : 1;
console.log(`Spawned asteroid, isGolden: ${isGolden}, hitsLeft: ${this.hitsLeft}`);
}
update() {

46
hamburger.js Normal file
View File

@@ -0,0 +1,46 @@
class Hamburger {
constructor(pos, r) {
this.pos = pos.copy();
this.r = r ? r / 2 : random(20, 30);
this.vel = p5.Vector.random2D();
this.vel.mult(random(1, 3));
}
update() {
this.pos.add(this.vel);
}
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;
}
hits(bullet) {
let d = dist(this.pos.x, this.pos.y, bullet.pos.x, bullet.pos.y);
return d < this.r;
}
show() {
push();
translate(this.pos.x, this.pos.y);
// Rotate to align with velocity
let angle = atan2(this.vel.y, this.vel.x);
rotate(angle);
// Draw hamburger image (10 points)
if (hamburgerImg) {
imageMode(CENTER);
let scaleFactor = (this.r * 2) / hamburgerImg.width; // Scale to match radius
scale(scaleFactor);
image(hamburgerImg, 0, 0);
} else {
// Fallback if image fails to load
fill(200, 150, 100); // Brighter brown for hamburger
stroke(255);
ellipse(0, 0, this.r * 2);
console.log('Hamburger image not loaded, using fallback');
}
pop();
}
}

BIN
hamburger.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 440 KiB

View File

@@ -8,6 +8,8 @@
<script src="bullet.js"></script>
<script src="shieldOrb.js"></script>
<script src="greenOrb.js"></script>
<script src="pizza.js"></script>
<script src="hamburger.js"></script>
<script src="sketch.js"></script>
</head>
<body>

68
pizza.js Normal file
View File

@@ -0,0 +1,68 @@
class Pizza {
constructor(pos, r) {
if (pos) {
this.pos = pos.copy();
this.r = r ? r / 2 : random(40, 60);
} else {
this.pos = createVector(random(width), random(height));
this.r = random(40, 60);
while (dist(this.pos.x, this.pos.y, ship.pos.x, ship.pos.y) < 100) {
this.pos = createVector(random(width), random(height));
}
}
this.vel = p5.Vector.random2D();
this.vel.mult(random(1, 3));
this.total = floor(random(8, 12)); // For potential future use
this.offset = [];
for (let i = 0; i < this.total; i++) {
this.offset[i] = random(-this.r * 0.3, this.r * 0.3);
}
}
update() {
this.pos.add(this.vel);
}
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;
}
hits(bullet) {
let d = dist(this.pos.x, this.pos.y, bullet.pos.x, bullet.pos.y);
return d < this.r;
}
breakup() {
let newHamburgers = [];
if (this.r > 20) {
newHamburgers.push(new Hamburger(this.pos, this.r));
newHamburgers.push(new Hamburger(this.pos, this.r));
}
return newHamburgers;
}
show() {
push();
translate(this.pos.x, this.pos.y);
// Rotate to align with velocity
let angle = atan2(this.vel.y, this.vel.x);
rotate(angle);
// Draw pizza image (20 points)
if (pizzaImg) {
imageMode(CENTER);
let scaleFactor = (this.r * 2) / pizzaImg.width; // Scale to match radius
scale(scaleFactor);
image(pizzaImg, 0, 0);
} else {
// Fallback if image fails to load
fill(255, 100, 100); // Brighter red for pizza
stroke(255);
ellipse(0, 0, this.r * 2);
console.log('Pizza image not loaded, using fallback');
}
pop();
}
}

BIN
pizza.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 KiB

107
sketch.js
View File

@@ -1,5 +1,7 @@
let ship;
let asteroids = [];
let pizzas = [];
let hamburgers = [];
let bullets = [];
let shieldOrb = null;
let greenOrb = null;
@@ -8,12 +10,29 @@ let score = 0;
let gameOver = false;
let lastShieldSpawn = 0;
let lastGreenSpawn = 0;
let pizzaImg, hamburgerImg;
let level = 1;
let initialObjects = 5;
function preload() {
pizzaImg = loadImage('pizza.png', () => console.log('Pizza image loaded'), () => console.error('Failed to load pizza.png'));
hamburgerImg = loadImage('hamburger.png', () => console.log('Hamburger image loaded'), () => console.error('Failed to load hamburger.png'));
}
function setup() {
createCanvas(800, 600);
ship = new Ship();
for (let i = 0; i < 5; i++) {
asteroids.push(new Asteroid());
spawnObjects(initialObjects);
}
function spawnObjects(numObjects) {
for (let i = 0; i < numObjects; i++) {
if (random() < 0.15) {
pizzas.push(new Pizza());
console.log(`Spawned pizza at level ${level}, total pizzas: ${pizzas.length}`);
} else {
asteroids.push(new Asteroid());
}
}
}
@@ -29,11 +48,12 @@ function draw() {
return;
}
// Display lives and score
// Display lives, score, and level
textSize(20);
fill(255);
text("Lives: " + lives, 50, 30);
text("Score: " + score, 50, 60);
text("Level: " + level, 50, 90);
// Handle continuous key input (WASD)
if (keyIsDown(65)) { // A key
@@ -121,11 +141,82 @@ function draw() {
asteroids.splice(i, 1);
bullets.splice(j, 1);
asteroids.push(...newAsteroids);
score += isGolden ? 20 : 10; // Use stored isGolden value
score += isGolden ? 50 : 10; // 50 points for golden, 10 for regular
break; // Exit bullet loop after collision
}
}
}
// Update and show pizzas
for (let i = pizzas.length - 1; i >= 0; i--) {
if (!pizzas[i]) continue; // Skip if pizza is undefined
pizzas[i].update();
pizzas[i].show();
pizzas[i].edges();
// Check collision with ship
if (ship.hits(pizzas[i]) && !ship.shieldActive) {
lives--;
if (lives <= 0) {
gameOver = true;
} else {
ship = new Ship(); // Reset ship position
}
}
// Check collision with bullets
for (let j = bullets.length - 1; j >= 0; j--) {
if (pizzas[i] && pizzas[i].hits(bullets[j])) {
let newHamburgers = pizzas[i].breakup();
pizzas.splice(i, 1);
bullets.splice(j, 1);
hamburgers.push(...newHamburgers);
score += 20; // 20 points for pizza
console.log(`Pizza destroyed, spawned ${newHamburgers.length} hamburgers, total hamburgers: ${hamburgers.length}`);
break; // Exit bullet loop after collision
}
}
}
// Update and show hamburgers
for (let i = hamburgers.length - 1; i >= 0; i--) {
if (!hamburgers[i]) continue; // Skip if hamburger is undefined
hamburgers[i].update();
hamburgers[i].show();
hamburgers[i].edges();
// Check collision with ship
if (ship.hits(hamburgers[i]) && !ship.shieldActive) {
lives--;
if (lives <= 0) {
gameOver = true;
} else {
ship = new Ship(); // Reset ship position
}
}
// Check collision with bullets
for (let j = bullets.length - 1; j >= 0; j--) {
if (hamburgers[i] && hamburgers[i].hits(bullets[j])) {
hamburgers.splice(i, 1);
bullets.splice(j, 1);
score += 10; // 10 points for hamburger
console.log(`Hamburger destroyed, total hamburgers: ${hamburgers.length}`);
break; // Exit bullet loop after collision
}
}
}
// Check for level completion
if (asteroids.length === 0 && pizzas.length === 0 && hamburgers.length === 0 && !gameOver) {
console.log(`Level ${level} completed! Asteroids: ${asteroids.length}, Pizzas: ${pizzas.length}, Hamburgers: ${hamburgers.length}`);
level++;
initialObjects = Math.ceil(initialObjects * 1.1); // Increase by 10%
bullets = []; // Clear bullets
ship = new Ship(); // Reset ship position
spawnObjects(initialObjects);
console.log(`Starting Level ${level} with ${initialObjects} objects`);
}
}
function keyPressed() {
@@ -143,16 +234,18 @@ function keyPressed() {
if (keyCode === 82 && gameOver) { // R to restart
lives = 3;
score = 0;
level = 1;
initialObjects = 5;
asteroids = [];
pizzas = [];
hamburgers = [];
bullets = [];
shieldOrb = null;
greenOrb = null;
ship = new Ship();
lastShieldSpawn = millis();
lastGreenSpawn = millis();
for (let i = 0; i < 5; i++) {
asteroids.push(new Asteroid());
}
spawnObjects(initialObjects);
gameOver = false;
}
}