Compare commits
12 Commits
68dfce8b12
...
d18415f472
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d18415f472 | ||
|
|
cef9f09c79 | ||
|
|
a9aed6b8d0 | ||
|
|
7f63ae9a31 | ||
|
|
a6c218061d | ||
|
|
8e5fe2143c | ||
|
|
fbad1a87d6 | ||
|
|
4bb6d6d6ee | ||
|
|
6ea443d92a | ||
|
|
2838b5fd14 | ||
|
|
69a8fb9dc4 | ||
|
|
eac8c59d96 |
115
arduino_pong.ino
115
arduino_pong.ino
@@ -21,24 +21,42 @@ byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = {
|
||||
|
||||
ArduinoLEDMatrix matrix;
|
||||
|
||||
int need_refresh= true;
|
||||
bool need_refresh= true;
|
||||
uint8_t hits= 0;
|
||||
long exec_t2= millis();
|
||||
|
||||
enum game_statuses : uint8_t {
|
||||
MENU,
|
||||
MENU_BOT_SKILLS,
|
||||
TIMER,
|
||||
RUN,
|
||||
SCORE,
|
||||
GAMEOVER,
|
||||
WAIT,
|
||||
};
|
||||
game_statuses game_status= TIMER;
|
||||
game_statuses game_status= MENU;
|
||||
|
||||
enum game_modes : uint8_t {PVP, PVC, CVC};
|
||||
game_modes game_mode = PVP;
|
||||
|
||||
|
||||
Ball ball(4, 6);
|
||||
Paddle p1(1);
|
||||
Paddle p2(4);
|
||||
Engine engine(p1, p2, ball, INITIAL_BALL_DELAY);
|
||||
Renderer renderer(p1, p2, ball, frame, matrix);
|
||||
|
||||
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);
|
||||
|
||||
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() {
|
||||
Serial.begin(9600);
|
||||
@@ -58,6 +76,84 @@ void loop() {
|
||||
|
||||
switch (game_status) {
|
||||
|
||||
case MENU: {
|
||||
if (digitalRead(P2_BTN_BOTTOM) == LOW && current_gmode_idx < sizeof(frame_gmodes)/sizeof(frame_gmodes[0]) -1) {
|
||||
current_gmode_idx += 1;
|
||||
update_menu= true;
|
||||
}
|
||||
else if (digitalRead(P2_BTN_UP) == LOW && current_gmode_idx > 0) {
|
||||
update_menu= true;
|
||||
current_gmode_idx -= 1;
|
||||
game_mode= PVP;
|
||||
}
|
||||
|
||||
// 1. P vs P
|
||||
else if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW && game_modes(current_gmode_idx) == PVP) {
|
||||
p1= &human_pad1;
|
||||
p2= &human_pad2;
|
||||
mode_selected= true;
|
||||
}
|
||||
// 2. P vs CPU
|
||||
else if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW && game_modes(current_gmode_idx) == PVC) {
|
||||
p1= &human_pad1;
|
||||
p2= &bot_pad2;
|
||||
mode_selected= true;
|
||||
update_menu= false;
|
||||
game_mode= PVC;
|
||||
}
|
||||
// 3. CPU vs CPU
|
||||
else if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW && game_modes(current_gmode_idx) == CVC) {
|
||||
p1= &bot_pad1;
|
||||
p2= &bot_pad2;
|
||||
mode_selected= true;
|
||||
update_menu= false;
|
||||
game_mode= CVC;
|
||||
}
|
||||
|
||||
if (update_menu) {
|
||||
// show menu on the matrix
|
||||
const byte (*current_gmode)[12]= frame_gmodes[current_gmode_idx];
|
||||
matrix.loadPixels((uint8_t*)current_gmode, MATRIX_HEIGHT * MATRIX_WIDTH);
|
||||
update_menu= false;
|
||||
delay(300);
|
||||
}
|
||||
else if (mode_selected) {
|
||||
engine.set_players(p1, p2);
|
||||
renderer.set_players(p1, p2);
|
||||
if (game_mode == PVC || game_mode == CVC) {
|
||||
game_status= MENU_BOT_SKILLS;
|
||||
delay(300); // avoid accidental double click for next menu
|
||||
}
|
||||
else game_status= TIMER;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case MENU_BOT_SKILLS: {
|
||||
if (digitalRead(P2_BTN_BOTTOM) == LOW && current_bot_menu_idx < sizeof(frame_bot_skills)/sizeof(frame_bot_skills[0]) -1) {
|
||||
current_bot_menu_idx += 1;
|
||||
update_menu_bot_skills= true;
|
||||
}
|
||||
else if (digitalRead(P2_BTN_UP) == LOW && current_bot_menu_idx > 0) {
|
||||
current_bot_menu_idx -= 1;
|
||||
update_menu_bot_skills= true;
|
||||
}
|
||||
else if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW) {
|
||||
if (!p1 -> is_human()) p1 -> set_skills(current_bot_menu_idx + 1);
|
||||
if (!p2 -> is_human()) p2 -> set_skills(current_bot_menu_idx + 1);
|
||||
game_status= TIMER;
|
||||
update_menu_bot_skills= false;
|
||||
}
|
||||
|
||||
if (update_menu_bot_skills) {
|
||||
const byte (*current_skill_frame)[12]= frame_bot_skills[current_bot_menu_idx];
|
||||
matrix.loadPixels((uint8_t*)current_skill_frame, MATRIX_HEIGHT * MATRIX_WIDTH);
|
||||
update_menu= false;
|
||||
delay(300);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case TIMER:
|
||||
for (int i = START_TIMER; i >= 0; i--) {
|
||||
renderer.render_timer(i);
|
||||
@@ -69,7 +165,7 @@ void loop() {
|
||||
break;
|
||||
|
||||
case RUN:
|
||||
need_refresh= check_paddle_movements(p1, p2);
|
||||
need_refresh= engine.control_players();
|
||||
|
||||
if (exec_t1 - exec_t2 > engine.ball_movement_delay()) {
|
||||
engine.run();
|
||||
@@ -90,7 +186,7 @@ void loop() {
|
||||
renderer.render_score();
|
||||
engine.restart_ball();
|
||||
delay(1000);
|
||||
if (p1.get_score() >= MAX_POINTS || p2.get_score() >= MAX_POINTS)
|
||||
if (p1 -> get_score() >= MAX_POINTS || p2 -> get_score() >= MAX_POINTS)
|
||||
game_status= GAMEOVER;
|
||||
else {
|
||||
game_status= RUN;
|
||||
@@ -108,7 +204,8 @@ void loop() {
|
||||
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) {
|
||||
if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW ||
|
||||
digitalRead(P2_BTN_UP) == LOW || digitalRead(P2_BTN_BOTTOM) == LOW) {
|
||||
game_status= TIMER;
|
||||
engine.reset();
|
||||
}
|
||||
|
||||
@@ -36,7 +36,9 @@ int8_t Ball::get_direction_y() {
|
||||
|
||||
void Ball::reset_position () {
|
||||
_x= BALL_RESET_X;
|
||||
_y= BALL_RESET_Y;
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "engine.h"
|
||||
|
||||
bool Engine::_check_pad_ball_collision(Paddle &p) {
|
||||
uint8_t ppos= p.get_position();
|
||||
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;
|
||||
@@ -29,7 +29,7 @@ void Engine::run() {
|
||||
}
|
||||
else if (bx <= 0) {
|
||||
// p2 scores
|
||||
_p2.increase_score();
|
||||
_p2 -> increase_score();
|
||||
_event= P2SCORE;
|
||||
return;
|
||||
}
|
||||
@@ -43,7 +43,7 @@ void Engine::run() {
|
||||
}
|
||||
else if (bx >= MATRIX_WIDTH-1) {
|
||||
// p1 scores
|
||||
_p1.increase_score();
|
||||
_p1 -> increase_score();
|
||||
_event= P1SCORE;
|
||||
return;
|
||||
}
|
||||
@@ -60,6 +60,21 @@ void Engine::run() {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -76,6 +91,6 @@ void Engine::restart_ball() {
|
||||
|
||||
void Engine::reset() {
|
||||
this -> restart_ball();
|
||||
_p1.reset();
|
||||
_p2.reset();
|
||||
_p1 -> reset();
|
||||
_p2 -> reset();
|
||||
}
|
||||
|
||||
12
src/engine.h
12
src/engine.h
@@ -11,21 +11,23 @@ enum EngineEvents : uint8_t {NONE, P1SCORE, P2SCORE, P1_COLLISION, P2_COLLISION,
|
||||
class Engine {
|
||||
|
||||
private:
|
||||
Paddle& _p1;
|
||||
Paddle& _p2;
|
||||
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);
|
||||
bool _check_pad_ball_collision(Paddle *p);
|
||||
|
||||
public:
|
||||
// inizialize Engine constructor, linking all args with private args
|
||||
Engine(Paddle &p_one, Paddle &p_two, Ball &ball, uint8_t ball_mv_delay)
|
||||
: _p1(p_one), _p2(p_two), _ball(ball), _ball_mv_delay(ball_mv_delay) {}
|
||||
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();
|
||||
|
||||
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};
|
||||
188
src/font.h
188
src/font.h
@@ -2,183 +2,19 @@
|
||||
#define FONT_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "config.h"
|
||||
|
||||
const uint32_t pone_wins[][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[][4] = {
|
||||
{
|
||||
0x79,
|
||||
0xe48279e4,
|
||||
0x1041e000,
|
||||
500
|
||||
},
|
||||
{
|
||||
0x11,
|
||||
0x1101501,
|
||||
0x501f0000,
|
||||
500
|
||||
},
|
||||
{
|
||||
0xe,
|
||||
0x400400,
|
||||
0x400e0000,
|
||||
500
|
||||
},
|
||||
{
|
||||
0x9,
|
||||
0xd00b00,
|
||||
0x90090000,
|
||||
500
|
||||
},
|
||||
{
|
||||
0xf,
|
||||
0x800f00,
|
||||
0x100f0000,
|
||||
500
|
||||
}
|
||||
};
|
||||
extern const uint32_t pone_wins[5][4];
|
||||
extern const uint32_t ptwo_wins[5][4];
|
||||
extern const byte font_pong[10][8][3];
|
||||
|
||||
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 }
|
||||
},
|
||||
};
|
||||
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
|
||||
|
||||
111
src/paddle.cpp
111
src/paddle.cpp
@@ -1,18 +1,22 @@
|
||||
#include "paddle.h"
|
||||
#include <cstdio>
|
||||
|
||||
void Paddle::move_pad_up() {
|
||||
if (_position > 0) {
|
||||
_position -= 1;
|
||||
if (_pos_y > 0) {
|
||||
_pos_y -= 1;
|
||||
}
|
||||
}
|
||||
void Paddle::move_pad_down() {
|
||||
if (_position + _height < MATRIX_HEIGHT) {
|
||||
_position += 1;
|
||||
if (_pos_y + _height < MATRIX_HEIGHT) {
|
||||
_pos_y += 1;
|
||||
}
|
||||
}
|
||||
|
||||
void run_paddle() {
|
||||
}
|
||||
|
||||
uint8_t Paddle::get_position() {
|
||||
return _position;
|
||||
return _pos_y;
|
||||
}
|
||||
|
||||
bool Paddle::is_human() {
|
||||
@@ -31,24 +35,93 @@ void Paddle::reset() {
|
||||
_score= 0;
|
||||
}
|
||||
|
||||
bool check_paddle_movements(Paddle &p1, Paddle &p2) {
|
||||
bool need_refresh= false;
|
||||
if (digitalRead(P1_BTN_UP) == LOW) {
|
||||
p1.move_pad_up();
|
||||
need_refresh= true;
|
||||
}
|
||||
else if (digitalRead(P1_BTN_BOTTOM) == LOW) {
|
||||
p1.move_pad_down();
|
||||
need_refresh= true;
|
||||
}
|
||||
bool Paddle::check_pad_movement() {
|
||||
// redefine me
|
||||
return false;
|
||||
}
|
||||
|
||||
if (digitalRead(P2_BTN_UP) == LOW) {
|
||||
p2.move_pad_up();
|
||||
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(P2_BTN_BOTTOM) == LOW) {
|
||||
p2.move_pad_down();
|
||||
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 < 0) _skills= 0;
|
||||
else if (skills > 1) _skills= 1;
|
||||
else _skills= skills;
|
||||
}
|
||||
|
||||
38
src/paddle.h
38
src/paddle.h
@@ -2,19 +2,21 @@
|
||||
#define PADDLE_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <cstdint>
|
||||
#include "config.h"
|
||||
#include "ball.h"
|
||||
|
||||
class Paddle {
|
||||
|
||||
private:
|
||||
protected:
|
||||
// define player coordinates
|
||||
uint8_t _position;
|
||||
uint8_t _pos_y;
|
||||
uint8_t _height= PADDLE_LENGTH;
|
||||
uint8_t _score= 0;
|
||||
bool _human= true;
|
||||
bool _human;
|
||||
|
||||
public:
|
||||
Paddle (uint8_t _position) : _position(_position) {}
|
||||
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();
|
||||
@@ -22,8 +24,34 @@ class Paddle {
|
||||
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);
|
||||
};
|
||||
|
||||
bool check_paddle_movements(Paddle &p1, Paddle &p2);
|
||||
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
|
||||
|
||||
@@ -9,6 +9,11 @@ void Renderer::_clear_matrix() {
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::set_players(Paddle *p1, Paddle *p2) {
|
||||
_p1= p1;
|
||||
_p2= p2;
|
||||
}
|
||||
|
||||
void Renderer::render_timer(uint8_t seconds) {
|
||||
this -> _clear_matrix();
|
||||
|
||||
@@ -22,8 +27,8 @@ void Renderer::render_timer(uint8_t seconds) {
|
||||
|
||||
void Renderer::render_matrix() {
|
||||
this -> _clear_matrix();
|
||||
uint8_t p1pos= _p1.get_position();
|
||||
uint8_t p2pos= _p2.get_position();
|
||||
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;
|
||||
@@ -46,12 +51,12 @@ void Renderer::render_score() {
|
||||
|
||||
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];
|
||||
_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];
|
||||
_frame[h][w+8]= font_pong[_p2 -> get_score()][h][w];
|
||||
}
|
||||
}
|
||||
_matrix.renderBitmap(_frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
||||
@@ -60,7 +65,7 @@ void Renderer::render_score() {
|
||||
void Renderer::render_winner() {
|
||||
this -> _clear_matrix();
|
||||
// check winner
|
||||
if (_p1.get_score() > _p2.get_score())
|
||||
if (_p1 -> get_score() > _p2 -> get_score())
|
||||
_matrix.loadSequence(pone_wins);
|
||||
else
|
||||
_matrix.loadSequence(ptwo_wins);
|
||||
|
||||
@@ -12,8 +12,8 @@ class Renderer {
|
||||
|
||||
private:
|
||||
// define player coordinates
|
||||
Paddle& _p1;
|
||||
Paddle& _p2;
|
||||
Paddle* _p1;
|
||||
Paddle* _p2;
|
||||
Ball& _ball;
|
||||
byte (&_frame)[MATRIX_HEIGHT][MATRIX_WIDTH];
|
||||
ArduinoLEDMatrix& _matrix;
|
||||
@@ -21,9 +21,10 @@ class Renderer {
|
||||
void _clear_matrix();
|
||||
|
||||
public:
|
||||
Renderer (Paddle &p1, Paddle &p2, Ball &ball, byte (&frame)[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix)
|
||||
: _p1(p1), _p2(p2), _ball(ball), _frame(frame), _matrix(matrix) {}
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user