Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #include <Keypad.h>
- const byte LINHAS = 4;
- const byte COLUNAS = 4;
- char MATRIZ_TECLAS[LINHAS][COLUNAS] =
- {
- {'1', '2', '3', 'A'},
- {'4', '5', '6', 'B'},
- {'7', '8', '9', 'C'},
- {'*', '0', '#', 'D'}
- };
- byte PINOS_LINHAS[LINHAS] = {7, 6, 5, 4};
- byte PINOS_COLUNAS[COLUNAS] = {11, 10, 9, 8}; // D4..D7 LCD / C1..C4 TEC
- Keypad teclado = Keypad(makeKeymap(MATRIZ_TECLAS), PINOS_LINHAS, PINOS_COLUNAS, LINHAS, COLUNAS);
- LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
- byte SMILEY[8] = {
- B00000,
- B10001,
- B00000,
- B00000,
- B10001,
- B01110,
- B00000,
- };
- byte PACMAN_LOOKING_RIGHT[8] = {
- B00000,
- B01110,
- B11101,
- B11111,
- B11000,
- B11111,
- B01110,
- B00000
- };
- byte PACMAN_LOOKING_LEFT[8] = {
- B00000,
- B01110,
- B10111,
- B11111,
- B00011,
- B11111,
- B01110,
- B00000
- };
- byte PACMAN_LOOKING_BOTTOM[8] = {
- B00000,
- B01110,
- B11111,
- B10111,
- B10111,
- B10101,
- B00110,
- B00000
- };
- byte PACMAN_LOOKING_TOP[8] = {
- B00000,
- B00110,
- B10101,
- B10111,
- B10111,
- B11111,
- B01110,
- B00000
- };
- byte PACMAN_FOOD[8] = {
- B00000,
- B00010,
- B00100,
- B01110,
- B01110,
- B01110,
- B00000,
- B00000
- };
- byte PACMAN_BLOCK[8] = {
- B11111,
- B11111,
- B11111,
- B11111,
- B11111,
- B11111,
- B11111,
- B11111
- };
- byte PACMAN_EMPTY_CODE = byte(0);
- byte PACMAN_LOOKING_RIGHT_CODE = byte(1);
- byte PACMAN_LOOKING_LEFT_CODE = byte(2);
- byte PACMAN_LOOKING_BOTTOM_CODE = byte(3);
- byte PACMAN_LOOKING_TOP_CODE = byte(4);
- byte PACMAN_FOOD_CODE = byte(5);
- byte PACMAN_BLOCK_CODE = byte(6);
- bool PACMAN_LOOSE = false;
- bool PACMAN_WINS = false;
- byte pacman_lookingNow = PACMAN_LOOKING_RIGHT_CODE;
- byte pacman_lcdMatrix[16][2];
- int pacman_positionCol = 0;
- int pacman_positionRow = 0;
- int pacman_foodEaten = 0;
- unsigned long previousMillisMove, previousMillisFood;
- unsigned long SECOND = 50;
- long intervalFood = SECOND * 3;
- int currentQuantityFood = 0;
- int currentQuantityBlocks = 0;
- void pacman_start() {
- lcd.createChar(PACMAN_LOOKING_RIGHT_CODE, PACMAN_LOOKING_RIGHT);
- lcd.createChar(PACMAN_LOOKING_LEFT_CODE, PACMAN_LOOKING_LEFT);
- lcd.createChar(PACMAN_LOOKING_BOTTOM_CODE, PACMAN_LOOKING_BOTTOM);
- lcd.createChar(PACMAN_LOOKING_TOP_CODE, PACMAN_LOOKING_TOP);
- lcd.createChar(PACMAN_FOOD_CODE, PACMAN_FOOD);
- lcd.createChar(PACMAN_BLOCK_CODE, PACMAN_BLOCK);
- lcd.begin(16, 2);
- lcd.write(PACMAN_LOOKING_RIGHT_CODE);
- pacman_lcdMatrix[0][0] = PACMAN_LOOKING_RIGHT_CODE;
- pacman_createBlocks();
- }
- void pacman_createBlocks() {
- int col, row;
- pacman_randomPosition(&col, &row);
- pacman_createBlock(col, 0);
- pacman_randomPosition(&col, &row);
- pacman_createBlock(col, 1);
- }
- void pacman_createBlock(int col, int row) {
- currentQuantityBlocks += 1;
- lcd.setCursor(col, row);
- lcd.write(PACMAN_BLOCK_CODE);
- pacman_lcdMatrix[col][row] = PACMAN_BLOCK_CODE;
- }
- void pacman_move() {
- if (PACMAN_LOOSE || PACMAN_WINS) return;
- lcd.setCursor(pacman_positionCol, pacman_positionRow);
- lcd.write(' ');
- pacman_lcdMatrix[pacman_positionCol][pacman_positionRow] = PACMAN_EMPTY_CODE;
- if (pacman_lookingNow == PACMAN_LOOKING_RIGHT_CODE) {
- pacman_positionCol = (pacman_positionCol + 1) % 16;
- } else if (pacman_lookingNow == PACMAN_LOOKING_LEFT_CODE) {
- pacman_positionCol = pacman_positionCol - 1;
- if (pacman_positionCol < 0) pacman_positionCol = 15;
- } else if (pacman_lookingNow == PACMAN_LOOKING_BOTTOM_CODE) {
- pacman_positionRow = (pacman_positionRow + 1) % 2;
- } else if (pacman_lookingNow == PACMAN_LOOKING_TOP_CODE) {
- pacman_positionRow = pacman_positionRow - 1;
- if (pacman_positionRow < 0) pacman_positionRow = 1;
- }
- byte newObjectCode = pacman_lcdMatrix[pacman_positionCol][pacman_positionRow];
- if (newObjectCode == PACMAN_BLOCK_CODE) {
- PACMAN_LOOSE = true;
- pacman_showLooseMessage();
- return;
- }
- if (newObjectCode == PACMAN_FOOD_CODE) {
- intervalFood -= (SECOND / 10);
- currentQuantityFood -= 1;
- if (intervalFood <= 0) {
- PACMAN_WINS = true;
- pacman_showWinsMessage();
- return;
- }
- }
- lcd.setCursor(pacman_positionCol, pacman_positionRow);
- lcd.write(pacman_lookingNow);
- pacman_lcdMatrix[pacman_positionCol][pacman_positionRow] = pacman_lookingNow;
- }
- void pacman_createFood() {
- if (PACMAN_LOOSE || PACMAN_WINS) return;
- int col, row;
- pacman_randomPosition(&col, &row);
- if (pacman_lcdMatrix[col][row] != PACMAN_EMPTY_CODE) {
- pacman_createFood();
- return;
- }
- currentQuantityFood += 1;
- lcd.setCursor(col, row);
- lcd.write(PACMAN_FOOD_CODE);
- pacman_lcdMatrix[col][row] = PACMAN_FOOD_CODE;
- int pacmanLcdSize = 1;
- if ((currentQuantityFood + currentQuantityBlocks + pacmanLcdSize) == (16 * 2)) {
- delay(500);
- PACMAN_LOOSE = true;
- pacman_showLooseMessage();
- }
- }
- void pacman_randomPosition(int *col, int *row) {
- int randCol = random(0, 16);
- int randRow = random(0, 2);
- *col = randCol;
- *row = randRow;
- }
- void pacman_showLooseMessage() {
- lcd.clear();
- lcd.write("You loose! Sorry");
- }
- void pacman_showWinsMessage() {
- lcd.clear();
- lcd.write("YOU WIN! YEAH");
- lcd.setCursor(0, 1);
- lcd.write("POINTS: ");
- char foodEatenAsChar[3];
- sprintf(foodEatenAsChar, "%d", pacman_foodEaten);
- lcd.write(foodEatenAsChar);
- }
- void setup() {
- Serial.begin(9600);
- Serial.println("Teclado 4x3 - Exemplo biblioteca Keypad");
- Serial.println("Aguardando aconamento das teclas...");
- Serial.println();
- randomSeed(analogRead(0));
- pacman_start();
- }
- void loop() {
- unsigned long currentMillisMove = millis();
- unsigned long currentMillisFood = millis();
- char teclaPressionada = teclado.getKey();
- switch (teclaPressionada) {
- case '2':
- pacman_lookingNow = PACMAN_LOOKING_TOP_CODE;
- break;
- case '6':
- pacman_lookingNow = PACMAN_LOOKING_RIGHT_CODE;
- break;
- case '8':
- pacman_lookingNow = PACMAN_LOOKING_BOTTOM_CODE;
- break;
- case '4':
- pacman_lookingNow = PACMAN_LOOKING_LEFT_CODE;
- break;
- }
- if ((currentMillisMove - previousMillisMove) > SECOND) {
- previousMillisMove = currentMillisMove;
- pacman_move();
- }
- if ((currentMillisFood - previousMillisFood) > intervalFood) {
- previousMillisFood = currentMillisFood;
- pacman_createFood();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement