#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include<Adafruit_GFX.h>
int screen_width = 128;
int screen_height = 64;
int reset = -1 ;
int address = 0x3C;
int leftbutton = 16;
int rightbutton = 18;
int led = 19;
int a = 64;
int b = 32;
int x = 55;
int y = 60;
int x_speed = 1, y_speed = 1;
int score = 0;
int k1, k2;
Adafruit_SSD1306 display(screen_width, screen_height, &Wire, reset);
void setup(){
pinMode(leftbutton, INPUT_PULLDOWN);
pinMode(rightbutton, INPUT_PULLDOWN);
pinMode(led, OUTPUT);
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC,address)){
Serial.println("display not connected");
for(;;);
}
display.clearDisplay();
}
void loop(){
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(115, 1);
display.print(score);
k1=digitalRead(16);
if(k1 == 1 && x >= 3.5){
x = x - 3.5;
Serial.println("Left");
}
k2=digitalRead(18);
if(k2 == 1 && x + 20 <= 127){
x = x + 3.5;
Serial.println("Right");
}
if(2 < a < 125 && 0 < b){
x_speed = x_speed;
y_speed = y_speed;
}
if(a == 125 || a == 2){
x_speed = -x_speed;
y_speed = y_speed;
}
if(b ==2 ){
x_speed = x_speed;
y_speed = -y_speed;
}
if(x <= a && a <= x+20 && b+2 == 59){
x_speed = x_speed;
y_speed = -y_speed;
score = score + 1;
digitalWrite(led, HIGH);
delay(20);
digitalWrite(led, LOW);
}
a = a + x_speed;
b = b + y_speed;
display.fillCircle(a, b, 2, WHITE);
display.fillRect(x, y, 20, 4, WHITE);
display.display();
display.clearDisplay();
if(b>=61){
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(25, 25);
display.print("Score ");
display.print(score);
display.display();
delay(2000);
display.clearDisplay();
a=64;
b=32;
score=0;
x=55;
y=60;
}
}