Created
August 31, 2015 10:28
-
-
Save emilioramirez/4036226144eebecdedc1 to your computer and use it in GitHub Desktop.
Arduino mini game using display lcd 16x2 and pulser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Arduino mini game using display lcd 16x2 and pulser | |
#include <LiquidCrystal.h> | |
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12); | |
char BLOQUE; | |
char PERSONAJE; | |
int LINEA_ARRIBA = 0; | |
int LINEA_ABAJO = 1; | |
int BOTON_PIN = 4; | |
int SCORE = -1; | |
void setup(){ | |
Serial.begin(9600); | |
lcd.begin(16, 2); | |
pinMode(BOTON_PIN, INPUT); | |
} | |
void mostrar_entrada(){ | |
int pboton = digitalRead(BOTON_PIN); | |
Serial.print("Mostrar entrada: "); | |
Serial.println(random(8, 15)); | |
lcd.setCursor(0, 0); | |
lcd.print(pboton); | |
delay(200); | |
} | |
void loop(){ | |
//mostrar_entrada(); | |
int bloque_x = random(5, 15); | |
juego(bloque_x); | |
} | |
int juego(int bloque_x){ | |
// Inicio el display | |
lcd.begin(16, 2); | |
// Ciclo de avanze del personaje y del bloque | |
for (int personaje_x=0; personaje_x<16; personaje_x++){ | |
// Limpio la pantalla | |
lcd.clear(); | |
// Leo la entrada | |
int boton = digitalRead(BOTON_PIN); | |
// Linea en la que se dibuja el personaje | |
int personaje_y = LINEA_ABAJO; | |
// Si el boton esta presionado, el personaje se dibuja en la linea superior | |
if (boton == HIGH){ | |
personaje_y = LINEA_ARRIBA; | |
} | |
personaje(personaje_x, personaje_y, 150); | |
bloque(bloque_x, 150); | |
detectar_colision(bloque_x, personaje_x, personaje_y); | |
} | |
SCORE += 1; | |
} | |
void personaje(int x, int y, int d){ | |
lcd.setCursor(x, y); | |
lcd.print("o"); | |
delay(d); | |
} | |
void bloque(int posicion_bloque, int d){ | |
lcd.setCursor(posicion_bloque, 1); | |
lcd.print("O"); | |
delay(d); | |
} | |
void detectar_colision(int bloque_x, int personaje_x, int personaje_y){ | |
if (bloque_x == personaje_x && personaje_y == LINEA_ABAJO){ | |
lcd.setCursor(0,0); | |
String score_string = String(SCORE); | |
lcd.print("GAME OVER: " + score_string); | |
SCORE = 0; | |
delay(1000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment