Jeu Arkanoid avec p5.js
De Wikiprompt, l’encyclopédie libre de prompts
Jeu Arkanoid avec p5.js Une invite unique pour générer un jeu Arkanoid jouable en p5.js, avec la raquette contrôlée par la souris. Inclut des instructions de suivi pour continuer ou déboguer le code.
Contenu du PromptEnregistrer
🌐
Voici le code p5.js pour un jeu d'Arkanoid où la souris contrôle la raquette :
```javascript
let paddle;
let ball;
let bricks = [];
let score = 0;
let gameOver = false;
let win = false;
function setup() {
createCanvas(800, 600);
paddle = new Paddle();
ball = new Ball();
initBricks();
}
function initBricks() {
bricks = [];
const rows = 5;
const cols = 8;
const brickWidth = 80;
const brickHeight = 20;
const gap = 5;
const offsetX = (width - (cols * (brickWidth + gap))) / 2;
const offsetY = 50;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
bricks.push(new Brick(offsetX + j * (brickWidth + gap), offsetY + i * (brickHeight + gap), brickWidth, brickHeight));
}
}
}
function draw() {
background(0);
if (gameOver) {
textSize(32);
fill(255);
textAlign(CENTER, CENTER);
text("Game Over - Click to restart", width/2, height/2);
if (mouseIsPressed) {
resetGame();
}
return;
}
if (win) {
textSize(32);
fill(255);
textAlign(CENTER, CENTER);
text("You Win! - Click to restart", width/2, height/2);
if (mouseIsPressed) {
resetGame();
}
return;
}
paddle.update();
paddle.show();
ball.update();
ball.show();
// Check paddle collision
if (ball.y + ball.r >= paddle.y && ball.y + ball.r <= paddle.y + paddle.h && ball.x >= paddle.x - paddle.w/2 && ball.x <= paddle.x + paddle.w/2) {
ball.bounce();
}
// Check brick collisions
for (let i = bricks.length - 1; i >= 0; i--) {
if (bricks[i].collide(ball)) {
bricks.splice(i, 1);
score += 10;
ball.bounce();
}
}
// Check win condition
if (bricks.length === 0) {
win = true;
}
// Display score
fill(255);
textSize(16);
textAlign(LEFT, TOP);
text("Score: " + score, 10, 10);
}
function resetGame() {
score = 0;
gameOver = false;
win = false;
ball.reset();
initBricks();
}
class Paddle {
constructor() {
this.w = 100;
this.h = 15;
this.x = width/2;
this.y = height - 30;
}
update() {
this.x = mouseX;
this.x = constrain(this.x, this.w/2, width - this.w/2);
}
show() {
fill(255);
rectMode(CENTER);
rect(this.x, this.y, this.w, this.h);
}
}
class Ball {
constructor() {
this.r = 10;
this.reset();
}
reset() {
this.x = width/2;
this.y = height - 50;
this.vx = random(-3, 3);
this.vy = -4;
}
update() {
this.x += this.vx;
this.y += this.vy;
// Wall collisions
if (this.x - this.r < 0 || this.x + this.r > width) {
this.vx *= -1;
}
if (this.y - this.r < 0) {
this.vy *= -1;
}
// Bottom - game over
if (this.y + this.r > height) {
gameOver = true;
}
}
bounce() {
this.vy *= -1;
// Add slight horizontal variation based on paddle position
if (this.vx === 0) {
this.vx = random(-2, 2);
}
}
show() {
fill(255, 0, 0);
ellipse(this.x, this.y, this.r * 2);
}
}
class Brick {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
collide(ball) {
return (ball.x + ball.r > this.x && ball.x - ball.r < this.x + this.w &&
ball.y + ball.r > this.y && ball.y - ball.r < this.y + this.h);
}
show() {
fill(0, 255, 0);
rect(this.x, this.y, this.w, this.h);
}
}
```
Ce code crée un jeu d'Arkanoid complet avec :
- Une raquette contrôlée par la souris
- Une balle qui rebondit sur les murs et la raquette
- Des briques disposées en grille
- Un système de score
- Des conditions de victoire et de défaite
- Un redémarrage en cliquant après la fin du jeu
La raquette suit la position de la souris horizontalement, et le jeu se termine si la balle tombe en bas ou si toutes les briques sont détruites.
Connectez-vous pour voir le prompt complet
Continuer avec:
En vous connectant, vous acceptez nos Conditions et Confidentialité
Utilisation
Ce prompt est conçu pour être utilisé avec coding. Copiez le contenu ci-dessus et collez-le dans votre outil d’IA préféré.
Pour de meilleurs résultats, personnalisez les espaces réservés (indiqués par des crochets ou des majuscules) selon vos besoins.
Références
- Catégorie: Prompts coding
- Source: https://x.com/icreatelife/status/1636940552632401920
Discussion
0 commentaires