p5.js小行星游戏,带自定义纹理
来自 Wikiprompt,自由的提示词百科全书
p5.js小行星游戏,带自定义纹理 一个一次性提示,用于生成完整的p5.js小行星游戏,支持鼠标控制和自定义纹理,适用于Code Interpreter或类似的AI编码工具。
提示词内容收藏
🌐
// Asteroids Game with Mouse Control and Custom Textures
// Use your own textures for spaceship and asteroids
let spaceship;
let asteroids = [];
let bullets = [];
let gameOver = false;
let win = false;
// Texture variables - replace with your own image paths
let spaceshipImg;
let asteroidImg;
function preload() {
// Load your textures here
// Make sure to replace 'spaceship.png' and 'asteroid.png' with your actual file paths
spaceshipImg = loadImage('spaceship.png');
asteroidImg = loadImage('asteroid.png');
}
function setup() {
createCanvas(800, 600);
// Initialize spaceship at center
spaceship = new Spaceship();
// Create initial asteroids
for (let i = 0; i < 8; i++) {
asteroids.push(new Asteroid());
}
}
function draw() {
background(0);
if (gameOver) {
textSize(32);
fill(255, 0, 0);
textAlign(CENTER, CENTER);
text('GAME OVER!', width/2, height/2);
return;
}
if (win) {
textSize(32);
fill(0, 255, 0);
textAlign(CENTER, CENTER);
text('YOU WIN!', width/2, height/2);
return;
}
// Update and display spaceship
spaceship.update();
spaceship.display();
// Update and display asteroids
for (let i = asteroids.length - 1; i >= 0; i--) {
asteroids[i].update();
asteroids[i].display();
// Check collision with spaceship
if (spaceship.hits(asteroids[i])) {
gameOver = true;
}
// Remove asteroid if it goes off screen
if (asteroids[i].offScreen()) {
asteroids.splice(i, 1);
}
}
// Update and display bullets
for (let i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
bullets[i].display();
// Check bullet-asteroid collision
for (let j = asteroids.length - 1; j >= 0; j--) {
if (bullets[i] && bullets[i].hits(asteroids[j])) {
asteroids.splice(j, 1);
bullets.splice(i, 1);
break;
}
}
}
// Check win condition
if (asteroids.length === 0) {
win = true;
}
}
function mousePressed() {
if (mouseButton === LEFT && !gameOver && !win) {
// Shoot bullet from spaceship towards mouse position
let angle = atan2(mouseY - spaceship.y, mouseX - spaceship.x);
bullets.push(new Bullet(spaceship.x, spaceship.y, angle));
}
}
// Spaceship class
class Spaceship {
constructor() {
this.x = width / 2;
this.y = height / 2;
this.size = 40;
this.angle = 0;
}
update() {
// Move spaceship towards mouse position
let targetX = mouseX;
let targetY = mouseY;
// Smooth movement
this.x = lerp(this.x, targetX, 0.05);
this.y = lerp(this.y, targetY, 0.05);
// Calculate angle towards mouse
this.angle = atan2(mouseY - this.y, mouseX - this.x);
}
display() {
push();
translate(this.x, this.y);
rotate(this.angle + PI/2); // Adjust for image orientation
imageMode(CENTER);
image(spaceshipImg, 0, 0, this.size, this.size);
pop();
}
hits(asteroid) {
let d = dist(this.x, this.y, asteroid.x, asteroid.y);
return d < (this.size / 2 + asteroid.size / 2);
}
}
// Asteroid class
class Asteroid {
constructor() {
this.x = random(width);
this.y = random(height);
this.size = random(30, 60);
this.speed = random(1, 3);
this.angle = random(TWO_PI);
this.vx = cos(this.angle) * this.speed;
this.vy = sin(this.angle) * this.speed;
this.rotation = random(TWO_PI);
this.rotationSpeed = random(-0.02, 0.02);
}
update() {
this.x += this.vx;
this.y += this.vy;
this.rotation += this.rotationSpeed;
// Wrap around edges
if (this.x < -this.size) this.x = width + this.size;
if (this.x > width + this.size) this.x = -this.size;
if (this.y < -this.size) this.y = height + this.size;
if (this.y > height + this.size) this.y = -this.size;
}
display() {
push();
translate(this.x, this.y);
rotate(this.rotation);
imageMode(CENTER);
image(asteroidImg, 0, 0, this.size, this.size);
pop();
}
offScreen() {
return false; // Asteroids wrap around, so they never go off screen
}
}
// Bullet class
class Bullet {
constructor(x, y, angle) {
this.x = x;
this.y = y;
this.angle = angle;
this.speed = 8;
this.vx = cos(this.angle) * this.speed;
this.vy = sin(this.angle) * this.speed;
this.size = 5;
this.life = 100;
}
update() {
this.x += this.vx;
this.y += this.vy;
this.life--;
// Remove bullet if it goes off screen or life expires
if (this.x < 0 || this.x > width || this.y < 0 || this.y > height || this.life <= 0) {
this.remove = true;
}
}
display() {
push();
translate(this.x, this.y);
rotate(this.angle);
fill(255, 255, 0);
noStroke();
ellipse(0, 0, this.size, this.size);
pop();
}
hits(asteroid) {
let d = dist(this.x, this.y, asteroid.x, asteroid.y);
return d < (this.size / 2 + asteroid.size / 2);
}
}
用法
此提示词专为 coding 设计。复制上方内容并粘贴到你常用的 AI 工具中。
为获得最佳效果,可将占位符(方括号或大写字母标示)替换为你的具体需求。
讨论
0 条评论