Skip to content

Instantly share code, notes, and snippets.

@NULLx76
Created June 14, 2016 12:30
Show Gist options
  • Save NULLx76/5a810de3f1e826e3fae91af10dafd5f1 to your computer and use it in GitHub Desktop.
Save NULLx76/5a810de3f1e826e3fae91af10dafd5f1 to your computer and use it in GitHub Desktop.
Arduino LCD Game
/*
A 'Dodge the things game' using the LiquidCrystal Library
By:
Quirijn
Victor
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// define posY for the enemies
int posY2 = floor(random(0, 1)); //Just one enemy would be boring right?
int posY3 = floor(random(0, 1));
int posY [4] = {1,0,posY2,posY3};
// Define the posX for the enemies
int posX3 = floor(random(9, 16)); //random(9, 16); returns a random number from 9 to 16
int posX [4] = {15,7,11,posX3};
bool Game = true; // If game is false: Game Over
int playerX = 1;
int playerY = 1;
int Score = 0; //Scores?
int Speed = 1000; //Gotta be able to change the speed 1 Speed == 1/10 millisecond, higher is slower
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void softReset() {
//Dirty trick to reset the program (jumps to 0)
asm volatile (" jmp 0");
}
void gameover() {
Game = false;
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Game over!");
lcd.setCursor(6,1);
lcd.print(Score);
playerX--;
playerY--;
delay(8000);
softReset();
}
void loop() {
if (Game) {
//No impossible combinations please!
//TODO: Simplify this code
if(posX[0] == posX[1]+1) {
posX[0]--;
}
if(posX[1] == posX[2]+1) {
posX[1]--;
}
if(posX[0] == posX[2]+1) {
posX[2]++;
}
if(posX[0] == posX[1]) {
posX[0]++;
}
if(posX[0] == posX[2]) {
posX[2]++;
}
if(posX[1] == posX[2]) {
posX[1]++;
}
if(posX[0] == posX[1]-1) {
posX[0]++;
}
if(posX[1] == posX[2]-1) {
posX[1]++;
}
if(posX[0] == posX[2]-1) {
posX[2]--;
}
lcd.clear();
//enemies
lcd.setCursor(posX[0],posY[0]);
lcd.print("X");
lcd.setCursor(posX[1],posY[1]);
lcd.print("X");
lcd.setCursor(posX[2], posY[2]);
lcd.print("X");
lcd.setCursor(posX[3], posY[3]);
lcd.print("X");
//player
lcd.setCursor(playerX,playerY);
lcd.print("O");
delay(Speed/10);
if(Speed >= 100) {
Speed--; //Everytime the game runs the speed goes down, shrinking exponentially by time
}
/* Score mechanic v1
if(Speed < 500) {
Score++;
if(Speed < 400) {
Score++;
if(Speed < 300) {
Score++;
if(Speed < 200) {
Score++;
if(Speed < 150) {
Score++;
if(Speed < 100) {
Score++;
Score++;
if(random(0, 1) == 1) {
Score++;
}
}
}
}
}
}
}
*/
Score = Score + floor(10 - Speed/100);
// Reset enemy pos if they go off the screen
if(posX[0] < 0){
posX[0] = 16;
}else{
posX[0]--;
}
if(posX[1] < 0){
posX[1] = 16;
}else{
posX[1]--;
}
if(posX[2] < 0){
posX[2] = 16;
posY[2] = floor(random(0, 2));
}else{
posX[2]--;
}
if(posX[3] < 0){
posX[3] = floor(random(9, 16));
posY[3] = floor(random(0, 1));
for(int posX3b = posX[3]; posX3b == posX[0] || posX3b == posX[1] || posX3b == posX[2]; posX3b++) {
posX[3]++;
}
for(int posX3c = posX[3]; posX3c == posX[0]++ || posX3c == posX[1]++ || posX3c == posX[2]++; posX3c++) {
posX[3]++;
}
for(int posX3d = posX[3]; posX3d == posX[0]-- || posX3d == posX[1]-- || posX3d == posX[2]--; posX3d++) { //to make sure posX[3] is not equal to any other X. "||" is a boolean for "or"
posX[3]--;
}
}else{
posX[3]--;
}
}
//Checks if player pos is equal to enemy pos
if ((playerX == posX[0] && playerY == posY[0]) || (playerX == posX[1] && playerY == posY[1]) || (playerX == posX[2] && playerY == posY[2]) || (playerX == posX[3] && playerY == posY[3])) {
gameover();
}
// print the number of seconds since reset:
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
//lcd.print("RIGHT ");
break;
}
case btnLEFT:
{
//lcd.print("LEFT ");
break;
}
case btnUP:
{
if(playerY == 1) { //Als de speler op de bovenste regel zit en "UP" indrukt, gebeurt er niets.
playerY--;
}
//lcd.print("UP ");
break;
}
case btnDOWN:
{
if(playerY == 0) { //Als de speler op de onderste regel zit en "UP" indrukt, gebeurt er niets.
playerY++;
}
//lcd.print("DOWN ");
break;
}
case btnSELECT:
{
softReset();
break;
}
case btnNONE:
{
//lcd.print("NONE ");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment