clean logs and improve engine handling by removing hits and movment_delay from the loop to the engine

This commit is contained in:
andrea
2026-03-18 21:41:51 +01:00
parent abc0ddb661
commit 49a772f6de
6 changed files with 51 additions and 50 deletions

View File

@@ -23,7 +23,6 @@ ArduinoLEDMatrix matrix;
int need_refresh= true; int need_refresh= true;
uint8_t hits= 0; uint8_t hits= 0;
uint8_t ball_delay= INITIAL_BALL_DELAY;
long exec_t2= millis(); long exec_t2= millis();
enum game_statuses : uint8_t { enum game_statuses : uint8_t {
@@ -38,7 +37,7 @@ game_statuses game_status= TIMER;
Ball ball(4, 6); Ball ball(4, 6);
Paddle p1(1); Paddle p1(1);
Paddle p2(4); Paddle p2(4);
Engine engine(p1, p2, ball); Engine engine(p1, p2, ball, INITIAL_BALL_DELAY);
Renderer renderer(p1, p2, ball, frame, matrix); Renderer renderer(p1, p2, ball, frame, matrix);
void setup() { void setup() {
@@ -72,18 +71,10 @@ void loop() {
case RUN: case RUN:
need_refresh= check_paddle_movements(p1, p2); need_refresh= check_paddle_movements(p1, p2);
if (exec_t1 - exec_t2 > ball_delay) { if (exec_t1 - exec_t2 > engine.ball_movement_delay()) {
engine.run(); engine.run();
if (engine.get_event() == P1SCORE || engine.get_event() == P2SCORE) { if (engine.get_event() == P1SCORE || engine.get_event() == P2SCORE)
game_status= SCORE; game_status= SCORE;
}
else if (engine.get_event() == P1_COLLISION || engine.get_event() == P2_COLLISION) {
hits++;
if (hits >= 6 && ball_delay >= 80) {
hits= 0;
ball_delay-= 20;
}
}
exec_t2= exec_t1; exec_t2= exec_t1;
need_refresh= true; need_refresh= true;
} }
@@ -95,19 +86,17 @@ void loop() {
break; break;
case SCORE: case SCORE:
delay(300); // small delay to let see the last ball position delay(300);
renderer.render_score(); renderer.render_score();
engine.restart_ball();
delay(1000); delay(1000);
ball.reset_position(); if (p1.get_score() >= MAX_POINTS || p2.get_score() >= MAX_POINTS)
ball_delay= INITIAL_BALL_DELAY;
if (p1.get_score() >= MAX_POINTS || p2.get_score() >= MAX_POINTS) {
game_status= GAMEOVER; game_status= GAMEOVER;
}
else { else {
game_status= RUN; game_status= RUN;
// before move again the ball wait a second // before move again the ball wait a second
renderer.render_matrix(); renderer.render_matrix();
delay(1000); exec_t2= millis() + FIRST_START_BALL_DELAY;
} }
break; break;

View File

@@ -1,6 +1,3 @@
#include <Arduino.h>
#include <stdint.h>
#include "config.h"
#include "ball.h" #include "ball.h"
void Ball::_init_directions(int8_t &_direction) { void Ball::_init_directions(int8_t &_direction) {
@@ -30,6 +27,13 @@ void Ball::bounce_on_sides() {
_direction_y *= -1; _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 () { void Ball::reset_position () {
_x= BALL_RESET_X; _x= BALL_RESET_X;
_y= BALL_RESET_Y; _y= BALL_RESET_Y;

View File

@@ -1,7 +1,7 @@
#ifndef BALL_H #ifndef BALL_H
#define BALL_H #define BALL_H
#include <stdint.h> #include <Arduino.h>
#include "config.h" #include "config.h"
class Ball { class Ball {
@@ -17,6 +17,8 @@ class Ball {
void move(); void move();
void bounce_on_pad(); void bounce_on_pad();
void bounce_on_sides(); void bounce_on_sides();
int8_t get_direction_x();
int8_t get_direction_y();
void reset_position (); void reset_position ();
uint8_t get_x(); uint8_t get_x();
uint8_t get_y(); uint8_t get_y();

View File

@@ -10,48 +10,40 @@ bool Engine::_check_pad_ball_collision(Paddle &p) {
return false; return false;
} }
void Engine::_print_score() {
Serial.print("P1: ");
Serial.print(_p1.get_score());
Serial.print(" - ");
Serial.print("P2: ");
Serial.print(_p2.get_score());
Serial.println();
}
void Engine::run() { void Engine::run() {
// if (_event == P1SCORE || _event == P2SCORE) this -> _soft_reset();
_event= NONE; _event= NONE;
_ball.move(); _ball.move();
uint8_t bx= _ball.get_x(); uint8_t bx= _ball.get_x();
uint8_t by= _ball.get_y(); 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 // pad is 1 pixel far from the edge, so i need to calc this delta
if (bx <= 1) { // 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 // score the point only if ball reached the edge
if (this -> _check_pad_ball_collision(_p1) && bx == 1) { if (this -> _check_pad_ball_collision(_p1) && bx <= 1) {
_ball.bounce_on_pad(); _ball.bounce_on_pad();
_event= P1_COLLISION; _event= P1_COLLISION;
_hits++;
} }
else if (bx <= 0) { else if (bx <= 0) {
// p2 scores // p2 scores
_p2.increase_score(); _p2.increase_score();
Serial.println("Player 2 Scores");
this -> _print_score();
_event= P2SCORE; _event= P2SCORE;
return; return;
} }
} }
else if (bx >= MATRIX_WIDTH-2) { else if (bx >= MATRIX_WIDTH-2 && ball_dir > 0) {
// score the point only if ball reached the edge // score the point only if ball reached the edge
if (this -> _check_pad_ball_collision(_p2) && bx == MATRIX_WIDTH-2) { if (this -> _check_pad_ball_collision(_p2) && bx >= MATRIX_WIDTH-2) {
_ball.bounce_on_pad(); _ball.bounce_on_pad();
_event= P2_COLLISION; _event= P2_COLLISION;
_hits++;
} }
else if (bx >= MATRIX_WIDTH-1) { else if (bx >= MATRIX_WIDTH-1) {
// p1 scores // p1 scores
_p1.increase_score(); _p1.increase_score();
Serial.println("Player 1 Scores");
this -> _print_score();
_event= P1SCORE; _event= P1SCORE;
return; return;
} }
@@ -61,14 +53,29 @@ void Engine::run() {
_ball.bounce_on_sides(); _ball.bounce_on_sides();
_event= WALL_COLLISION; _event= WALL_COLLISION;
} }
if (_hits >= 6 && _ball_mv_delay >= 80) {
_hits= 0;
_ball_mv_delay -= 20;
}
} }
uint8_t Engine::ball_movement_delay() {
return _ball_mv_delay;
}
EngineEvents Engine::get_event() { EngineEvents Engine::get_event() {
return _event; return _event;
} }
void Engine::restart_ball() {
_ball.reset_position();
_ball_mv_delay= INITIAL_BALL_DELAY;
}
void Engine::reset() { void Engine::reset() {
this -> restart_ball();
_p1.reset(); _p1.reset();
_p2.reset(); _p2.reset();
_ball.reset_position();
} }

View File

@@ -15,17 +15,20 @@ class Engine {
Paddle& _p2; Paddle& _p2;
Ball& _ball; Ball& _ball;
EngineEvents _event= NONE; 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);
void _print_score();
public: public:
// inizialize Engine constructor, linking all args with private args // inizialize Engine constructor, linking all args with private args
Engine(Paddle &p_one, Paddle &p_two, Ball &ball) Engine(Paddle &p_one, Paddle &p_two, Ball &ball, uint8_t ball_mv_delay)
: _p1(p_one), _p2(p_two), _ball(ball) {} : _p1(p_one), _p2(p_two), _ball(ball), _ball_mv_delay(ball_mv_delay) {}
void run(); void run();
uint8_t ball_movement_delay();
EngineEvents get_event(); EngineEvents get_event();
void restart_ball();
void reset(); void reset();
}; };

View File

@@ -60,13 +60,9 @@ void Renderer::render_score() {
void Renderer::render_winner() { void Renderer::render_winner() {
this -> _clear_matrix(); this -> _clear_matrix();
// check winner // check winner
if (_p1.get_score() > _p2.get_score()) { if (_p1.get_score() > _p2.get_score())
Serial.println("Player 1 wins!!!");
_matrix.loadSequence(pone_wins); _matrix.loadSequence(pone_wins);
} else
else {
Serial.println("Player 2 wins!!!");
_matrix.loadSequence(ptwo_wins); _matrix.loadSequence(ptwo_wins);
}
_matrix.play(true); _matrix.play(true);
} }