用Arduino Nano模拟在线重建“Flappy Bird”
扫描二维码
随时随地手机看文章
学习重新创建经典的手机游戏“Flappy Bird”使用Arduino Nano!为了乐趣和创造力,潜入编码和电子领域。
我们将指导您通过使用Arduino Nano和OLED显示器复制曾经占主导地位的手机游戏Flappy Bird的过程。对于这个项目,我们将利用PCBX在线仿真平台,它允许您在不需要物理硬件的情况下设计,原型和模拟您的Arduino项目。这意味着你可以直接在你的网页浏览器中玩游戏!
图书馆需要
要在PCBX中使用OLED显示器,您将需要在仿真中实现以下库:
Adafruit GFX
Adafruit SSD1306
这些库将成为在线模拟环境的一部分。
守则解释
1. 库和常量
我们首先包含必要的库并定义屏幕尺寸、鸟类属性、管道属性和游戏机制的常量。
2. 设置函数
在setup()函数中,我们初始化显示并配置输入按钮。我们还调用initializePipes()来设置管道的初始位置。
3. 主循环
loop()函数包含游戏逻辑:
输入检测:当按下按钮时,小鸟拍动。
鸟的运动:鸟的位置是根据它的速度来更新的,这是受重力影响的。
管道移动:管道在屏幕上向左移动,当它们退出屏幕时生成新的管道。
碰撞检测:如果小鸟与管道碰撞,游戏结束。
显示更新:屏幕被清除,并绘制当前游戏状态(鸟,管道,分数)。
4. 游戏结束处理
当游戏结束时,会显示“游戏结束”的消息以及分数。短暂停顿后,游戏重新开始。
5. 管道初始化和间隙生成
initializePipes()函数设置管道的初始位置和间隙,而generateUniqueGapHeight()确保管道之间的间隙是唯一的。
在线玩游戏
使用PCBX,您可以轻松地模拟游戏并直接在web浏览器中玩它。只需创建一个新项目,使用上面提供的代码,并运行模拟来体验您自己的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