Orodha ya maudhui:

Matrix ya NeoPixels: Mchezo wa Nyoka: Hatua 4
Matrix ya NeoPixels: Mchezo wa Nyoka: Hatua 4

Video: Matrix ya NeoPixels: Mchezo wa Nyoka: Hatua 4

Video: Matrix ya NeoPixels: Mchezo wa Nyoka: Hatua 4
Video: Introducing NeoPixels by Adafruit! 2024, Novemba
Anonim
Matrix ya NeoPixels: Mchezo wa Nyoka
Matrix ya NeoPixels: Mchezo wa Nyoka

Je! Bado unakumbuka mchezo wa nyoka tuliocheza kwenye sanduku letu la mchezo au rununu wakati wa utoto wetu? Leo tutajifunza jinsi ya kutengeneza mchezo wa nyoka na Matrix 8 * 8 ya NeoPixels. Tunachagua Arduino uno kama kituo cha kudhibiti na moduli ya kuzuka kwa Joystick kudhibiti nyoka.

Hatua ya 1: Orodha ya Vipengele:

Vifaa:

1 X Freaduino UNO Rev2.2 MB_EFUNO

1 X Flexible 64 RGB LED 8 * 8 NeoPixels Matrix

1 X Moduli ya kuzuka kwa Joystick BKOUT_MD01

3 X Mstari wa Mwongozo

Programu:

Arduino IDE

Hatua ya 2: Uunganisho wa vifaa

Unganisha NeoPixels kubandika D2.

"X" kwenye moduli ya kuzuka kwa Joystick unganisha pini A0 kwenye bodi ya Arduino UNO, "Y" unganisha pini A1, "K" unganisha pini A2.

Hatua ya 3: Programu

P # ni pamoja na

