用Arduino Nano模擬在線重建“Flappy Bird”
學習重新創(chuàng)建經(jīng)典的手機游戲“Flappy Bird”使用Arduino Nano!為了樂趣和創(chuàng)造力,潛入編碼和電子領域。
我們將指導您通過使用Arduino Nano和OLED顯示器復制曾經(jīng)占主導地位的手機游戲Flappy Bird的過程。對于這個項目,我們將利用PCBX在線仿真平臺,它允許您在不需要物理硬件的情況下設計,原型和模擬您的Arduino項目。這意味著你可以直接在你的網(wǎng)頁瀏覽器中玩游戲!
圖書館需要
要在PCBX中使用OLED顯示器,您將需要在仿真中實現(xiàn)以下庫:
Adafruit GFX
Adafruit SSD1306
這些庫將成為在線模擬環(huán)境的一部分。
守則解釋
1. 庫和常量
我們首先包含必要的庫并定義屏幕尺寸、鳥類屬性、管道屬性和游戲機制的常量。
2. 設置函數(shù)
在setup()函數(shù)中,我們初始化顯示并配置輸入按鈕。我們還調用initializePipes()來設置管道的初始位置。
3. 主循環(huán)
loop()函數(shù)包含游戲邏輯:
輸入檢測:當按下按鈕時,小鳥拍動。
鳥的運動:鳥的位置是根據(jù)它的速度來更新的,這是受重力影響的。
管道移動:管道在屏幕上向左移動,當它們退出屏幕時生成新的管道。
碰撞檢測:如果小鳥與管道碰撞,游戲結束。
顯示更新:屏幕被清除,并繪制當前游戲狀態(tài)(鳥,管道,分數(shù))。
4. 游戲結束處理
當游戲結束時,會顯示“游戲結束”的消息以及分數(shù)。短暫停頓后,游戲重新開始。
5. 管道初始化和間隙生成
initializePipes()函數(shù)設置管道的初始位置和間隙,而generateUniqueGapHeight()確保管道之間的間隙是唯一的。
在線玩游戲
使用PCBX,您可以輕松地模擬游戲并直接在web瀏覽器中玩它。只需創(chuàng)建一個新項目,使用上面提供的代碼,并運行模擬來體驗您自己的Flappy Bird游戲!
代碼
#include
#include
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin not needed
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Bird parameters
#define BIRD_X 20
#define BIRD_WIDTH 8
#define BIRD_HEIGHT 8
#define FLAP_FORCE -2
#define GRAVITY 1
int birdY = SCREEN_HEIGHT / 2;
int birdVelocity = 0;
// Pipe parameters
#define PIPE_WIDTH 5
#define GAP_HEIGHT 20
#define PIPE_COUNT 3 // Display three pipes at once
int pipeX[PIPE_COUNT] = {SCREEN_WIDTH, SCREEN_WIDTH + SCREEN_WIDTH/PIPE_COUNT, SCREEN_WIDTH + 2*(SCREEN_WIDTH/PIPE_COUNT)};
int pipeGapY[PIPE_COUNT];
// Game and input parameters
#define BUTTON_PIN 2
bool game_over = false;
int score = 0;
// Bird bitmap
const uint8_t PROGMEM birdBitmap[] = {
0x00, 0x28, 0x1C, 0x1B, 0x1C, 0x28, 0x00, 0x00
};
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
// Initialize random heights for pipe gaps
initializePipes();
}
void loop() {
if (!game_over) {
// Button input detection
if (digitalRead(BUTTON_PIN) == LOW) {
birdVelocity = FLAP_FORCE;
}
// Update bird position
birdY += birdVelocity;
birdVelocity += GRAVITY;
// Update position of each pipe
for (int i = 0; i < PIPE_COUNT; i++) {
pipeX[i] -= 2;
if (pipeX[i] < -PIPE_WIDTH) {
pipeX[i] = SCREEN_WIDTH;
// Set different gap height for new pipe
pipeGapY[i] = generateUniqueGapHeight(i);
score++;
}
// Collision detection
if (pipeX[i] < BIRD_X + BIRD_WIDTH && pipeX[i] + PIPE_WIDTH > BIRD_X &&
(birdY < pipeGapY[i] || birdY + BIRD_HEIGHT > pipeGapY[i] + GAP_HEIGHT)) {
game_over = true;
}
}
// Clear display and draw the next frame
display.clearDisplay();
display.drawBitmap(BIRD_X, birdY, birdBitmap, BIRD_WIDTH, BIRD_HEIGHT, SSD1306_WHITE);
for (int i = 0; i < PIPE_COUNT; i++) {
display.fillRect(pipeX[i], 0, PIPE_WIDTH, pipeGapY[i], SSD1306_WHITE);
display.fillRect(pipeX[i], pipeGapY[i] + GAP_HEIGHT, PIPE_WIDTH, SCREEN_HEIGHT - pipeGapY[i] - GAP_HEIGHT, SSD1306_WHITE);
}
display.setCursor(0, 0);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.print("Score: ");
display.print(score);
display.display();
// Control update rate
delay(30);
} else {
// Game over display
display.clearDisplay();
display.setCursor(10, 20);
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.print("Game Over");
display.setCursor(20, 45);
display.setTextSize(1);
display.print("Score: ");
display.print(score);
display.display();
delay(2000);
// Reset game
resetGame();
}
}
void resetGame() {
birdY = SCREEN_HEIGHT / 2;
birdVelocity = 0;
initializePipes();
score = 0;
game_over = false;
}
void initializePipes() {
for (int i = 0; i < PIPE_COUNT; i++) {
pipeX[i] = SCREEN_WIDTH + i * (SCREEN_WIDTH / PIPE_COUNT);
pipeGapY[i] = generateUniqueGapHeight(i);
}
}
int generateUniqueGapHeight(int currentPipe) {
int gapHeight;
bool isUnique;
do {
gapHeight = random(GAP_HEIGHT + 10, SCREEN_HEIGHT - GAP_HEIGHT - 10);
isUnique = true;
for (int j = 0; j < currentPipe; j++) {
if (pipeGapY[j] == gapHeight) {
isUnique = false;
break;
}
}
} while (!isUnique);
return gapHeight;
}
本文編譯自hackster.io