用Arduino Nano復(fù)制Dino Run手機(jī)游戲
掃描二維碼
隨時(shí)隨地手機(jī)看文章
把經(jīng)典的Dino Run變成親身體驗(yàn)!使用Arduino Nano,以有趣,引人入勝的方式將互動(dòng)游戲帶入生活!
我們將使用Arduino創(chuàng)建一個(gè)簡(jiǎn)單的“Dino Run”游戲,并使用PCBX在線模擬。
硬件需求
?Arduino板(如Arduino Uno)
?OLED顯示屏(如SSD1306)
?按鈕
?電阻器(10kΩ按鈕)
?連接電線
軟件需求
?Arduino IDE編碼
?用于模擬項(xiàng)目的PCBX在線模擬器
接線圖
在深入編碼部分之前,讓我們先設(shè)置電路。這是連接OLED顯示器和Arduino按鈕的基本接線圖:
OLED顯示器連接:
?VCC到Arduino 5V
?GND到Arduino GND
?SCL到Arduino A5 (I2C時(shí)鐘)
?SDA到Arduino A4 (I2C數(shù)據(jù))
按鈕連接:
?一端到Arduino引腳2
?另一側(cè)到GND(帶上拉電阻連接5V)
?用PCBX模擬項(xiàng)目
要在線模擬這個(gè)Arduino項(xiàng)目:
?訪問PCBX:進(jìn)入PCBX在線仿真網(wǎng)站。
?模擬項(xiàng)目在線:項(xiàng)目代碼和詳細(xì)信息在這里:
結(jié)論
現(xiàn)在,您已經(jīng)使用Arduino創(chuàng)建了一個(gè)簡(jiǎn)單的“Dino Run”游戲,并使用PCBX在線模擬了它。這個(gè)項(xiàng)目不僅展示了基本的游戲機(jī)制,還使您熟悉使用I2C設(shè)備,如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);
}
}
本文編譯自hackster.io