Compare commits
47 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0b3c7e0f22 | ||
|
|
f1059588ff | ||
|
|
a2f5049bfe | ||
|
|
f88de159f5 | ||
|
|
fae479ce90 | ||
|
|
d65e7ef1c8 | ||
|
|
8c402ff13a | ||
|
|
d544867269 | ||
|
|
d18415f472 | ||
|
|
cef9f09c79 | ||
|
|
a9aed6b8d0 | ||
|
|
7f63ae9a31 | ||
|
|
a6c218061d | ||
|
|
8e5fe2143c | ||
|
|
fbad1a87d6 | ||
|
|
4bb6d6d6ee | ||
|
|
6ea443d92a | ||
|
|
2838b5fd14 | ||
|
|
69a8fb9dc4 | ||
|
|
eac8c59d96 | ||
|
|
68dfce8b12 | ||
|
|
49a772f6de | ||
|
|
abc0ddb661 | ||
|
|
33e070c442 | ||
|
|
1f6e65c5ec | ||
|
|
7afa70c9d8 | ||
|
|
792c0f835c | ||
|
|
5afa726015 | ||
|
|
48fef4be86 | ||
|
|
48c2610561 | ||
|
|
3d8881ab3d | ||
|
|
d8efabd6da | ||
|
|
c774e11923 | ||
|
|
b3f6aeb3fe | ||
|
|
6e5fc8ea10 | ||
|
|
28f029ce03 | ||
|
|
cb42a45aa7 | ||
|
|
c47291e258 | ||
|
|
fec4db0c5d | ||
|
|
7c065d8799 | ||
|
|
31891bbc0e | ||
|
|
5eeb735364 | ||
|
|
a938adff31 | ||
|
|
1f143520a8 | ||
|
|
cbdfcce354 | ||
|
|
66957b5e19 | ||
|
|
f12af76845 |
8
.github/workflows/cd.yml
vendored
8
.github/workflows/cd.yml
vendored
@@ -5,6 +5,9 @@ on:
|
|||||||
tags:
|
tags:
|
||||||
- 'v*'
|
- 'v*'
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -15,13 +18,12 @@ jobs:
|
|||||||
- name: Install Arduino CLI
|
- name: Install Arduino CLI
|
||||||
run: |
|
run: |
|
||||||
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh
|
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh
|
||||||
echo "$HOME/bin" >> $GITHUB_PATH
|
echo "./bin" >> $GITHUB_PATH
|
||||||
|
|
||||||
- name: Install Board Core and Libs
|
- name: Install Board Core and Libs
|
||||||
run: |
|
run: |
|
||||||
arduino-cli core update-index
|
arduino-cli core update-index
|
||||||
arduino-cli core install arduino:renesas_uno
|
arduino-cli core install arduino:renesas_uno
|
||||||
arduino-cli lib install "Arduino_LED_Matrix"
|
|
||||||
|
|
||||||
- name: Build Binary
|
- name: Build Binary
|
||||||
run: make compile
|
run: make compile
|
||||||
@@ -30,6 +32,6 @@ jobs:
|
|||||||
uses: softprops/action-gh-release@v2
|
uses: softprops/action-gh-release@v2
|
||||||
with:
|
with:
|
||||||
files: |
|
files: |
|
||||||
build/arduino.renesas_uno.unor4wifi/arduino_pong.ino.bin
|
build/*.bin
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|||||||
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@@ -7,6 +7,8 @@ on:
|
|||||||
- 'arduino_pong.ino'
|
- 'arduino_pong.ino'
|
||||||
- 'Makefile'
|
- 'Makefile'
|
||||||
- '.github/workflows/ci.yml'
|
- '.github/workflows/ci.yml'
|
||||||
|
- 'src/*.cpp'
|
||||||
|
- 'src/*.h'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
.secrets
|
.secrets
|
||||||
.arduino_cache/
|
.arduino_cache/
|
||||||
bin/
|
bin/
|
||||||
|
build/
|
||||||
|
|||||||
7
Makefile
7
Makefile
@@ -30,8 +30,13 @@ prepare:
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
compile:
|
compile:
|
||||||
|
@mkdir -p build
|
||||||
@echo -e "$(GREEN)🛠️ Compiling $(SKETCH)...$(CLEAR)"
|
@echo -e "$(GREEN)🛠️ Compiling $(SKETCH)...$(CLEAR)"
|
||||||
@$(CLI) compile -b $(BOARD) $(SKETCH)
|
@$(CLI) compile -b $(BOARD) --output-dir build $(SKETCH)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@echo -e "$(GREEN)🛠️ Removing build files...$(CLEAR)"
|
||||||
|
@rm -rf build/
|
||||||
|
|
||||||
prepare_and_compile:
|
prepare_and_compile:
|
||||||
make prepare --no-print-directory
|
make prepare --no-print-directory
|
||||||
|
|||||||
@@ -3,11 +3,12 @@
|
|||||||
A classic implementation of the Pong game developed specifically for the Arduino UNO R4 WiFi, utilizing its built-in 12×8 LED matrix as the game screen.
|
A classic implementation of the Pong game developed specifically for the Arduino UNO R4 WiFi, utilizing its built-in 12×8 LED matrix as the game screen.
|
||||||
|
|
||||||
[](https://github.com/Dea1993/arduino_pong/actions/workflows/ci.yml)
|
[](https://github.com/Dea1993/arduino_pong/actions/workflows/ci.yml)
|
||||||
|
[](https://github.com/Dea1993/arduino_pong/actions/workflows/cd.yml)
|
||||||
[](LICENSE)
|
[](LICENSE)
|
||||||
|
|
||||||
# 📹 Preview
|
# 📹 Preview
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Youtube: https://youtu.be/ouLBTDjpKqc
|
Youtube: https://youtu.be/ouLBTDjpKqc
|
||||||
|
|
||||||
|
|||||||
360
arduino_pong.ino
360
arduino_pong.ino
@@ -1,208 +1,216 @@
|
|||||||
#include "Arduino_LED_Matrix.h"
|
#include "Arduino_LED_Matrix.h"
|
||||||
|
|
||||||
#define P1_BTN_UP 13
|
#include "src/config.h"
|
||||||
#define P1_BTN_BOTTOM 12
|
#include "src/menu.h"
|
||||||
#define P2_BTN_UP 11
|
#include "src/renderer.h"
|
||||||
#define P2_BTN_BOTTOM 10
|
#include "src/engine.h"
|
||||||
#define MATRIX_WIDTH 12
|
#include "src/paddle.h"
|
||||||
#define MATRIX_HEIGHT 8
|
#include "src/ball.h"
|
||||||
|
|
||||||
// create LED matrix object
|
|
||||||
ArduinoLEDMatrix matrix;
|
|
||||||
|
|
||||||
// initial pong frame
|
// initial pong frame matrix
|
||||||
byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = {
|
byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = {
|
||||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
|
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
|
||||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
||||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
|
||||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
// players coordinates
|
ArduinoLEDMatrix matrix;
|
||||||
int p1_start= 1;
|
|
||||||
int p1_end= 3;
|
|
||||||
|
|
||||||
int p2_start= 4;
|
|
||||||
int p2_end= 6;
|
|
||||||
|
|
||||||
// initials balls coordinates
|
|
||||||
int ball_reset_x= MATRIX_WIDTH / 2;
|
|
||||||
int ball_reset_y= MATRIX_HEIGHT / 2;
|
|
||||||
int ball_x= ball_reset_x;
|
|
||||||
int ball_y= ball_reset_y;
|
|
||||||
|
|
||||||
// initially ball has no movements
|
|
||||||
// once game/round starts, balls gets random x and y movements
|
|
||||||
int ball_move_x= 0;
|
|
||||||
int ball_move_y= 0;
|
|
||||||
|
|
||||||
int bar_length= 3;
|
|
||||||
int p1_score= 0;
|
|
||||||
int p2_score= 0;
|
|
||||||
|
|
||||||
int need_refresh= 1;
|
|
||||||
|
|
||||||
int initial_loop_delay= 200;
|
|
||||||
int loop_delay= initial_loop_delay;
|
|
||||||
// used to increase speed when game is too easy
|
|
||||||
int hits= 0;
|
|
||||||
|
|
||||||
|
bool need_refresh= true;
|
||||||
|
uint8_t hits= 0;
|
||||||
long exec_t2= millis();
|
long exec_t2= millis();
|
||||||
|
|
||||||
|
enum game_statuses : uint8_t {
|
||||||
|
MENU,
|
||||||
|
MENU_BOT_SKILLS,
|
||||||
|
TIMER,
|
||||||
|
RUN,
|
||||||
|
SCORE,
|
||||||
|
GAMEOVER,
|
||||||
|
WAIT,
|
||||||
|
};
|
||||||
|
game_statuses game_status= MENU;
|
||||||
|
|
||||||
|
|
||||||
|
Ball ball(4, 6);
|
||||||
|
|
||||||
|
Paddle* p1= nullptr;
|
||||||
|
Paddle* p2= nullptr;
|
||||||
|
HumanPaddle human_pad1(1, P1_BTN_UP, P1_BTN_BOTTOM);
|
||||||
|
HumanPaddle human_pad2(4, P2_BTN_UP, P2_BTN_BOTTOM);
|
||||||
|
BotPaddle bot_pad1(1, 0);
|
||||||
|
BotPaddle bot_pad2(4, MATRIX_WIDTH-1);
|
||||||
|
Menu menu;
|
||||||
|
|
||||||
|
// uint8_t current_gmode_idx= 0;
|
||||||
|
// bool update_menu= true;
|
||||||
|
// bool mode_selected= false;
|
||||||
|
// uint8_t current_bot_menu_idx= 0;
|
||||||
|
// bool update_menu_bot_skills= true;
|
||||||
|
|
||||||
|
Engine engine(ball, INITIAL_BALL_DELAY);
|
||||||
|
Renderer renderer(ball, frame, matrix);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
//Serial.begin(115200);
|
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
// stard LED matrix
|
// start LED matrix
|
||||||
matrix.begin();
|
matrix.begin();
|
||||||
|
|
||||||
pinMode(P1_BTN_UP, INPUT_PULLUP);
|
pinMode(P1_BTN_UP, INPUT_PULLUP);
|
||||||
pinMode(P1_BTN_BOTTOM, INPUT_PULLUP);
|
pinMode(P1_BTN_BOTTOM, INPUT_PULLUP);
|
||||||
pinMode(P2_BTN_UP, INPUT_PULLUP);
|
pinMode(P2_BTN_UP, INPUT_PULLUP);
|
||||||
pinMode(P2_BTN_BOTTOM, INPUT_PULLUP);
|
pinMode(P2_BTN_BOTTOM, INPUT_PULLUP);
|
||||||
|
|
||||||
randomSeed(analogRead(0));
|
randomSeed(millis());
|
||||||
}
|
|
||||||
|
|
||||||
void render_matrix() {
|
|
||||||
if (!need_refresh) return;
|
|
||||||
need_refresh= 0;
|
|
||||||
// clear
|
|
||||||
for (int x=0; x < MATRIX_WIDTH; x++) {
|
|
||||||
for (int y=0; y < MATRIX_HEIGHT; y++) {
|
|
||||||
frame[y][x]= 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// players coords
|
|
||||||
for (int i= p1_start; i < p1_start+bar_length; i++) {
|
|
||||||
frame[i][0]= 1;
|
|
||||||
}
|
|
||||||
for (int i= p2_start; i < p2_start+bar_length; i++) {
|
|
||||||
frame[i][MATRIX_WIDTH-1]= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ball coords
|
|
||||||
frame[ball_y][ball_x]= 1;
|
|
||||||
|
|
||||||
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
|
||||||
}
|
|
||||||
|
|
||||||
void pong_move_p1() {
|
|
||||||
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
|
|
||||||
p1_start -= 1;
|
|
||||||
need_refresh= 1;
|
|
||||||
}
|
|
||||||
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
|
|
||||||
p1_start += 1;
|
|
||||||
need_refresh= 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void pong_move_p2() {
|
|
||||||
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
|
|
||||||
p2_start -= 1;
|
|
||||||
need_refresh= 1;
|
|
||||||
}
|
|
||||||
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
|
|
||||||
p2_start += 1;
|
|
||||||
need_refresh= 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int ball_player_collision(int player) {
|
|
||||||
for (int p= player; p < player + bar_length; p++) {
|
|
||||||
if (ball_y == p) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void point_scored() {
|
|
||||||
ball_x= ball_reset_x;
|
|
||||||
ball_y= ball_reset_y;
|
|
||||||
Serial.print("P1: ");
|
|
||||||
Serial.print(p1_score);
|
|
||||||
Serial.print(" - ");
|
|
||||||
Serial.print("P2: ");
|
|
||||||
Serial.print(p2_score);
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
hits= 0;
|
|
||||||
loop_delay= initial_loop_delay;
|
|
||||||
}
|
|
||||||
|
|
||||||
void move_ball() {
|
|
||||||
need_refresh= 1;
|
|
||||||
if (ball_x < 0 || ball_x > MATRIX_WIDTH-1 || ball_y < 0 || ball_y > MATRIX_HEIGHT-1) {
|
|
||||||
// ball out of matrix limits
|
|
||||||
ball_x= ball_reset_x;
|
|
||||||
ball_y= ball_reset_y;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if ball is not moving, get random direction
|
|
||||||
// this is the initial position
|
|
||||||
if (ball_move_x == 0 || ball_move_y == 0) {
|
|
||||||
// extract random number between 0 or 1 to select the directions
|
|
||||||
if (random(2) == 0) ball_move_x= 1;
|
|
||||||
else ball_move_x= -1;
|
|
||||||
if (random(2) == 0) ball_move_y= 1;
|
|
||||||
else ball_move_y= -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (ball_x == 0) {
|
|
||||||
// if p1 collision: reverse x, go left
|
|
||||||
if (!ball_player_collision(p1_start)) {
|
|
||||||
// else p2 score, reset board
|
|
||||||
p2_score += 1;
|
|
||||||
Serial.println("Player 2 Scores");
|
|
||||||
point_scored();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
hits += 1;
|
|
||||||
ball_move_x= ball_move_x * -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (ball_x == MATRIX_WIDTH-1) {
|
|
||||||
if (!ball_player_collision(p2_start)) {
|
|
||||||
// else p1 score, reset board
|
|
||||||
p1_score += 1;
|
|
||||||
Serial.println("Player 1 Scores");
|
|
||||||
point_scored();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
hits += 1;
|
|
||||||
ball_move_x= ball_move_x * -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ball_y == 0 || ball_y == MATRIX_HEIGHT-1) {
|
|
||||||
// reverse y, go down
|
|
||||||
ball_move_y= ball_move_y * -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hits >= 6 && loop_delay >= 80) {
|
|
||||||
// increase ball speed
|
|
||||||
hits = 0;
|
|
||||||
loop_delay -= 20;
|
|
||||||
}
|
|
||||||
|
|
||||||
ball_x+= ball_move_x;
|
|
||||||
ball_y+= ball_move_y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
long exec_t1= millis();
|
long exec_t1= millis();
|
||||||
pong_move_p1();
|
|
||||||
pong_move_p2();
|
switch (game_status) {
|
||||||
render_matrix();
|
|
||||||
if (exec_t1 - exec_t2 > loop_delay) {
|
case MENU: {
|
||||||
move_ball();
|
// switch modes
|
||||||
|
if (digitalRead(P2_BTN_BOTTOM) == LOW) {
|
||||||
|
menu.next_mode();
|
||||||
|
const byte (*current_gmode)[12]= frame_gmodes[menu.get_mode()];
|
||||||
|
matrix.loadPixels((uint8_t*)current_gmode, MATRIX_HEIGHT * MATRIX_WIDTH);
|
||||||
|
delay(300);
|
||||||
|
}
|
||||||
|
else if (digitalRead(P2_BTN_UP) == LOW) {
|
||||||
|
menu.prev_mode();
|
||||||
|
const byte (*current_gmode)[12]= frame_gmodes[menu.get_mode()];
|
||||||
|
matrix.loadPixels((uint8_t*)current_gmode, MATRIX_HEIGHT * MATRIX_WIDTH);
|
||||||
|
delay(300);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. P vs P
|
||||||
|
else if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW && menu.number_of_bots() == 0) {
|
||||||
|
p1= &human_pad1;
|
||||||
|
p2= &human_pad2;
|
||||||
|
engine.set_players(p1, p2);
|
||||||
|
renderer.set_players(p1, p2);
|
||||||
|
game_status= TIMER;
|
||||||
|
}
|
||||||
|
// 2. P vs CPU
|
||||||
|
else if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW && menu.number_of_bots() == 1) {
|
||||||
|
p1= &human_pad1;
|
||||||
|
p2= &bot_pad2;
|
||||||
|
engine.set_players(p1, p2);
|
||||||
|
renderer.set_players(p1, p2);
|
||||||
|
game_status= MENU_BOT_SKILLS;
|
||||||
|
delay(300); // avoid accidental double click for next menu
|
||||||
|
}
|
||||||
|
// 3. CPU vs CPU
|
||||||
|
else if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW && menu.number_of_bots() == 2) {
|
||||||
|
p1= &bot_pad1;
|
||||||
|
p2= &bot_pad2;
|
||||||
|
engine.set_players(p1, p2);
|
||||||
|
renderer.set_players(p1, p2);
|
||||||
|
game_status= MENU_BOT_SKILLS;
|
||||||
|
delay(300); // avoid accidental double click for next menu
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
const byte (*current_gmode)[12]= frame_gmodes[menu.get_mode()];
|
||||||
|
matrix.loadPixels((uint8_t*)current_gmode, MATRIX_HEIGHT * MATRIX_WIDTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case MENU_BOT_SKILLS: {
|
||||||
|
// switch difficulty level
|
||||||
|
if (digitalRead(P2_BTN_BOTTOM) == LOW) {
|
||||||
|
menu.increase_skills();
|
||||||
|
const byte (*current_skill_frame)[12]= frame_bot_skills[menu.get_skill()];
|
||||||
|
matrix.loadPixels((uint8_t*)current_skill_frame, MATRIX_HEIGHT * MATRIX_WIDTH);
|
||||||
|
delay(300);
|
||||||
|
}
|
||||||
|
else if (digitalRead(P2_BTN_UP) == LOW) {
|
||||||
|
menu.decrease_skills();
|
||||||
|
const byte (*current_skill_frame)[12]= frame_bot_skills[menu.get_skill()];
|
||||||
|
matrix.loadPixels((uint8_t*)current_skill_frame, MATRIX_HEIGHT * MATRIX_WIDTH);
|
||||||
|
delay(300);
|
||||||
|
}
|
||||||
|
|
||||||
|
// choose difficulty level
|
||||||
|
else if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW) {
|
||||||
|
if (!p1 -> is_human()) p1 -> set_skills(menu.get_skill() + 1);
|
||||||
|
if (!p2 -> is_human()) p2 -> set_skills(menu.get_skill() + 1);
|
||||||
|
game_status= TIMER;
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
const byte (*current_skill_frame)[12]= frame_bot_skills[menu.get_skill()];
|
||||||
|
matrix.loadPixels((uint8_t*)current_skill_frame, MATRIX_HEIGHT * MATRIX_WIDTH);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case TIMER:
|
||||||
|
for (int i = START_TIMER; i >= 0; i--) {
|
||||||
|
renderer.render_timer(i);
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
|
game_status= RUN;
|
||||||
|
// delay the first ball movement
|
||||||
|
exec_t2= millis() + FIRST_START_BALL_DELAY;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case RUN:
|
||||||
|
need_refresh= engine.control_players();
|
||||||
|
|
||||||
|
if (exec_t1 - exec_t2 > engine.ball_movement_delay()) {
|
||||||
|
engine.run();
|
||||||
|
if (engine.get_event() == P1SCORE || engine.get_event() == P2SCORE)
|
||||||
|
game_status= SCORE;
|
||||||
exec_t2= exec_t1;
|
exec_t2= exec_t1;
|
||||||
|
need_refresh= true;
|
||||||
|
}
|
||||||
|
// rerender matrix only if something is changed
|
||||||
|
if (need_refresh) {
|
||||||
|
renderer.render_matrix();
|
||||||
|
need_refresh= false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SCORE:
|
||||||
|
delay(300);
|
||||||
|
renderer.render_score();
|
||||||
|
engine.restart_ball();
|
||||||
|
delay(1000);
|
||||||
|
if (p1 -> get_score() >= MAX_POINTS || p2 -> get_score() >= MAX_POINTS)
|
||||||
|
game_status= GAMEOVER;
|
||||||
|
else {
|
||||||
|
game_status= RUN;
|
||||||
|
// before move again the ball wait a second
|
||||||
|
renderer.render_matrix();
|
||||||
|
exec_t2= millis() + FIRST_START_BALL_DELAY;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GAMEOVER:
|
||||||
|
renderer.render_winner();
|
||||||
|
game_status= WAIT;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case WAIT:
|
||||||
|
// keep showing the winner waiting for a restart
|
||||||
|
// restart game once one button is pressed
|
||||||
|
if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW ||
|
||||||
|
digitalRead(P2_BTN_UP) == LOW || digitalRead(P2_BTN_BOTTOM) == LOW) {
|
||||||
|
engine.reset();
|
||||||
|
game_status= MENU;
|
||||||
|
delay(300);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
delay(50);
|
delay(50);
|
||||||
}
|
}
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 847 KiB After Width: | Height: | Size: 847 KiB |
BIN
circuit/arduino_pong_circuit.png
Normal file
BIN
circuit/arduino_pong_circuit.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.2 MiB |
1
circuit/arduino_pong_circuit.svg
Normal file
1
circuit/arduino_pong_circuit.svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 2.7 MiB |
52
src/ball.cpp
Normal file
52
src/ball.cpp
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#include "ball.h"
|
||||||
|
|
||||||
|
void Ball::_init_directions(int8_t &_direction) {
|
||||||
|
if (random(2) == 0) _direction= 1;
|
||||||
|
else _direction= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ball::move() {
|
||||||
|
if (!_direction_x) this -> _init_directions(_direction_x);
|
||||||
|
if (!_direction_y) this -> _init_directions(_direction_y);
|
||||||
|
|
||||||
|
// if (_x < 0 || _x > MATRIX_WIDTH-1 || _y < 0 || _y > MATRIX_HEIGHT-1) {
|
||||||
|
// this -> reset_position();
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (_x + _direction_x >= 0 && _x + _direction_x < MATRIX_WIDTH)
|
||||||
|
_x+= _direction_x;
|
||||||
|
if (_y + _direction_y >= 0 && _y + _direction_y < MATRIX_HEIGHT)
|
||||||
|
_y+= _direction_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ball::bounce_on_pad() {
|
||||||
|
_direction_x *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ball::bounce_on_sides() {
|
||||||
|
_direction_y *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int8_t Ball::get_direction_x() {
|
||||||
|
return _direction_x;
|
||||||
|
}
|
||||||
|
int8_t Ball::get_direction_y() {
|
||||||
|
return _direction_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Ball::reset_position () {
|
||||||
|
_x= BALL_RESET_X;
|
||||||
|
int8_t delta= random(3);
|
||||||
|
if (random(2) == 0) delta *= -1;
|
||||||
|
_y= BALL_RESET_Y + delta;
|
||||||
|
this -> _init_directions(_direction_x);
|
||||||
|
this -> _init_directions(_direction_y);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Ball::get_x() {
|
||||||
|
return _x;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Ball::get_y() {
|
||||||
|
return _y;
|
||||||
|
}
|
||||||
27
src/ball.h
Normal file
27
src/ball.h
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#ifndef BALL_H
|
||||||
|
#define BALL_H
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
class Ball {
|
||||||
|
private:
|
||||||
|
uint8_t _x, _y;
|
||||||
|
int8_t _direction_x, _direction_y;
|
||||||
|
|
||||||
|
void _init_directions(int8_t &_direction);
|
||||||
|
|
||||||
|
public:
|
||||||
|
Ball (uint8_t _x, uint8_t _y) : _x(_x), _y(_y) {}
|
||||||
|
|
||||||
|
void move();
|
||||||
|
void bounce_on_pad();
|
||||||
|
void bounce_on_sides();
|
||||||
|
int8_t get_direction_x();
|
||||||
|
int8_t get_direction_y();
|
||||||
|
void reset_position ();
|
||||||
|
uint8_t get_x();
|
||||||
|
uint8_t get_y();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
15
src/config.h
Normal file
15
src/config.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#define P1_BTN_UP 12
|
||||||
|
#define P1_BTN_BOTTOM 11
|
||||||
|
#define P2_BTN_UP 10
|
||||||
|
#define P2_BTN_BOTTOM 9
|
||||||
|
|
||||||
|
#define MATRIX_WIDTH 12
|
||||||
|
#define MATRIX_HEIGHT 8
|
||||||
|
#define BALL_RESET_X (MATRIX_WIDTH / 2)
|
||||||
|
#define BALL_RESET_Y (MATRIX_HEIGHT / 2)
|
||||||
|
#define PADDLE_LENGTH 3
|
||||||
|
#define INITIAL_BALL_DELAY 200
|
||||||
|
|
||||||
|
#define MAX_POINTS 9
|
||||||
|
#define START_TIMER 3
|
||||||
|
#define FIRST_START_BALL_DELAY 500
|
||||||
96
src/engine.cpp
Normal file
96
src/engine.cpp
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
#include "engine.h"
|
||||||
|
|
||||||
|
bool Engine::_check_pad_ball_collision(Paddle *p) {
|
||||||
|
uint8_t ppos= p -> get_position();
|
||||||
|
for (int p= ppos; p < ppos + PADDLE_LENGTH; p++) {
|
||||||
|
if (_ball.get_y() == p) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::run() {
|
||||||
|
// if (_event == P1SCORE || _event == P2SCORE) this -> _soft_reset();
|
||||||
|
|
||||||
|
_event= NONE;
|
||||||
|
_ball.move();
|
||||||
|
uint8_t bx= _ball.get_x();
|
||||||
|
uint8_t by= _ball.get_y();
|
||||||
|
int8_t ball_dir= _ball.get_direction_x();
|
||||||
|
// pad is 1 pixel far from the edge, so i need to calc this delta
|
||||||
|
// check also if the ball is already moving away, to skip useless checks
|
||||||
|
if (bx <= 1 && ball_dir < 0) {
|
||||||
|
// score the point only if ball reached the edge
|
||||||
|
if (this -> _check_pad_ball_collision(_p1) && bx <= 1) {
|
||||||
|
_ball.bounce_on_pad();
|
||||||
|
_event= P1_COLLISION;
|
||||||
|
_hits++;
|
||||||
|
}
|
||||||
|
else if (bx <= 0) {
|
||||||
|
// p2 scores
|
||||||
|
_p2 -> increase_score();
|
||||||
|
_event= P2SCORE;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (bx >= MATRIX_WIDTH-2 && ball_dir > 0) {
|
||||||
|
// score the point only if ball reached the edge
|
||||||
|
if (this -> _check_pad_ball_collision(_p2) && bx >= MATRIX_WIDTH-2) {
|
||||||
|
_ball.bounce_on_pad();
|
||||||
|
_event= P2_COLLISION;
|
||||||
|
_hits++;
|
||||||
|
}
|
||||||
|
else if (bx >= MATRIX_WIDTH-1) {
|
||||||
|
// p1 scores
|
||||||
|
_p1 -> increase_score();
|
||||||
|
_event= P1SCORE;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (by == 0 || by == MATRIX_HEIGHT-1) {
|
||||||
|
_ball.bounce_on_sides();
|
||||||
|
_event= WALL_COLLISION;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_hits >= 6 && _ball_mv_delay >= 80) {
|
||||||
|
_hits= 0;
|
||||||
|
_ball_mv_delay -= 20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::set_players(Paddle *p1, Paddle *p2) {
|
||||||
|
_p1= p1;
|
||||||
|
_p2= p2;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Engine::control_players() {
|
||||||
|
bool need_refresh= false;
|
||||||
|
|
||||||
|
if (_p1 -> is_human()) need_refresh |= _p1 -> check_pad_movement();
|
||||||
|
else need_refresh |= _p1 -> check_pad_movement(_ball);
|
||||||
|
if (_p2 -> is_human()) need_refresh |= _p2 -> check_pad_movement();
|
||||||
|
else need_refresh |= _p2 -> check_pad_movement(_ball);
|
||||||
|
return need_refresh;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Engine::ball_movement_delay() {
|
||||||
|
return _ball_mv_delay;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
EngineEvents Engine::get_event() {
|
||||||
|
return _event;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::restart_ball() {
|
||||||
|
_ball.reset_position();
|
||||||
|
_ball_mv_delay= INITIAL_BALL_DELAY;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::reset() {
|
||||||
|
this -> restart_ball();
|
||||||
|
_p1 -> reset();
|
||||||
|
_p2 -> reset();
|
||||||
|
}
|
||||||
37
src/engine.h
Normal file
37
src/engine.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#ifndef ENGINE_H
|
||||||
|
#define ENGINE_H
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include "ball.h"
|
||||||
|
#include "paddle.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
enum EngineEvents : uint8_t {NONE, P1SCORE, P2SCORE, P1_COLLISION, P2_COLLISION, WALL_COLLISION};
|
||||||
|
|
||||||
|
class Engine {
|
||||||
|
|
||||||
|
private:
|
||||||
|
Paddle* _p1;
|
||||||
|
Paddle* _p2;
|
||||||
|
Ball& _ball;
|
||||||
|
EngineEvents _event= NONE;
|
||||||
|
uint8_t _ball_mv_delay;
|
||||||
|
uint8_t _hits = 0;
|
||||||
|
|
||||||
|
bool _check_pad_ball_collision(Paddle *p);
|
||||||
|
|
||||||
|
public:
|
||||||
|
// inizialize Engine constructor, linking all args with private args
|
||||||
|
Engine(Ball &ball, uint8_t ball_mv_delay)
|
||||||
|
: _ball(ball), _ball_mv_delay(ball_mv_delay) {}
|
||||||
|
|
||||||
|
void run();
|
||||||
|
void set_players(Paddle *p_one, Paddle *p_two);
|
||||||
|
bool control_players();
|
||||||
|
uint8_t ball_movement_delay();
|
||||||
|
EngineEvents get_event();
|
||||||
|
void restart_ball();
|
||||||
|
void reset();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
238
src/font.cpp
Normal file
238
src/font.cpp
Normal file
@@ -0,0 +1,238 @@
|
|||||||
|
#include "font.h";
|
||||||
|
|
||||||
|
const uint32_t pone_wins[5][4] = {
|
||||||
|
{
|
||||||
|
0x78,
|
||||||
|
0xc4847844,
|
||||||
|
0x440e000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0x11,
|
||||||
|
0x1101501,
|
||||||
|
0x501f0000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0xe,
|
||||||
|
0x400400,
|
||||||
|
0x400e0000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0x9,
|
||||||
|
0xd00b00,
|
||||||
|
0x90090000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0xf,
|
||||||
|
0x800f00,
|
||||||
|
0x100f0000,
|
||||||
|
500
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const uint32_t ptwo_wins[5][4] = {
|
||||||
|
{
|
||||||
|
0x79,
|
||||||
|
0xe48279e4,
|
||||||
|
0x1041e000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0x11,
|
||||||
|
0x1101501,
|
||||||
|
0x501f0000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0xe,
|
||||||
|
0x400400,
|
||||||
|
0x400e0000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0x9,
|
||||||
|
0xd00b00,
|
||||||
|
0x90090000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0xf,
|
||||||
|
0x800f00,
|
||||||
|
0x100f0000,
|
||||||
|
500
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const byte font_pong[10][8][3] = {
|
||||||
|
// Number 0
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 1
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 0 },
|
||||||
|
{ 0, 1, 0 },
|
||||||
|
{ 0, 1, 0 },
|
||||||
|
{ 0, 1, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 2
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 3
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 4
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 5
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 6
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 0, 0 },
|
||||||
|
{ 1, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 7
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 8
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 9
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const byte frame_pvp[MATRIX_HEIGHT][MATRIX_WIDTH] = {
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
|
{ 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0 },
|
||||||
|
{ 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
|
||||||
|
{ 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0 },
|
||||||
|
{ 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 },
|
||||||
|
{ 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0 },
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
const byte frame_pvc[MATRIX_HEIGHT][MATRIX_WIDTH] = {
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
|
{ 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
|
||||||
|
{ 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
|
||||||
|
{ 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0 },
|
||||||
|
{ 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 },
|
||||||
|
{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0 },
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
const byte frame_cvc[MATRIX_HEIGHT][MATRIX_WIDTH] = {
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
|
{ 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
|
||||||
|
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
|
||||||
|
{ 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 },
|
||||||
|
{ 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 },
|
||||||
|
{ 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0 },
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
const byte (*frame_gmodes[3])[12]= {frame_pvp, frame_pvc, frame_cvc};
|
||||||
|
|
||||||
|
const byte frame_bot_skill_easy[MATRIX_HEIGHT][MATRIX_WIDTH] = {
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
|
{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
|
||||||
|
{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
|
||||||
|
{ 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0 },
|
||||||
|
{ 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0 },
|
||||||
|
{ 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0 },
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
const byte frame_bot_skill_hard[MATRIX_HEIGHT][MATRIX_WIDTH] = {
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
||||||
|
{ 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0 },
|
||||||
|
{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
|
||||||
|
{ 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 },
|
||||||
|
{ 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 },
|
||||||
|
{ 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0 },
|
||||||
|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
const byte (*frame_bot_skills[3])[12]= {frame_bot_skill_easy, frame_bot_skill_hard};
|
||||||
20
src/font.h
Normal file
20
src/font.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef FONT_H
|
||||||
|
#define FONT_H
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
extern const uint32_t pone_wins[5][4];
|
||||||
|
extern const uint32_t ptwo_wins[5][4];
|
||||||
|
extern const byte font_pong[10][8][3];
|
||||||
|
|
||||||
|
extern const byte frame_pvp[MATRIX_HEIGHT][MATRIX_WIDTH];
|
||||||
|
extern const byte frame_pvc[MATRIX_HEIGHT][MATRIX_WIDTH];
|
||||||
|
extern const byte frame_cvc[MATRIX_HEIGHT][MATRIX_WIDTH];
|
||||||
|
extern const byte (*frame_gmodes[3])[12];
|
||||||
|
|
||||||
|
extern const byte frame_bot_skill_easy[MATRIX_HEIGHT][MATRIX_WIDTH];
|
||||||
|
extern const byte frame_bot_skill_hard[MATRIX_HEIGHT][MATRIX_WIDTH];
|
||||||
|
extern const byte (*frame_bot_skills[3])[12];
|
||||||
|
|
||||||
|
#endif
|
||||||
42
src/menu.cpp
Normal file
42
src/menu.cpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#include "menu.h"
|
||||||
|
|
||||||
|
void Menu::next_mode() {
|
||||||
|
if (_game_mode < _game_modes(COUNT_MODES) - 1)
|
||||||
|
_game_mode= _game_modes(_game_mode+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu::prev_mode() {
|
||||||
|
if (_game_mode > 0) {
|
||||||
|
_game_mode= _game_modes(_game_mode-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Menu::get_mode() {
|
||||||
|
return _game_mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Menu::number_of_bots() {
|
||||||
|
if (_game_mode == PVC) return 1;
|
||||||
|
else if (_game_mode == CVC) return 2;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu::increase_skills() {
|
||||||
|
if (_bot_skill < _bot_skill_levels(COUNT_SKILLS) - 1)
|
||||||
|
_bot_skill= _bot_skill_levels(_bot_skill+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu::decrease_skills() {
|
||||||
|
if (_bot_skill > 0) {
|
||||||
|
_bot_skill= _bot_skill_levels(_bot_skill-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Menu::get_skill() {
|
||||||
|
return _bot_skill;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu::reset_menu() {
|
||||||
|
_game_mode= PVP;
|
||||||
|
_bot_skill= EASY;
|
||||||
|
}
|
||||||
26
src/menu.h
Normal file
26
src/menu.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef MENU_H
|
||||||
|
#define MENU_H
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include "font.h"
|
||||||
|
|
||||||
|
class Menu {
|
||||||
|
|
||||||
|
private:
|
||||||
|
enum _game_modes : uint8_t {PVP, PVC, CVC, COUNT_MODES};
|
||||||
|
_game_modes _game_mode = PVP;
|
||||||
|
enum _bot_skill_levels : uint8_t {EASY, HARD, COUNT_SKILLS};
|
||||||
|
_bot_skill_levels _bot_skill = EASY;
|
||||||
|
|
||||||
|
public:
|
||||||
|
void next_mode();
|
||||||
|
void prev_mode();
|
||||||
|
uint8_t get_mode();
|
||||||
|
uint8_t number_of_bots();
|
||||||
|
void increase_skills();
|
||||||
|
void decrease_skills();
|
||||||
|
uint8_t get_skill();
|
||||||
|
void reset_menu();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
127
src/paddle.cpp
Normal file
127
src/paddle.cpp
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
#include "paddle.h"
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
void Paddle::move_pad_up() {
|
||||||
|
if (_pos_y > 0) {
|
||||||
|
_pos_y -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void Paddle::move_pad_down() {
|
||||||
|
if (_pos_y + _height < MATRIX_HEIGHT) {
|
||||||
|
_pos_y += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void run_paddle() {
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Paddle::get_position() {
|
||||||
|
return _pos_y;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Paddle::is_human() {
|
||||||
|
return _human;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Paddle::increase_score() {
|
||||||
|
if (_score <= 9) _score += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Paddle::get_score() {
|
||||||
|
return _score;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Paddle::reset() {
|
||||||
|
_score= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Paddle::check_pad_movement() {
|
||||||
|
// redefine me
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Paddle::check_pad_movement(Ball &ball) {
|
||||||
|
// redefine me
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Paddle::get_skills() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Paddle::set_skills(uint8_t skills) {
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HumanPaddle::check_pad_movement() {
|
||||||
|
bool need_refresh= false;
|
||||||
|
if (digitalRead(_pin_btn_top) == LOW) {
|
||||||
|
this -> move_pad_up();
|
||||||
|
need_refresh= true;
|
||||||
|
}
|
||||||
|
else if (digitalRead(_pin_btn_bottom) == LOW) {
|
||||||
|
this -> move_pad_down();
|
||||||
|
need_refresh= true;
|
||||||
|
}
|
||||||
|
return need_refresh;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BotPaddle::check_pad_movement(Ball &ball) {
|
||||||
|
uint8_t ball_y= ball.get_y();
|
||||||
|
int8_t ball_dir= ball.get_direction_x();
|
||||||
|
int8_t ball_dir_ver= ball.get_direction_y();
|
||||||
|
|
||||||
|
uint8_t skills= this -> get_skills();
|
||||||
|
|
||||||
|
// ball is moving left and pad is on right, do not move
|
||||||
|
if (ball_dir < 0 && _pos_x > MATRIX_WIDTH / 2) return false;
|
||||||
|
// ball is moving right and pad is on left, do not move
|
||||||
|
else if (ball_dir > 0 && _pos_x < MATRIX_WIDTH / 2) return false;
|
||||||
|
|
||||||
|
uint8_t ball_x= ball.get_x();
|
||||||
|
int8_t ball_distance= ball_x - _pos_x;
|
||||||
|
if (ball_distance < 0) ball_distance *= -1;
|
||||||
|
switch (skills) {
|
||||||
|
case 1:
|
||||||
|
if (ball_distance > 3) return false;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
if (ball_distance > 4) return false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t move_chances= random(skills * 10) % 2;
|
||||||
|
if (!move_chances) return false;
|
||||||
|
|
||||||
|
enum Movement {NONE, UP, DOWN};
|
||||||
|
Movement _movment= NONE;
|
||||||
|
|
||||||
|
for (uint8_t py= _pos_y; py < _pos_y+PADDLE_LENGTH-1; py++) {
|
||||||
|
if (_pos_y - ball_y >= 0 && ball_dir_ver < 0) {
|
||||||
|
_movment= UP;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (_pos_y - ball_y <= 0 && ball_dir_ver > 0) {
|
||||||
|
_movment= DOWN;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (_movment == UP) {
|
||||||
|
this -> move_pad_up();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (_movment == DOWN) {
|
||||||
|
this -> move_pad_down();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t BotPaddle::get_skills() {
|
||||||
|
return _skills;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BotPaddle::set_skills(uint8_t skills) {
|
||||||
|
if (skills < 1) _skills= 1;
|
||||||
|
else if (skills > 2) _skills= 2;
|
||||||
|
else _skills= skills;
|
||||||
|
}
|
||||||
57
src/paddle.h
Normal file
57
src/paddle.h
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
#ifndef PADDLE_H
|
||||||
|
#define PADDLE_H
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <cstdint>
|
||||||
|
#include "config.h"
|
||||||
|
#include "ball.h"
|
||||||
|
|
||||||
|
class Paddle {
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// define player coordinates
|
||||||
|
uint8_t _pos_y;
|
||||||
|
uint8_t _height= PADDLE_LENGTH;
|
||||||
|
uint8_t _score= 0;
|
||||||
|
bool _human;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Paddle (uint8_t pos_y, bool human) : _pos_y(pos_y), _human(human) {}
|
||||||
|
void move_pad_up();
|
||||||
|
void move_pad_down();
|
||||||
|
uint8_t get_position();
|
||||||
|
bool is_human();
|
||||||
|
void increase_score();
|
||||||
|
uint8_t get_score();
|
||||||
|
void reset();
|
||||||
|
virtual bool check_pad_movement();
|
||||||
|
virtual bool check_pad_movement(Ball &ball);
|
||||||
|
virtual uint8_t get_skills();
|
||||||
|
virtual void set_skills(uint8_t skills);
|
||||||
|
};
|
||||||
|
|
||||||
|
class HumanPaddle : public Paddle {
|
||||||
|
private:
|
||||||
|
uint8_t _pin_btn_top;
|
||||||
|
uint8_t _pin_btn_bottom;
|
||||||
|
public:
|
||||||
|
HumanPaddle(uint8_t position, uint8_t pin_btn_top, uint8_t pin_btn_bottom)
|
||||||
|
: Paddle(position, true), _pin_btn_top(pin_btn_top), _pin_btn_bottom(pin_btn_bottom) {}
|
||||||
|
bool check_pad_movement();
|
||||||
|
};
|
||||||
|
|
||||||
|
class BotPaddle : public Paddle {
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t _pos_x;
|
||||||
|
uint8_t _skills; // this is the difficulty level
|
||||||
|
|
||||||
|
public:
|
||||||
|
BotPaddle(uint8_t position, uint8_t pos_x)
|
||||||
|
: Paddle(position, false), _pos_x(pos_x) {}
|
||||||
|
bool check_pad_movement(Ball &ball);
|
||||||
|
uint8_t get_skills();
|
||||||
|
void set_skills(uint8_t skills);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
73
src/renderer.cpp
Normal file
73
src/renderer.cpp
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#include "renderer.h"
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::_clear_matrix() {
|
||||||
|
for (int x=0; x < MATRIX_WIDTH; x++) {
|
||||||
|
for (int y=0; y < MATRIX_HEIGHT; y++) {
|
||||||
|
_frame[y][x]= 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::set_players(Paddle *p1, Paddle *p2) {
|
||||||
|
_p1= p1;
|
||||||
|
_p2= p2;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::render_timer(uint8_t seconds) {
|
||||||
|
this -> _clear_matrix();
|
||||||
|
|
||||||
|
for (int h=0; h < 8; h++) {
|
||||||
|
for (int w=0; w < 3; w++) {
|
||||||
|
_frame[h][w+5]= font_pong[seconds][h][w];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_matrix.renderBitmap(_frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::render_matrix() {
|
||||||
|
this -> _clear_matrix();
|
||||||
|
uint8_t p1pos= _p1 -> get_position();
|
||||||
|
uint8_t p2pos= _p2 -> get_position();
|
||||||
|
// players coords
|
||||||
|
for (int i= p1pos; i < p1pos+PADDLE_LENGTH; i++) {
|
||||||
|
_frame[i][0]= 1;
|
||||||
|
}
|
||||||
|
for (int i= p2pos; i < p2pos+PADDLE_LENGTH; i++) {
|
||||||
|
_frame[i][MATRIX_WIDTH-1]= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ball coords
|
||||||
|
uint8_t bx= _ball.get_x();
|
||||||
|
uint8_t by= _ball.get_y();
|
||||||
|
_frame[by][bx]= 1;
|
||||||
|
_matrix.renderBitmap(_frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::render_score() {
|
||||||
|
this -> _clear_matrix();
|
||||||
|
_frame[4][5]= 1;
|
||||||
|
_frame[4][6]= 1;
|
||||||
|
|
||||||
|
for (int h=0; h < 8; h++) {
|
||||||
|
for (int w=0; w < 3; w++) {
|
||||||
|
_frame[h][w+1]= font_pong[_p1 -> get_score()][h][w];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int h=0; h < 8; h++) {
|
||||||
|
for (int w=0; w < 3; w++) {
|
||||||
|
_frame[h][w+8]= font_pong[_p2 -> get_score()][h][w];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_matrix.renderBitmap(_frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::render_winner() {
|
||||||
|
this -> _clear_matrix();
|
||||||
|
// check winner
|
||||||
|
if (_p1 -> get_score() > _p2 -> get_score())
|
||||||
|
_matrix.loadSequence(pone_wins);
|
||||||
|
else
|
||||||
|
_matrix.loadSequence(ptwo_wins);
|
||||||
|
_matrix.play(true);
|
||||||
|
}
|
||||||
34
src/renderer.h
Normal file
34
src/renderer.h
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#ifndef RENDERER_H
|
||||||
|
#define RENDERER_H
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include "Arduino_LED_Matrix.h"
|
||||||
|
#include "config.h"
|
||||||
|
#include "font.h"
|
||||||
|
#include "paddle.h"
|
||||||
|
#include "ball.h"
|
||||||
|
|
||||||
|
class Renderer {
|
||||||
|
|
||||||
|
private:
|
||||||
|
// define player coordinates
|
||||||
|
Paddle* _p1;
|
||||||
|
Paddle* _p2;
|
||||||
|
Ball& _ball;
|
||||||
|
byte (&_frame)[MATRIX_HEIGHT][MATRIX_WIDTH];
|
||||||
|
ArduinoLEDMatrix& _matrix;
|
||||||
|
|
||||||
|
void _clear_matrix();
|
||||||
|
|
||||||
|
public:
|
||||||
|
Renderer (Ball &ball, byte (&frame)[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix)
|
||||||
|
: _ball(ball), _frame(frame), _matrix(matrix) {}
|
||||||
|
|
||||||
|
void set_players(Paddle *p1, Paddle *p2);
|
||||||
|
void render_timer(uint8_t seconds);
|
||||||
|
void render_matrix();
|
||||||
|
void render_score();
|
||||||
|
void render_winner();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user