Arkanoid-Spiel mit p5.js
Von Wikiprompt, der freien Prompt-Enzyklopädie
Arkanoid-Spiel mit p5.js Ein Einzel-Prompt zur Erzeugung eines spielbaren Arkanoid-Spiels in p5.js, bei dem das Paddle mit der Maus gesteuert wird. Enthält Folgeanweisungen zum Fortsetzen oder Debuggen des Codes.
Prompt-InhaltSpeichern
🌐
Here is a complete p5.js sketch for an Arkanoid-style game where the mouse controls the paddle. You can copy and paste this into the p5.js editor or an HTML file with the p5.js library.
```javascript
let paddle;
let ball;
let bricks = [];
let cols = 8;
let rows = 5;
let brickWidth, brickHeight;
let gameOver = false;
let score = 0;
function setup() {
createCanvas(800, 600);
brickWidth = width / cols;
brickHeight = 30;
// Initialize paddle
paddle = {
width: 100,
height: 15,
x: width / 2 - 50,
y: height - 30,
speed: 8
};
// Initialize ball
ball = {
x: width / 2,
y: height - 50,
vx: 3,
vy: -3,
radius: 10
};
// Create bricks
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
bricks.push({
x: i * brickWidth,
y: j * brickHeight + 50,
width: brickWidth - 2,
height: brickHeight - 2,
alive: true
});
}
}
}
function draw() {
background(0);
// Draw bricks
for (let brick of bricks) {
if (brick.alive) {
fill(255, 100, 100);
rect(brick.x, brick.y, brick.width, brick.height);
}
}
// Draw paddle
fill(255);
rect(paddle.x, paddle.y, paddle.width, paddle.height);
// Draw ball
fill(255, 255, 0);
ellipse(ball.x, ball.y, ball.radius * 2);
// Move paddle with mouse
paddle.x = mouseX - paddle.width / 2;
paddle.x = constrain(paddle.x, 0, width - paddle.width);
// Move ball
ball.x += ball.vx;
ball.y += ball.vy;
// Ball collision with walls
if (ball.x < ball.radius || ball.x > width - ball.radius) {
ball.vx *= -1;
}
if (ball.y < ball.radius) {
ball.vy *= -1;
}
// Ball collision with paddle
if (ball.y + ball.radius > paddle.y &&
ball.y - ball.radius < paddle.y + paddle.height &&
ball.x > paddle.x &&
ball.x < paddle.x + paddle.width) {
ball.vy *= -1;
// Add slight angle based on where ball hits paddle
let hitPos = (ball.x - paddle.x) / paddle.width; // 0 to 1
ball.vx = (hitPos - 0.5) * 6;
}
// Ball collision with bricks
for (let brick of bricks) {
if (brick.alive) {
if (ball.x + ball.radius > brick.x &&
ball.x - ball.radius < brick.x + brick.width &&
ball.y + ball.radius > brick.y &&
ball.y - ball.radius < brick.y + brick.height) {
brick.alive = false;
ball.vy *= -1;
score += 10;
break;
}
}
}
// Ball falls below screen
if (ball.y > height) {
gameOver = true;
}
// Display score and game over
fill(255);
textSize(20);
text("Score: " + score, 10, 30);
if (gameOver) {
textSize(40);
text("GAME OVER", width/2 - 100, height/2);
noLoop();
}
// Check win condition
let allDead = bricks.every(brick => !brick.alive);
if (allDead) {
textSize(40);
text("YOU WIN!", width/2 - 100, height/2);
noLoop();
}
}
```
This code creates a fully functional Arkanoid game. The paddle follows the mouse horizontally, the ball bounces off walls, paddle, and bricks, and the game tracks score and win/lose conditions.
Melde dich an, um den vollständigen Prompt zu sehen
Weiter mit:
Mit der Anmeldung akzeptierst du unsere Nutzungsbedingungen und Datenschutz
Verwendung
Dieser Prompt ist für die Verwendung mit coding gedacht. Kopiere den Inhalt oben und füge ihn in dein bevorzugtes KI-Tool ein.
Für beste Ergebnisse passe die Platzhalter (eckige Klammern oder Großbuchstaben) an deine Anforderungen an.
Referenzen
- Kategorie: coding-Prompts
- Quelle: https://x.com/icreatelife/status/1636940552632401920
Diskussion
0 Kommentare