#include "gameEngine.h" // Assuming your provided code is in a file named "gameEngine.h"
// Constants for the game
const byte SCREEN_HEIGHT = 8;
const byte SCREEN_WIDTH = 8;
const byte PADDLE_HEIGHT = 2;
const byte PADDLE_WIDTH = 1;
const byte PLAYER_1_UP_PIN = 9; // Pin for player 1 paddle up movement
const byte PLAYER_1_DOWN_PIN = 10; // Pin for player 1 paddle down movement
const byte PLAYER_2_UP_PIN = 2; // Pin for player 2 paddle up movement
const byte PLAYER_2_DOWN_PIN = 3; // Pin for player 2 paddle down movement
const byte BALL_SIZE = 1;
bool multiplayer = false; // Set to true for multiplayer, false for AI control
Window gameWindow(SCREEN_HEIGHT, SCREEN_WIDTH, 6); // PIN 6 for NeoPixel matrix
// Create sprites for paddles and ball
Color paddleColor(0, 255, 0); // Green color for paddles
Color ballColor(255, 255, 0); // Yellow color for ball
byte paddleAlpha[PADDLE_HEIGHT][PADDLE_WIDTH] = {
{127},
{127}
};
Color paddleSprite[PADDLE_HEIGHT][PADDLE_WIDTH] = {
{paddleColor},
{paddleColor}
};
byte ballAlpha[BALL_SIZE][BALL_SIZE] = {
{127}
};
Color ballSprite[BALL_SIZE][BALL_SIZE] = {
{ballColor}
};
const Color backgroundColor = {255, 255, 255};
// Create the paddles and ball
gameObject player1(Box<float>(0, 0, PADDLE_HEIGHT-1, PADDLE_WIDTH-1), Sprite(paddleSprite, paddleAlpha), SCREEN_HEIGHT/2.f, 0);
gameObject player2(Box<float>(0, 0, PADDLE_HEIGHT-1, SCREEN_WIDTH - 1), Sprite(paddleSprite, paddleAlpha), SCREEN_HEIGHT/2.0, SCREEN_WIDTH-1);
gameObject ball(Box<float>(0, 0, BALL_SIZE-1, BALL_SIZE-1), Sprite(ballSprite, ballAlpha), SCREEN_HEIGHT / 2, SCREEN_WIDTH / 2);
// Initialize game variables
const float ballSpeedX = 8.5f, ballSpeedY = 8.5f;
float p1Speed = 6.5f, p2Speed = multiplayer ? p1Speed : p1Speed-1;
void setup() {
pinMode(PLAYER_1_UP_PIN, INPUT);
pinMode(PLAYER_1_DOWN_PIN, INPUT);
if (multiplayer) {
pinMode(PLAYER_2_UP_PIN, INPUT);
pinMode(PLAYER_2_DOWN_PIN, INPUT);
}
gameWindow.setBackgroundColor(backgroundColor);
gameWindow.setBrightness(0.5);
ball.vx = ballSpeedX, ball.vy = ballSpeedY;
}
void loop() {
calcDT();
// Player 1 movement
if (digitalRead(PLAYER_1_UP_PIN) == HIGH && player1.x > 0) {
player1.vx = -p1Speed;
} else if (digitalRead(PLAYER_1_DOWN_PIN) == HIGH && player1.x < SCREEN_HEIGHT - PADDLE_HEIGHT) {
player1.vx = p1Speed;
} else {
player1.vx = 0;
}
// Player 2 movement
if (multiplayer) {
// Human control for player 2
if (digitalRead(PLAYER_2_UP_PIN) == HIGH && player2.x > 0) {
player2.vx = -p2Speed;
} else if (digitalRead(PLAYER_2_DOWN_PIN) == HIGH && player2.x < SCREEN_HEIGHT - PADDLE_HEIGHT) {
player2.vx = p2Speed;
} else {
player2.vx = 0;
}
} else {
// Simple AI control for player 2
if (ball.x < player2.x && player2.x > 0) {
player2.vx = -p2Speed;
} else if (ball.x > player2.x + PADDLE_HEIGHT - 1 && player2.x < SCREEN_HEIGHT - PADDLE_HEIGHT) {
player2.vx = p2Speed;
} else {
player2.vx = 0;
}
}
player1.x += player1.vx*dt;
player2.x += player2.vx*dt;
if(player1.intersects(ball))
player1.vx = 0;
if(player2.intersects(ball))
player2.vx = 0;
player1.x -= player1.vx*dt;
player2.x -= player2.vx*dt;
// Ball collision with top and bottom walls
if (ball.x <= 0 || ball.x >= SCREEN_HEIGHT - BALL_SIZE) {
ball.vx *= -1;
}
// Ball collision with paddles
if (ball.intersects(player1)) {
ball.vy = abs(ball.vy);
}
else if(ball.intersects(player2))
ball.vy = -abs(ball.vy);
// Ball out of bounds
if (ball.y < 0 || ball.y >= SCREEN_WIDTH) {
// Reset the ball position to the center
ball.x = SCREEN_HEIGHT / 2;
ball.y = SCREEN_WIDTH / 2;
ball.vx = ballSpeedX;
ball.vy = ballSpeedY;
}
// Update paddle and ball positions
player1.update();
player2.update();
ball.update();
// Clear the window and render objects
gameWindow.clear();
player1.render(gameWindow);
player2.render(gameWindow);
ball.render(gameWindow);
gameWindow.render();
prevTime = millis();
delay(SPF*1000);
}