diff --git a/src/ball.cpp b/src/ball.cpp new file mode 100644 index 0000000..a18ecda --- /dev/null +++ b/src/ball.cpp @@ -0,0 +1,51 @@ +#include +#include +#include "config.h" + +class Ball { + + private: + + uint8_t _x, _y; + int8_t _direction_x, _direction_y; + + void _init_directions(int8_t &_direction) { + if (random(2) == 0) _direction= 1; + else _direction= -1; + } + + public: + + void move() { + if (!_direction_x) _init_directions(_direction_x); + if (!_direction_y) _init_directions(_direction_y); + + 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 bounce_on_pad() { + _direction_x *= -1; + } + + 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; + } +}; diff --git a/src/ball.h b/src/ball.h new file mode 100644 index 0000000..0987edd --- /dev/null +++ b/src/ball.h @@ -0,0 +1,21 @@ +#ifndef PLAYER_H +#define PLAYER_H + +#include +#include "config.h" + +class Ball { + private: + uint8_t _x, _y; + int8_t _direction_x, _direction_y; + + public: + void move(); + void bounce_on_pad(); + void bounce_on_sides(); + void reset_position (); + uint8_t get_x(); + uint8_t get_y(); +}; + +#endif diff --git a/src/engine.cpp b/src/engine.cpp new file mode 100644 index 0000000..bf68d2e --- /dev/null +++ b/src/engine.cpp @@ -0,0 +1,69 @@ +#include +#include "ball.h" +#include "paddle.h" +#include "config.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; + } + + void point_scored(Ball &ball) { + ball.reset_position(); + + Serial.print("P1: "); + Serial.print(_p1_score); + Serial.print(" - "); + Serial.print("P2: "); + Serial.print(_p2_score); + Serial.println(); + + _hits= 0; + // ball_delay= INITIAL_BALL_DELAY; + } + + 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; + } + } +}; diff --git a/src/paddle.cpp b/src/paddle.cpp new file mode 100644 index 0000000..3b7c33c --- /dev/null +++ b/src/paddle.cpp @@ -0,0 +1,32 @@ +#include +#include "config.h" + +class Paddle { + + private: + // define player coordinates + uint8_t _position, _height; + bool _human; + + 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; + } + } + + uint8_t get_position() { + return _position; + } + + bool is_human() { + return _human; + } +}; diff --git a/src/paddle.h b/src/paddle.h new file mode 100644 index 0000000..3f0a927 --- /dev/null +++ b/src/paddle.h @@ -0,0 +1,21 @@ +#ifndef PADDLE_H +#define PADDLE_H + +#include +#include "config.h" + +class Paddle { + + private: + // define player coordinates + uint8_t position, height; + bool human; + + public: + void move_pad_up(bool &need_refresh); + void move_pad_down(bool &need_refresh); + uint8_t get_position(); + bool is_human(); +}; + +#endif