GPT-4 का उपयोग करके p5.js के साथ टेट्रिस बनाएं
Wikiprompt से, मुफ्त प्रॉम्प्ट विश्वकोश
GPT-4 का उपयोग करके p5.js के साथ टेट्रिस बनाएं GPT-4 के साथ टेट्रिस गेम बनाने के लिए चरण-दर-चरण मार्गदर्शिका, जिसमें प्रारंभिक कोड जनरेशन और लाइन क्लियरिंग और मूवमेंट जैसे पुनरावृत्त सुधारों के लिए प्रॉम्प्ट शामिल हैं।
संबंधित मीडिया
प्रॉम्प्ट सामग्रीसहेजें
🌐
यहाँ p5.js में टेट्रिस का कोड है, जिसमें आप तीर कुंजियों से नियंत्रित कर सकते हैं। हर टेट्रिमिनो इंद्रधनुषी रंगों में है, लाइनें क्लियर होती हैं, और नीचे तीर दबाने पर टुकड़ा तेजी से गिरता है।
```javascript
let board = [];
let blockSize = 30;
let cols = 10;
let rows = 20;
let currentPiece;
let nextPiece;
let score = 0;
let gameOver = false;
let dropInterval = 500;
let lastDrop = 0;
let fastDrop = false;
const SHAPES = [
[[1,1,1,1]], // I
[[1,1],[1,1]], // O
[[0,1,0],[1,1,1]], // T
[[1,0,0],[1,1,1]], // L
[[0,0,1],[1,1,1]], // J
[[0,1,1],[1,1,0]], // S
[[1,1,0],[0,1,1]] // Z
];
const COLORS = [
[255, 0, 0], // लाल
[255, 165, 0], // नारंगी
[255, 255, 0], // पीला
[0, 255, 0], // हरा
[0, 255, 255], // सियान
[0, 0, 255], // नीला
[255, 0, 255] // मैजेंटा
];
function setup() {
createCanvas(cols * blockSize, rows * blockSize);
initBoard();
currentPiece = newPiece();
nextPiece = newPiece();
textSize(20);
fill(255);
}
function initBoard() {
for (let i = 0; i < rows; i++) {
board[i] = [];
for (let j = 0; j < cols; j++) {
board[i][j] = 0;
}
}
}
function newPiece() {
let index = floor(random(SHAPES.length));
return {
shape: SHAPES[index],
color: COLORS[index],
x: floor(cols / 2) - floor(SHAPES[index][0].length / 2),
y: 0
};
}
function draw() {
background(0);
// बोर्ड ड्रा करें
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (board[i][j] !== 0) {
fill(board[i][j][0], board[i][j][1], board[i][j][2]);
rect(j * blockSize, i * blockSize, blockSize, blockSize);
}
}
}
// करंट पीस ड्रा करें
if (currentPiece) {
for (let i = 0; i < currentPiece.shape.length; i++) {
for (let j = 0; j < currentPiece.shape[i].length; j++) {
if (currentPiece.shape[i][j]) {
fill(currentPiece.color[0], currentPiece.color[1], currentPiece.color[2]);
rect((currentPiece.x + j) * blockSize, (currentPiece.y + i) * blockSize, blockSize, blockSize);
}
}
}
}
// स्कोर दिखाएं
fill(255);
text("स्कोर: " + score, 10, height - 10);
// गेम ओवर
if (gameOver) {
fill(255, 0, 0);
textSize(40);
text("गेम ओवर", width / 2 - 100, height / 2);
textSize(20);
return;
}
// ड्रॉप टाइमर
let interval = fastDrop ? 50 : dropInterval;
if (millis() - lastDrop > interval) {
moveDown();
lastDrop = millis();
}
}
function moveDown() {
if (!currentPiece) return;
currentPiece.y++;
if (collision()) {
currentPiece.y--;
lockPiece();
clearLines();
currentPiece = nextPiece;
nextPiece = newPiece();
if (collision()) {
gameOver = true;
}
}
}
function collision() {
for (let i = 0; i < currentPiece.shape.length; i++) {
for (let j = 0; j < currentPiece.shape[i].length; j++) {
if (currentPiece.shape[i][j]) {
let newX = currentPiece.x + j;
let newY = currentPiece.y + i;
if (newX < 0 || newX >= cols || newY >= rows) return true;
if (newY >= 0 && board[newY][newX] !== 0) return true;
}
}
}
return false;
}
function lockPiece() {
for (let i = 0; i < currentPiece.shape.length; i++) {
for (let j = 0; j < currentPiece.shape[i].length; j++) {
if (currentPiece.shape[i][j]) {
let x = currentPiece.x + j;
let y = currentPiece.y + i;
if (y >= 0) {
board[y][x] = currentPiece.color;
}
}
}
}
}
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;
}
score += 100;
i++;
}
}
}
function keyPressed() {
if (gameOver) return;
if (keyCode === LEFT_ARROW) {
currentPiece.x--;
if (collision()) currentPiece.x++;
} else if (keyCode === RIGHT_ARROW) {
currentPiece.x++;
if (collision()) currentPiece.x--;
} else if (keyCode === DOWN_ARROW) {
fastDrop = true;
} else if (keyCode === UP_ARROW) {
rotatePiece();
}
}
function keyReleased() {
if (keyCode === DOWN_ARROW) {
fastDrop = false;
}
}
function rotatePiece() {
let oldShape = currentPiece.shape;
let rotated = [];
for (let i = 0; i < oldShape[0].length; i++) {
let newRow = [];
for (let j = oldShape.length - 1; j >= 0; j--) {
newRow.push(oldShape[j][i]);
}
rotated.push(newRow);
}
let oldX = currentPiece.x;
currentPiece.shape = rotated;
// बाएं/दाएं एडजस्ट करें
if (collision()) {
currentPiece.x--;
if (collision()) {
currentPiece.x += 2;
if (collision()) {
currentPiece.x = oldX;
currentPiece.shape = oldShape;
}
}
}
}
```
यह कोड पूरा काम करता है - टुकड़े हिलते हैं, लाइनें क्लियर होती हैं, और नीचे तीर दबाने पर तेजी से गिरता है। आप इसे p5.js एडिटर में चला सकते हैं।
पूरा प्रॉम्प्ट देखने के लिए साइन इन करें
Continue with:
By logging in, you agree to our Terms of Use and Privacy Policy
उपयोग
यह प्रॉम्प्ट coding के साथ उपयोग के लिए डिज़ाइन किया गया है। ऊपर प्रॉम्प्ट सामग्री कॉपी करें और अपने पसंदीदा AI टूल में पेस्ट करें।
सर्वोत्तम परिणामों के लिए, आप अपनी विशिष्ट आवश्यकताओं के अनुसार प्लेसहोल्डर (वर्ग कोष्ठक या बड़े अक्षरों में दर्शाए गए) को अनुकूलित कर सकते हैं।
चर्चा
0 टिप्पणियाँ