讨论

使用GPT-4和p5.js构建俄罗斯方块

来自 Wikiprompt,自由的提示词百科全书

Kris Kashtanova

2023年5月12日

使用GPT-4和p5.js构建俄罗斯方块 使用GPT-4创建俄罗斯方块游戏的分步指南,包括用于初始代码生成和迭代改进(如消行和移动)的提示词。

相关媒体

提示词内容收藏

🌐
let board = []; const COLS = 10; const ROWS = 20; const BLOCK_SIZE = 30; let currentPiece = null; let currentX = 0; let currentY = 0; let dropInterval = 500; let lastDrop = 0; let score = 0; const PIECES = [ [[1,1,1,1]], [[1,1],[1,1]], [[0,1,0],[1,1,1]], [[1,0,0],[1,1,1]], [[0,0,1],[1,1,1]], [[1,1,0],[0,1,1]], [[0,1,1],[1,1,0]] ]; const COLORS = ['#ff0000', '#ff7f00', '#ffff00', '#00ff00', '#0000ff', '#4b0082', '#8b00ff']; function setup() { createCanvas(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE); for (let i = 0; i < ROWS; i++) { board[i] = new Array(COLS).fill(0); } 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(COLORS[board[r][c] - 1]); rect(c * BLOCK_SIZE, r * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } // Draw current piece if (currentPiece) { for (let r = 0; r < currentPiece.length; r++) { for (let c = 0; c < currentPiece[r].length; c++) { if (currentPiece[r][c] === 1) { fill(COLORS[currentPieceIndex]); rect((currentX + c) * BLOCK_SIZE, (currentY + r) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } } // Auto drop if (millis() - lastDrop > dropInterval) { moveDown(); lastDrop = millis(); } // Score display fill(255); textSize(20); text('Score: ' + score, 10, 30); } function spawnPiece() { currentPieceIndex = floor(random(PIECES.length)); currentPiece = PIECES[currentPieceIndex]; currentX = floor(COLS / 2) - floor(currentPiece[0].length / 2); currentY = 0; // Check game over if (collision(currentX, currentY)) { noLoop(); fill(255, 0, 0); textSize(40); text('GAME OVER', width/2 - 100, height/2); } } function collision(x, y) { for (let r = 0; r < currentPiece.length; r++) { for (let c = 0; c < currentPiece[r].length; c++) { if (currentPiece[r][c] === 1) { let boardX = x + c; let boardY = y + r; if (boardX < 0 || boardX >= COLS || boardY >= ROWS) return true; if (boardY >= 0 && board[boardY][boardX] !== 0) return true; } } } return false; } function moveDown() { if (!collision(currentX, currentY + 1)) { currentY++; } else { lockPiece(); clearLines(); spawnPiece(); } } function lockPiece() { for (let r = 0; r < currentPiece.length; r++) { for (let c = 0; c < currentPiece[r].length; c++) { if (currentPiece[r][c] === 1) { let boardY = currentY + r; let boardX = currentX + c; if (boardY >= 0) { board[boardY][boardX] = currentPieceIndex + 1; } } } } } 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++; } } } function keyPressed() { if (keyCode === LEFT_ARROW && !collision(currentX - 1, currentY)) { currentX--; } else if (keyCode === RIGHT_ARROW && !collision(currentX + 1, currentY)) { currentX++; } else if (keyCode === DOWN_ARROW) { moveDown(); lastDrop = millis(); } else if (keyCode === UP_ARROW) { rotatePiece(); } } function rotatePiece() { let rotated = []; for (let c = 0; c < currentPiece[0].length; c++) { let newRow = []; for (let r = currentPiece.length - 1; r >= 0; r--) { newRow.push(currentPiece[r][c]); } rotated.push(newRow); } let oldPiece = currentPiece; currentPiece = rotated; if (collision(currentX, currentY)) { currentPiece = oldPiece; } }

登录以查看完整提示词

继续使用:

登录即表示你同意我们的 使用条款 和 隐私政策

用法

此提示词专为 coding 设计。复制上方内容并粘贴到你常用的 AI 工具中。

为获得最佳效果,可将占位符(方括号或大写字母标示)替换为你的具体需求。

参考资料

分类:coding| twitter| tetris| gpt-4

讨论