新增步數器如果達到一定程度的步數遊戲就會結束
void displayGameOver() { fill(255, 0, 0); textSize(32); textAlign(CENTER, CENTER); text("遊戲結束!", width / 2, height / 2 - 20); textSize(16); text("按一下回到主畫面", width / 2, height / 2 + 20); }
void displayStepCount() { fill(0); textSize(16); textAlign(LEFT); text("步數:" + stepCount, 10, height - 10); // 顯示步數 }
void keyPressed() { if (keyCode == UP && ballY > 0 && !grid[ballX][ballY - 1]) { ballY--; stepCount++; } else if (keyCode == DOWN && ballY < rows - 1 && !grid[ballX][ballY + 1]) { ballY++; stepCount++; } else if (keyCode == LEFT && ballX > 0 && !grid[ballX - 1][ballY]) { ballX--; stepCount++; } else if (keyCode == RIGHT && ballX < cols - 1 && !grid[ballX + 1][ballY]) { ballX++; stepCount++; if (stepCount >= maxSteps) { gamestopGame(); } }

沒有留言:
張貼留言