#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
int k1;
int k2;
int x1=64;
int z1=32;
int x_speed=1;
int z_speed=1;
int score=0;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using I2C
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int x=55;
int y=60;
void setup()
{ pinMode(16,INPUT);
pinMode(15,INPUT);
Serial.begin(9600);
// initialize the OLED object
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
}
void loop(){
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor( 115, 1);
display.print(score);
k1=digitalRead(15);
if(k1==1 && x+20<=127){
x=x+3;
Serial.println("Right");
delay(10);
digitalWrite(15,LOW);
}
k2=digitalRead(16);
if(k2==1 && x>=1){
x=x-3;
Serial.println("left");
delay(10);
digitalWrite(16,LOW);
}
if(2<x1<125 && 0<z1){
x_speed=x_speed;
z_speed=z_speed;
}
if(x<=x1 && x1<=x+20 && z1+2==59){
z_speed=-z_speed;
x_speed=x_speed;
score=score+1;
}
if(x1==125 || x1==2){
x_speed=-x_speed;
z_speed=z_speed;
}
if(z1==2 ){
z_speed=-z_speed;
x_speed=x_speed;
}
x1=x1+x_speed;
z1=z1+z_speed;
display.fillCircle(x1, z1 , 2 ,WHITE);
display.fillRect(x,y,20,4, WHITE);
display.display();
display.clearDisplay();
while(z1==61){
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor( 25,25);
display.print("score ");
display.print(score);
display.display();
}
}