#include <LiquidCrystal_I2C.h>
#include <stdlib.h>
#include <time.h>
#define U 2
#define D 4
#define L 15
#define R 5
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
// Define una estructura para representar las posiciones X e Y
typedef struct {
int x;
int y;
} Posicion;
Posicion pacman;
Posicion fantasmas;
int mapa[2][16];
int direccion = 6;
int turno = 0; //el fantasma se mueve cada x movimientos de pacman
/*
4 = izquierda
6 = derecha
8 = arriba
2 = abajo
*/
// Define un conjunto de caracteres personalizados para Pac-Man
byte pacmanCharacter[8] = {
B00111,
B01111,
B01100,
B01100,
B01111,
B01111,
B00111,
};
byte pacmanCharacter2[8] = {
B00111,
B01111,
B01111,
B01111,
B01111,
B01111,
B00111,
};
byte fantasmaCharacter[8] = {
B01110,
B11111,
B10001,
B11111,
B11111,
B10101,
B10101,
};
byte comidaCharacter[8] = {
B00000,
B01110,
B11111,
B11111,
B01110,
B00000,
B00000,
};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
lcd.init();
lcd.createChar(0, pacmanCharacter); // Crea el carácter personalizado en la posición 0
lcd.createChar(1, pacmanCharacter2);
lcd.createChar(2, fantasmaCharacter);
lcd.createChar(3, comidaCharacter);
pinMode(U, INPUT);
pinMode(D, INPUT);
pinMode(L, INPUT);
pinMode(R, INPUT);
lcd.backlight();
configurarPosiciones();
}
void configurarPosiciones(){
pacman.x = 0;
pacman.y = 0;
fantasmas.x = 15;
fantasmas.y = 1;
srand(time(NULL));
for(int j=0; j<2; j++)
{
int numero1 = rand() % 2;
int numero2 = rand() % 16;
mapa[numero1][numero2]=1;
}
direccion = 6;
turno=0;
for(int fila = 0; fila < 2; fila++) {
for (int columna = 0; columna < 16; columna++) {
if(mapa[fila][columna]==1)
{
lcd.setCursor(columna,fila);
lcd.write(byte(3));
}
}
}
lcd.setCursor(fantasmas.x,fantasmas.y);
lcd.write(byte(2));
lcd.setCursor(pacman.x,pacman.y);
lcd.write(byte(0));
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
delay(100); // this speeds up the simulation
juego();
}
void juego(){
moverPacman();
verificarMuerte();
verificarVictoria();
if(turno%10 == 0){
moverFantasmas();
verificarMuerte();
}
turno++;
}
void moverPacman(){
lcd.setCursor(pacman.x, pacman.y);
lcd.print(" ");
if(digitalRead(R) == HIGH){
direccion = 6;
}
else if(digitalRead(L) == HIGH){
direccion = 4;
}
else if(digitalRead(D) == HIGH){
direccion = 2;
}
else if(digitalRead(U) == HIGH){
direccion = 8;
}
if(direccion==6 && pacman.x<15){
pacman.x++;
}
else if(direccion==4 && pacman.x>0){
pacman.x--;
}
else if(direccion==2 && pacman.y<1){
pacman.y++;;
}
else if(direccion==8 && pacman.y>0){
pacman.y--;
}
if(mapa[pacman.y][pacman.x]==1)
{
mapa[pacman.y][pacman.x]=0;
}
lcd.setCursor(pacman.x, pacman.y);
if(turno%2==0)
{
lcd.write(byte(0)); //Dibuja
}
else
{
lcd.write(byte(1));
}
}
void moverFantasmas(){
lcd.setCursor(fantasmas.x,fantasmas.y);
if(mapa[fantasmas.y][fantasmas.x]==1)
{
lcd.write(byte(3));
}
else{
lcd.print(" ");
}
if(pacman.x>fantasmas.x)
{
fantasmas.x++;
}
else if(pacman.x<fantasmas.x){
fantasmas.x--;
}
else if(pacman.y>fantasmas.y)
{
fantasmas.y++;
}
else{
fantasmas.y--;
}
lcd.setCursor(fantasmas.x,fantasmas.y);
lcd.write(byte(2));
}
void verificarMuerte(){
if (pacman.x == fantasmas.x && pacman.y == fantasmas.y) {
lcd.clear();
lcd.setCursor(4,1);
lcd.print("PERDISTE");
delay(3000);
lcd.clear();
limpiarComida();
configurarPosiciones();
return;
}
}
void limpiarComida(){
for(int fila = 0; fila < 2; fila++) {
for (int columna = 0; columna < 16; columna++) {
mapa[fila][columna]=0;
}
}
}
void verificarVictoria(){
for(int fila = 0; fila < 2; fila++) {
for (int columna = 0; columna < 16; columna++) {
if(mapa[fila][columna]==1)
{
return;
}
}
}
lcd.clear();
lcd.setCursor(4,1);
lcd.print("GANASTE :D");
delay(3000);
lcd.clear();
configurarPosiciones();
}