Cov txheej txheem:

Arduino OLED Snake Game: 3 Kauj Ruam
Arduino OLED Snake Game: 3 Kauj Ruam

Video: Arduino OLED Snake Game: 3 Kauj Ruam

Video: Arduino OLED Snake Game: 3 Kauj Ruam
Video: Arduino Snake Game on an OLED display. 2024, Hlis ntuj nqeg
Anonim
Arduino OLED Snake Game
Arduino OLED Snake Game

Nyob zoo thiab txais tos, rau peb cov lus qhia yuav ua li cas thiab arduino OLED Game, txoj haujlwm no tau los txog thaum peb tau sim ua peb thawj qhov kev ua si nrog arduino, soooo, peb xav tias qhov twg zoo dua los pib dua nokia classic Snake (zoo ntawm tsawg kawg yog tus nab clone:)).

Koj Yuav Tsum Tau Dab Tsi

Khoom siv

Arduino UNO lossis clone

OLED Zaub

4 Cov diode

500-1k resistor

4 cov nyees khawm

Passive Piezo buzzer

yeem

Solderless BreadBoard

Thov nco ntsoov cov kev txuas no yog piv txwv nkaus xwb

Kauj ruam 1: Circuit Court

Qhov Circuit
Qhov Circuit

Hauv daim duab saum toj no koj tuaj yeem pom peb lub voj voog, peb siv tus pin d3 ntawm arduino ua qhov cuam tshuam thov tus pin kom cov arduino yuav muab qhov tseem ceeb rau kev nyeem cov khoom siv uas yog d4 d5 d6 d7. Cov hauv paus ntawm Circuit yog tias lub khawm taw qhia tau nyem uas mus rau siab 5v qhov no ua rau cuam tshuam cuam tshuam thov tus pin (d3 ntshav xaim) thiab tus pin coj mus, qhov cuam tshuam kev ua haujlwm hu ua hloov tshiab kev ua haujlwm thiab tus lej ntawd txav tus nab raws. Tus pin 9 tau siv ua tus pin zoo li nws yog PWM (~ mem tes dav tes taws los piav) uas txuas ncaj qha rau 5v piezo ntawm + tus pin thiab - rov qab mus rau 0v/hauv av.

(FYI ntawm arduino uno thiab clones tsuas yog d2 thiab d3 tuaj yeem ua raws li cuam tshuam thov pins).

Kev taw qhia pins:

d4 nce ORANGE

d5 Down PINK

d6 Sab laug BLUE

d7 Txoj cai xim av

d9 suab zoo

Txhua lub pob muaj 5v kev txuas txuas thiab cov zis uas txuas nrog ua ntej rau lawv cov lej digital ntawm arduino, tib qhov txiaj ntsig ntawm txhua lub pob yog tom qab txuas nrog nws tus kheej diode, peb siv lub diodes kom nres qhov hluav taws xob rov qab mus rau lwm cov nyees khawm thiab qhib lawv. Ntawm cov cathode (-) kawg ntawm tag nrho 4 diodes peb koom ua ke los tsim cov khoom sib txuas uas txuas rau d3 thiab tom qab ntawd dhau los ntawm tus tiv thaiv mus rau 0v/lub ntiaj teb los rub cov arduino pins qis kom tsis txhob tawm ntawm cov pins ntab thaum tsis yog qhib

(FYI tus pin ntab tuaj yeem tau txais phantom voltage thiab ua rau tus cwj pwm txawv txawv)

2 analog pins tau siv los tsav cov zaub, cov no yog cov khoom kho vajtse arduino i2c pins.

A5 txuas nrog SCL YELLOW

A4 txuas nrog SDA GREEN

Qhov +5v thiab 0v (lub ntiaj teb) tso tawm los ntawm arduino tau siv los ua lub hauv paus ntawm lub zog rau tag nrho cov hluav taws xob uas tuaj yeem siv tau los ntawm usb lossis them lub xov tooj.

Kauj ruam 2: Txoj Cai

// ------------------------ ANJAWARE SNAKE Games Nrog kev pab los ntawm cov neeg net --------------- -------

#suav nrog

