#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
int Screen_width = 128;
int Screen_ht = 64;
int reset = -1;
int address = 0x3C;
int x11 = 64; int y11=32; // position coordite for center of the ball
int x = 55; int y = 60; // top left corner coordinates of the particle
int x_speed = 1; int y_speed = 1; //speed of the ball
int score=0;
int k1,k2;
Adafruit_SSD1306 display(Screen_width,Screen_ht,&Wire,reset);
void setup()
{
Serial.begin(9600);
pinMode(16, INPUT_PULLDOWN);
pinMode(18, INPUT_PULLDOWN);
pinMode(19, OUTPUT);
if(!display.begin(SSD1306_SWITCHCAPVCC,address))
{
Serial.println("Display not connected");
for(;;);
}
display.clearDisplay();
}
void loop()
{
//Current score display on screen
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(115,1);
display.print(score);
//code for movement of paddle
k1=digitalRead(18);
if(k1==1 && x+20<=127)
{
x=x+3.5;
Serial.println("Right");
}
k2=digitalRead(16);
if(k2==1 && x>=1)
{
x=x-3.5;
Serial.println("Left");
}
//boundary conditions
if(2<x11<125 && 0<y11)
{
x_speed=x_speed;
y_speed=y_speed;
}
if(x11==125||x11==2) //left and right edges
{
x_speed=-x_speed;
y_speed=y_speed;
}
if(y11==2) //top edges
{
x_speed=x_speed;
y_speed=-y_speed;
}
if(x<=x11 && x11<=x+20 && y11+2==59)
{
x_speed=x_speed;
y_speed=-y_speed;
score = score + 1;
digitalWrite(19,HIGH);
delay(20);
digitalWrite(19,LOW);
}
x11=x11+x_speed;
y11=y11+y_speed;
display.fillCircle(x11, y11, 2, WHITE);
display.fillRect(x, y, 20, 4, WHITE);
display.display();
display.clearDisplay();
if(y11>=61)
{
digitalWrite(19, HIGH);
delay(1000);
digitalWrite(19, LOW);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(25,25);
display.print("SCORE:");
display.print(score);
if(k1==1||k2==1)
{
display.clearDisplay();
x11=64;
y11=32;
score=0;
x=55;
y=60;
}
}
}