使用GPT-4和AI纹理的 asteroids 游戏
来自 Wikiprompt,自由的提示词百科全书
使用GPT-4和AI纹理的 asteroids 游戏 一个完整的Asteroids游戏提示,用p5.js生成,包含自定义纹理,通过鼠标控制并左键射击。
提示词内容收藏
🌐
let ship;
let asteroids = [];
let bullets = [];
let gameOver = false;
let win = false;
// 纹理变量(请替换为您的图片路径)
let shipImg;
let asteroidImg;
function preload() {
// 加载您的纹理,例如:
// shipImg = loadImage('ship.png');
// asteroidImg = loadImage('asteroid.png');
// 如果未提供,将使用默认形状
}
function setup() {
createCanvas(800, 600);
// 如果未加载图片,创建默认纹理(可选)
if (!shipImg) {
shipImg = createGraphics(40, 40);
shipImg.fill(255);
shipImg.triangle(20, 0, 0, 40, 40, 40);
}
if (!asteroidImg) {
asteroidImg = createGraphics(60, 60);
asteroidImg.fill(150);
asteroidImg.ellipse(30, 30, 60, 60);
}
// 初始化飞船
ship = new Ship();
// 生成小行星
for (let i = 0; i < 8; i++) {
asteroids.push(new Asteroid());
}
}
function draw() {
background(0);
if (gameOver) {
textSize(32);
fill(255, 0, 0);
text('游戏结束!', width/2 - 80, height/2);
return;
}
if (win) {
textSize(32);
fill(0, 255, 0);
text('你赢了!', width/2 - 60, height/2);
return;
}
// 更新飞船(跟随鼠标)
ship.update();
ship.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();
// 检查飞船与小行星碰撞
if (dist(ship.x, ship.y, asteroids[i].x, asteroids[i].y) < ship.r + asteroids[i].r) {
gameOver = true;
return;
}
// 检查子弹与小行星碰撞
for (let j = bullets.length - 1; j >= 0; j--) {
if (dist(bullets[j].x, bullets[j].y, asteroids[i].x, asteroids[i].y) < bullets[j].r + asteroids[i].r) {
asteroids.splice(i, 1);
bullets.splice(j, 1);
break;
}
}
}
// 检查是否所有小行星被摧毁
if (asteroids.length === 0) {
win = true;
}
}
function mousePressed() {
if (mouseButton === LEFT && !gameOver && !win) {
// 从飞船位置发射子弹
let angle = atan2(mouseY - ship.y, mouseX - ship.x);
bullets.push(new Bullet(ship.x, ship.y, angle));
}
}
class Ship {
constructor() {
this.x = width/2;
this.y = height/2;
this.r = 20;
}
update() {
// 飞船跟随鼠标
this.x = mouseX;
this.y = mouseY;
}
show() {
push();
translate(this.x, this.y);
// 使用纹理绘制飞船
imageMode(CENTER);
image(shipImg, 0, 0);
pop();
}
}
class Asteroid {
constructor() {
this.x = random(width);
this.y = random(height);
this.r = random(20, 50);
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 < -this.r) this.x = width + this.r;
if (this.x > width + this.r) this.x = -this.r;
if (this.y < -this.r) this.y = height + this.r;
if (this.y > height + this.r) this.y = -this.r;
}
show() {
push();
translate(this.x, this.y);
// 使用纹理绘制小行星
imageMode(CENTER);
image(asteroidImg, 0, 0, this.r * 2, this.r * 2);
pop();
}
}
class Bullet {
constructor(x, y, angle) {
this.x = x;
this.y = y;
this.vx = cos(angle) * 5;
this.vy = sin(angle) * 5;
this.r = 5;
this.life = 100;
}
update() {
this.x += this.vx;
this.y += this.vy;
this.life--;
}
show() {
fill(255, 255, 0);
noStroke();
ellipse(this.x, this.y, this.r * 2);
}
offscreen() {
return this.life <= 0 || this.x < 0 || this.x > width || this.y < 0 || this.y > height;
}
}
用法
此提示词专为 coding 设计。复制上方内容并粘贴到你常用的 AI 工具中。
为获得最佳效果,可将占位符(方括号或大写字母标示)替换为你的具体需求。
讨论
0 条评论