#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
int cs = 10;
int dc = 9;
int rst = 8;
Adafruit_ILI9341 display = Adafruit_ILI9341(cs, dc, rst);
//Variavéis Accionador
int mSec;
int mSecLimit;
int speedVal = 0;
//Variavéis Jogador 1
int player1Up = 3;
int player1Down = 2;
int player1PosX = 20;
int player1PosY = 64;
int oldPlayer1PosY;
//Variavéis Jogador 2
int player2Up = 5;
int player2Down = 4;
int player2PosX = 140;
int player2PosY = 64;
int oldPlayer2PosY;
//Variavéis Bola
int ballPosX = 80;
int ballPosY = 64;
int oldBallPosX;
int oldBallPosY;
int ballDirectionX = 1;
int ballDirectionY = 1;
char player1ScorePrint[2];
int player1Score = 1;
char player2ScorePrint[3];
int player2Score = 1;
void setup() {
//Controlos dos jogadores
pinMode(player1Up, INPUT);
pinMode(player1Down, INPUT);
pinMode(player2Up, INPUT);
pinMode(player2Down, INPUT);
//display Screen
display.begin();
display.fillScreen(0, 0, 0);
}
//Accionador
void loop() {
mSec ++;
mSecLimit = (5000 - speedVal);
if (mSec == mSecLimit) {
mSec = 0;
}
if (mSecLimit < 500) {
speedVal == 500;
}
if (mSec == (mSecLimit -= 1)) {
playerControls();
playerVisuals();
ballPhysics();
ballVisuals();
gameScore();
/*
display.fill(255, 255, 255);
display.rect(80, 0, 1, 128);
*/
display.fillRect(80, 0, 1, 128, WHITE)
if (mSecLimit < 1) {
speedVal == 0;
}
}
}
//Controlos dos Jogadores
void playerControls() {
if (digitalRead(player1Up)) {
player1PosY += 1;
}
if (digitalRead(player1Down)) {
player1PosY -= 1;
}
if (player1PosY > 108) {
player1PosY = 108;
}
if (player1PosY < 0) {
player1PosY = 0;
}
if (digitalRead(player2Up)) {
player2PosY += 1;
}
if (digitalRead(player2Down)) {
player2PosY -= 1;
}
if (player2PosY > 108) {
player2PosY = 108;
}
if (player2PosY < 0) {
player2PosY = 0;
}
}
//Gráficos Jogadores
void playerVisuals() {
if (oldPlayer1PosY != player1PosY) {
/*
display.fill(0, 0, 0);
display.rect(player1PosX, oldPlayer1PosY, 2, 25);
*/
display.fillRect(player1PosX, oldPlayer1PosY, 2, 25, BLACK);
}
/*
display.fill(255, 0, 0);
display.rect(player1PosX, player1PosY, 2, 25);
*/
display.fillRect(player1PosX, player1PosY, 2, 25, RED);
if (oldPlayer2PosY != player2PosY) {
/*
display.fill(0, 0, 0);
display.rect(player2PosX, oldPlayer2PosY, 2, 25);
*/
display.fillRect(player2PosX, oldPlayer2PosY, 2, 25, BLACK);
}
/*
display.fill(255, 255, 0);
display.rect(player2PosX, player2PosY, 2, 25);
*/
display.fillRect(player2PosX, oldPlayer2PosY, 2, 25, YELLOW);
oldPlayer1PosY = player1PosY;
oldPlayer2PosY = player2PosY;
}
//Comportamento da Bola
void ballPhysics() {
//Se Jogador 2 Marcar
if (ballPosX > 155) {
player1Score += 1;
ballDirectionX = -ballDirectionX;
ballPosX = 120;
ballPosY = 64;
player1PosX = 20;
player1PosY = 64;
player2PosX = 140;
player2PosY = 64;
mSec = 0;
speedVal = 0;
mSecLimit = 5000;
tone(6, 800, 10);
delay(100);
tone(6, 1000, 40);
delay(100);
tone(6, 1500, 80);
delay(100);
/*
display.fill(255, 0, 0);
display.rect(0, 0, 160, 128);
*/
display.fillRect(0, 0, 160, 128, RED);
display.setTextSize(5);
display.stroke(0, 0, 0);
display.text(player1ScorePrint, 0, 20);
delay(1000);
display.noStroke();
/*
display.fill(0, 0, 0);
display.rect(0, 0, 160, 128);
*/
display.fillRect(0, 0, 160, 128, BLACK);
}
//Se Jogador 2 Marcar
if (ballPosX < 0) {
player2Score += 1;
ballDirectionX = -ballDirectionX;
ballPosX = 30;
ballPosY = 64;
player1PosX = 20;
player1PosY = 64;
player2PosX = 140;
player2PosY = 64;
mSec = 0;
speedVal = 0;
mSecLimit = 5000;
tone(6, 800, 10);
delay(100);
tone(6, 1000, 40);
delay(100);
tone(6, 1200, 80);
delay(100);
/*
display.fill(255, 255, 0);
display.rect(0, 0, 160, 128);
*/
display.fillRect(0, 0, 160, 128, YELLOW);
display.setTextSize(5);
display.stroke(0, 0, 0);
display.text(player2ScorePrint, 0, 20);
delay(1000);
display.noStroke();
/*
display.fill(0, 0, 0);
display.rect(0, 0, 160, 128);
*/
display.fillRect(0, 0, 160, 128, BLACK);
}
//Bola Intersecta Limites do Ecrã
if (ballPosY > 128 || ballPosY < 0) {
ballDirectionY = -ballDirectionY;
}
//Bola Intersecta Jogador 1
if (inPlayer1(ballPosX, ballPosY, player1PosX, player1PosY, 2, 25)) {
ballDirectionY = ballDirectionY;
ballDirectionX = -ballDirectionX;
tone(6, 1500, 10);
mSec = 0;
speedVal += 200;
}
//Bola Intersecta Jogador 2
if (inPlayer2(ballPosX, ballPosY, player2PosX, player2PosY, 2, 25)) {
ballDirectionY = ballDirectionY;
ballDirectionX = -ballDirectionX;
tone(6, 1200, 10);
mSec = 0;
speedVal += 200;
}
//Direcção da Bola
ballPosX += ballDirectionX;
ballPosY += ballDirectionY;
}
//Gráficos da Bola
void ballVisuals() {
//display.fill(0, 0, 0);
if (oldBallPosX != ballPosX || oldBallPosY != ballPosY) {
//display.rect(oldBallPosX, oldBallPosY, 6, 6);
display.fillRect(oldBallPosX, oldBallPosY, 6, 6, BLACK);
}
/*
display.fill(255, 255, 255);
display.rect(ballPosX, ballPosY, 6, 6);
*/
display.fillRect(ballPosX, ballPosY, 6, 6, WHITE);
oldBallPosX = ballPosX;
oldBallPosY = ballPosY;
}
//Pontuação
void gameScore() {
String player1ScoreVal = String(player1Score);
player1ScoreVal.toCharArray(player1ScorePrint, 2);
String player2ScoreVal = String(player2Score);
player2ScoreVal.toCharArray(player2ScorePrint, 3);
}
//Verificador de colisão entre jogador-bola
boolean inPlayer1(int ballX, int ballY, int playerRectX, int playerRectY, int playerRectHeight, int playerRectWidth) {
boolean result = false;
if ((ballX >= playerRectX && ballX <= (playerRectX + playerRectHeight)) &&
(ballY >= playerRectY && ballY <= (playerRectY + playerRectWidth))) {
result = true;
}
return result;
}
boolean inPlayer2(int ballX, int ballY, int playerRectX, int playerRectY, int playerRectHeight, int playerRectWidth) {
boolean result = false;
if ((ballX >= playerRectX && ballX <= (playerRectX + playerRectHeight)) &&
(ballY >= playerRectY && ballY <= (playerRectY + playerRectWidth))) {
result = true;
}
return result;
}