#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//botones
#define boton_A 2
#define boton_B 3
#define boton_arriba 6
#define boton_abajo 7
#define boton_izq 8
#define boton_der 9
#define boton_casa 10
//pantalla
Adafruit_SSD1306 display(128, 64, &Wire, -1);
//variables del sistema
int juego_seleccionado = 0;
byte num_max_juegos = 1;
bool en_juego = false;
void setup() {
randomSeed(analogRead(28));
//inicializar pantalla
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for(;;);
}
//inicializar botones
pinMode(boton_A, INPUT_PULLUP);
pinMode(boton_B, INPUT_PULLUP);
pinMode(boton_arriba, INPUT_PULLUP);
pinMode(boton_abajo, INPUT_PULLUP);
pinMode(boton_izq, INPUT_PULLUP);
pinMode(boton_der, INPUT_PULLUP);
pinMode(boton_casa, INPUT_PULLUP);
//inicio
display.display();
delay(1000);
display.clearDisplay();
display.setCursor(2, 2);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.println(F("Inicializando..."));
display.display();
delay(500);
}
void loop() {
switch (juego_seleccionado){
case 0:
juego1();
break;
case 1:
juego2();
break;
}
if (en_juego == false){
if (digitalRead(boton_der) == false){
juego_seleccionado += 1;
while (digitalRead(boton_der) == false){
}
} else if (digitalRead(boton_izq) == false){
juego_seleccionado -= 1;
while (digitalRead(boton_izq) == false){
}
}
if (digitalRead(boton_A) == false){
en_juego = true;
}
if (juego_seleccionado == -1){
juego_seleccionado = num_max_juegos;
} else if (juego_seleccionado == num_max_juegos + 1){
juego_seleccionado = 0;
}
}
display.setCursor(120, 56);
display.print(juego_seleccionado);
display.display();
delay(10); // this speeds up the simulation
}
//JUEGO 1
static const unsigned char PROGMEM minj1_bmp[] =
{
0xff,0xff,0xff,0xff,0x80,0x1,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x8,0x0,0x1,0xb8,0x0,0x30,0x1,0xb8,0x0,0x30,0x1,0xb8,0x40,0x0,0x1,0xb8,0x0,0x0,0x1,0xb8,0x0,0x0,0x1,0xba,0x0,0x0,0x1,0xb8,0x0,0x0,0x1,0xb8,0x0,0x0,0x1,0xb8,0x0,0x0,0x1d,0xb8,0x0,0x0,0x1d,0xb8,0x0,0x0,0x1d,0x80,0x0,0x0,0x1d,0xb8,0xe0,0x0,0x1d,0xa4,0x90,0x0,0x1d,0xba,0xe6,0x49,0x9d,0xa0,0x89,0x6a,0x1d,0xa2,0x89,0x5a,0x9d,0xa2,0x86,0x49,0x9d,0x80,0x0,0x0,0x1d,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0xb8,0x0,0x0,0x19,0x88,0x0,0x0,0x5,0x90,0x0,0x0,0x9,0x90,0x0,0x0,0x5,0x90,0x0,0x0,0x19,0x80,0x0,0x0,0x1,0xff,0xff,0xff,0xff
};
void juego1(){
if (en_juego == false){
display.clearDisplay();
display.setCursor(1, 2);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.print(F("juego 1"));
display.setCursor(2, 10);
display.setTextSize(2);
display.print(F("PiPong"));
display.drawBitmap(8, 32, minj1_bmp, 32, 32, 1);
display.setCursor(50, 26);
display.setTextSize(1);
display.print(F("Jugadores: 2"));
display.setCursor(50, 36);
display.setTextSize(1);
display.print(F("Tipo: "));
display.setCursor(55, 44);
display.setTextSize(1);
display.print(F("Competitivo"));
} else {
logica_j1();
}
}
void logica_j1(){
int puntuacion1 = 0;
int puntuacion2 = 0;
byte j1Y = 16;
byte j2Y = 32;
byte bolaX = 32;
byte bolaY = 16;
bool bolaDirX = true;
bool bolaDirY = true;
float aleatorio = 0;
display.clearDisplay();
display.display();
//loop
while (en_juego == true){
display.clearDisplay();
//Entradas
if (digitalRead(boton_arriba) == false && j1Y >= 1){
j1Y -= 2;
} else if (digitalRead(boton_abajo) == false && j1Y <= 47){
j1Y += 2;
}
if (digitalRead(boton_A) == false && j2Y >= 1){
j2Y -= 2;
} else if (digitalRead(boton_B) == false && j2Y <= 47){
j2Y += 2;
}
if (digitalRead(boton_casa) == false){
en_juego = false;
}
//bola
if (bolaDirY == true){
bolaY += 1 + aleatorio / 2;
if (bolaY >= 63){
bolaY == 62;
bolaDirY = false;
aleatorio == random(0.00, 1.00);
}
} else if (bolaDirY == false){
bolaY -= 1 + aleatorio / 2;
if (bolaY <= 0){
bolaY == 1;
bolaDirY = true;
aleatorio == random(0.00, 1.00);
}
}
if (bolaDirX == true){
bolaX += 1 + aleatorio / 2;
if (bolaX >= 122 && bolaY >= j2Y && bolaY <= j2Y + 16){
bolaDirX = false;
aleatorio == random(0.00, 1.00);
}
} else if (bolaDirX == false){
bolaX -= 1 + aleatorio / 2;
if (bolaX <= 5 && bolaY >= j1Y && bolaY <= j1Y + 16){
bolaDirX = true;
aleatorio == random(0.00, 1.00);
}
}
if (bolaDirX == true){
if (bolaX >= 122 && !(bolaY >= j2Y && bolaY <= j2Y + 16)){
bolaX -= 112;
puntuacion1 += 1;
}
} else if (bolaDirX == false){
if (bolaX <= 5 && !(bolaY >= j1Y && bolaY <= j1Y + 16)){
bolaX += 112;
puntuacion2 += 1;
}
}
//pantalla
display.fillRect(4, j1Y, 3, 16, SSD1306_WHITE);
display.fillRect(121, j2Y, 3, 16, SSD1306_WHITE);
display.drawPixel(bolaX, bolaY, SSD1306_WHITE);
display.setCursor(2, 2);
display.setTextSize(1);
display.setTextColor(SSD1306_INVERSE);
display.print(puntuacion1);
display.setCursor(125 - String(puntuacion2).length() * 5, 2);
display.setTextSize(1);
display.print(puntuacion2);
display.display();
delay(8);
}
}
//juego 2
static const unsigned char PROGMEM minj2_bmp[] =
{
0xff,0xff,0xff,0xff,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0x80,0x0,0x0,0x1,0xff,0xff,0xff,0xff
};
void juego2(){
if (en_juego == false){
display.clearDisplay();
display.setCursor(1, 2);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.print(F("juego 2"));
display.setCursor(2, 10);
display.setTextSize(2);
display.print(F("Titulo"));
display.drawBitmap(8, 32, minj2_bmp, 32, 32, 1);
display.setCursor(50, 26);
display.setTextSize(1);
display.print(F("Jugadores: X"));
display.setCursor(50, 36);
display.setTextSize(1);
display.print(F("Tipo: "));
display.setCursor(55, 44);
display.setTextSize(1);
display.print(F("XXXXXXXXXXXX"));
} else {
logica_j1();
}
}