Cov txheej txheem:

Tsis Txaus Ntshai Racer Arduino OLED Game, AdafruitGFX thiab Bitmaps Basics: 6 Kauj Ruam
Tsis Txaus Ntshai Racer Arduino OLED Game, AdafruitGFX thiab Bitmaps Basics: 6 Kauj Ruam

Video: Tsis Txaus Ntshai Racer Arduino OLED Game, AdafruitGFX thiab Bitmaps Basics: 6 Kauj Ruam

Video: Tsis Txaus Ntshai Racer Arduino OLED Game, AdafruitGFX thiab Bitmaps Basics: 6 Kauj Ruam
Video: Tsis Tau Txaus Koj | Kong Chue (Official Music Video) 2024, Hlis ntuj nqeg
Anonim
Tsis Txaus Ntshai Racer Arduino OLED Game, AdafruitGFX thiab Bitmaps Basics
Tsis Txaus Ntshai Racer Arduino OLED Game, AdafruitGFX thiab Bitmaps Basics
Tsis Txaus Ntshai Racer Arduino OLED Game, AdafruitGFX thiab Bitmaps Basics
Tsis Txaus Ntshai Racer Arduino OLED Game, AdafruitGFX thiab Bitmaps Basics

Hauv qhov kev qhia no peb yuav saib yuav siv bitmaps siv Adafruit_GFX.c lub tsev qiv ntawv li cas ntawm sprites hauv kev ua si. Qhov kev ua si yooj yim tshaj plaws uas peb tuaj yeem xav txog yog ib txoj kab tsheb nqaj hlau hloov pauv kev ua si tsheb, thaum kawg peb tus neeg sim beta thiab tus pab tus coder txiav txim siab "Racless Racer" raws li lub npe, vim nws zoo nkauj heev ua rau tsav tsis raug hauv txoj kev tsheb !!

Kev Tsim Qauv ntawm peb lub voj voos yog nyob hauv cov duab suav nrog saum toj no thiab tau ntxaws ntxaws hauv peb qhov haujlwm kawg/qhia ntawm no Snake Instructables uas piav qhia txog kev ua haujlwm hauv Circuit.

peb yuav xav tau

Adafruit_GFX

Paint.net

Arduino IDE windowslinux

thiab thov ua tibzoo saib Cov Lus Qhia Snake rau cov khoom siv tas.

Khoom siv

Nab Game

Kauj Ruam 1: Txhim Kho Paint.net

Txhim kho Paint.net
Txhim kho Paint.net
Txhim kho Paint.net
Txhim kho Paint.net
Txhim kho Paint.net
Txhim kho Paint.net

Peb tab tom siv paint.net raws li software tsis pub dawb yog li siv dawb koj tuaj yeem rub tawm Paint. Net ntawm no.

Txhawm rau teeb tsa paint.net ob zaug nyem qhov program rub tawm thiab teb zoo li tom ntej no, yog, ok, kuv pom zoo thiab cov duab saum toj no yuav qhia rau koj.

Kauj Ruam 2: Teeb Duab Yooj Yim Splash Screen

Teeb duab yooj yim Splash Screen
Teeb duab yooj yim Splash Screen
Teeb duab yooj yim Splash Screen
Teeb duab yooj yim Splash Screen
Teeb duab yooj yim Splash Screen
Teeb duab yooj yim Splash Screen

Thaum koj nyob hauv paint.net tsim cov duab tshiab los ntawm txhaj Cov Ntaub Ntawv tshiab, teeb duab loj rau 1260x620 (saib thawj daim duab) nyem ok thaum koj muaj koj nplooj ntawv tshiab kos lub vijtsam txaws siv tsuas yog 2 xim dub thiab dawb siv tus xaum cuab yeej (pic2), thaum koj tau kos duab (lossis muab tso rau hauv) koj lub vijtsam duab txaws, nyem rau ntawm daim duab tom qab hloov pauv (duab 4), hauv qhov pop up hloov qhov loj ntawm 1260x620 txog 126x62 (2 pixels me dua koj cov zaub) (pic5) Nyem OK.

tom ntej no nyem Cov ntawv qhia zaub mov ces txuag li (pic6).

