#include <TFT_eSPI.h>
#include <SPI.h> // this is needed for display
#include <Wire.h> // this is needed for FT6206
#include <Adafruit_FT6206.h>
#include <QList.h>
#include <ArxSmartPtr.h>
float PIE = 3.14;
// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ctp = Adafruit_FT6206();
//320,240
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 15
#define TFT_DC 2
#define TFT_MOSI 23
#define TFT_SCLK 18
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define ILI9341_RED TFT_RED
#define ILI9341_YELLOW TFT_YELLOW
#define ILI9341_GREEN TFT_GREEN
#define ILI9341_CYAN TFT_CYAN
#define ILI9341_BLUE TFT_BLUE
#define ILI9341_MAGENTA TFT_MAGENTA
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
#include "random.h";
TFT_eSPI screen = TFT_eSPI();
short fps = 0;
long time_now = 0;
int player1 = 0;
int player2 = 0;
float x = 320 / 2;
float y = 240 / 2;
float paddle1Y = 240 / 2;
float paddle2Y = 240 / 2;
int size = 10;
float direction = 30;
float speed = 0.8;
int color = TFT_YELLOW;
void setup() {
pinMode(21, INPUT);
pinMode(22, INPUT);
screen.init();
screen.setRotation(1);
screen.fillScreen(TFT_BLACK);
screen.setTextColor(TFT_BLACK); // Background is not defined so it is transparent
}
void drawCircle() {
screen.fillCircle(x, y, size, color);
screen.drawCircle(x, y, size, TFT_BLACK);
screen.drawCircle(x, y, size + 1, TFT_BLACK);
}
void drawPaddle1() {
screen.fillRect(0, paddle1Y - 10, 10, 10, TFT_BLACK);
screen.fillRect(0, paddle1Y, 10, 100, TFT_RED);
screen.fillRect(0, paddle1Y + 100, 10, 100, TFT_BLACK);
}
void drawPaddle2() {
screen.fillRect(310, paddle2Y - 10, 10, 10, TFT_BLACK);
screen.fillRect(310, paddle2Y, 10, 100, TFT_GREEN);
screen.fillRect(310, paddle2Y + 100, 10, 100, TFT_BLACK);
}
void loop() {
fps++; //add 1 to fps per loop
if (millis() > time_now + 1000) {
screen.setTextColor(TFT_WHITE, TFT_BLACK); // Set text colour to white and background to black
screen.drawString("FPS: " + String(fps) + " ", 10, 0, 2);
screen.drawString("Player1: " + String(player1) + " ", 10, 20, 2);
screen.drawString("Player2: " + String(player2) + " ", 10, 40, 2);
time_now = millis();
fps = 0;
}
drawCircle(); //this draws the circle
drawPaddle1(); //this draws the paddle1
drawPaddle2(); //this draws the paddle2
if (digitalRead(22) == true) {
if (paddle2Y < 240 - 100) {
paddle2Y++;
}
} else {
if (paddle2Y > 0) {
paddle2Y--;
}
}
if (digitalRead(21) == true) {
if (paddle1Y < 240 - 100) {
paddle1Y++;
}
} else {
if (paddle1Y > 0) {
paddle1Y--;
}
}
x = x + (cos(direction * 3.1415 / 180) * speed);
y = y + (sin(direction * 3.2415 / 180) * speed);
if (x < size + 10) {
if (y > paddle1Y && y < (paddle1Y + 100)) {
direction = (180 - direction);
} else {
screen.fillCircle(x, y, size, TFT_BLACK);
x = 320 / 2;
y = 240 / 2;
player2++;
}
}
if (x > (320 - size - 10)) {
if (y > paddle2Y && y < (paddle2Y + 100)) {
direction = (180 - direction);
} else {
screen.fillCircle(x, y, size, TFT_BLACK);
x = 320 / 2;
y = 240 / 2;
player1++;
}
}
if (y < size + 20) {
direction = -direction;
}
if (y > (240 - size)) {
direction = -direction;
}
}