當(dāng)前位置:首頁 > 工業(yè)控制 > 電路設(shè)計(jì)項(xiàng)目集錦
[導(dǎo)讀]我們將使用Arduino創(chuàng)建一個(gè)簡(jiǎn)單的“Dino Run”游戲,并使用PCBX在線模擬。

把經(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

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