#fafanua PIN 2 // Pembe ya Pato #fafanua MAX_LED 64 // Wingi wa shanga uint32_t c; nambari = 0, nambari; char huan isiyosainiwa = 0, ci = 0; char iliyosainiwa u = 40; // Ukubwa wa font wa kikundi cha maneno unsigned char x = 0; rangi isiyosajiliwa ya char [3]; char iliyosainiwa go = {2, 3, 4, 5, 9, 14, 17, 20, 22, 26, 27, 29, 34, 35, 36, 37, 41, 46, 49, 54, 58, 59, 60, 61}; // 24 taa ya char isiyosainiwa [8] [8] = {{0, 1, 2, 3, 4, 5, 6, 7}, {15, 14, 13, 12, 11, 10, 9, 8}, {16, 17, 18, 19, 20, 21, 22, 23}, {31, 30, 29, 28, 27, 26, 25, 24}, {32, 33, 34, 35, 36, 37, 38, 39}, {47, 46, 45, 44, 43, 42, 41, 40}, {48, 49, 50, 51, 52, 53, 54, 55}, {63, 62, 61, 60, 59, 58, 57, 56},}; tabia ya char isiyosainiwa [8] = // Weka neno litakalotumwa {{0, 1, 1, 1, 1, 1, 0, 0}, {0, 1, 0, 0, 0, 1, 0, 0}, // 0 {0, 1, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, // 1 {0, 1, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 1, 1, 0, 0}, {0, 1, 0, 1, 0, 1, 0, 0}, // 2 {0, 1, 1, 1, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 0, 1, 0, 0}, {0, 1, 0, 1, 0, 1, 0, 0}, // 3 {0, 1, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0}, // 4 {0, 1, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 0, 1, 0, 0}, {0, 1, 0, 1, 0, 1, 0, 0}, // 5 {0, 1, 0, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 1, 1, 0, 0}, {0, 1, 0, 1, 0, 1, 0, 0}, // 6 {0, 1, 0, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0}, // 7 {0, 1, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 1, 1, 0, 0}, {0, 1, 0, 1, 0, 1, 0, 0}, // 8 {0, 1, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 0, 1, 0, 0}, {0, 1, 0, 1, 0, 1, 0, 0}, // 9 {0, 1, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0 },}; char iliyosajiliwa inaonyesha [8] [8]; kasi ya intS = 400; // Kasi ya awali ya alama ya nyoka; // alama ya mchezo int xValue; // JoyStick-X int yValue; // JoyStick-Y int zValue; // JoyStick-Z int FX, FY; // Uratibu wa chakula int SX, SY; // Uratibu wa kichwa cha nyoka int KEY, K; int Imewezeshwa; // Wezesha kuanzisha tena mchezo char sx [64]; // Uratibu wa mwili wa nyoka char sy [64]; Ukanda wa Adafruit_NeoPixel = Adafruit_NeoPixel (MAX_LED, PIN, NEO_RGB + NEO_KHZ800); RANDOM tupu () {A: FX = nasibu (0, 7); FY = bila mpangilio (0, 7); kwa (int i = 0; i <3 + alama; i ++) // Kuzuia onyesho la chakula kwenye mwili wa nyoka {if (FX == sx && FY == sy ) goto A; }} batili myDelay (unsigned int Time) {// Wakati wa kuchelewa kwa harakati ya nyoka, lazima tufanye kitu kama glitter ya chakula na kusoma mwelekeo wa Joystick kwa (int t = 1; t 950 && KEY! = 5) { K = 8; } vingine ikiwa (yValue <50 && KEY! = 8) {K = 5; } vingine ikiwa (xValue 950 && KEY! = 4) {K = 6; }} // Hapa unaweza kulinganisha ikiwa mwelekeo wa kitufe ni kinyume na mwelekeo wa hoja ya nyoka. Ikiwa ni kinyume, basi toa keypad. / ************************************************* ************************************************** ************************************************** **************************************************. thamani ya char isiyosainiwa, y; Rangi [0] = nasibu (3, 18); // Weka rangi. Kigezo ni R G B, masafa 0-255. Rangi [1] = bila mpangilio (3, 18); Rangi [2] = nasibu (3, 18); c = ukanda. Rangi (Rangi [1], Rangi [0], Rangi [2]); thamani = alama / 10; thamani = thamani * 4; y = 0; kwa (idadi = thamani; nambari <thamani + 4; nambari ++) {kwa (unsigned char wima = 0; wima <8; wima ++) {showscore [y] [wima] = tabia [idadi] [wima]; } y ++; } thamani = alama% 10; thamani = thamani * 4; kwa (nambari = thamani; nambari <thamani + 4; nambari ++) {kwa (unsigned char wima = 0; wima <8; wima ++) {showscore [y] [wima] = tabia [idadi] [wima]; } y ++; } kwa imewashwa. {strip.setPixelColor (mwanga [usawa] [wima], c); } mwingine strip.setPixelColor (mwanga [usawa] [wima], 0); }} strip.show (); // Tuma ucheleweshaji wa data (5000); K = 0; // Rudisha mwelekeo wa Joystick sx [0] = 2; sx [1] = 1; sx [2] = 0; sy [0] = 1; sy [1] = 1; sy [2] = 1; // Rudisha alama ya uratibu wa nyoka = 0; // Rudisha kasi ya alama ya mchezoS = 400; // Kasi ya awali ya nyoka} kuanzisha batili () {Serial.begin (9600); // Anzisha ukanda wa maktaba. Anza (); // Tuma data. Default rangi ya kila nukta kama 0. Kwa hivyo kila nukta haiangazwi mwanzoni. onyesha (); K = 0; // Rudisha mwelekeo wa Joystick sx [0] = 2; sx [1] = 1; sx [2] = 0; sy [0] = 1; sy [1] = 1; sy [2] = 1; // Rudisha alama ya uratibu wa nyoka = 0; // Rudisha alama ya mchezo Rangi [0] = bila mpangilio (3, 18); // Weka rangi. Kigezo ni R G B, masafa 0-255. Rangi [1] = bila mpangilio (3, 18); Rangi [2] = nasibu (3, 18); c = ukanda. Rangi (Rangi [1], Rangi [0], Rangi [2]); // Rangi ya Bluu Nyekundu // Weka rangi ya (nambari = 0; nambari <24; nambari ++) {strip.setPixelColor (nenda [nambari], c); } strip.show (); // Tuma ucheleweshaji wa data (2000); RANDOM (); // Tengeneza chakula} batili ya rununu () {KEY = K; // Kila harakati ya nyoka hufanya mabadiliko ya mwelekeo mara moja. ikiwa (KEY == 8) // Nyoka kuelekea juu {kwa (int i = 2 + alama; i> 0; i--) {sx = sx [i - 1]; sy = sy [i - 1]; } sy [0] = sy [0] - 1; ikiwa (sy [0] <0) // Nenda zaidi ya mpaka na uendelee upande wa pili sy [0] = 7; } vingine ikiwa (KEY == 5) // Nyoka kushuka chini {kwa (int i = 2 + alama; i> 0; i--) {sx = sx [i - 1]; sy = sy [i - 1]; } sy [0] = sy [0] + 1; ikiwa (sy [0]> 7) // Nenda zaidi ya mpaka na uendelee upande wa pili sy [0] = 0; } mwingine ikiwa (KEY == 4) // Nyoka kushoto harakati {for (int i = 2 + alama; i> 0; i--) {sx = sx [i - 1]; sy = sy [i - 1]; } sx [0] = sx [0] - 1; ikiwa (sx [0] <0) // Nenda zaidi ya mpaka na uendelee upande wa pili sx [0] = 7; } kingine ikiwa (KEY == 6) // Nyoka harakati ya kulia {kwa (int i = 2 + alama; i> 0; i--) {sx = sx [i - 1]; sy = sy [i - 1]; } sx [0] = sx [0] + 1; ikiwa (sx [0]> 7) // Nenda zaidi ya mpaka na uendelee upande mwingine sx [0] = 0; } // Songa mwili wa nyoka na kitufe} onyesho batili () {kwa (nambari = 0; nambari <64; nambari ++) // Futa skrini {strip.setPixelColor (namba, 0); } onyesha (); Rangi [0] = 40; // Weka rangi. Kigezo ni R G B, masafa 0-255. Rangi [1] = 0; Rangi [2] = 0; c = ukanda. Rangi (Rangi [1], Rangi [0], Rangi [2]); // Bluu Nyekundu Nyekundu // Weka rangi x = mwanga [FX] [FY]; // Onyesha ukanda wa chakula.setPixelColor (x, c); Rangi [0] = nasibu (3, 18); // Weka rangi. Kigezo ni R G B, masafa 0-255. Rangi [1] = bila mpangilio (3, 18); Rangi [2] = nasibu (3, 18); c = ukanda. Rangi (Rangi [1], Rangi [0], Rangi [2]); // Rangi ya Bluu Nyekundu // Weka rangi ya (int i = 2 + alama; i> 0; i--) // Onyesha mwili wa nyoka {x = mwanga [sx [sy ; strip.setPixelColor (x, c); } Rangi [0] = 0; // Weka rangi. Kigezo ni R G B, masafa 0-255. Rangi [1] = 40; Rangi [2] = 0; c = ukanda. Rangi (Rangi [1], Rangi [0], Rangi [2]); // Bluu Nyekundu Nyekundu // Weka rangi x = mwanga [sx [0] [sy [0]; // Onyesha ukanda wa kichwa cha nyoka.setPixelColor (x, c); strip.show (); // Tuma data} batili kitanzi () {mobile (); myDelay (kasiS); // Ndani ya () ni wakati wa kuchelewa. maonyesho (); SX = sx [0]; SY = sy [0]; // Pata uratibu wa kichwa cha nyoka kwa (int i = 1; i <= 2 + alama; i ++) {if (SX == sx && SY == sy ) gameover (); } // Hukumu ikiwa kichwa cha nyoka kiligusa mwili wake. Ikiwa imeguswa, basi songa mbele kwa GameOver. ikiwa (SY == FY && SX == FX) {RANDOM (); alama ++; Rangi [0] = 40; // Weka rangi. Kigezo ni R G B, masafa 0-255. Rangi [1] = 0; Rangi [2] = 0; c = ukanda. Rangi (Rangi [1], Rangi [0], Rangi [2]); // Bluu Nyekundu Nyekundu // Weka rangi x = mwanga [FX] [FY]; // Onyesha ukanda wa chakula.setPixelColor (x, c); strip.show (); // Tuma data ikiwa (! (alama% 5)) {// Kuharakisha kasi ya nyoka kulingana na alama. Kila chakula 5 kinacholiwa, ongeza 100ms. kasiS = kasiS - 50; ikiwa (kasiS <150) // Kikomo cha chini ni 200ms. Ikiwa kasi iko chini ya 200ms, kasi inakaa 200ms. kasiS = 150; }} // Hukumu ikiwa chakula kinaliwa. Ikiwa nyoka alikula chakula, basi pamoja na alama na kuzaliwa upya chakula bila mpangilio. }

Matokeo ya Jaribio

Hatua ya 4: Matokeo ya Jaribio

Masomo ya Jamaa:

Gari la Bluetooth la FreaksBot

Washa Shanga la Kwanza kwenye Matrix ya NeoPixels ya 8 * 8 na Arduino

Jaribio ndogo: kidogo 05: RGB LED - Elecfreaks Mirco: Kozi ya Starter Kit

Ijumaa Bidhaa Posta: Kitanda cha Kuonyesha Soldering ya Muziki wa LED ya Frequency

Tumia ElecFreaks Micro: Kit Tinker Kit kutengeneza Taa mahiri

Ilipendekeza: