讨论

小行星游戏代码生成器

来自 Wikiprompt,自由的提示词百科全书

Kris Kashtanova

2023年4月15日

小行星游戏代码生成器 生成一个完整的p5.js小行星游戏提示,包含自定义纹理、胜负条件和鼠标控制。

提示词内容收藏

🌐
let spaceship; let asteroids = []; let bullets = []; let gameOver = false; let win = false; // 加载纹理 let spaceshipImg; let asteroidImg; function preload() { // 在这里加载你自己的纹理 spaceshipImg = loadImage('spaceship.png'); // 替换为你的飞船纹理路径 asteroidImg = loadImage('asteroid.png'); // 替换为你的小行星纹理路径 } function setup() { createCanvas(800, 600); spaceship = new Spaceship(); // 生成初始小行星 for (let i = 0; i < 8; i++) { asteroids.push(new Asteroid()); } } function draw() { background(0); if (!gameOver && !win) { // 更新飞船位置(跟随鼠标) spaceship.update(); spaceship.show(); // 更新子弹 for (let i = bullets.length - 1; i >= 0; i--) { bullets[i].update(); bullets[i].show(); if (bullets[i].offscreen()) { bullets.splice(i, 1); } } // 更新小行星 for (let i = asteroids.length - 1; i >= 0; i--) { asteroids[i].update(); asteroids[i].show(); // 子弹与小行星碰撞检测 for (let j = bullets.length - 1; j >= 0; j--) { if (asteroids[i] && bullets[j] && asteroids[i].hits(bullets[j])) { asteroids.splice(i, 1); bullets.splice(j, 1); break; } } // 飞船与小行星碰撞检测 if (asteroids[i] && spaceship.hits(asteroids[i])) { gameOver = true; } } // 检查胜利条件 if (asteroids.length === 0) { win = true; } } else if (gameOver) { textSize(32); fill(255, 0, 0); textAlign(CENTER, CENTER); text('游戏结束!你输了', width / 2, height / 2); } else if (win) { textSize(32); fill(0, 255, 0); textAlign(CENTER, CENTER); text('恭喜!你赢了!', width / 2, height / 2); } } function mousePressed() { if (mouseButton === LEFT && !gameOver && !win) { // 发射子弹 let bullet = new Bullet(spaceship.x, spaceship.y, mouseX, mouseY); bullets.push(bullet); } } // 飞船类 class Spaceship { constructor() { this.x = width / 2; this.y = height / 2; this.size = 40; } update() { // 飞船跟随鼠标位置 this.x = mouseX; this.y = mouseY; } show() { // 绘制飞船纹理 imageMode(CENTER); image(spaceshipImg, this.x, this.y, this.size, this.size); } hits(asteroid) { // 碰撞检测(简单圆形检测) let d = dist(this.x, this.y, asteroid.x, asteroid.y); return d < (this.size / 2 + asteroid.size / 2); } } // 小行星类 class Asteroid { constructor() { this.x = random(width); this.y = random(height); this.size = random(30, 60); this.vx = random(-2, 2); this.vy = random(-2, 2); // 确保小行星不在飞船初始位置附近生成 if (dist(this.x, this.y, width / 2, height / 2) < 150) { this.x = random(width); this.y = random(height); } } update() { this.x += this.vx; this.y += this.vy; // 边缘环绕 if (this.x > width + this.size) this.x = -this.size; if (this.x < -this.size) this.x = width + this.size; if (this.y > height + this.size) this.y = -this.size; if (this.y < -this.size) this.y = height + this.size; } show() { // 绘制小行星纹理 imageMode(CENTER); image(asteroidImg, this.x, this.y, this.size, this.size); } hits(bullet) { // 碰撞检测 let d = dist(this.x, this.y, bullet.x, bullet.y); return d < (this.size / 2 + bullet.size / 2); } } // 子弹类 class Bullet { constructor(x, y, targetX, targetY) { this.x = x; this.y = y; this.size = 8; this.speed = 8; // 计算子弹方向(朝鼠标点击位置) let angle = atan2(targetY - y, targetX - x); this.vx = cos(angle) * this.speed; this.vy = sin(angle) * this.speed; } update() { this.x += this.vx; this.y += this.vy; } show() { fill(255, 255, 0); noStroke(); ellipse(this.x, this.y, this.size, this.size); } offscreen() { return this.x < 0 || this.x > width || this.y < 0 || this.y > height; } }

登录以查看完整提示词

继续使用:

登录即表示你同意我们的 使用条款 和 隐私政策

用法

此提示词专为 coding 设计。复制上方内容并粘贴到你常用的 AI 工具中。

为获得最佳效果,可将占位符(方括号或大写字母标示)替换为你的具体需求。

参考资料

分类:coding| twitter| p5js| game-development

讨论