#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
unsigned long dur_t, start_t;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define LOGO_HEIGHT 32
#define LOGO_WIDTH 32
int pos_x = 127;
bool game = true;
bool jumping = false;
const unsigned char bitmap_DINO[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x3f, 0xf0, 0x00, 0x00, 0xef, 0xfc, 0x00, 0x00, 0xef, 0xfc, 0x00, 0x00, 0xff, 0xfc,
0x00, 0x00, 0xff, 0xfc, 0x00, 0x00, 0xff, 0xfc, 0x00, 0x00, 0xff, 0xfc, 0x00, 0x00, 0xfe, 0x00,
0x00, 0x00, 0xff, 0xe0, 0x0c, 0x00, 0xff, 0xe0, 0x0c, 0x01, 0xfc, 0x00, 0x0c, 0x07, 0xfc, 0x00,
0x0e, 0x1f, 0xff, 0x00, 0x0f, 0x1f, 0xff, 0x00, 0x0f, 0xff, 0xff, 0x00, 0x0f, 0xff, 0xfc, 0x00,
0x0f, 0xff, 0xfc, 0x00, 0x0f, 0xff, 0xfc, 0x00, 0x03, 0xff, 0xf0, 0x00, 0x03, 0xff, 0xf0, 0x00,
0x00, 0x7f, 0xe0, 0x00, 0x00, 0x7f, 0xc0, 0x00, 0x00, 0x7d, 0xc0, 0x00, 0x00, 0x78, 0xc0, 0x00,
0x00, 0x78, 0xc0, 0x00, 0x00, 0x60, 0xc0, 0x00, 0x00, 0x78, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00};
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while(true){}
}
}
void loop() {
if (digitalRead(2) == LOW) {
dur_t = counting(2);
if (dur_t < 1000) {
display.clearDisplay();
display.fillRect(pos_x --, 64-12, 4, 12, SSD1306_INVERSE);
display.drawBitmap(0, 10, bitmap_DINO, LOGO_WIDTH, LOGO_HEIGHT, 1);
display.display();
delay(500);
}
}
else {
display.clearDisplay();
if (!game) {
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 20);
display.println("Game Over");
display.display();
return;
}
if (jumping) {
pos_x += 2;
if (pos_x >= 127) {
pos_x = 127;
jumping = false;
}
} else {
pos_x--;
if (pos_x < 1) {
game = false;
}
}
display.fillRect(pos_x, 64-12, 4, 12, SSD1306_INVERSE); // Redraw the obstacle
testdrawbitmap(32);
display.display();
}
}
void testdrawbitmap(int dino_height) {
display.drawBitmap(0, dino_height, bitmap_DINO, LOGO_WIDTH, LOGO_HEIGHT, 1);
}
unsigned long counting(int pin){
start_t = millis();
while(digitalRead(pin) == LOW){
//do nothing
}
return millis() - start_t;
}