thaum lub pop up apears nyob rau hauv cov hom ntawv poob rau hauv cov ntawv qhia zaub mov xaiv BMP (bitmap). (pic7), ntaus lub npe cov ntaub ntawv thiab nyem txuag, thaum cov pop tshwm tshwm teeb tsa ob qho tib si rau 0 thiab teeb tsa rau 8 ntsis, nyem ok (pic8)).

Kauj Ruam 3: Hloov BMP rau C Bitmap File

Hloov BMP rau C Cov Ntaub Ntawv Bitmap
Hloov BMP rau C Cov Ntaub Ntawv Bitmap
Hloov BMP rau C Cov Ntaub Ntawv Bitmap
Hloov BMP rau C Cov Ntaub Ntawv Bitmap
Hloov BMP rau C Cov Ntaub Ntawv Bitmap
Hloov BMP rau C Cov Ntaub Ntawv Bitmap
Hloov BMP rau C Cov Ntaub Ntawv Bitmap
Hloov BMP rau C Cov Ntaub Ntawv Bitmap

Tam sim no peb yuav tsum hloov peb cov duab rau hom ntawv uas arduino tuaj yeem nkag siab muaj ntau cov cuab yeej muaj los ua qhov no tab sis kuv "mus rau" qhov chaw yog marlin webs site bitmap hloov cuab yeej …

marlinfw.org/tools/u8glib/converter.html

Yog li peb pib ntu no los ntawm kev siv qhov txuas saum toj no los qhib lub vev xaib uas tau qhia hauv pic1

nyem rau ntawm xaiv cov ntaub ntawv thiab xaiv qhov bitmap koj tsim ua ntej (pic2)

marlin bitmap hloov pauv yuav cia li hloov koj cov duab rau hauv c chaws laug ob npaug nyem rau tus lej uas yuav tsum tau hais txog tus lej, tom qab ntawd nyem txoj cai thiab nyem luam (pic3)

tom ntej Peb tsim txoj nyem thiab tsim cov ntawv sau tshiab (pic4)

ob npaug nias rau daim ntawv tshiab, thaum qhib txoj cai nyem thiab muab cov cai (pic5)

tom ntej no peb yuav tsum ntxiv kab nyob ze rau sab saum toj ntawm tus lej #suav nrog qhov no tso cai rau peb khaws cov ntaub ntawv bitmap rau lub cim xeeb nyem ntawm arduino, tom qab ntawd peb hloov npe #txhais qhov dav, qhov siab thiab lub npe rau qee yam yooj yim siv cov no tau hais qhia hauv daim duab 6, peb hloov lawv los ntawm kev tsim cov cim peb hloov pauv lawv mus rau qhov piv txwv hauv qab no

#define LOGOWIDTH

#Txheeb xyuas LOGOHEIGHT

const unsigned char LOGOPIC PROGMEM

tom ntej nyem cov ntaub ntawv tom qab ntawd khaws cia, khaws cov ntawv ua logo.c kaw notepad, nyem rau ntawm logo.c thiab nyem luam.

Kauj Ruam 4: Qhia LOGO Siv DrawBitmap

Qhia LOGO Siv DrawBitmap
Qhia LOGO Siv DrawBitmap
Qhia LOGO Siv DrawBitmap
Qhia LOGO Siv DrawBitmap
Qhia LOGO Siv DrawBitmap
Qhia LOGO Siv DrawBitmap
Qhia LOGO Siv DrawBitmap
Qhia LOGO Siv DrawBitmap

Tam sim no peb thauj cov arduino IDE thiab tsim cov duab kos tshiab thiab khaws nws lub npe ua logoexample.ino, tom ntej no yog kev dag hauv arduino lub tswv yim nyem rau cov ntawv qhia zaub mov ces txuag raws li, rov qab mus rau qhov haujlwm nplaub tshev txoj cai nyem thiab muab tso rau hauv.c ntawv (pic2) tom qab ntawv nyem qhov rho tawm, qhov no cawm koj kom tau mus rau hauv daim nplaub tshev kom muab tshuaj txhuam rau hauv cov ntawv.

ntaus cov cai hauv qab no rau hauv arduino IDE lossis rub tawm hauv ino.

(peb pom zoo kom ntaus ntawv tsis yog theej thiab muab tshuaj lossis siv cov ntawv hauv qab no nws yog txoj hauv kev zoo tshaj los kawm)

