소행성 게임 코드 생성기
Wikiprompt, 무료 프롬프트 백과사전에서
소행성 게임 코드 생성기 P5.js 애스터로이드 게임을 커스텀 텍스처로 완성하도록 생성하는 프롬프트로, 승리/패배 조건과 마우스 컨트롤을 포함한다.
프롬프트 내용저장
🌐
Here is the p5.js code for an Asteroids game where you control the ship with the mouse, shoot with left click, and use your own textures for the ship and asteroids.
```javascript
let ship;
let asteroids = [];
let bullets = [];
let gameOver = false;
let win = false;
let shipImg, asteroidImg;
function preload() {
// Load your own textures here
// Make sure the files are in the same folder as your sketch
shipImg = loadImage('ship.png'); // Replace with your ship texture
asteroidImg = loadImage('asteroid.png'); // Replace with your asteroid texture
}
function setup() {
createCanvas(windowWidth, windowHeight);
ship = new Ship();
// Create initial asteroids
for (let i = 0; i < 8; i++) {
asteroids.push(new Asteroid());
}
}
function draw() {
background(0);
if (!gameOver && !win) {
// Update and display ship
ship.update();
ship.show();
// Update and display 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 display asteroids
for (let i = asteroids.length - 1; i >= 0; i--) {
asteroids[i].update();
asteroids[i].show();
// Check bullet collisions
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;
}
}
// Check ship collision
if (asteroids[i] && ship.hits(asteroids[i])) {
gameOver = true;
}
}
// Check win condition
if (asteroids.length === 0) {
win = true;
}
}
// Display game state
if (gameOver) {
fill(255, 0, 0);
textSize(48);
textAlign(CENTER, CENTER);
text('GAME OVER', width / 2, height / 2);
textSize(20);
text('Click to restart', width / 2, height / 2 + 40);
} else if (win) {
fill(0, 255, 0);
textSize(48);
textAlign(CENTER, CENTER);
text('YOU WIN!', width / 2, height / 2);
textSize(20);
text('Click to restart', width / 2, height / 2 + 40);
}
}
function mousePressed() {
if (gameOver || win) {
// Restart game
gameOver = false;
win = false;
asteroids = [];
bullets = [];
ship = new Ship();
for (let i = 0; i < 8; i++) {
asteroids.push(new Asteroid());
}
} else if (mouseButton === LEFT) {
// Shoot bullet
ship.shoot();
}
}
class Ship {
constructor() {
this.pos = createVector(width / 2, height / 2);
this.size = 30;
}
update() {
// Move ship towards mouse
let target = createVector(mouseX, mouseY);
let dir = p5.Vector.sub(target, this.pos);
dir.setMag(3); // Speed
this.pos.add(dir);
// Keep ship 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 towards mouse
let angle = atan2(mouseY - this.pos.y, mouseX - this.pos.x);
rotate(angle + PI / 2);
imageMode(CENTER);
image(shipImg, 0, 0, this.size * 2, this.size * 2);
pop();
}
shoot() {
let angle = atan2(mouseY - this.pos.y, mouseX - this.pos.x);
let bulletPos = createVector(
this.pos.x + cos(angle) * this.size,
this.pos.y + sin(angle) * this.size
);
bullets.push(new Bullet(bulletPos, angle));
}
hits(asteroid) {
let d = dist(this.pos.x, this.pos.y, asteroid.pos.x, asteroid.pos.y);
return d < this.size + asteroid.size;
}
}
class Bullet {
constructor(pos, angle) {
this.pos = pos.copy();
this.vel = p5.Vector.fromAngle(angle);
this.vel.setMag(8);
this.size = 5;
}
update() {
this.pos.add(this.vel);
}
show() {
fill(255);
noStroke();
ellipse(this.pos.x, this.pos.y, this.size * 2);
}
offscreen() {
return (
this.pos.x < 0 || this.pos.x > width ||
this.pos.y < 0 || this.pos.y > height
);
}
}
class Asteroid {
constructor() {
this.pos = createVector(random(width), random(height));
// Ensure asteroid doesn't spawn 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.vel = p5.Vector.random2D();
this.vel.setMag(random(1, 3));
this.size = random(30, 60);
}
update() {
this.pos.add(this.vel);
// Wrap around edges
if (this.pos.x < -this.size) this.pos.x = width + this.size;
if (this.pos.x > width + this.size) this.pos.x = -this.size;
if (this.pos.y < -this.size) this.pos.y = height + this.size;
if (this.pos.y > height + this.size) this.pos.y = -this.size;
}
show() {
push();
translate(this.pos.x, this.pos.y);
imageMode(CENTER);
image(asteroidImg, 0, 0, this.size * 2, this.size * 2);
pop();
}
hits(bullet) {
let d = dist(this.pos.x, this.pos.y, bullet.pos.x, bullet.pos.y);
return d < this.size + bullet.size;
}
}
```
**Instructions for using your own textures:**
1. Place your ship image file (e.g., `ship.png`) and asteroid image file (e.g., `asteroid.png`) in the same folder as your p5.js sketch.
2. In the `preload()` function, replace `'ship.png'` and `'asteroid.png'` with the actual filenames of your textures.
3. The images will be displayed centered on the objects, and their sizes are scaled based on the object's `size` property.
**Game mechanics:**
- The ship follows your mouse cursor smoothly.
- Left-click to shoot bullets toward the mouse position.
- The ship rotates to face the mouse direction.
- Colliding with an asteroid ends the game.
- Destroy all asteroids to win.
- After game over or win, click anywhere to restart.
The code is self-contained and ready to run in the p5.js editor or any HTML page with p5.js included.
전체 프롬프트를 보려면 로그인하세요
Continue with:
By logging in, you agree to our Terms of Use and Privacy Policy
사용법
이 프롬프트는 coding와 함께 사용하도록 설계되었습니다. 위의 프롬프트 내용을 복사하여 원하는 AI 도구에 붙여넣으세요.
최상의 결과를 얻으려면 자리 표시자(대괄호 또는 대문자로 표시)를 특정 요구 사항으로 사용자 지정할 수 있습니다.
토론
댓글 0개