/*
A simple Pong game.
https://notabug.org/Maverick/WokwiPong
Based on Arduino Pong by eholk
https://github.com/eholk/Arduino-Pong
*/
#include <EEPROM.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
int score0;
int score1;
int player;
unsigned char level = 2;
void setup()
{
InitEprom();
score0 = 0;
score1 = 0;
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE, BLACK);
display.setCursor(0, 0);
}
void loop()
{
SaveScore(0, score0);
SaveScore(1, score1);
print_scores();
// display.clearDisplay();
// display.setCursor(0, 0);
// display.print(score);
// display.display();
// for(;;);
score0 = score0 + level;
score1 = score1 + level;
}
void print_scores() {
char score_string0[5];
char score_string1[5];
ultoa(ReadScore(0), score_string0, 10);
itoa(ReadScore(1), score_string1, 10);
display.clearDisplay();
display.setCursor(0, 0);
display.write(score_string0);
display.setCursor(25, 0);
display.write(score_string1);
display.display();
}
// Read score from non volatile memory
unsigned char ReadScore(unsigned char player){
return(EEPROM.read(player));
}
void SaveScore(unsigned char player, unsigned char score){
if (score > ReadScore(player)){
EEPROM.update(player, score);
}
}
void InitEprom(){
EEPROM.update(0, 0);
EEPROM.update(1, 0);
}