#include // https://github.com/adafruit/Adafruit-GFX-Library #include // https://github.com/adafruit/Adafruit-GFX-Library // zaub teeb (dav, siab) Adafruit_SSD1306 zaub (128, 64); // txhais tus lej nkag cov no yog cov pins ntawm arduino lawv tsis hloov pauv li #define #define 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 # txhais DIRLEFT 3 #define DIRRIGHT 4

// teeb khawm hloov pauv

// volitile cos peb xav tau nws hloov kho nrog kev cuam tshuam yog li tuaj yeem yog ib qho me ntsis ntawm cov voj voog

// yeej tsis siab dua 4 yog li tsuas yog xav tau 8bit int kom txuag tau cov peev txheej hloov pauv uint8_t khawm = 0; bool tab = 0; bool tab = 0; // peb siv qhov no los teeb tsa qhov tseeb kom "txheeb xyuas" qhov kev taw qhia nias bool butleft = 0; bool butright = 0;

// nab ints

byte snakePosX [30]; // array ua lub cev ntawm nab byte snakePosY [30];

int nabX = 30; // nab lub taub hau txoj haujlwm

int nabY = 30; int nabSize = 1; // nab loj suav suav rau qhov loj ntawm cov kab

// ntiaj teb ints

uint8_t worldMinX = 0; // cov no teev cov kev txwv ntawm thaj chaw ua si

uint8_t worldMaxX = 128; uint8_t worldMinY = 10; uint8_t worldMaxY = 63;

// sau scran (zaub mov) thiab txoj haujlwm ntawm scran

bool scranAte = 0; uint8_t scranPosX = 0; uint8_t scranPosY = 0;

// qhab nia sib txawv

ntev playscore = 0; highscore ntev = 30; // teeb tsa cov qhab nia siab rau 3 sau ua qhov pib

// --------------------------- qhov no yog qhov kev cuam tshuam cuam tshuam ntawm kev nce hluav taws xob ------------ ---------------

