Diskussion

Erstelle Tetris mit GPT-4 unter Verwendung von p5.js

Von Wikiprompt, der freien Prompt-Enzyklopädie

Kris Kashtanova
Beigetragen vonKris KashtanovaXQuelle

12. Mai 2023

Erstelle Tetris mit GPT-4 unter Verwendung von p5.js Eine Schritt-für-Schritt-Anleitung zur Erstellung eines Tetris-Spiels mit GPT-4, einschließlich Prompts für die anfängliche Codegenerierung und iterative Verbesserungen wie Linienlöschung und Bewegung.

Zugehörige Medien

Prompt-InhaltSpeichern

🌐
let board = []; const COLS = 10; const ROWS = 20; const BLOCK_SIZE = 30; let currentPiece = null; let pieceQueue = []; let score = 0; let gameOver = false; let dropCounter = 0; let dropInterval = 1000; let lastTime = 0; const PIECES = [ { shape: [[1,1,1,1]], color: [255,0,0] }, // I { shape: [[1,1],[1,1]], color: [255,165,0] }, // O { shape: [[0,1,0],[1,1,1]], color: [255,255,0] },// T { shape: [[1,0,0],[1,1,1]], color: [0,255,0] }, // L { shape: [[0,0,1],[1,1,1]], color: [0,255,255] },// J { shape: [[0,1,1],[1,1,0]], color: [0,0,255] }, // S { shape: [[1,1,0],[0,1,1]], color: [255,0,255] } // Z ]; function setup() { createCanvas(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE); initBoard(); spawnPiece(); } function initBoard() { board = []; for (let i = 0; i < ROWS; i++) { board.push(new Array(COLS).fill(0)); } } function spawnPiece() { if (pieceQueue.length === 0) { pieceQueue = PIECES.slice(); // Shuffle for (let i = pieceQueue.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [pieceQueue[i], pieceQueue[j]] = [pieceQueue[j], pieceQueue[i]]; } } const piece = pieceQueue.shift(); currentPiece = { shape: piece.shape.map(row => row.slice()), color: piece.color, x: Math.floor((COLS - piece.shape[0].length) / 2), y: 0 }; if (collides(currentPiece.shape, currentPiece.x, currentPiece.y)) { gameOver = true; } } function collides(shape, offsetX, offsetY) { for (let r = 0; r < shape.length; r++) { for (let c = 0; c < shape[r].length; c++) { if (shape[r][c]) { const boardX = offsetX + c; const boardY = offsetY + r; if (boardX < 0 || boardX >= COLS || boardY >= ROWS) return true; if (boardY >= 0 && board[boardY][boardX]) return true; } } } return false; } function mergePiece() { for (let r = 0; r < currentPiece.shape.length; r++) { for (let c = 0; c < currentPiece.shape[r].length; c++) { if (currentPiece.shape[r][c]) { const boardY = currentPiece.y + r; const boardX = currentPiece.x + c; if (boardY >= 0) { board[boardY][boardX] = currentPiece.color; } } } } } function clearLines() { for (let r = ROWS - 1; r >= 0; r--) { if (board[r].every(cell => cell !== 0)) { board.splice(r, 1); board.unshift(new Array(COLS).fill(0)); score += 100; r++; // Check same row again } } } function movePiece(dx, dy) { if (!collides(currentPiece.shape, currentPiece.x + dx, currentPiece.y + dy)) { currentPiece.x += dx; currentPiece.y += dy; return true; } return false; } function rotatePiece() { const rotated = currentPiece.shape[0].map((_, i) => currentPiece.shape.map(row => row[i]).reverse() ); if (!collides(rotated, currentPiece.x, currentPiece.y)) { currentPiece.shape = rotated; } } function hardDrop() { while (movePiece(0, 1)) {} lockPiece(); } function lockPiece() { mergePiece(); clearLines(); spawnPiece(); } function draw() { background(0); // Draw board for (let r = 0; r < ROWS; r++) { for (let c = 0; c < COLS; c++) { if (board[r][c] !== 0) { fill(board[r][c]); rect(c * BLOCK_SIZE, r * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } // Draw current piece if (currentPiece) { fill(currentPiece.color); for (let r = 0; r < currentPiece.shape.length; r++) { for (let c = 0; c < currentPiece.shape[r].length; c++) { if (currentPiece.shape[r][c]) { rect((currentPiece.x + c) * BLOCK_SIZE, (currentPiece.y + r) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } } // Score fill(255); textSize(20); text("Score: " + score, 10, 30); if (gameOver) { fill(255, 0, 0); textSize(40); text("GAME OVER", width/2 - 100, height/2); } } function keyPressed() { if (gameOver) return; if (keyCode === LEFT_ARROW) { movePiece(-1, 0); } else if (keyCode === RIGHT_ARROW) { movePiece(1, 0); } else if (keyCode === DOWN_ARROW) { if (movePiece(0, 1)) { score += 1; dropCounter = 0; } } else if (keyCode === UP_ARROW) { rotatePiece(); } else if (key === ' ') { hardDrop(); } } function draw() { // ... (same as before) } function update(time = 0) { const deltaTime = time - lastTime; lastTime = time; dropCounter += deltaTime; if (dropCounter > dropInterval) { if (!movePiece(0, 1)) { lockPiece(); } dropCounter = 0; } draw(); requestAnimationFrame(update); } // Override draw to use update loop function draw() { // Empty - we use update instead } requestAnimationFrame(update);

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

Kategorien:coding| twitter| tetris| gpt-4

Diskussion