#include <LiquidCrystal_I2C.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,20,4);
byte pacmanUp[8] = {
B00000,
B00000,
B00000,
B11011,
B10101,
B10101,
B10001,
B01110
};
byte pacmanDown[8] = {
B00000,
B00000,
B00000,
B01110,
B10001,
B10101,
B10101,
B11011
};
byte pacmanRight[8] = {
B00000,
B00000,
B00000,
B01111,
B10001,
B10110,
B10001,
B01111
};
byte pacmanLeft[8] = {
B00000,
B00000,
B00000,
B11110,
B10001,
B01101,
B10001,
B11110
};
byte gift[8] = {
B00000,
B01010,
B00100,
B11111,
B10101,
B11011,
B10101,
B11111
};
int x=0;
int y=0;
int xGift;
int yGift;
int score;
void setup() {
Serial.begin(9600);
// pin setup
pinMode(13, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
// display
lcd.init();
lcd.backlight();
lcd.blink();
// characters setup
lcd.createChar(0, pacmanRight);
lcd.createChar(1, pacmanLeft);
lcd.createChar(2, pacmanUp);
lcd.createChar(3, pacmanDown);
lcd.createChar(5, gift);
// gift position
xGift=(rand()%19);
yGift=(rand()%3);
lcd.setCursor(0,0);
lcd.write(byte(0));
lcd.setCursor(xGift,yGift);
lcd.write(byte(5));
lcd.setCursor(0,0);
}
void loop() {
// analog read for joystick
int xValue = analogRead(12);
int yValue = analogRead(13);
delay(100);
if(x<19){
if(xValue == 0){
lcd.setCursor(x,y);
lcd.print(" ");
x++;
lcd.setCursor(x,y);
lcd.write(byte(0));
};
};
if(x>0){
if(xValue == 4095){
lcd.setCursor(x,y);
lcd.print(" ");
x--;
lcd.setCursor(x,y);
lcd.write(byte(1));
};
};
if(y<3){
if(yValue == 0){
lcd.setCursor(x,y);
lcd.print(" ");
y++;
lcd.setCursor(x,y);
lcd.write(byte(3));
};
};
if(y>0){
if(yValue == 4095){
lcd.setCursor(x,y);
lcd.print(" ");
y--;
lcd.setCursor(x,y);
lcd.write(byte(2));
};
};
if(x == xGift && y == yGift ){
score++;
Serial.println("Tvoje skore: ");
Serial.println(score);
xGift=(rand()%19);
yGift=(rand()%3);
lcd.setCursor(xGift,yGift);
lcd.write(byte(5));
};
}