#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Use I2C interface (address 0x3C by default)
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define gravity 0.5
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
unsigned long currentTime; // currint esp time
// class for DINO
class Deno {
public:
//states
bool inframe = true;
bool is_grounded = true;
//values
float x = 24;
float y = SCREEN_HEIGHT - 10;
float width = 8;
float hight = 8;
float speedx = 0;
float speedy = 0;
//actions
void start_jump(float speed){
is_grounded = false;
speedy = -speed;
}
void jump(){
if(is_grounded) return;
speedy += gravity;
}
void ground(){
is_grounded = 1;
speedy = 0;
y = SCREEN_HEIGHT - 10;
}
};
// class for obst
class Box {
public:
bool inframe = false;
float x = 196;
float y = SCREEN_HEIGHT - 10;
float width = 8;
float hight = 8;
float speedx = 0;
float speedy = 0;
// Method to reset the Box state
void reset() {
inframe = false;
x = 196;
speedx = 0;
speedy = 0;
}
// Method to spawn the Box
void spawn() {
inframe = true;
x = SCREEN_WIDTH + random(0, 20);
speedx = -2;
}
};
void rendNUpdateBox(Box &box){
// view cord ([0,0],[80,160])
//if outof view skip render
if(box.x>160) return;
// render box and update pos
display.fillRect(box.x, box.y, box.width, box.hight, SSD1306_WHITE);
// display.display();
box.x += box.speedx;
box.y += box.speedy;
// if after pos update outof view reset
if(box.x>160 || box.x + box.width<0 || box.y>80 || box.y + box.hight<0){
box.reset();
}
// if(box.x <= 0 || box.x+box.width >= SCREEN_WIDTH ){
// box.speedx = -box.speedx;
// }
// if(box.y <= 0 || box.y+box.hight >= SCREEN_HEIGHT ){
// box.speedy = -box.speedy;
// }
}
void rendNUpdateDino(Deno &dino){
// view cord ([0,0],[80,160])
//if outof view skip render
if(dino.x>160) return;
// render dino and update pos
display.fillRect(dino.x, dino.y, dino.width, dino.hight, SSD1306_WHITE);
dino.x += dino.speedx;
dino.y += dino.speedy;
if(dino.y >= SCREEN_HEIGHT - 10) dino.ground();
return;
}
// array to store all obst obj
Box boxes[5];
// main dino
Deno dinos[1];
void step(){
//cleare buffer
display.clearDisplay();
//loop through boxes
for(int i=0;i<5;i++){
Serial.print("No: ");
Serial.print(i);
Serial.print(", x: ");
Serial.print(boxes[i].x);
Serial.print(", y: ");
Serial.print(boxes[i].y);
Serial.print(", inframe: ");
Serial.println(boxes[i].inframe);
//if in frame render
if(boxes[i].inframe) rendNUpdateBox(boxes[i]);
//if cross line 88 86
if((boxes[i].x <=SCREEN_WIDTH - 40)&&(boxes[i].x >SCREEN_WIDTH - 42)){
Serial.println("8888888888888888888");
//look for one reserve box and spawn it
for(int j=0;j<5;j++){
if(!boxes[j].inframe) {
// Serial.println("8888888888888888888");
boxes[j].spawn();
break;
}
}
}
}
//display buffer
display.display();
return ;
}
void step2(){
//cleare buffer
display.clearDisplay();
//loop through boxes
for(int i=0;i<1;i++){
dinos[i].jump();
Serial.print("is_grounded: ");
Serial.print(dinos[i].is_grounded);
Serial.print(", x: ");
Serial.print(dinos[i].x);
Serial.print(", y: ");
Serial.print(dinos[i].y);
Serial.print(", speedy: ");
Serial.println(dinos[i].speedy);
//if in frame render
if(dinos[i].inframe) rendNUpdateDino(dinos[i]);
if(dinos[i].is_grounded) dinos[0].start_jump(4);
}
//display buffer
display.display();
return ;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// Initialize the display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Halts the program
}
// Clear the buffer
//display.display();
delay(1000);
display.clearDisplay();
// Display text
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("Hello, ESP32!"));
display.display(); // Show the buffer on the screen
boxes[0].spawn();
Serial.print("is_grounded: ");
Serial.println(dinos[0].is_grounded);
dinos[0].start_jump(4);
Serial.print("is_grounded: ");
Serial.println(dinos[0].is_grounded);
delay(2000);
}
void loop() {
step();
// Serial.println(random(10, 20));
delay(10);
}