#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// If using the breakout, change pins as desired
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
void setup() {
Serial.begin(9600);
Serial.println("ILI9341 Test!");
tft.begin();
Serial.println(F("Done!"));
tft.setRotation(1);
int h = tft.height();
tft.drawRect(0,0, tft.width(), tft.height(), ILI9341_WHITE);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
}
int ballx = 12;
int bally = 66;
int xVelocity = 1;
int yVelocity = -1;
int wallLeft = 0;
int wallRight = 0;
int timeInterval = 0;
bool game = true;
int drawWalls(){
if(wallLeft != map(analogRead(A0), 0, 1023, 17, tft.height()-17)){
tft.drawLine(10, wallLeft - 16, 10, wallLeft + 16, ILI9341_BLACK);
wallLeft = map(analogRead(A0), 0, 1023, 17, tft.height()-17);
tft.drawLine(10, wallLeft - 16, 10, wallLeft + 16, ILI9341_WHITE);
}
if(wallRight != map(analogRead(A1), 0, 1023, 17, tft.height()-17)){
tft.drawLine(tft.width()-10, wallRight - 16, tft.width()-10, wallRight + 16, ILI9341_BLACK);
wallRight = map(analogRead(A1), 0, 1023, 17, tft.height()-17);
tft.drawLine(tft.width()-10, wallRight - 16, tft.width()-10, wallRight + 16, ILI9341_WHITE);
}
}
int between(bool Left = false, bool right = false){
Serial.println("finished 5 ssecond brreak");
for(int i; i<=200; i++){
drawWalls();
delay(25);
}
Serial.println("finished 5 ssecond brreak");
if(Left){
ballx = tft.width()-13;
bally = wallRight;
}else{
ballx = 12;
bally = wallLeft;
}
tft.fillCircle(ballx, bally, 1, ILI9341_WHITE);
while (true){
if(right && wallLeft > map(analogRead(A0), 0, 1023, 17, tft.height()-17)){
Serial.println("1");
yVelocity = -1;
xVelocity = 1;
break;
}
if(right && wallLeft < map(analogRead(A0), 0, 1023, 17, tft.height()-17)){
Serial.println("2");
yVelocity = 1;
xVelocity = 1;
break;
}
if(Left && wallRight > map(analogRead(A1), 0, 1023, 17, tft.height()-17)){
Serial.println("3");
yVelocity = -1;
xVelocity = -1;
break;
}
if(Left && wallRight < map(analogRead(A1), 0, 1023, 17, tft.height()-17)){
Serial.println("4");
yVelocity = 1;
xVelocity = -1;
break;
}
drawWalls();
delay(25);
}
}
void loop(void) {
drawWalls();
if(game){
tft.fillCircle(ballx, bally, 1, ILI9341_BLACK);
}
ballx += xVelocity;
bally += yVelocity;
if(bally>=tft.height()-2){
ballx -= xVelocity;
bally -= yVelocity;
yVelocity *= -1;
ballx += xVelocity;
bally += yVelocity;
}else if(bally<2){
yVelocity *= -1;
ballx += xVelocity;
bally += yVelocity;
}
if(ballx == 11 && bally>=wallLeft-16 && bally <= wallLeft + 16){
xVelocity *= -1;
ballx += xVelocity;
bally += yVelocity;
}else if(ballx == tft.width()-11 && bally >= wallRight - 16 && bally <= wallRight+16){
xVelocity *= -1;
ballx += xVelocity;
bally += yVelocity;
}
if(ballx<=10 && xVelocity != 0 && yVelocity != 0){
Serial.println("right side point!");
xVelocity = 0;
yVelocity = 0;
between(true);
}else if(ballx>=tft.width()-10 && xVelocity != 0 && yVelocity != 0){
Serial.println("left side point!");
xVelocity = 0;
yVelocity = 0;
between(false, true);
}
tft.fillCircle(ballx, bally, 1, ILI9341_WHITE);
delay(10);
}