토론

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; const SHAPES = [ [[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 = [ [255, 0, 0], [255, 127, 0], [255, 255, 0], [0, 255, 0], [0, 255, 255], [0, 0, 255], [255, 0, 255] ]; let currentPiece = null; let currentX = 0; let currentY = 0; let currentColor = 0; let dropCounter = 0; let dropInterval = 1000; let lastTime = 0; function setup() { createCanvas(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE); for (let i = 0; i < ROWS; i++) { board[i] = []; for (let j = 0; j < COLS; j++) { board[i][j] = 0; } } spawnPiece(); } function draw() { background(0); let now = millis(); let deltaTime = now - lastTime; lastTime = now; dropCounter += deltaTime; if (dropCounter > dropInterval) { moveDown(); dropCounter = 0; } drawBoard(); drawPiece(); } function spawnPiece() { let shapeIndex = floor(random(SHAPES.length)); currentPiece = SHAPES[shapeIndex]; currentX = floor(COLS / 2) - floor(currentPiece[0].length / 2); currentY = 0; currentColor = floor(random(COLORS.length)); } function drawBoard() { for (let i = 0; i < ROWS; i++) { for (let j = 0; j < COLS; j++) { if (board[i][j] !== 0) { let colorIndex = board[i][j] - 1; fill(COLORS[colorIndex][0], COLORS[colorIndex][1], COLORS[colorIndex][2]); rect(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } } function drawPiece() { fill(COLORS[currentColor][0], COLORS[currentColor][1], COLORS[currentColor][2]); for (let i = 0; i < currentPiece.length; i++) { for (let j = 0; j < currentPiece[i].length; j++) { if (currentPiece[i][j] === 1) { rect((currentX + j) * BLOCK_SIZE, (currentY + i) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } } function moveDown() { if (!collision(currentPiece, currentX, currentY + 1)) { currentY++; } else { lockPiece(); clearLines(); spawnPiece(); } } function moveLeft() { if (!collision(currentPiece, currentX - 1, currentY)) { currentX--; } } function moveRight() { if (!collision(currentPiece, currentX + 1, currentY)) { currentX++; } } function rotatePiece() { let rotated = []; for (let i = 0; i < currentPiece[0].length; i++) { rotated[i] = []; for (let j = 0; j < currentPiece.length; j++) { rotated[i][j] = currentPiece[currentPiece.length - 1 - j][i]; } } if (!collision(rotated, currentX, currentY)) { currentPiece = rotated; } } function collision(piece, x, y) { for (let i = 0; i < piece.length; i++) { for (let j = 0; j < piece[i].length; j++) { if (piece[i][j] === 1) { let boardX = x + j; let boardY = y + i; if (boardX < 0 || boardX >= COLS || boardY >= ROWS) { return true; } if (boardY >= 0 && board[boardY][boardX] !== 0) { return true; } } } } return false; } function lockPiece() { for (let i = 0; i < currentPiece.length; i++) { for (let j = 0; j < currentPiece[i].length; j++) { if (currentPiece[i][j] === 1) { let boardY = currentY + i; let boardX = currentX + j; if (boardY >= 0) { board[boardY][boardX] = currentColor + 1; } } } } } function clearLines() { for (let i = ROWS - 1; i >= 0; i--) { let full = true; for (let j = 0; j < COLS; j++) { if (board[i][j] === 0) { full = false; break; } } if (full) { board.splice(i, 1); board.unshift([]); for (let j = 0; j < COLS; j++) { board[0][j] = 0; } i++; } } } function keyPressed() { if (keyCode === LEFT_ARROW) { moveLeft(); } else if (keyCode === RIGHT_ARROW) { moveRight(); } else if (keyCode === DOWN_ARROW) { moveDown(); } else if (keyCode === UP_ARROW) { rotatePiece(); } }

전체 프롬프트를 보려면 로그인하세요

Continue with:

By logging in, you agree to our Terms of Use and Privacy Policy

사용법

이 프롬프트는 coding와 함께 사용하도록 설계되었습니다. 위의 프롬프트 내용을 복사하여 원하는 AI 도구에 붙여넣으세요.

최상의 결과를 얻으려면 자리 표시자(대괄호 또는 대문자로 표시)를 특정 요구 사항으로 사용자 지정할 수 있습니다.

참고 자료

분류:coding| twitter| tetris| gpt-4

토론