Skip to content

Commit 2ec57ad

Browse files
authored
Update FlappyBird.ino
1 parent a650cb9 commit 2ec57ad

File tree

1 file changed

+14
-33
lines changed

1 file changed

+14
-33
lines changed

examples/Games/FlappyBird/FlappyBird.ino

+14-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// By Ponticelli Domenico.
2+
// 12NOV2020 EEPROM Fix added, modified by Zontex
23
// https://github.com/pcelli85/M5Stack_FlappyBird_game
34

45
#include <M5Stack.h>
@@ -27,7 +28,8 @@
2728
// grass size
2829
#define GRASSH 4 // grass height (inside floor, starts at floor y)
2930

30-
int maxScore = 0;
31+
int address = 0;
32+
int maxScore = EEPROM.readInt(address);
3133
const int buttonPin = 2;
3234
// background
3335
const unsigned int BCKGRDCOL = M5.Lcd.color565(138,235,244);
@@ -92,10 +94,12 @@ static short tmpx, tmpy;
9294

9395
void setup() {
9496
// put your setup code here, to run once:
95-
M5.begin();
96-
M5.Power.begin();
97-
resetMaxScore();
98-
97+
M5.begin();
98+
M5.Power.begin();
99+
EEPROM.begin(1000);
100+
//resetMaxScore();
101+
Serial.println("last score:");
102+
Serial.println(EEPROM.readInt(address));
99103
}
100104

101105
void loop() {
@@ -323,11 +327,12 @@ void game_init() {
323327
// ---------------
324328
void game_over() {
325329
M5.Lcd.fillScreen(TFT_BLACK);
326-
EEPROM_Read(&maxScore,0);
330+
maxScore = EEPROM.readInt(address);
327331

328332
if(score>maxScore)
329333
{
330-
EEPROM_Write(&score,0);
334+
EEPROM.writeInt(address, score);
335+
EEPROM.commit();
331336
maxScore = score;
332337
M5.Lcd.setTextColor(TFT_RED);
333338
M5.Lcd.setTextSize(2);
@@ -360,31 +365,7 @@ void game_over() {
360365

361366
void resetMaxScore()
362367
{
363-
EEPROM_Write(&maxScore,0);
364-
}
365-
366-
367-
368-
void EEPROM_Write(int *num, int MemPos)
369-
{
370-
byte ByteArray[2];
371-
memcpy(ByteArray, num, 2);
372-
for(int x = 0; x < 2; x++)
373-
{
374-
EEPROM.write((MemPos * 2) + x, ByteArray[x]);
375-
}
368+
EEPROM.writeInt(address, 0);
369+
EEPROM.commit();
376370
}
377371

378-
379-
380-
void EEPROM_Read(int *num, int MemPos)
381-
{
382-
byte ByteArray[2];
383-
for(int x = 0; x < 2; x++)
384-
{
385-
ByteArray[x] = EEPROM.read((MemPos * 2) + x);
386-
}
387-
memcpy(num, ByteArray, 2);
388-
}
389-
390-

0 commit comments

Comments
 (0)