Hello,
I am making a game on an Arduino LCD Shield. I am trying to display the high score for when a user loses. When I make a new high score, the high score is always 1+ than the score.
Example: Score: 13
Best: 14
Can anybody tell me why this is happening, and if possible, fix it? I cleared the EEPROM addresses that the high score variable uses.
BTW: I am a noob at this stuff.
Parts used:
- Arduino Leonardo Clone from "keyestudio"
- LCD Shield from "Kuman"
Sketch code for tab 1 (main) (Name: Chrome_Dino_Game_on_LCD):
#include <LiquidCrystal.h>
#include <EEPROM.h>
#include "bitmaps.h"
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
byte runnerArea[16] {32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32};
byte jump = 32;
int score = 0;
bool freeze_score = 0;
unsigned long previousMillis = 0;
unsigned long previousMillisLED = 0;
unsigned long jumpTime = 0;
const int jumpLength = 500;
#define checkSafe runnerArea[1] == 32 || runnerArea[1] == 0
const byte chance_of_ob = 15;
int speedOfScroller = 300;
void setup() {
lcd.begin(16, 2);
lcd.createChar(0, dino);
lcd.createChar(1, cacti);
lcd.createChar(2, bird);
lcd.createChar(3, block);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(13, OUTPUT);
randomSeed(A1);
lcd.clear();
showSplashScreen(1000, true);
}
void loop() {
unsigned long currentMillis = millis();
unsigned long currentMillisOb = millis();
if (currentMillisOb - previousMillis >= speedOfScroller) {
previousMillis = currentMillisOb;
if (random(chance_of_ob) == 0) {
runnerArea[15] = 1;
} else if (random(chance_of_ob) == 1) {
runnerArea[15] = 2;
} else {
runnerArea[15] = 32;
}
for (int i = 0; i <= 15; i++) {
runnerArea[i] = runnerArea[i + 1];
}
if (freeze_score == 0) {
score++;
}
}
drawBarrier();
if (read_LCD_buttons() == btnSELECT) {
runnerArea[1] = 32;
jump = 0;
freeze_score = 1;
jumpTime = millis();
}
if (millis() - jumpTime >= jumpLength) {
if (checkSafe) {
runnerArea[1] = 0;
jump = 32;
freeze_score = 0;
} else {
showCrashScreen();
}
}
updateLcd();
printScore();
if (millis() - previousMillisLED >= 500) {
previousMillisLED = currentMillis;
digitalWrite(13, !digitalRead(13));
}
}
Sketch code for tab 2(user generated, made by user) (Name: Functions):
// Functions for main.cpp
int read_LCD_buttons() {
adc_key_in = analogRead(0);
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 10) return btnRIGHT;
if (adc_key_in < 110) return btnUP;
if (adc_key_in < 300) return btnDOWN;
if (adc_key_in < 450) return btnLEFT;
if (adc_key_in < 700) return btnSELECT;
return btnNONE;
}
void updateLcd() {
for (int i = 0; i <= 15; i++) {
lcd.setCursor(i, 1);
lcd.write(runnerArea[i]);
}
lcd.setCursor(1, 0);
lcd.write(jump);
}
void drawBarrier() {
runnerArea[0] = 3;
}
void printScore() {
lcd.setCursor(4, 0);
lcd.print("Score: ");
lcd.setCursor(11, 0);
lcd.print(score);
}
void showSplashScreen(int delayTime, boolean wait_for_start) {
lcd.home();
lcd.print("Chrome Dino Game");
drawSplashGraphics();
delay(delayTime);
if (wait_for_start) {
while (read_LCD_buttons() != btnSELECT) {
checkForEEPROMUserInitErase();
}
}
lcd.clear();
}
void checkForEEPROMUserInitErase() {
if (read_LCD_buttons() == btnDOWN) {
lcd.setCursor(0, 1);
lcd.print("Clearing EEPROM");
EEPROMWriteInt(0, 0);
delay(250);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Done!");
digitalWrite(13, !digitalRead(13));
delay(100);
digitalWrite(13, !digitalRead(13));
delay(400);
lcd.setCursor(0, 1);
lcd.print(" ");
drawSplashGraphics();
}
}
void drawRandChar() {
lcd.setCursor(random(3, 15), 1);
lcd.write(byte(random(1, 3)));
}
void drawSplashGraphics() {
lcd.setCursor(0, 1);
lcd.write(byte(3));
lcd.setCursor(15, 1);
lcd.write(byte(3));
lcd.setCursor(1, 1);
lcd.write(byte(0));
drawRandChar();
drawRandChar();
drawRandChar();
}
void showCrashScreen() {
lcd.setCursor(4, 1);
lcd.print("Game Over!");
delay(2500);
lcd.setCursor(4, 1);
lcd.print("Best: ");
lcd.setCursor(10, 1);
lcd.print(" ");
lcd.setCursor(10, 1);
if (EEPROMReadInt(0) <= score) {
EEPROMWriteInt(0, score);
}
lcd.print(EEPROMReadInt(0));
while (true) {
digitalWrite(13, !digitalRead(13));
delay(500);
}
}
void EEPROMWriteInt(int address, int value)
{
byte two = (value & 0xFF);
byte one = ((value >> 8) & 0xFF);
EEPROM.update(address, two);
EEPROM.update(address + 1, one);
}
int EEPROMReadInt(int address)
{
long two = EEPROM.read(address);
long one = EEPROM.read(address + 1);
return ((two << 0) & 0xFFFFFF) + ((one << 8) & 0xFFFFFFFF);
}
Sketch code for tab 3(user made) (Name: bitmaps.h):
// Bitmaps-Do not modify
byte dino[8] = {
0b00000,
0b00111,
0b00111,
0b10110,
0b11111,
0b01010,
0b01010,
0b00000
};
byte cacti[8] = {
0b00100,
0b00101,
0b10101,
0b10101,
0b10111,
0b11100,
0b00100,
0b00000
};
byte bird[8] = {
0b00000,
0b00100,
0b01100,
0b11110,
0b00111,
0b00110,
0b00100,
0b00000
};
byte block[8] = {
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111
};
I really appreciate it if you can help me.
Also, I don't know whether I should keep the thread here or move it to the Displays sections.