Untitled
unknown
plain_text
2 years ago
4.7 kB
3
Indexable
#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); const int BUTTON_PIN_ID = 8; const int CACTUSES_COUNT = 3; const int CACTUSES_SKIN = 4; const int TREASURES_COUNT = 3; //Bitmapa dinozaura byte dinosaur [8] { B00000, B00111, B00101, B10111, B11100, B11111, B01101, B01100, }; //bitmapa skarbów byte treasures [TREASURES_COUNT][8] { { B00000, B01110, B10001, B10001, B10001, B01110, B00000, B00000, }, { B11111, B11111, B11111, B01110, B00100, B00100, B01110, B11111, }, { B01110, B10001, B10001, B01010, B01010, B00100, B00000, B00000, }, }; //Bitmapa kaktusów byte cactus [CACTUSES_SKIN][8] { { B00011, B11011, B11011, B11011, B11011, B11111, B01110, B01110 }, { B11000, B11011, B11011, B11011, B11011, B11111, B01110, B01110 }, { B00001, B11011, B11011, B11011, B11011, B11111, B01110, B01110 }, { B10000, B11011, B11011, B11011, B11011, B11111, B01110, B01110 }, }; //entity id const int ENTITY_DINOSAUR_ID = 0; const int ENTITY_TREASURES_ID = 1; const int ENTITY_CACTUS_ID = 4; //cactuses position int cactuses_pos[CACTUSES_COUNT] {16, 0, 0}; int cactuses_skin[CACTUSES_COUNT] {0, 0, 0}; int treasures_pos = 0; int treasures_type = 0; //game states int score = 0; boolean isDinosaurOnGround = true; void setup() { lcd.begin(16, 2); lcd.createChar(ENTITY_DINOSAUR_ID, dinosaur); for (int i = 0; i < CACTUSES_SKIN; ++i) { lcd.createChar(ENTITY_CACTUS_ID+i, cactus[i]); } for (int i = 0; i < TREASURES_COUNT; ++i) { lcd.createChar(ENTITY_TREASURES_ID+i, treasures[i]); } Serial.begin(9600); pinMode(BUTTON_PIN_ID, INPUT_PULLUP); clear(); } void clear() { lcd.clear(); score = 0; treasures_pos = random(24, 36); treasures_type = random(0, TREASURES_COUNT); cactuses_pos[0] = 16; for (int i = 1; i < CACTUSES_COUNT; ++i) { cactuses_skin[i] = random(0, CACTUSES_SKIN); cactuses_pos[i] = cactuses_pos[i-1] + random(3, 8); } } void loop() { showTreasures(); showCactuses(); showDino(); if (isDinosaurOnGround) { for (int i = 0; i < CACTUSES_COUNT; ++i) { if (cactuses_pos[i] == 1) { endGameInfo(); clear(); break; } } } else { if (treasures_pos == 1) { score += 2 * (treasures_type+1); treasures_pos = random(16, 28); treasures_type = random(0, TREASURES_COUNT); } } score++; lcd.setCursor(12, 0); lcd.print(score); delay(500); } void endGameInfo() { lcd.clear(); lcd.print("KONIEC GRY :("); lcd.setCursor(0, 1); lcd.print("WYNIK: "); lcd.print(score); while (true) { int buttonState = digitalRead(BUTTON_PIN_ID); if (buttonState != HIGH) break; delay(250); } } int findMaxCactusesPos() { int max = -1; int id = -1; for (int i = 0; i < CACTUSES_COUNT; ++i) { if (cactuses_pos[i] >= max) { max = cactuses_pos[i]; id = i; } } return id; } void showTreasures() { --treasures_pos; if (treasures_pos > 15) return; else if (treasures_pos < 0) { lcd.setCursor(0, 0); lcd.write(" "); treasures_pos = random(16, 28); treasures_type = random(0, TREASURES_COUNT); return; } lcd.setCursor(treasures_pos, 0); lcd.write(ENTITY_TREASURES_ID+treasures_type); lcd.setCursor(treasures_pos + 1, 0); lcd.print(" "); } void showCactuses() { for (int i = 0; i < CACTUSES_COUNT; ++i) { --cactuses_pos[i]; if (cactuses_pos[i] > 15) continue; else if (cactuses_pos[i] < 0) { lcd.setCursor(0, 1); lcd.write(" "); cactuses_pos[i] = cactuses_pos[findMaxCactusesPos()] + random(3, 8); cactuses_skin[i] = random(0, CACTUSES_SKIN); continue; } lcd.setCursor(cactuses_pos[i], 1); lcd.write(ENTITY_CACTUS_ID + cactuses_skin[i]); lcd.setCursor(cactuses_pos[i] + 1, 1); lcd.print(" "); } } void showDino() { int buttonState = digitalRead(BUTTON_PIN_ID); if (buttonState == HIGH || (!isDinosaurOnGround)) { lcd.setCursor(1, 1); lcd.write(ENTITY_DINOSAUR_ID); isDinosaurOnGround = true; if (treasures_pos == 1) return; lcd.setCursor(1, 0); lcd.print(" "); } else { lcd.setCursor(1, 0); lcd.write(ENTITY_DINOSAUR_ID); isDinosaurOnGround = false; for (int i = 0; i < CACTUSES_COUNT; ++i) { if (cactuses_pos[i] == 1) return; } lcd.setCursor(1, 1); lcd.print(" "); } }
Editor is loading...