用Arduino Nano复制Dino Run手机游戏

  发布人:amy2025 时间:2025-09-02

把经典的Dino Run变成亲身体验!使用Arduino Nano,以有趣,引人入胜的方式将互动游戏带入生活!

我们将使用Arduino创建一个简单的“Dino Run”游戏,并使用PCBX在线模拟。

硬件需求

•Arduino板(如Arduino Uno)

•OLED显示屏(如SSD1306)

•按钮

•电阻器(10kΩ按钮)

•连接电线

软件需求

•Arduino IDE编码

•用于模拟项目的PCBX在线模拟器

接线图

在深入编码部分之前,让我们先设置电路。这是连接OLED显示器和Arduino按钮的基本接线图:

OLED显示器连接:

•VCC到Arduino 5V

•GND到Arduino GND

•SCL到Arduino A5 (I2C时钟)

•SDA到Arduino A4 (I2C数据)

按钮连接:

•一端到Arduino引脚2

•另一侧到GND(带上拉电阻连接5V)

•用PCBX模拟项目

要在线模拟这个Arduino项目:

•访问PCBX:进入PCBX在线仿真网站。

•模拟项目在线:项目代码和详细信息在这里:

结论

现在,您已经使用Arduino创建了一个简单的“Dino Run”游戏,并使用PCBX在线模拟了它。这个项目不仅展示了基本的游戏机制,还使您熟悉使用I2C设备,如OLED显示器和Arduino。你可以随意修改游戏。

代码

#include

#include

#include

#define SCREEN_WIDTH 128 // OLED display width, in pixels

#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define BUTTON_PIN 2 // Pin for the jump button

// Declaration for an SSD1306 display connected to I2C (Wire)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Bitmap data for the dinosaur image

const unsigned char dinosaur[] PROGMEM = {

0x00,0x00,0xFF,0x80,0x00,0x01,0xFF,0x80,0x00,0x03,0xFF,0xC0,0x00,0x03,0x3F,0xC0,

0x00,0x03,0xFF,0xC0,0x00,0x03,0xFF,0xC0,0x00,0x03,0xFF,0xC0,0x00,0x03,0xFF,0xC0,

0x00,0x03,0xF0,0x00,0x00,0x07,0xFF,0x00,0xC0,0x0F,0xFF,0x00,0xC0,0x3F,0xF0,0x00,

0xE0,0xFF,0xF0,0x00,0xF1,0xFF,0xFC,0x00,0xFF,0xFF,0xFE,0x00,0x7F,0xFF,0xF0,0x00,

0x3F,0xFF,0xF0,0x00,0x1F,0xFF,0xF0,0x00,0x0F,0xFF,0xE0,0x00,0x07,0xFF,0xE0,0x00,

0x03,0xFF,0xC0,0x00,0x03,0xFF,0xC0,0x00,0x03,0xF8,0x70,0x00,0x03,0x80,0x00,0x00,

0x03,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x01,0x80,0x00,0x00,};

int dinosaurX = 10; // Initial x position of the dinosaur

int dinosaurY = 35; // Initial y position of the dinosaur

int dinosaurHeight = 26; // Height of the dinosaur image

int dinosaurWidth = 27; // Width of the dinosaur image

bool jumping = false; // Flag to indicate if the dinosaur is jumping

int jumpHeight = 5; // Jump height in pixels

int jumpSpeed = 10; // Speed of the jump

int jumpCounter = 0; // Jump counter

bool buttonPressed = false; // Flag to indicate if the button has been pressed

bool gameOver = false; // Game over flag

int score = 0; // Score

int obstacleX = SCREEN_WIDTH; // Initial x position of the obstacle

int obstacleY = 40; // y position of the obstacle

int obstacleWidth = 10; // Width of the obstacle

int obstacleHeight = 20; // Height of the obstacle

int obstacleSpeed = 8; // Speed of the obstacle

void setup() {

pinMode(BUTTON_PIN, INPUT_PULLUP); // Set the button pin as input with pull-up resistor

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C is the I2C address of the OLED display

Serial.println(F("SSD1306 allocation failed"));

for(;;); // Do not proceed

}

display.clearDisplay(); // Clear the display

display.setTextSize(1); // Set text size

display.setTextColor(WHITE); // Set text color

}

void loop() {

if (gameOver) {

display.clearDisplay();

display.setCursor(0, 0);

display.println("Game Over!");

display.println("Score: " + String(score));

display.display();

delay(2000);

return;

}

// Check if the button is pressed (button is normally HIGH, LOW when pressed)

if (!jumping && !buttonPressed && digitalRead(BUTTON_PIN) == LOW) {

buttonPressed = true;

jumping = true;

jumpCounter = 0; // Reset jump counter when starting a jump

}

if (jumping) {

if (jumpCounter  jumpHeight) {

// Ascend phase

dinosaurY -= jumpSpeed;

} else if (jumpCounter >= jumpHeight && jumpCounter  jumpHeight * 2) {

// Descend phase

dinosaurY += jumpSpeed;

} else {

// End jump

jumping = false;

dinosaurY = 35; // Reset the dinosaur to the initial y position

buttonPressed = false; // Reset button pressed flag

}

jumpCounter++;

}

// Move the obstacle

obstacleX -= obstacleSpeed;

if (obstacleX  -obstacleWidth) {

obstacleX = SCREEN_WIDTH;

score++;

}

// Collision detection

if (dinosaurX  obstacleX + obstacleWidth && dinosaurX + dinosaurWidth > obstacleX &&

dinosaurY  obstacleY + obstacleHeight && dinosaurY + dinosaurHeight > obstacleY) {

gameOver = true;

}

display.clearDisplay(); // Clear the display

display.drawBitmap(dinosaurX, dinosaurY, dinosaur, dinosaurWidth, dinosaurHeight, WHITE); // Draw the dinosaur

display.fillRect(obstacleX, obstacleY, obstacleWidth, obstacleHeight, WHITE); // Draw the obstacle

drawRoad(); // Draw the road

display.setCursor(0, 0);

display.print("Score: ");

display.println(score);

display.display(); // Update the display

delay(10); // Small delay for smoother animation

}

void drawCross(int x, int y, int width, int height) {

display.drawLine(x, y, x, y + height, WHITE);

display.drawLine(x + width, y, x + width, y + height, WHITE);

display.drawLine(x, y + height / 2, x + width, y + height / 2, WHITE);

}

void drawRoad() {

int roadWidth = 130; // Width of the road (in pixels)

int roadHeight = 2; // Height of the road (in pixels)

int roadX = (SCREEN_WIDTH - roadWidth) / 2; // Center the road horizontally

int roadY = SCREEN_HEIGHT - roadHeight; // Position the road at the bottom

display.fillRect(roadX, roadY, roadWidth, roadHeight, WHITE);

int lineSpacing = 10;

for (int i = 0; i  roadWidth; i += lineSpacing) {

display.drawLine(roadX + i, roadY, roadX + i, roadY + roadHeight, BLACK);

}

}

关键词: ArduinoNano OLED显示器 PCBX在线模拟

加入微信
获取电子行业最新资讯
搜索微信公众号:EEPW

或用微信扫描左侧二维码

相关电路