diff --git a/arduino_pong.ino b/arduino_pong.ino index 03d7406..edd1678 100644 --- a/arduino_pong.ino +++ b/arduino_pong.ino @@ -2,8 +2,10 @@ #include "src/config.h" #include "src/pong_render.h" -#include "src/pong_player.h" -#include "src/pong_ball.h" + +#include "src/paddle.h" +#include "src/ball.h" +#include "src/engine.h" // create LED matrix object ArduinoLEDMatrix matrix; @@ -20,18 +22,8 @@ byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; -// players coordinates -int players_coords[2]= {1, 4}; -int players_scores[2]= {0, 0}; - -// initials balls coordinates -int ball_x= BALL_RESET_X; -int ball_y= BALL_RESET_Y; - int need_refresh= true; - -int ball_delay= INITIAL_BALL_DELAY; - +uint8_t ball_delay= INITIAL_BALL_DELAY; long exec_t2= millis(); enum game_statuses : uint8_t { @@ -43,6 +35,10 @@ enum game_statuses : uint8_t { }; game_statuses game_status= TIMER; +Ball ball(4, 6); +Paddle p1(1); +Paddle p2(4); +Engine engine(p1, p2, ball); void setup() { Serial.begin(9600); @@ -61,6 +57,7 @@ void loop() { long exec_t1= millis(); switch (game_status) { + case TIMER: for (int i = START_TIMER; i >= 0; i--) { render_timer(frame, i); @@ -73,38 +70,52 @@ void loop() { break; case RUN: - pong_move_p1(players_coords[0], need_refresh); - pong_move_p2(players_coords[1], need_refresh); - if (exec_t1 - exec_t2 > ball_delay) { + if (digitalRead(P1_BTN_UP) == LOW) { + p1.move_pad_up(); need_refresh= true; - bool scored= move_ball(ball_x, ball_y, ball_delay, players_coords, players_scores); - if (scored) { + } + else if (digitalRead(P1_BTN_BOTTOM) == LOW) { + p1.move_pad_down(); + need_refresh= true; + } + if (digitalRead(P2_BTN_UP) == LOW) { + p2.move_pad_up(); + need_refresh= true; + } + else if (digitalRead(P2_BTN_BOTTOM) == LOW) { + p2.move_pad_down(); + need_refresh= true; + } + if (exec_t1 - exec_t2 > ball_delay) { + engine.run(ball_delay); + need_refresh= true; + if (engine.get_event() == P1SCORE || engine.get_event() == P2SCORE) { game_status= SCORE; // delay the ball movement after score - exec_t2= millis() + FIRST_START_BALL_DELAY; + exec_t2= millis() + FIRST_START_BALL_DELAY + 1000; } else exec_t2= exec_t1; } // rerender matrix only if something is changed if (need_refresh) { - render_matrix(frame, players_coords, ball_x, ball_y); + render_matrix(frame, p1, p2, ball); matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); - need_refresh= 0; + need_refresh= false; } delay(50); break; case SCORE: - render_score(frame, players_scores); + render_score(frame, p1, p2); matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH); delay(1000); - if (players_scores[0] >= MAX_POINTS || players_scores[1] >= MAX_POINTS) { + if (p1.get_score() >= MAX_POINTS || p2.get_score() >= MAX_POINTS) { game_status= GAMEOVER; } else game_status= RUN; break; case GAMEOVER: - render_winner(frame, matrix, players_scores); + render_winner(frame, matrix, p1, p2); game_status= WAIT; break; @@ -113,8 +124,7 @@ void loop() { // 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) { game_status= TIMER; - players_scores[0]= 0; - players_scores[1]= 0; + engine.reset(); } delay(100); break; diff --git a/src/ball.cpp b/src/ball.cpp index a18ecda..81b61fe 100644 --- a/src/ball.cpp +++ b/src/ball.cpp @@ -1,51 +1,46 @@ #include #include #include "config.h" +#include "ball.h" -class Ball { +void Ball::_init_directions(int8_t &_direction) { + if (random(2) == 0) _direction= 1; + else _direction= -1; +} - private: +void Ball::move() { + if (!_direction_x) this -> _init_directions(_direction_x); + if (!_direction_y) this -> _init_directions(_direction_y); - uint8_t _x, _y; - int8_t _direction_x, _direction_y; + // if (_x < 0 || _x > MATRIX_WIDTH-1 || _y < 0 || _y > MATRIX_HEIGHT-1) { + // this -> reset_position(); + // } - void _init_directions(int8_t &_direction) { - if (random(2) == 0) _direction= 1; - else _direction= -1; - } + 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; +} - public: +void Ball::bounce_on_pad() { + _direction_x *= -1; +} - void move() { - if (!_direction_x) _init_directions(_direction_x); - if (!_direction_y) _init_directions(_direction_y); +void Ball::bounce_on_sides() { + _direction_y *= -1; +} - 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::reset_position () { + _x= BALL_RESET_X; + _y= BALL_RESET_Y; + this -> _init_directions(_direction_x); + this -> _init_directions(_direction_y); +} - void bounce_on_pad() { - _direction_x *= -1; - } +uint8_t Ball::get_x() { + return _x; +} - void bounce_on_sides() { - _direction_y *= -1; - } - - void reset_position () { - _x= BALL_RESET_X; - _y= BALL_RESET_Y; - _init_directions(_direction_x); - _init_directions(_direction_y); - } - - uint8_t get_x() { - return _x; - } - - uint8_t get_y() { - return _y; - } -}; +uint8_t Ball::get_y() { + return _y; +} diff --git a/src/ball.h b/src/ball.h index 0987edd..f358079 100644 --- a/src/ball.h +++ b/src/ball.h @@ -1,5 +1,5 @@ -#ifndef PLAYER_H -#define PLAYER_H +#ifndef BALL_H +#define BALL_H #include #include "config.h" @@ -9,7 +9,11 @@ class Ball { 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(); diff --git a/src/config.h b/src/config.h index 60c907f..333d4cc 100644 --- a/src/config.h +++ b/src/config.h @@ -7,7 +7,7 @@ #define MATRIX_HEIGHT 8 #define BALL_RESET_X (MATRIX_WIDTH / 2) #define BALL_RESET_Y (MATRIX_HEIGHT / 2) -#define BAR_LENGTH 3 +#define PADDLE_LENGTH 3 #define INITIAL_BALL_DELAY 200 #define MAX_POINTS 9 diff --git a/src/engine.cpp b/src/engine.cpp index bf68d2e..27a2f5f 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -2,68 +2,87 @@ #include "ball.h" #include "paddle.h" #include "config.h" +#include "engine.h" -class Engine { - - private: - Paddle& _p1; - Paddle& _p2; - Ball& _ball; - uint8_t _p1_score, _p2_score; - uint8_t _hits; - - bool _check_pad_ball_collision(Paddle &p1, Ball &ball) { - return false; +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 point_scored(Ball &ball) { - ball.reset_position(); +void Engine::_print_score() { + Serial.print("P1: "); + Serial.print(_p1.get_score()); + Serial.print(" - "); + Serial.print("P2: "); + Serial.print(_p2.get_score()); + Serial.println(); +} - Serial.print("P1: "); - Serial.print(_p1_score); - Serial.print(" - "); - Serial.print("P2: "); - Serial.print(_p2_score); - Serial.println(); +void Engine::run(uint8_t &ball_delay) { + _event= NONE; + _ball.move(); + uint8_t bx= _ball.get_x(); + uint8_t by= _ball.get_y(); - _hits= 0; - // ball_delay= INITIAL_BALL_DELAY; + if (bx == 0) { + if (!this -> _check_pad_ball_collision(_p1)) { + // p2 scores + _p2.increase_score(); + _ball.reset_position(); + ball_delay= INITIAL_BALL_DELAY; + Serial.println("Player 2 Scores"); + this -> _print_score(); + _event= P2SCORE; + return; } - - public: - // inizialize Engine constructor, linking all args with private args - Engine(Paddle &p_one, Paddle &p_two, Ball &ball): _p1(p_one), _p2(p_two), _ball(ball) {} - - void run(uint8_t &ball_delay) { - _ball.move(); - uint8_t bx= _ball.get_x(); - uint8_t by= _ball.get_y(); - - if (bx == 0) { - if (!_check_pad_ball_collision(_p1, _ball)) { - // p2 scores - _p2_score +=1; - Serial.println("Player 2 Scores"); - } - else { - _hits += 1; - _ball.bounce_on_pad(); - } - } - else if (bx >= MATRIX_WIDTH-1) { - if (!_check_pad_ball_collision(_p2, _ball)) { - // p1 scores - _p1_score += 1; - Serial.println("Player 1 Scores"); - } - } - - if (by == 0 || by == MATRIX_HEIGHT-1) _ball.bounce_on_sides(); - - if (_hits >= 6 && ball_delay >= 80) { - // increase ball speed - _hits= 0; - ball_delay-= 20; - } + else { + _hits += 1; + _ball.bounce_on_pad(); + _event= P2_COLLISION; } -}; + } + else if (bx >= MATRIX_WIDTH-1) { + if (!this -> _check_pad_ball_collision(_p2)) { + // p1 scores + _p1.increase_score(); + _ball.reset_position(); + ball_delay= INITIAL_BALL_DELAY; + Serial.println("Player 1 Scores"); + this -> _print_score(); + _event= P1SCORE; + return; + } + else { + _hits += 1; + _ball.bounce_on_pad(); + _event= P1_COLLISION; + } + } + + if (by == 0 || by == MATRIX_HEIGHT-1) { + _ball.bounce_on_sides(); + _event= WALL_COLLISION; + } + + // increase ball speed every 6 hits on pads + // if ball is not at max speed + if (_hits >= 6 && ball_delay >= 80) { + _hits= 0; + ball_delay-= 20; + } +} + +EngineEvents Engine::get_event() { + return _event; +} + +void Engine::reset() { + _p1.reset(); + _p2.reset(); + _ball.reset_position(); +} diff --git a/src/engine.h b/src/engine.h new file mode 100644 index 0000000..05f3ea6 --- /dev/null +++ b/src/engine.h @@ -0,0 +1,33 @@ +#ifndef ENGINE_H +#define ENGINE_H + +#include +#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; + uint8_t _hits; + EngineEvents _event= NONE; + + bool _check_pad_ball_collision(Paddle &p); + void _print_score(); + + public: + // inizialize Engine constructor, linking all args with private args + Engine(Paddle &p_one, Paddle &p_two, Ball &ball) + : _p1(p_one), _p2(p_two), _ball(ball) {} + + void run(uint8_t &ball_delay); + EngineEvents get_event(); + void reset(); +}; + +#endif diff --git a/src/paddle.cpp b/src/paddle.cpp index 3b7c33c..b14b516 100644 --- a/src/paddle.cpp +++ b/src/paddle.cpp @@ -1,32 +1,34 @@ -#include +#include #include "config.h" +#include "paddle.h" -class Paddle { +void Paddle::move_pad_up() { + if (_position > 0) { + _position -= 1; + } +} +void Paddle::move_pad_down() { + if (_position + _height < MATRIX_HEIGHT) { + _position += 1; + } +} - private: - // define player coordinates - uint8_t _position, _height; - bool _human; +uint8_t Paddle::get_position() { + return _position; +} - public: - void move_pad_up(bool &need_refresh) { - if (_position > 0) { - _position -= 1; - need_refresh= true; - } - } - void move_pad_down(bool &need_refresh) { - if (_position + _height <= MATRIX_HEIGHT) { - _position += 1; - need_refresh= true; - } - } +bool Paddle::is_human() { + return _human; +} - uint8_t get_position() { - return _position; - } +void Paddle::increase_score() { + if (_score <= 9) _score += 1; +} - bool is_human() { - return _human; - } -}; +uint8_t Paddle::get_score() { + return _score; +} + +void Paddle::reset() { + _score= 0; +} diff --git a/src/paddle.h b/src/paddle.h index 3f0a927..0d7318b 100644 --- a/src/paddle.h +++ b/src/paddle.h @@ -1,21 +1,27 @@ #ifndef PADDLE_H #define PADDLE_H -#include +#include #include "config.h" class Paddle { private: // define player coordinates - uint8_t position, height; - bool human; + uint8_t _position; + uint8_t _height= PADDLE_LENGTH; + uint8_t _score= 0; + bool _human= true; public: - void move_pad_up(bool &need_refresh); - void move_pad_down(bool &need_refresh); + Paddle (uint8_t _position) : _position(_position) {} + void move_pad_up(); + void move_pad_down(); uint8_t get_position(); bool is_human(); + void increase_score(); + uint8_t get_score(); + void reset(); }; #endif diff --git a/src/pong_ball.cpp b/src/pong_ball.cpp deleted file mode 100644 index 1b86829..0000000 --- a/src/pong_ball.cpp +++ /dev/null @@ -1,97 +0,0 @@ -#include -#include "config.h" -#include "pong_player.h" - -// used to increase speed when game is too easy -int hits= 0; - -// 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; - -void random_ball_movement(int &ball_move_x, int &ball_move_y) { - 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; -} - -void point_scored(int &ball_x, int &ball_y, int &ball_delay, int players_scores[2], int &ball_move_x, int &ball_move_y) { - ball_x= BALL_RESET_X; - ball_y= BALL_RESET_Y; - random_ball_movement(ball_move_x, ball_move_y); - - Serial.print("P1: "); - Serial.print(players_scores[0]); - Serial.print(" - "); - Serial.print("P2: "); - Serial.print(players_scores[1]); - Serial.println(); - - hits= 0; - ball_delay= INITIAL_BALL_DELAY; -} - -bool move_ball(int &ball_x, int &ball_y, int &ball_delay, int players_coords[2], int players_scores[2]) { - 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 false; - } - - bool scored= false; - - // 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 - random_ball_movement(ball_move_x, ball_move_y); - } - - else if (ball_x == 0) { - // if p1 collision: reverse x, go left - if (!ball_player_collision(players_coords[0], ball_y)) { - // else p2 score, reset board - players_scores[1] += 1; - scored= true; - Serial.println("Player 2 Scores"); - point_scored(ball_x, ball_y, ball_delay, players_scores, ball_move_x, ball_move_y); - return true; - } - else { - hits += 1; - ball_move_x= ball_move_x * -1; - } - } - else if (ball_x == MATRIX_WIDTH-1) { - if (!ball_player_collision(players_coords[1], ball_y)) { - // else p1 score, reset board - players_scores[0] += 1; - scored= true; - Serial.println("Player 1 Scores"); - point_scored(ball_x, ball_y, ball_delay, players_scores, ball_move_x, ball_move_y); - return true; - } - 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 && ball_delay >= 80) { - // increase ball speed - hits= 0; - ball_delay-= 20; - } - - ball_x+= ball_move_x; - ball_y+= ball_move_y; - return scored; -} diff --git a/src/pong_ball.h b/src/pong_ball.h deleted file mode 100644 index 37a0e0f..0000000 --- a/src/pong_ball.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef PONG_BALL_H -#define PONG_BALL_H - -bool move_ball(int &ball_x, int &ball_y, int &loop_delay, int players_coords[2], int players_scores[2]); -void foo(); -#endif diff --git a/src/pong_player.cpp b/src/pong_player.cpp deleted file mode 100644 index e3238e8..0000000 --- a/src/pong_player.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include "config.h" - -int ball_player_collision(int player, int ball_y) { - for (int p= player; p < player + BAR_LENGTH; p++) { - if (ball_y == p) { - return 1; - } - } - return 0; -} - -int pong_move_p1(int &p1_start, int &need_refresh) { - if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) { - p1_start -= 1; - need_refresh= true; - } - else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) { - p1_start += 1; - need_refresh= true; - } -} - -int pong_move_p2(int &p2_start, int &need_refresh) { - if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) { - p2_start -= 1; - need_refresh= true; - } - else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) { - p2_start += 1; - need_refresh= true; - } -} diff --git a/src/pong_player.h b/src/pong_player.h deleted file mode 100644 index 6692400..0000000 --- a/src/pong_player.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef PONG_PLAYER_H -#define PONG_PLAYER_H - -int ball_player_collision(int player, int ball_y); -int pong_move_p1(int &p1_start, int &need_refresh); -int pong_move_p2(int &p2_start, int &need_refresh); - -#endif diff --git a/src/pong_render.cpp b/src/pong_render.cpp index a32e6aa..275e955 100644 --- a/src/pong_render.cpp +++ b/src/pong_render.cpp @@ -1,6 +1,8 @@ #include #include "Arduino_LED_Matrix.h" #include "config.h" +#include "paddle.h" +#include "ball.h" #include "font.h" void clear_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH]) { @@ -11,28 +13,26 @@ void clear_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH]) { } } -void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int players_coords[2], int ball_x, int ball_y) { +void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], Paddle &p1, Paddle &p2, Ball &ball) { clear_matrix(frame); - int player_one= players_coords[0]; - int player_two= players_coords[1]; + uint8_t p1pos= p1.get_position(); + uint8_t p2pos= p2.get_position(); // players coords - for (int i= player_one; i < player_one+BAR_LENGTH; i++) { + for (int i= p1pos; i < p1pos+PADDLE_LENGTH; i++) { frame[i][0]= 1; } - for (int i= player_two; i < player_two+BAR_LENGTH; i++) { + for (int i= p2pos; i < p2pos+PADDLE_LENGTH; i++) { frame[i][MATRIX_WIDTH-1]= 1; } // ball coords - frame[ball_y][ball_x]= 1; + uint8_t bx= ball.get_x(); + uint8_t by= ball.get_y(); + frame[by][bx]= 1; } -void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int players_scores[2]) { +void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], Paddle &p1, Paddle &p2) { clear_matrix(frame); - int player_one= players_scores[0]; - int player_two= players_scores[1]; - if (player_one > 9) player_one = 9; - if (player_two > 9) player_two = 9; // player score separator (-) frame[4][5]= 1; @@ -40,12 +40,12 @@ void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int players_scores[2] for (int h=0; h < 8; h++) { for (int w=0; w < 3; w++) { - frame[h][w+1]= font_pong[player_one][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[player_two][h][w]; + frame[h][w+8]= font_pong[p2.get_score()][h][w]; } } } @@ -60,10 +60,10 @@ void render_timer(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int seconds) { } } -void render_winner(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix, int players_scores[2]) { +void render_winner(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix, Paddle &p1, Paddle &p2) { clear_matrix(frame); // check winner - if (players_scores[0] > players_scores[1]) { + if (p1.get_score() > p2.get_score()) { Serial.println("Player 1 wins!!!"); matrix.loadSequence(pone_wins); } diff --git a/src/pong_render.h b/src/pong_render.h index 5d935e1..2323b20 100644 --- a/src/pong_render.h +++ b/src/pong_render.h @@ -4,10 +4,12 @@ #include #include "Arduino_LED_Matrix.h" #include "config.h" +#include "paddle.h" +#include "ball.h" -void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int players_coords[2], int ball_x, int ball_y); -void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int players_scores[2]); +void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], Paddle &p1, Paddle &p2, Ball &ball); +void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], Paddle &p1, Paddle &p2); void render_timer(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int seconds); -void render_winner(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix, int players_scores[2]); +void render_winner(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix, Paddle &p1, Paddle &p2); #endif