#suav nrog /* qhov no yuav sib txawv nyob ntawm qhov chaw koj khaws

qhov ino feem ntau hauv daim nplaub tshev C: Cov Neeg Siv ~ npe neeg siv Cov Ntaub Ntawv Arduino project ~ lub npe

thiab qhov no yog qhov peb txuas rau peb cov bitmap */

#suav nrog

#suav nrog

uint8_t bmpX, bmpY = 0; /* khaws lub cim xeeb rau 2 X 8 ntsis cov lej, peb tsuas xav tau 8 ntsis ints

raws li tus nqi yeej tsis siab dua 128 (pixels) yog li peb tuaj yeem txuag chaw siv 8 ntsis ints (uas muaj tus nqi siab tshaj ntawm 255) */

void teeb tsa ()

{ncua (100); // muab sijhawm tso rau lwm lub sijhawm rau lub zog ntawm display.begin (SSD1306_SWITCHCAPVCC, 0x3C); // qhov no yog qhov pib ua kom pom cov duab pom tseeb. Deview (); // pib nrog lub vijtsam dawb}

/* thov nco tias koj tsis tas yuav ntaus cov lus no hauv lawv yog qhov siv rau …………..

cov lus txib peb yuav tsom mus rau yog display.drawBitmap, qhov no yog dab tsi kos peb lub vijtsam txaws. (bmpX, yog X axis tus nqi ntawm lub vijtsam uas X qhov taw tes ntawm qhov bitmap yuav yog thiab bmpX thiab bmpY yog qhov muaj txiaj ntsig peb txaus siab los tsim kev txav chaw (bmpY, yog Y axis tus nqi ntawm qhov screen uas Y thauj tog rau nkoj taw tes ntawm qhov bitmap yuav yog peb tau txais cov npe siv raws li peb tau piav qhia lawv hauv logo.c (LOGOPIC, yog lub npe ntawm bitmap hauv #suav nrog cov ntaub ntawv logo.c (LOGOWIDTH, yog pes tsawg pixels ntxiv (X) los kos bitmap los ntawm qhov taw tes thauj tog rau nkoj (LOGOHEIGHT, yog pes tsawg tus pixels nqis (Y) txhawm rau kos lub bitmap los ntawm tus thauj tog rau nkoj taw tes X thiab Y pixels tuaj yeem nkag mus tau ntawm tus kheej tab sis nws yooj yim dua rau siv cov uas tau npaj ua ntej dua nco qab lawv txhua tus (1, tus nqi kawg yog xim raws li lub vijtsam yog mono 0 dub 1 dawb. ok pib ntaus los ntawm kab ntawv tom ntej: lolD lol*/ void loop () {display.clearDisplay (); // blank screen // bitmap kos los ntawm sab saum toj sab laug, x, y, npe bitmap, dav X, qhov siab Y, xim zaub.drawBitmap (bmpX, bmpY, LOGOPIC, LOGOWIDTH, LOGOHEIGHT, 1); display.display (); // qhov no yeej thim qhov tsis mus rau cov zaub ib txwm}

upload koj cov cai koj arduino thiab paub meej tias nws ua haujlwm (pic3).

Kauj ruam 5: Tsiv Bitmap Sprite

Image
Image
Tsiv Bitmap Sprite
Tsiv Bitmap Sprite
Tsiv Bitmap Sprite
Tsiv Bitmap Sprite

siv cov lus qhia ua ntej siv paint.net thiab ua daim ntawv tshiab ua rau nws 30x15 pixels (pic1) thiab kos lub tsheb ntxhib peb cov tub ntxhais tsim qauv pib nrog lub tsom iav ua ntej (duab 2 & 3).

rov txuag nws ua cov ntaub ntawv windows bmp (zoo li hauv step2), hloov mus rau C bitmap (step3) thiab tso cov ntawv car.c (lossis yam koj tau txiav txim siab li cas) hauv tib daim nplaub tshev uas yog tsim tshiab arduino ino (kos duab) ntaub ntawv.

(p.s. nco ntsoov ntxiv kab #suav nrog hauv tsheb.c qhov no siv los ntes peb ntau zaus)

Ua ntej txuas koj qhov sib npaug ntawm car.c

#suav nrog

#include #include Adafruit_GFX.h> // https://github.com/adafruit/Adafruit-GFX-Library #include Adafruit_SSD1306 //

Adafruit_SSD1306 zaub (128, 64); // teeb qhov kev daws teeb meem ntawm cov zaub

/* bmpX/bmpY peb xav tau cov no los ua cov hloov pauv raws li hloov cov txiaj ntsig no thiab rov ua dua

lub vijtsam yog li cas peb tsim kev txav txav cov nyhuv. hitSide thiab hitTop yog li cas peb khaws sprite hauv qhov screen */ uint8_t bmpX, bmpY = 0; // tseg nco rau 2 8 ntsis ints (0-255) peb tsis xav tau loj dua 128 yuav yog tus lej loj tshaj siv bool hitSide = 0; bool hitTop = 0;

void teeb tsa ()

{ncua (100); // muab sijhawm tso rau lwm lub sijhawm rau lub zog ntawm display.begin (SSD1306_SWITCHCAPVCC, 0x3C); // qhov no yog qhov pib ua kom pom cov duab pom tseeb. Deview (); // pib nrog lub vijtsam dawb

}

void lub voj ()

{display.clearDisplay (); // khoob qhov screen // bitmap kos los ntawm sab saum toj sab laug, x, y, lub npe bitmap, dav X, qhov siab Y, xim zaub.drawBitmap (bmpX, bmpY, CARSPRITE, CARWIDTH, CARHEIGHT, 1); // display.display (); // qhov no yeej thim qhov tsis mus rau cov zaub puas/ * qhov no yog li cas peb taug qab ntug ntawm lub vijtsam thiab txiav txim siab seb puas yuav ntxiv pixel txav mus rau saum hauv qab) lossis tshem tawm pixel (txav hauv qab mus rau saum) */ hloov (hitSide) // qhov no xaiv cov lus qhia ntawm lub tsheb raws li boole {rooj plaub 0: bmpX ++; tawg;

rooj plaub 1:

bmpX--; tawg; } // cov 2 no yog cov lus teev bool rau qhov tseeb lossis cuav yog (bmpX == 0) {hitSide = 0; } yog (bmpX == 96) // qhov dav ntawm qhov screen rho tawm lub tsheb {hitSide = 1; } // tib yam li saum toj no rau Y axis yog (bmpY == 0) {hitTop = 0; } yog (bmpY == 49) // npo qhov siab rho tawm lub tsheb siab {hitTop = 1; } hloov (hitTop) {rooj plaub 0: bmpY ++; tawg; rooj plaub 1: bmpY--; tawg; }

}

koj tuaj yeem pom qhov haujlwm ua haujlwm hauv daim vis dis aus txuas nrog

Kauj Ruam 6: Ua Kev Tsav Tsheb

Image
Image

Ua ntej Peb pib los ntawm kev kos ob peb lub tsheb sib txawv lossis cov teeb meem zoo li hauv qib ua ntej ntawm kev qhia ua lawv 30x15 pixels. Tom qab ntawd peb hloov lawv mus rau c bitmaps thiab txuas hauv cov cai.

#suav nrog // cov kev no yuav tau hloov pauv

// ntawm qhov chaw koj khaws cov ntawv // kho: kuv nyuam qhuav pom tawm yog tias koj hloov // nrog "" koj tsis xav tau txoj hauv kev // nrog koj tus kheej lub tsev qiv ntawv #suav nrog

#suav nrog

#ncig #ncig

#suav nrog

#include // https://github.com/adafruit/Adafruit-GFX-Library #include <Adafruit_SSD1306 //

Adafruit_SSD1306 zaub (128, 64); // txhais cov zaub tsis

txhais cov kev hloov pauv thiab cov txiaj ntsig ruaj khov

// txhais tus lej nkag cov no yog cov pins ntawm arduino lawv tsis hloov pauv li #txhais #txhais INTPIN 3 // tsuas yog tus pin 2 thiab 3 tuaj yeem cuam tshuam tus pin ntawm UNO #define UPPIN 4 // cov no yog cov pins txuas nrog tp cuam tshuam hloov #define DWNPIN 5 #define LFTPIN 6 #define RHTPIN 7 #define SND 9 // txhais cov lus qhia

#define DIRUP 1 // cov txiaj ntsig no yog dab tsi "nab" zoo li txiav txim siab-

#define DIRDOWN 2 // kev coj ua tus nab yuav mus #define DIRLEFT 3 #define DIRRIGHT 4

uint8_t dirPressed = 0; // tus nqi rau npe rau kev taw qhia kom txav mus rau tus pin mus siab

// booleans khw uas tus pin nce siab

pob BUTUP = 0; bool BUTDWN = 0; bool BUTLFT = 0; bool BUTRHT = 0; // vars rau txoj haujlwm ntawm lub tsheb uint8_t carPosX = 1; uint8_t carPosY = {0, 16, 32, 48}; // xav tau tus nqi cahnging tus array

uint8_t lanePosArr = {0, 16, 32, 48}; // array khaws qhov twg txhua txoj kab yog

uint8_t carPosYCnt = 0; uint8_t carYTmp = 0; // hloov pauv rau cov kab hauv txoj kev uint8_t roadLineX1 = 51; // cov no tau txiav txim siab ua ntej ntawm qhov pib thaum ntawd cov kab zoo li tsis sib xws uint8_t roadLineX2 = 102; uint8_t roadLineX3 = 153; uint8_t roadLineX4 = 254; uint8_t roadLineX5 = 200;

// qhov no yog pes tsawg pixels thaj chaw ua si txav ntawm ib lub sijhawm

uint8_t drawSpeed = 4;

// vars rau tus yeeb ncuab0

uint8_t enemy0PosX = 255; uint8_t yeeb ncuab0PosY = 0; uint8_t enemy1PosX = 255; uint8_t enemy1PosY = 0; uint8_t yeeb ncuab2PosX = 255; uint8_t yeeb ncuab2PosY = 0;

// varialble rau random muab tus lej xov tooj rau teeb meem

uint8_t laneGen = 0;

uint8_t laneGen0 = 0; uint8_t laneGen1 = 0; uint8_t laneGen2 = 0;

// tus lej suav

qhab nia ntev = 0; // qhov no yog tus qhab nia:/ lol ntev sib piv = 0; // qhov no khaws cov qhab nia ntawm qib kawg nce los sib piv rov mus rau highScore = 25; uint8_t metreCnt = 0;

qhov no yog qhov uas peb pib ua haujlwm

// qhov no yog txheej ntawm cov lus txib yog tias kev cuam tshuam tau qhib ua haujlwm tsis muaj cuam tshuam () {ncua sijhawm (150); updateDirection (); } // hloov kho tus nqi twg yog nyob rau hauv kev coj ua var los ntawm kev tshuaj xyuas DIR bools // -------------------------- Hloov kho qhov tseeb (player)- ------------------------- void updateDirection () {//Serial.println("updateDirection Called "); TABSIS = digitalRead (UPPIN); BUTDWN = digitalRead (DWNPIN); BUTLFT = digitalRead (LFTPIN); BUTRHT = digitalRead (RHTPIN); yog (BUTUP == muaj tseeb) {dirPressed = DIRUP; } yog (BUTDWN == muaj tseeb) {dirPressed = DIRDOWN; } yog (BUTLFT == muaj tseeb) {dirPressed = DIRLEFT; } yog (BUTRHT == muaj tseeb) {dirPressed = DIRRIGHT; }

}

// ------------------------------- CAR CAR --------------- ------------------------------- ua tsaug

// qhov no yuav hloov kho lub vijtsam lub tsheb khiav

tsis muaj dabtsis moveCar ()

{hloov (dirPressed) {rooj plaub DIRUP: carPosYCnt--; carPosY [carPosYCnt]; suab (SND, 100, 100); yog (carPosYCnt == 255) {carPosYCnt = 0; } carYTmp = carPosY [carPosYCnt]; dirPressed = 0; // Serial.println ("carPosY nce"); // Serial.println (carPosYCnt); tawg; rooj DIRDOWN: carPosYCnt ++; suab (SND, 100, 100); yog (carPosYCnt == 4) {carPosYCnt = 3; } // Serial.println ("carPosY"); // Serial.println (carPosYCnt); carYTmp = carPosY [carPosYCnt]; dirPressed = 0; tawg; // tawm tswv yim tawm tsheb tuaj yeem txav sab laug thiab sab xis nrhiav kev sib tsoo tsis zoo li ntawd /* rooj plaub DIRLEFT: carPosX--; yog (carPosX == 0) {carPosX = 1; } // Serial.println ("carPosX"); // Serial.println (carPosX); dirPressed = 0; tawg; */ rooj plaub DIRRIGHT: // tsuas yog kev lom zem yog tias koj nias txoj cai qhov kev ua si yuav ua suab nrov (SND, 100, 50); // carPosX ++; // yog (carPosX == 128) // {// carPosX = 127; //} // Serial.println ("carPosX"); // Serial.println (carPosX); // dirPressed = 0; tawg; } updateDisplay (); }

// -------------------------- RANDOM POS X ---------------------- ---------------

uint8_t randomPosX () // 2 txoj haujlwm no tsuas yog tsim txoj haujlwm random rau qhov teeb meem

{uint8_t posValTmp = 0; posValTmp = random (129, 230); //Serial.println("random x "); //Serial.println(posValTmp); rov qab (posValTmp); }

// --------------------------- RANDOM POS Y ------------------ ---------------------- ua tsaug

uint8_t randomPosY ()

{uint8_t laneVal = 0; laneVal = random (0, 4); // ntxiv txoj kab ntxiv rau kev tsis pom zoo piv txwv li tsis muaj cov khoom ntawm lub vijtsam thaum nyob hauv txoj kab ntawd //Serial.println("RandomY "); //Serial.println(lanePosArr[laneVal]); rov qab (lanePosArr [laneVal]); }// ------------------------------- SET GAME SPEED -------------- -------------- tsis muaj teeb meem setGameSpeed () // qhov no nres qib mus siab dua 20 ua game tsis tuaj yeem ua si {yog (drawSpeed <21) {drawSpeed = drawSpeed+2; }}// ------------------------------------ DETECT CRASH --------------- ----------------------- void detectCrash () {

yog (enemy0PosX = 0 && enemy0PosY == carYTmp)

{// Serial.println ("Game Over CRAASSSSHHHHHHHEEEEDDD into Traffic 0"); gameOver (); } yog (enemy1PosX = 0 && enemy1PosY == carYTmp) {//Serial.println("Game Over CRAASSSSHHHHHHHHEEEEDDD rau hauv tsheb 1 "); gameOver (); } yog (yeeb ncuab2PosX = 0 && enemy2PosY == carYTmp) {//Serial.println("Game Over CRAASSSSHHHHHHHHEEEEDDD mus rau hauv tsheb 2 "); gameOver (); }}

cov no yog cov txheej txheem uas kos cov zaub.

// ------------------------------- DRAW ROAD --------------- --------------------- void drawRoad () // X, Y, ntev, dav {display.fillRect (roadLineX1, 15, 30, 4, WHITE); display.fillRect (roadLineX1, 30, 30, 4, DAWB); display.fillRect (roadLineX1, 45, 30, 4, DAWB); display.fillRect (roadLineX2, 15, 30, 4, DAWB); display.fillRect (roadLineX2, 30, 30, 4, DAWB); display.fillRect (roadLineX2, 45, 30, 4, DAWB); display.fillRect (roadLineX3, 15, 30, 4, DAWB); display.fillRect (roadLineX3, 30, 30, 4, DAWB); display.fillRect (roadLineX3, 45, 30, 4, DAWB); display.fillRect (roadLineX4, 15, 30, 4, DAWB); display.fillRect (roadLineX4, 30, 30, 4, DAWB); display.fillRect (roadLineX4, 45, 30, 4, DAWB); display.fillRect (roadLineX5, 15, 30, 4, DAWB); display.fillRect (roadLineX5, 30, 30, 4, DAWB); display.fillRect (roadLineX5, 45, 30, 4, DAWB);

roadLineX1 = txoj kevLineX1-drawSpeed;

roadLineX2 = txoj kevLineX2-drawSpeed; roadLineX3 = txoj kevLineX3-drawSpeed; roadLineX4 = txoj kevLineX4-drawSpeed; roadLineX5 = txoj kevLineX5-drawSpeed; display.display (); } // ----------------------------------------- DRAW enemys ---- --------------------------------------- void enemysDraw () {// X, Y, bmp lub npe, dav, qhov siab, xim tso saib.drawBitmap (enemy0PosX, enemy0PosY, ENEMY0, ENEMY0_WIDTH, ENEMY0_HEIGHT, 1); enemy0PosX = enemy0PosX-drawSpeed; display.drawBitmap (enemy1PosX, enemy1PosY, ENEMY1, ENEMY1_WIDTH, ENEMY1_HEIGHT, 1); enemy1PosX = enemy1PosX-drawSpeed; display.drawBitmap (enemy2PosX, enemy2PosY, ENEMY2, ENEMY2_WIDTH, ENEMY2_HEIGHT, 1); enemy2PosX = enemy2PosX-drawSpeed; display.display (); yog (enemy0PosX> 231 && enemy0PosX231 && enemy1PosX <255) {enemy1PosX = randomPosX (); enemy1PosY = randomPosY (); checkDuplicate (); }

yog (enemy2PosX> 231 && enemy2PosX <255) {enemy2PosX = randomPosX (); enemy2PosY = randomPosY (); }} // ------------------------------------ Hloov kho dua tshiab -------- ---------------------------------------- void updateDisplay () {display.clearDisplay (); display.drawBitmap (carPosX, carPosY [carPosYCnt], CARSPRITE, 30, 15, 1); display.fillRect (100, 0, 28, 10, BLACK); display.setCursor (100, 0); display.setTextColor (WHITE, BLACK); display.println (qhab nia); display.display ();

}

// ------------------------- tos nias lub voj ------------------- ------

// qhov no yog lub vijtsam hauv tsev tsis muaj dab tsi tosForPress () {splashScreen (); bool tos = 0; // loop xaus thaum qhov no yog qhov tseeb display.clearDisplay (); thaum (tos == 0) {

display.fillRect (19, 20, 90, 32, BLACK); // blank tom qab rau cov ntawv

display.setTextColor (WHITE); display.setCursor (23, 24); display.setTextSize (0); display.println ("Tsis Txaus Siab"); display.setCursor (36, 34); display.println ("Racer"); display.drawBitmap (74, 24, CARSPRITE, CARWIDTH, CARHEIGHT, 1); // x y w h r col display.drawRoundRect (21, 21, 86, 23, 4, WHITE); // ciam Snake display.drawRect (19, 20, 90, 33, WHITE); // ciam teb lub thawv - 3 display.setCursor (25, 43); display.setTextSize (0); // font rov mus rau qhov qub display.println ("nias ib qho yuam sij"); display.fillRect (0, 0, 127, 8, BLACK); display.setCursor (10, 0); display.print ("Cov qhab nia siab:"); // tso saib cov qhab nias siab display.print (highScore); display.display (); tos = digitalRead (INTPIN); // txheeb xyuas seb puas tseem ceeb nias tos yuav hloov mus rau 1 xaus thaum dirPressed = 0; // rov pib khawm nias kom tsis muaj kev taw qhia}} // -------------------------------------- ----- Hloov kho cov game ----------------------------------------- tsis muaj qhov hloov tshiab game () {moveCar (); drawRoad (); enemysDraw (); // enemy1Draw (); // yeeb ncuab2Draw (); metreCnt ++; ntes tsoo (); yog (metreCnt == 5) // ntxiv cov ntsiab lus rau txhua 10 cycles los teev cov qhab nia {metreCnt = 0; qhab nia ++; } yog (qhab nia == sib piv+5) // nce qhov kev ua si txhua 5 cov ntsiab lus mus rau qhov siab tshaj 20 nrawm {sib piv = qhab nia; setGameSpeed (); } noTone (SND); updateDisplay ();

}

// ------------------------------ GAME OVER ---------------- ---------------------- -----------------------

// qhov kev niaj hnub kos kab nyob ib puag ncig lub tsheb tuag hero ces qhia qhov kev ua si hla lub vijtsam

tsis muaj gameOver ()

{suab (SND, 200, 200); // ua si suab uint8_t linePosX, linePosY, pixwidth, pixheight = 0; // teeb vars los kos lub thawv nyob ib ncig ntawm lub tsheb linePosX = carPosY; linePosY = carYTmp; pkt = 30; hli = 15; display.drawRect (linePosX, linePosY, Pixwidth, Pixheight, WHITE); display.display (); rau (int i = 0; i <= 26; i ++) // qhov no puag ncig lub tsheb hauv cov duab plaub simulating tawg {linePosX = linePosX-2; linePosY = linePosY-2; Pixwidth = Pixwidth+4; pixheight = hli+4; display.drawRect (linePosX, linePosY, Pixwidth, Pixheight, BLACK); display.drawRect (linePosX, linePosY, Pixwidth, Pixheight, WHITE); display.display (); suab (SND, i*20, 50); ncua (10); } display.setTextSize (2); display.setTextColor (WHITE, BLACK); display.setCursor (10, 23); suab (SND, 50, 500); display.print ("GAME"); display.display (); ncua (500); suab (SND, 40, 500); display.print ("QHOV"); display.setTextSize (0); display.display (); ncua (3000); restartGame (); tosForPress (); }

// ----------------------------------------- RESTART GAME ----- ----------------------------------------------------------------------- -----

void restartGame () // qhov no theej cov qhab nia siab thiab rov pib dua txhua qhov stats thiab tsim txoj haujlwm tsis raug

{yog (tau qhab nia> = siab dua) // kos saib seb puas tau qhab nia siab dua li cov qhab nia siab {highScore = qhab nia; // ib leeg yog statment los hloov kho cov qhab nia siab}

tau = 0;

drawSpeed = 4; metreCnt = 0; carPosYCnt = 0; enemy0PosX = randomPosX (); enemy0PosY = randomPosY (); enemy1PosX = randomPosX (); enemy1PosY = randomPosY (); enemy2PosX = randomPosX (); enemy2PosY = randomPosY (); noTone (SND);

checkDuplicate ();

}

// ------------------------------------------------------- -Txheeb xyuas ob daim ntawv ----------------------------------------------- ------ void checkDuplicate () // cov tshev no los saib seb puas muaj teeb meem nyob hauv tib qhov chaw ua si {// Serial.println ("theej theej tawm"); yog (enemy2PosX> 230 && enemy2PosX <255) {thaum (enemy2PosY == enemy1PosY || enemy2PosY == enemy0PosY) {enemy2PosY = randomPosY (); }}

yog (enemy0PosX> 230 && enemy0PosX230 && enemy2PosXenemy1PosX && enemy2PosX230 && enemy0PosXenemy1PosX && enemy0PosX

// ------------------------------------------- TSHIAB SCREEN --- ---------------------------------

tsis muaj dab tsi splashScreen ()

{display.clearDisplay (); display.drawBitmap (0, 0, CRASH, CRASHWIDTH, CRASHHEIGHT, 1); display.display (); ncua (2000); } // ------------------------------------------------------- Kev teeb tsa ---------------------------------------------------------------------- ----------- teeb tsa tsis muaj dab tsi () {ncua (100); // cia txhua yam pib // Serial.begin (9600); // tsis pom zoo qhov no thiab txhua Serial. cov lus txib rau kev ua txhaum daim duab display.begin (SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay (); display.setTextColor (WHITE, BLACK); display.setTextWrap (tsis tseeb); display.dim (0); pinMode (INTPIN, INPUT); pinMode (UPPIN, INPUT); pinMode (DWNPIN, INPUT); pinMode (LFTPIN, INPUT); pinMode (RHTPIN, INPUT);

attachInterrupt (digitalPinToInterrupt (INTPIN), cuam tshuam, RISING);

// Random tso qhov thaiv kev ua yeeb ncuab0PosX = randomPosX (); enemy0PosY = randomPosY (); enemy1PosX = randomPosX (); enemy1PosY = randomPosY (); enemy2PosX = randomPosX (); enemy2PosY = randomPosY (); checkDuplicate (); // txheeb xyuas qhov chaw sib txawv // Serial.println ("teeb tsa Ua tiav"); splashScreen (); tosForPress (); } // ------------------------------------------------------- ----- LOOP ------------------------------------------------------ ---------------

void lub voj ()

{updateGame (); }

thiab qhov ntawd zoo nkauj npaum li nws, txhua qhov kev hloov kho thiab kev tawm tswv yim yuav txais tos. Cov teeb meem peb xav tau los daws qhov teeb meem ntawm lub vijtsam peb yuav tsum tau saib xyuas yuav ua li cas thiaj li txo qis nws thiab cov yeeb ncuab lub tsheb tseem muaj peev xwm nyob hauv tib qhov chaw.

Pom zoo: