Discussion

Voici un jeu Arkanoid complet en p5.js. Copiez ce code dans un fichier HTML ou utilisez l'éditeur p5.js. ```javascript let paddle; let ball; let bricks = []; let score = 0; let lives = 3; let gameState = 'start'; // 'start', 'playing', 'gameover', 'win' function setup() { createCanvas(800, 600); paddle = new Paddle(); ball = new Ball(); resetBricks(); } function draw() { background(0); // Afficher le score et les vies textSize(20); fill(255); text('Score: ' + score, 10, 30); text('Vies: ' + lives, width - 100, 30); if (gameState === 'start') { textAlign(CENTER); textSize(40); fill(255); text('ARKANOID', width/2, height/2 - 20); textSize(20); text('Cliquez pour commencer', width/2, height/2 + 20); textAlign(LEFT); } else if (gameState === 'playing') { paddle.show(); paddle.move(); ball.show(); ball.move(); ball.checkPaddle(paddle); ball.checkBricks(); checkWin(); } else if (gameState === 'gameover') { textAlign(CENTER); textSize(40); fill(255); text('GAME OVER', width/2, height/2 - 20); textSize(20); text('Score final: ' + score, width/2, height/2 + 20); text('Cliquez pour rejouer', width/2, height/2 + 50); textAlign(LEFT); } else if (gameState === 'win') { textAlign(CENTER); textSize(40); fill(255); text('VOUS AVEZ GAGNÉ!', width/2, height/2 - 20); textSize(20); text('Score final: ' + score, width/2, height/2 + 20); text('Cliquez pour rejouer', width/2, height/2 + 50); textAlign(LEFT); } // Afficher les briques for (let brick of bricks) { brick.show(); } } function mousePressed() { if (gameState === 'start') { gameState = 'playing'; } else if (gameState === 'gameover' || gameState === 'win') { resetGame(); } } function resetGame() { score = 0; lives = 3; ball.reset(); resetBricks(); gameState = 'playing'; } function resetBricks() { bricks = []; let rows = 5; let cols = 8; let brickWidth = 80; let brickHeight = 20; let spacing = 10; let offsetX = (width - (cols * (brickWidth + spacing))) / 2; let offsetY = 50; for (let i = 0; i < rows; i++) { for (let j = 0; j < cols; j++) { let x = offsetX + j * (brickWidth + spacing); let y = offsetY + i * (brickHeight + spacing); let colors = [color(255, 0, 0), color(255, 165, 0), color(255, 255, 0), color(0, 255, 0), color(0, 0, 255)]; bricks.push(new Brick(x, y, brickWidth, brickHeight, colors[i])); } } } function checkWin() { if (bricks.length === 0) { gameState = 'win'; } } class Paddle { constructor() { this.width = 100; this.height = 15; this.x = width/2 - this.width/2; this.y = height - 30; this.speed = 8; } show() { fill(255); rect(this.x, this.y, this.width, this.height); } move() { if (keyIsDown(LEFT_ARROW)) { this.x -= this.speed; } if (keyIsDown(RIGHT_ARROW)) { this.x += this.speed; } this.x = constrain(this.x, 0, width - this.width); } } class Ball { constructor() { this.radius = 8; this.reset(); } reset() { this.x = width/2; this.y = height - 50; this.vx = random(-3, 3); this.vy = -4; this.speed = 5; } show() { fill(255); ellipse(this.x, this.y, this.radius * 2); } move() { this.x += this.vx; this.y += this.vy; // Rebondir sur les murs if (this.x - this.radius < 0 || this.x + this.radius > width) { this.vx *= -1; } if (this.y - this.radius < 0) { this.vy *= -1; } // Perdre une vie si la balle tombe if (this.y - this.radius > height) { lives--; if (lives <= 0) { gameState = 'gameover'; } else { this.reset(); } } } checkPaddle(paddle) { if (this.y + this.radius >= paddle.y && this.y + this.radius <= paddle.y + paddle.height && this.x >= paddle.x && this.x <= paddle.x + paddle.width) { this.vy *= -1; // Ajouter un peu de variation à la direction this.vx += (this.x - (paddle.x + paddle.width/2)) * 0.1; this.vx = constrain(this.vx, -5, 5); } } checkBricks() { for (let i = bricks.length - 1; i >= 0; i--) { let brick = bricks[i]; if (this.x + this.radius > brick.x && this.x - this.radius < brick.x + brick.w && this.y + this.radius > brick.y && this.y - this.radius < brick.y + brick.h) { bricks.splice(i, 1); this.vy *= -1; score += 10; } } } } class Brick { constructor(x, y, w, h, color) { this.x = x; this.y = y; this.w = w; this.h = h; this.color = color; } show() { fill(this.color); rect(this.x, this.y, this.w, this.h); } } ``` Ce jeu Arkanoid comprend : - Une raquette contrôlée avec les flèches gauche/droite - Une balle qui rebondit - 40 briques colorées à détruire - Un système de score et de vies - Des écrans de début, de victoire et de défaite - Des mécaniques de rebond avec variation de direction Cliquez pour commencer et utilisez les flèches pour jouer !

