Gerador de Código de Jogo de Asteroides

De Wikiprompt, a enciclopédia livre de prompts

Kris Kashtanova
Contribuído porKris KashtanovaXFonte

15 de abr. de 2023

Gerador de Código de Jogo de Asteroides Um prompt para gerar um jogo completo de Asteroids em p5.js com texturas personalizadas, incluindo condições de vitória/derrota e controles de mouse.

Conteúdo do PromptSalvar

🌐
Aqui está o código p5.js para Asteroids, onde você controla a nave com o mouse e atira nos asteroides com o clique esquerdo. Se a nave colidir com um asteroide, você perde. Se destruir todos, você vence! Use suas próprias texturas para a nave e para os asteroides. ```javascript let ship; let asteroids = []; let bullets = []; let gameOver = false; let win = false; let shipImg, asteroidImg; function preload() { // Carregue suas texturas aqui (substitua pelos caminhos dos seus arquivos) shipImg = loadImage('nave.png'); asteroidImg = loadImage('asteroide.png'); } function setup() { createCanvas(800, 600); ship = new Ship(); // Cria 8 asteroides iniciais for (let i = 0; i < 8; i++) { asteroids.push(new Asteroid()); } } function draw() { background(0); if (!gameOver && !win) { ship.update(); ship.show(); // Atualiza e mostra balas for (let i = bullets.length - 1; i >= 0; i--) { bullets[i].update(); bullets[i].show(); if (bullets[i].offscreen()) { bullets.splice(i, 1); } } // Atualiza e mostra asteroides for (let i = asteroids.length - 1; i >= 0; i--) { asteroids[i].update(); asteroids[i].show(); // Colisão com a nave if (ship.hits(asteroids[i])) { gameOver = true; } // Colisão com balas for (let j = bullets.length - 1; j >= 0; j--) { if (asteroids[i].hits(bullets[j])) { asteroids.splice(i, 1); bullets.splice(j, 1); break; } } } // Verifica vitória if (asteroids.length === 0) { win = true; } } else { // Tela de fim de jogo textAlign(CENTER, CENTER); textSize(32); fill(255); if (gameOver) { text('Você perdeu!', width/2, height/2); } else if (win) { text('Você venceu!', width/2, height/2); } text('Clique para reiniciar', width/2, height/2 + 40); } } function mousePressed() { if (gameOver || win) { // Reinicia o jogo gameOver = false; win = false; asteroids = []; bullets = []; ship = new Ship(); for (let i = 0; i < 8; i++) { asteroids.push(new Asteroid()); } } else if (mouseButton === LEFT) { // Dispara uma bala bullets.push(new Bullet(ship.x, ship.y, ship.angle)); } } class Ship { constructor() { this.x = width/2; this.y = height/2; this.angle = 0; this.size = 40; } update() { // A nave segue o mouse this.x = mouseX; this.y = mouseY; // Calcula o ângulo em relação ao mouse (para a nave apontar na direção do movimento) this.angle = atan2(mouseY - this.y, mouseX - this.x); } show() { push(); translate(this.x, this.y); rotate(this.angle + PI/2); // Ajusta para a textura apontar para cima por padrão imageMode(CENTER); image(shipImg, 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); } } class Asteroid { constructor() { this.x = random(width); this.y = random(height); // Garante que não nasça muito perto da nave while (dist(this.x, this.y, width/2, height/2) < 150) { this.x = random(width); this.y = random(height); } this.size = random(40, 80); this.vx = random(-2, 2); this.vy = random(-2, 2); this.angle = random(TWO_PI); } update() { this.x += this.vx; this.y += this.vy; // Mantém dentro da tela if (this.x < 0) this.x = width; if (this.x > width) this.x = 0; if (this.y < 0) this.y = height; if (this.y > height) this.y = 0; } show() { push(); translate(this.x, this.y); rotate(this.angle); imageMode(CENTER); image(asteroidImg, 0, 0, this.size, this.size); pop(); } 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, angle) { this.x = x; this.y = y; this.angle = angle; this.speed = 8; this.size = 8; 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); } } ``` **Instruções:** 1. Substitua `'nave.png'` e `'asteroide.png'` pelos caminhos dos seus próprios arquivos de textura. 2. A nave segue o mouse e atira na direção em que está apontando (para a direita por padrão, ajuste o ângulo conforme sua textura). 3. Clique com o botão esquerdo para atirar. 4. Se colidir com um asteroide, perde. Se destruir todos, vence. 5. Clique em qualquer lugar para reiniciar após o fim do jogo.

Entre para ver o prompt completo

Continuar com:

Ao entrar, você concorda com nossos Termos de uso e Política de privacidade

Uso

Este prompt foi projetado para uso com coding. Copie o conteúdo acima e cole na sua ferramenta de IA preferida.

Para melhores resultados, personalize os marcadores (indicados por colchetes ou maiúsculas) com seus requisitos específicos.

Referências

Categorias:coding| twitter| p5js| game-development

Discussão

0 comentários