tsis muaj dabtsis cuam tshuam () {ncua sijhawm (150); // qeeb me ntsis ntxiv rau "thaws" tiv thaiv kev hloov kho tshiab (); } // ------------------ hloov kho tus nqi taw qhia los ntawm khawm nias ----------------- tsis muaj qhov hloov kho tshiab () { // Serial.println ("kev hloov kho tshiab"); butup = digitalRead (UPPIN); // txheeb xyuas cov tswv yim twg tau nce siab thiab teeb tsa cov ntsiab lus tseem ceeb bool tseeb butdown = digitalRead (DWNPIN); butleft = digitalRead (LFTPIN); butright = digitalRead (RHTPIN); // cov no yog cov neeg saib xyuas saib qhov twg nkag mus siab thiab nkag mus rau tus nqi cuam tshuam hauv "khawm" // sib txawv, qhov sib txawv no hais qhia qhov kev taw qhia ntawm kev txav mus yog (tab sis == muaj tseeb) {buttonpressed = DIRUP; // Serial.println ("UP nias"); // Serial.println (khawm); butup = tsis tseeb; suab (SND, 1500, 10); } yog (butdown == muaj tseeb) {khawm = DIRDOWN; // Serial.println ("DOWN nias"); // Serial.println (khawm); butdown = tsis tseeb; suab (SND, 1500, 10); }

yog tias (butleft == muaj tseeb)

{buttonpressed = DIRLEFT; // Serial.println ("LEFT nias"); // Serial.println (khawm); butleft = tsis tseeb; suab (SND, 1500, 10); } yog (tab sis == muaj tseeb) {khawm = DIRRIGHT; // Serial.println ("RIGHT pressed"); // Serial.println (khawm); butright = tsis tseeb; suab (SND, 1500, 10); }}

// -------------------------- kos cov duab niaj hnub ------------------ -----------------

void updateDisplay () // kos cov qhab nia thiab cov qauv

{// Serial.println ("Hloov Kho Zaub");

display.fillRect (0, 0, display.width ()-1, 8, BLACK);

display.setTextSize (0); display.setTextColor (WHITE); // kos cov qhab nia display.setCursor (2, 1); display.print ("Cov qhab nia:"); display.print (Txoj hlua (playscore, DEC)); display.setCursor (66, 1); display.print ("Siab:"); display.print (Txoj hlua (qhab nia siab, DEC)); // kos thaj chaw ua si // pos 1x, 1y, 2x, 2y, xim tso tawm.drawLine (0, 0, 127, 0, WHITE); // heev ciam teb sab saum toj display.drawLine (63, 0, 63, 9, DAWB); // qhab nia sib cais display.drawLine (0, 9, 127, 9, WHITE); // hauv qab cov ntawv ciam teb display.drawLine (0, 63, 127, 63, WHITE); // hauv qab ciam teb tso tawm.drawLine (0, 0, 0, 63, WHITE); // sab laug ciam teb display.drawLine (127, 0, 127, 63, WHITE); // txoj cai ciam teb

}

// ----------------------------------- hloov chaw ua si ---------- ----------------------- Ua Tsaug Ntau

tsis muaj dabtsis updateGame () // qhov no hloov kho thaj chaw ua si

{display.clearDisplay ();

display.drawPixel (scranPosX, scranPosY, WHITE);

scranAte = scranFood ();

// txheeb xyuas cov txheej txheem nab

yog (outOfArea () || selfCollision ())

{gameOver (); }

// tso saib nab

rau (int i = 0; i0; i--) {snakePosX = snakePosX [i-1]; snakePosY = snakePosY [i-1]; } // ntxiv pixel ntxiv rau tus nab yog (scranAte) {snakeSize+= 1; snakePosX [snakeSize-1] = snakeX; snakePosY [snakeSize-1] = snakeY; }

hloov (khawm khawm) // yog nabDirection

{case DIRUP: snakeY- = 1; tawg; case DIRDOWN: snakeY+= 1; tawg; case DIRLEFT: snakeX- = 1; tawg; rooj plaub DIRRIGHT: snakeX+= 1; tawg; } snakePosX [0] = nabX; snakePosY [0] = snakeY; updateDisplay (); display.display (); // --------------------- tso lub suab --------------------------

tsis muaj qhov chawScran ()

{scranPosX = random (worldMinX+1, worldMaxX-1); scranPosY = random (worldMinY+1, worldMaxY-1); } // ------------------------ SCRAN ATE POINT UP ---------------- bool scranFood () {yog (snakeX == scranPosX && snakeY == scranPosY) {playscore = playscore+10; suab (SND, 2000, 10); updateDisplay (); qhov chawScran (); rov 1; } lwm {xa rov qab 0; }} // --------------------- tawm ntawm thaj chaw ---------------------- bool outOfArea () {return snakeX = worldMaxX || snakeY = worldMaxY; } // ---------------------- game dhau ----------------------- --- void gameOver () {uint8_t rectX1, rectY1, rectX2, rectY2; rect1 = 38; rect1 = 28; rectX2 = 58; rectY2 = 12; display.clearDisplay (); display.setCursor (40, 30); display.setTextSize (1); suab (SND, 2000, 50); display.print ("GAME"); suab (SND, 1000, 50); display.print ("QHOV"); yog (playscore> = highscore) // txheeb xyuas seb puas tau qhab nia siab dua li cov qhab nia siab {highscore = playscore; // ib leeg yog hais txog qhov hloov kho cov qhab nia siab} rau (int i = 0; i <= 16; i ++) // qhov no yog txhawm rau kho cov teeb tsa ib puag ncig kev ua si dhau {display.drawRect (rectX1, rectY1, rectX2, rectY2, WHITE); Serial.println ("yog lub voj"); display.display (); rectX1- = 2; // hloov dua los ntawm 2 pixels rectY1- = 2; rectX2+= 4; // hloov dua 2 pixels los ntawm qhov kawg rectY2+= 4; suab (SND, i*200, 3); } display.display (); // Screen So tom qab lub koob meej dhau rectX1 = 0; // teeb tsa txoj haujlwm pib ntawm kab rectY1 = 0; rectX2 = 0; rectY2 = 63; rau (int kuv = 0; kuv <= 127; kuv ++) {uint8_t cnt = 0; display.drawLine (rectX1, rectY1, rectX2, rectY2, BLACK); rectX1 ++; rectX2 ++; display.display (); } display.clearDisplay (); playscore = 0; // rov pib nab thiab cov ntsiab lus neeg ua si snakeSize = 1; snakeX = display.width ()/2; nabY = display.height ()/2; tosForPress (); // tos rau tus neeg ua si pib game} // ------------------------- tos rau nias lub voj ---------- --------------- tsis muaj dab tsi waitForPress () {bool tos = 0; // loop xaus whjen qhov no yog qhov tseeb display.clearDisplay (); thaum (tos == 0) {drawALineForMe (WHITE); // kos kab kab dawb uas tsis raug kos drawALineForMe (BLACK); // kos kab kab dub dub kom lub vijtsam tsis tiav cov duab dawb.fillRect (19, 20, 90, 32, BLACK); // khoob tom qab rau cov ntawv nyeem display.setTextColor (WHITE); display.setCursor (35, 25); display.setTextSize (2); // loj dua font display.println ("SNAKE"); // x y w h r col display.drawRoundRect (33, 22, 62, 20, 4, DAWB); // ciam Snake display.drawRect (19, 20, 90, 32, DAWB); // ciam teb lub thawv - 3 display.setCursor (28, 42); display.setTextSize (0); // font rov mus rau qhov qub display.println ("nias ib qho yuam sij"); display.display (); tos = digitalRead (INTPIN); // txheeb xyuas seb puas tseem ceeb nias tos yuav hloov mus rau 1 qhov xaus thaum khawm khawm = 0; // pib dua khawm nias}} // -------------------- Kos ib kab ntawv nkag xim xim uint8_t -------------- ----- void drawALineForMe (uint8_t clr) {uint8_t line1X, line1Y, line2X, line2Y = 0; // teeb tsa kev sib koom ua ke tsis raug rau kab ces kos nws // sib txawv tsis muaj tsawg dua tsis muaj kab1X = random (worldMinX+1, worldMaxX-1); line1Y = random (worldMinY+1, worldMaxY-1); line2X = random (worldMinX+1, worldMaxX-1); line2Y = random (worldMinY+1, worldMaxY-1); display.drawLine (kab 1X, kab 1Y, kab 2X, kab 2Y, clr); } // ------------------------------------- kev sib tsoo sib tsoo -------- ---------------------------- Ua tsaug

rau (byte i = 4; i <snakeSize; i ++) {if (snakeX == snakePosX && snakeY == snakePosy ) {return 1; suab (SND, 2000, 20); suab (SND, 1000, 20); } rov qab 0; }

// -------------------------------- SETUP --------------- ---------------------- -----------------------

void teeb () {ncua (100); // tsuas yog muab sijhawm rau "khau raj" // Serial.begin (9600); // uncheck qhov no yog tias koj xav pom cov ntawv tso tawm tso tawm display.begin (SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay (); // pib nrog cov zaub huv huv display.setTextColor (WHITE); // teeb tsa cov ntawv xim sib hloov me me thiab lwm yam display.setRotation (0); display.setTextWrap (tsis tseeb); display.dim (0); // teeb tsa cov zaub brighness pinMode (INTPIN, INPUT); // teeb tsa cov chaw nres nkoj kom raug rau kev nkag mus pinMode (UPPIN, INPUT); pinMode (DWNPIN, INPUT); pinMode (LFTPIN, INPUT); pinMode (RHTPIN, INPUT); // qhov no yog cov lus txib cuam tshuam qhov no "nres" arduino los nyeem cov ntawv nkag // hais kom ua-ua haujlwm-tus lej-ua haujlwm kom ua tiav-mob ntawm tus pin txuasInterrupt (digitalPinToInterrupt (INTPIN), cuam tshuam, RISING); // Serial.println ("Teeb Dhau"); tosForPress (); // tso saib tus nab pib qhov screen screenScran (); // tso thawj qhov khoom noj} // --------------------- MAIN LOOP ----------------- ---------------------- tsis muaj dab tsi voj () {updateGame (); // txoj haujlwm no yog dab tsi nqa tus lej tseem ceeb}

Kauj ruam 3:

Pom zoo: