Diskussion

Asteroiden-Spiel-Code-Generator

Von Wikiprompt, der freien Prompt-Enzyklopädie

Kris Kashtanova
Beigetragen vonKris KashtanovaXQuelle

15. Apr. 2023

Asteroiden-Spiel-Code-Generator Ein Prompt zur Erstellung eines vollständigen p5.js Asteroids-Spiels mit benutzerdefinierten Texturen, einschließlich Gewinn-/Verlustbedingungen und Maussteuerung.

Prompt-InhaltSpeichern

🌐
Here is a complete p5.js sketch for an Asteroids-style game with mouse control and custom textures. ```javascript let ship; let asteroids = []; let bullets = []; let gameOver = false; let win = false; let shipImg, asteroidImg; function preload() { // Replace these with your own image paths shipImg = loadImage('ship.png'); asteroidImg = loadImage('asteroid.png'); } function setup() { createCanvas(800, 600); ship = new Ship(); // Spawn initial asteroids for (let i = 0; i < 8; i++) { asteroids.push(new Asteroid()); } } function draw() { background(0); if (gameOver) { textAlign(CENTER, CENTER); textSize(40); fill(255, 0, 0); text('GAME OVER', width/2, height/2); return; } if (win) { textAlign(CENTER, CENTER); textSize(40); fill(0, 255, 0); text('YOU WIN!', width/2, height/2); return; } ship.update(); ship.show(); // Update and show bullets for (let i = bullets.length - 1; i >= 0; i--) { bullets[i].update(); bullets[i].show(); if (bullets[i].offscreen()) { bullets.splice(i, 1); } } // Update and show asteroids for (let i = asteroids.length - 1; i >= 0; i--) { asteroids[i].update(); asteroids[i].show(); // Check collision with ship if (ship.hits(asteroids[i])) { gameOver = true; } // Check collision with bullets for (let j = bullets.length - 1; j >= 0; j--) { if (asteroids[i].hits(bullets[j])) { asteroids.splice(i, 1); bullets.splice(j, 1); break; } } } // Check win condition if (asteroids.length === 0) { win = true; } } function mousePressed() { if (mouseButton === LEFT && !gameOver && !win) { ship.shoot(); } } class Ship { constructor() { this.pos = createVector(width/2, height/2); this.r = 20; } update() { // Move towards mouse let target = createVector(mouseX, mouseY); let dir = p5.Vector.sub(target, this.pos); dir.setMag(3); this.pos.add(dir); // Keep on screen this.pos.x = constrain(this.pos.x, 0, width); this.pos.y = constrain(this.pos.y, 0, height); } show() { push(); translate(this.pos.x, this.pos.y); // Rotate to face mouse let angle = atan2(mouseY - this.pos.y, mouseX - this.pos.x); rotate(angle + HALF_PI); imageMode(CENTER); image(shipImg, 0, 0, this.r * 2, this.r * 2); pop(); } shoot() { let angle = atan2(mouseY - this.pos.y, mouseX - this.pos.x); let bullet = new Bullet(this.pos.x, this.pos.y, angle); bullets.push(bullet); } hits(asteroid) { let d = dist(this.pos.x, this.pos.y, asteroid.pos.x, asteroid.pos.y); return d < this.r + asteroid.r; } } class Asteroid { constructor() { this.pos = createVector(random(width), random(height)); // Ensure not too close to ship while (dist(this.pos.x, this.pos.y, width/2, height/2) < 150) { this.pos = createVector(random(width), random(height)); } this.r = random(30, 60); this.vel = p5.Vector.random2D().mult(random(1, 3)); } update() { this.pos.add(this.vel); // Wrap around edges if (this.pos.x > width + this.r) this.pos.x = -this.r; if (this.pos.x < -this.r) this.pos.x = width + this.r; if (this.pos.y > height + this.r) this.pos.y = -this.r; if (this.pos.y < -this.r) this.pos.y = height + this.r; } show() { imageMode(CENTER); image(asteroidImg, this.pos.x, this.pos.y, this.r * 2, this.r * 2); } hits(bullet) { let d = dist(this.pos.x, this.pos.y, bullet.pos.x, bullet.pos.y); return d < this.r + bullet.r; } } class Bullet { constructor(x, y, angle) { this.pos = createVector(x, y); this.vel = p5.Vector.fromAngle(angle).mult(8); this.r = 4; } update() { this.pos.add(this.vel); } show() { fill(255); noStroke(); ellipse(this.pos.x, this.pos.y, this.r * 2, this.r * 2); } offscreen() { return this.pos.x < 0 || this.pos.x > width || this.pos.y < 0 || this.pos.y > height; } } ``` **Instructions:** 1. Place your `ship.png` and `asteroid.png` files in the same folder as your sketch. 2. Adjust the image paths in `preload()` if needed. 3. The ship follows your mouse cursor and rotates to face it. 4. Left-click to shoot bullets toward the mouse position. 5. Colliding with an asteroid ends the game; destroying all asteroids wins. The code uses `imageMode(CENTER)` so your textures are centered on the objects. You can adjust the ship and asteroid sizes by changing the `this.r` values in their constructors.

Melde dich an, um den vollständigen Prompt zu sehen

Weiter mit:

Mit der Anmeldung akzeptierst du unsere Nutzungsbedingungen und Datenschutz

Verwendung

Dieser Prompt ist für die Verwendung mit coding gedacht. Kopiere den Inhalt oben und füge ihn in dein bevorzugtes KI-Tool ein.

Für beste Ergebnisse passe die Platzhalter (eckige Klammern oder Großbuchstaben) an deine Anforderungen an.

Referenzen

Kategorien:coding| twitter| p5js| game-development

Diskussion