This is the code for the game:
// the setup function runs once when you press reset or power the board
#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Waveshare_ILI9486.h>
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// Declare variables
int xpos;
int ypos;
int xdir;
int ydir;
int rect;
int i = 0;
int barx = 135;
int bary = 350;
int score = 0;
int spd = 5;
namespace
{
Waveshare_ILI9486 Waveshield;
}
void setup()
{
SPI.begin();
Waveshield.begin();
Waveshield.setRotation(0);
Waveshield.setTextSize(0);
Waveshield.setRotation(0);
xpos = 0;
ypos = 0;
xdir = 1;
ydir = 1;
Serial.begin(9600);
}
// the loop function runs over and over again until power down or reset
void loop() {
// Waveshield.fillCircle(xpos, ypos, 20, BLACK);
// Step 1: This gets the coordinates that the user is touching
TSPoint p = Waveshield.getPoint();
//Serial.println("the x is " + String(p.x));
// Step 2: This enables the ball to bounce left and right
if (xdir == 1) {
xpos += spd;
}
if (xdir == 0) {
xpos -= spd;
}
if (xpos > 320) {
xdir = 0;
}
if (xpos < 0) {
xdir = 1;
}
// Step 3: This enables the ball to bounce up and down
if (ydir == 0) {
ypos -= spd;
}
if (ydir == 1) {
ypos += spd;
}
if (ypos > 480) {
ydir = 0;
}
if (ypos < 65) {
ydir = 1;
}
// Step 4: This draws and deletes the ball
Waveshield.fillCircle(xpos, ypos, 20, WHITE);
Waveshield.fillCircle(xpos, ypos, 20, BLACK);
// Step 5: This creates the paddle
Waveshield.fillRect(barx, bary, 50, 20, WHITE);
// Step 6: This creates the left and right control buttons
Waveshield.fillCircle(300, 460 , 20, WHITE);
Waveshield.fillCircle(20, 460 , 20, WHITE);
// Step 7A: This detects if the right button is pressed, and moves the paddle right
if ( p.x > 850 && p.x < 950 && p.y > 850 && p.y < 970) {
//Serial.println("ok righty");
Waveshield.fillRect(barx, bary, 50, 20, BLACK);
barx += 10;
Waveshield.fillRect(barx, bary, 50, 20, WHITE);
}
// Step 7B: This detects if the left button is pressed, and moves the paddle left
if ( p.x > 1 && p.x < 200 && p.y > 850 && p.y < 970) {
//Serial.println("ok lefty");
Waveshield.fillRect(barx, bary, 50, 20, BLACK);
barx -= 10;
Waveshield.fillRect(barx, bary, 50, 20, WHITE);
}
// Step 8: This detects if the ball is touching the paddle and changes the ball to move upwards
if (ypos > bary && ypos < bary + 30 && barx < xpos && xpos < barx + 75) {
//Serial.println("Da ball hit da pdddle tingy");
score++;
spd++;
Waveshield.fillRect(0, 0, 320, 40, BLUE);
Waveshield.setTextSize(4);
Waveshield.setTextColor(WHITE);
Waveshield.setCursor(160, 0);
Waveshield.print(score);
ydir = 0;
}
// Step 9: This checks if the ball has gone below the bar and prints the losing message
if (ypos > 400) {
Serial.print("YOU LOSE");
score = 0;
spd = 5;
Waveshield.setCursor(60, 140);
Waveshield.setTextSize(4);
Waveshield.setTextColor(WHITE);
Waveshield.print("YOU LOSE");
}
}