De Wikiprompt, l’encyclopédie libre de prompts

Kris Kashtanova

18 mars 2023

Voici un jeu Arkanoid complet en p5.js. Copiez ce code dans un fichier HTML ou utilisez l'éditeur p5.js. ```javascript let paddle; let ball; let bricks = []; let score = 0; let lives = 3; let gameState = 'start'; // 'start', 'playing', 'gameover', 'win' function setup() { createCanvas(800, 600); paddle = new Paddle(); ball = new Ball(); resetBricks(); } function draw() { background(0); // Afficher le score et les vies textSize(20); fill(255); text('Score: ' + score, 10, 30); text('Vies: ' + lives, width - 100, 30); if (gameState === 'start') { textAlign(CENTER); textSize(40); fill(255); text('ARKANOID', width/2, height/2 - 20); textSize(20); text('Cliquez pour commencer', width/2, height/2 + 20); textAlign(LEFT); } else if (gameState === 'playing') { paddle.show(); paddle.move(); ball.show(); ball.move(); ball.checkPaddle(paddle); ball.checkBricks(); checkWin(); } else if (gameState === 'gameover') { textAlign(CENTER); textSize(40); fill(255); text('GAME OVER', width/2, height/2 - 20); textSize(20); text('Score final: ' + score, width/2, height/2 + 20); text('Cliquez pour rejouer', width/2, height/2 + 50); textAlign(LEFT); } else if (gameState === 'win') { textAlign(CENTER); textSize(40); fill(255); text('VOUS AVEZ GAGNÉ!', width/2, height/2 - 20); textSize(20); text('Score final: ' + score, width/2, height/2 + 20); text('Cliquez pour rejouer', width/2, height/2 + 50); textAlign(LEFT); } // Afficher les briques for (let brick of bricks) { brick.show(); } } function mousePressed() { if (gameState === 'start') { gameState = 'playing'; } else if (gameState === 'gameover' || gameState === 'win') { resetGame(); } } function resetGame() { score = 0; lives = 3; ball.reset(); resetBricks(); gameState = 'playing'; } function resetBricks() { bricks = []; let rows = 5; let cols = 8; let brickWidth = 80; let brickHeight = 20; let spacing = 10; let offsetX = (width - (cols * (brickWidth + spacing))) / 2; let offsetY = 50; for (let i = 0; i < rows; i++) { for (let j = 0; j < cols; j++) { let x = offsetX + j * (brickWidth + spacing); let y = offsetY + i * (brickHeight + spacing); let colors = [color(255, 0, 0), color(255, 165, 0), color(255, 255, 0), color(0, 255, 0), color(0, 0, 255)]; bricks.push(new Brick(x, y, brickWidth, brickHeight, colors[i])); } } } function checkWin() { if (bricks.length === 0) { gameState = 'win'; } } class Paddle { constructor() { this.width = 100; this.height = 15; this.x = width/2 - this.width/2; this.y = height - 30; this.speed = 8; } show() { fill(255); rect(this.x, this.y, this.width, this.height); } move() { if (keyIsDown(LEFT_ARROW)) { this.x -= this.speed; } if (keyIsDown(RIGHT_ARROW)) { this.x += this.speed; } this.x = constrain(this.x, 0, width - this.width); } } class Ball { constructor() { this.radius = 8; this.reset(); } reset() { this.x = width/2; this.y = height - 50; this.vx = random(-3, 3); this.vy = -4; this.speed = 5; } show() { fill(255); ellipse(this.x, this.y, this.radius * 2); } move() { this.x += this.vx; this.y += this.vy; // Rebondir sur les murs if (this.x - this.radius < 0 || this.x + this.radius > width) { this.vx *= -1; } if (this.y - this.radius < 0) { this.vy *= -1; } // Perdre une vie si la balle tombe if (this.y - this.radius > height) { lives--; if (lives <= 0) { gameState = 'gameover'; } else { this.reset(); } } } checkPaddle(paddle) { if (this.y + this.radius >= paddle.y && this.y + this.radius <= paddle.y + paddle.height && this.x >= paddle.x && this.x <= paddle.x + paddle.width) { this.vy *= -1; // Ajouter un peu de variation à la direction this.vx += (this.x - (paddle.x + paddle.width/2)) * 0.1; this.vx = constrain(this.vx, -5, 5); } } checkBricks() { for (let i = bricks.length - 1; i >= 0; i--) { let brick = bricks[i]; if (this.x + this.radius > brick.x && this.x - this.radius < brick.x + brick.w && this.y + this.radius > brick.y && this.y - this.radius < brick.y + brick.h) { bricks.splice(i, 1); this.vy *= -1; score += 10; } } } } class Brick { constructor(x, y, w, h, color) { this.x = x; this.y = y; this.w = w; this.h = h; this.color = color; } show() { fill(this.color); rect(this.x, this.y, this.w, this.h); } } ``` Ce jeu Arkanoid comprend : - Une raquette contrôlée avec les flèches gauche/droite - Une balle qui rebondit - 40 briques colorées à détruire - Un système de score et de vies - Des écrans de début, de victoire et de défaite - Des mécaniques de rebond avec variation de direction Cliquez pour commencer et utilisez les flèches pour jouer ! Un prompt simple en une seule fois pour générer un jeu Arkanoid jouable en p5.js, avec la souris contrôlant la raquette. Inclut des instructions de suivi pour la complétion et la correction de bugs.

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 gameWin = 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 = 30; const gap = 10; const offsetX = (width - (cols * (brickWidth + gap) - 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, color(100 + i * 30, 150, 200 - i * 20) )); } } } function draw() { background(30); if (!gameOver && !gameWin) { paddle.update(); ball.update(); ball.checkPaddle(paddle); ball.checkBricks(bricks); ball.checkEdges(); if (ball.y > height) { gameOver = true; } if (bricks.length === 0) { gameWin = true; } } paddle.show(); ball.show(); for (let brick of bricks) { brick.show(); } // Score display fill(255); textSize(20); textAlign(LEFT, TOP); text('Score: ' + score, 10, 10); if (gameOver) { textAlign(CENTER, CENTER); textSize(40); fill(255, 0, 0); text('GAME OVER', width/2, height/2); textSize(20); text('Click to restart', width/2, height/2 + 40); } if (gameWin) { textAlign(CENTER, CENTER); textSize(40); fill(0, 255, 0); text('YOU WIN!', width/2, height/2); textSize(20); text('Click to restart', width/2, height/2 + 40); } } function mousePressed() { if (gameOver || gameWin) { gameOver = false; gameWin = false; score = 0; ball.reset(); initBricks(); } } class Paddle { constructor() { this.width = 100; this.height = 15; this.y = height - 30; this.x = width/2 - this.width/2; } update() { // Mouse controls paddle this.x = mouseX - this.width/2; this.x = constrain(this.x, 0, width - this.width); } show() { fill(200); rect(this.x, this.y, this.width, this.height); } } class Ball { constructor() { this.radius = 10; this.reset(); } reset() { this.x = width/2; this.y = height - 50; this.speedX = random(-3, 3); this.speedY = -4; } update() { this.x += this.speedX; this.y += this.speedY; } checkEdges() { if (this.x - this.radius < 0 || this.x + this.radius > width) { this.speedX *= -1; } if (this.y - this.radius < 0) { this.speedY *= -1; } } checkPaddle(paddle) { if (this.y + this.radius >= paddle.y && this.y + this.radius <= paddle.y + paddle.height && this.x >= paddle.x && this.x <= paddle.x + paddle.width) { this.speedY *= -1; // Add slight angle based on hit position let hitPos = (this.x - paddle.x) / paddle.width; this.speedX = (hitPos - 0.5) * 8; } } checkBricks(bricks) { for (let i = bricks.length - 1; i >= 0; i--) { let brick = bricks[i]; if (this.x + this.radius > brick.x && this.x - this.radius < brick.x + brick.w && this.y + this.radius > brick.y && this.y - this.radius < brick.y + brick.h) { // Determine collision side let overlapLeft = this.x + this.radius - brick.x; let overlapRight = brick.x + brick.w - (this.x - this.radius); let overlapTop = this.y + this.radius - brick.y; let overlapBottom = brick.y + brick.h - (this.y - this.radius); let minOverlap = Math.min(overlapLeft, overlapRight, overlapTop, overlapBottom); if (minOverlap === overlapLeft || minOverlap === overlapRight) { this.speedX *= -1; } else { this.speedY *= -1; } bricks.splice(i, 1); score += 10; break; } } } show() { fill(255); ellipse(this.x, this.y, this.radius * 2); } } class Brick { constructor(x, y, w, h, col) { this.x = x; this.y = y; this.w = w; this.h = h; this.col = col; } show() { fill(this.col); rect(this.x, this.y, this.w, this.h); } } ``` Ce code crée un jeu d'Arkanoid complet où la raquette suit la position de la souris. Le jeu comprend des briques colorées, un système de score, et des conditions de victoire/défaite. Cliquez pour recommencer après une partie terminée.

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égories :coding| twitter| p5js| game-development

Discussion