#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
uint8_t ball_x = 64, ball_y = 32;
uint8_t scoreLeft = 0;
uint8_t scoreRight = 0;
int16_t paddleNumLeft = 32;
int16_t paddleNumRight = 32;
bool horizontal = false;
bool vertical = false;
bool win = false;
String winner;
int16_t paddleNum = 32;
int16_t cpuPaddleNum = 32;
void drawCourt(){
display.drawRect(0, 0, 128, 64, WHITE);
}
void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(6, INPUT);
pinMode(5, INPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
}
void displayScoreLeft() {
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(2,2);
display.print(scoreLeft);
}
void displayScoreRight() {
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(118,2);
display.print(scoreRight);
}
void displayWin() {
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(15,15);
display.print(winner);
}
void loop() {
start1PlayerGame();
// display.clearDisplay();
// display.setTextSize(1);
// display.setTextColor(SSD1306_WHITE);
// display.setCursor(20, 10);
// display.println("Choose mode:");
// display.setCursor(35, 30);
// display.println("1 Player");
// display.setCursor(35, 40);
// display.println("2 Players");
// display.display();
}
void ballRight() {
display.drawPixel(ball_x, ball_y, WHITE);
ball_x += 1;
}
void ballLeft() {
display.drawPixel(ball_x, ball_y, WHITE);
ball_x -= 1;
}
void ballDown() {
display.drawPixel(ball_x, ball_y, WHITE);
ball_y += 1;
}
void ballUp() {
display.drawPixel(ball_x, ball_y, WHITE);
ball_y -= 1;
}
void leftPaddle() {
// x, y, h, color
display.drawFastVLine(10, paddleNumLeft, 20, WHITE);
}
void rightPaddle() {
// x, y, h, color
display.drawFastVLine(115, paddleNumRight, 20, WHITE);
}
void paddle() {
// x, y, h, color
display.drawFastVLine(10, paddleNum, 20, WHITE);
}
void cpuPaddle() {
// x, y, h, color
display.drawFastVLine(115, cpuPaddleNum, 20, WHITE);
}
void start1PlayerGame() {
// Your logic for the 1 player game goes here
// This function should handle the game mechanics for 1 player mode
uint8_t ball_x = 64, ball_y = 32;
uint8_t score = 0;
bool horizontal = false;
bool vertical = false;
bool win = false;
String winner = "you win!";
display.clearDisplay();
if(win) {
displayWin();
}else {
displayScoreLeft();
}
cpuPaddle();
paddle();
drawCourt();
//move paddle up
if(digitalRead(3) == LOW && paddleNum > 1) {
paddleNum -= 1;
digitalWrite(3, HIGH);
}
//move paddle down
if(digitalRead(2) == LOW && paddleNum < 43) {
paddleNum += 1;
digitalWrite(2, HIGH);
}
// ball movement logic
if (!horizontal) {
ballRight();
} else {
ballLeft();
}
if (!vertical) {
ballDown();
} else {
ballUp();
}
//tally score
if(ball_x <= 1) {
score = 0;
}
if((ball_x == 11 && ball_y <= paddleNum + 20 && ball_y >= paddleNum)) {
score += 1;
if(score == 2) {
win = true;
}
}
//bounce ball off right paddle
if(ball_x >= 114) {
horizontal = true;
}
//bounce ball off left wall
if(ball_x <= 1) {
horizontal = false;
}
//bounce ball off bottom wall
if(ball_y >= 61) {
vertical = true;
}
//bounce ball off top wall
if(ball_y <= 1) {
vertical = false;
}
//bounce ball off back of paddle
if(ball_x == 9 && ball_y <= paddleNum + 20 && ball_y >= paddleNum) {
horizontal = true;
}
//bounce ball off front of paddle
if(ball_x == 11 && ball_y <= paddleNum + 20 && ball_y >= paddleNum) {
horizontal = false;
}
//cpuPaddle movement
if(ball_y <= 10) {
cpuPaddleNum = 1;
}else if(ball_y >= 51) {
cpuPaddleNum = 43;
}else{
cpuPaddleNum = ball_y - 10;
}
display.display();
}
void start2PlayersGame() {
// Your logic for the 2 players game goes here
// This function should handle the game mechanics for 2 players mode
display.clearDisplay();
if(win) {
displayWin();
}else {
displayScoreLeft();
displayScoreRight();
}
rightPaddle();
leftPaddle();
drawCourt();
//move left paddle up
if(digitalRead(3) == LOW && paddleNumLeft > 1) {
paddleNumLeft -= 1;
digitalWrite(3, HIGH);
}
//move left paddle down
if(digitalRead(2) == LOW && paddleNumLeft < 43) {
paddleNumLeft += 1;
digitalWrite(2, HIGH);
}
//move right right paddle up
if(digitalRead(6) == LOW && paddleNumRight > 1) {
paddleNumRight -= 1;
digitalWrite(6, HIGH);
}
//move right right paddle down
if(digitalRead(5) == LOW && paddleNumRight < 43) {
paddleNumRight += 1;
digitalWrite(5, HIGH);
}
// ball movement logic
if (!horizontal) {
ballRight();
} else {
ballLeft();
}
if (!vertical) {
ballDown();
} else {
ballUp();
}
//tally scoreLeft
if(ball_x <= 1) {
scoreLeft = 0;
}
if((ball_x == 11 && ball_y <= paddleNumLeft + 20 && ball_y >= paddleNumLeft)) {
scoreLeft += 1;
if(scoreLeft >= scoreRight + 2) {
winner = "p1 wins!";
win = true;
}
}
//tally scoreRight
if(ball_x >= 123) {
scoreRight = 0;
}
if(ball_x == 114 && ball_y <= paddleNumRight + 20 && ball_y >= paddleNumRight) {
scoreRight += 1;
if(scoreRight >= scoreLeft + 2) {
winner = "p2 wins!";
win = true;
}
}
//bounce ball off right wall
if(ball_x >= 123) {
horizontal = true;
}
//bounce ball off left wall
if(ball_x <= 1) {
horizontal = false;
}
//bounce ball off bottom wall
if(ball_y >= 61) {
vertical = true;
}
//bounce ball off top wall
if(ball_y <= 1) {
vertical = false;
}
//bounce ball off back of left paddle
if(ball_x == 9 && ball_y <= paddleNumLeft + 20 && ball_y >= paddleNumLeft) {
horizontal = true;
}
//bounce ball off front of left paddle
if(ball_x == 11 && ball_y <= paddleNumLeft + 20 && ball_y >= paddleNumLeft) {
horizontal = false;
}
//bounce ball off front of right paddle
if(ball_x == 114 && ball_y <= paddleNumRight + 20 && ball_y >= paddleNumRight) {
horizontal = true;
}
//bounce ball off back of right paddle
if(ball_x == 117 && ball_y <= paddleNumRight + 20 && ball_y >= paddleNumRight) {
horizontal = false;
}
display.display();
}