start refactoring pong entities to use classes
This commit is contained in:
51
src/ball.cpp
Normal file
51
src/ball.cpp
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
};
|
||||||
21
src/ball.h
Normal file
21
src/ball.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#ifndef PLAYER_H
|
||||||
|
#define PLAYER_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#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
|
||||||
69
src/engine.cpp
Normal file
69
src/engine.cpp
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
32
src/paddle.cpp
Normal file
32
src/paddle.cpp
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
};
|
||||||
21
src/paddle.h
Normal file
21
src/paddle.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#ifndef PADDLE_H
|
||||||
|
#define PADDLE_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#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
|
||||||
Reference in New Issue
Block a user