renderer class

This commit is contained in:
andrea
2026-03-18 19:46:52 +01:00
parent 7afa70c9d8
commit 1f6e65c5ec
5 changed files with 10 additions and 110 deletions

View File

@@ -1,14 +1,11 @@
#include "Arduino_LED_Matrix.h"
#include "src/config.h"
#include "src/pong_render.h"
#include "src/renderer.h"
#include "src/engine.h"
#include "src/paddle.h"
#include "src/ball.h"
#include "src/engine.h"
// create LED matrix object
ArduinoLEDMatrix matrix;
// initial pong frame matrix
byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = {
@@ -22,6 +19,8 @@ byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
ArduinoLEDMatrix matrix;
int need_refresh= true;
uint8_t hits= 0;
uint8_t ball_delay= INITIAL_BALL_DELAY;
@@ -40,6 +39,7 @@ Ball ball(4, 6);
Paddle p1(1);
Paddle p2(4);
Engine engine(p1, p2, ball);
Renderer renderer(p1, p2, ball, frame, matrix);
void setup() {
Serial.begin(9600);
@@ -61,8 +61,7 @@ void loop() {
case TIMER:
for (int i = START_TIMER; i >= 0; i--) {
render_timer(frame, i);
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
renderer.render_timer(i);
delay(1000);
}
game_status= RUN;
@@ -90,16 +89,14 @@ void loop() {
}
// rerender matrix only if something is changed
if (need_refresh) {
render_matrix(frame, p1, p2, ball);
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
renderer.render_matrix();
need_refresh= false;
}
break;
case SCORE:
delay(300); // small delay to let see the last ball position
render_score(frame, p1, p2);
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
renderer.render_score();
delay(1000);
ball.reset_position();
ball_delay= INITIAL_BALL_DELAY;
@@ -109,14 +106,13 @@ void loop() {
else {
game_status= RUN;
// before move again the ball wait a second
render_matrix(frame, p1, p2, ball);
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
renderer.render_matrix();
delay(1000);
}
break;
case GAMEOVER:
render_winner(frame, matrix, p1, p2);
renderer.render_winner();
game_status= WAIT;
break;

View File

@@ -1,7 +1,3 @@
#include <Arduino.h>
#include "ball.h"
#include "paddle.h"
#include "config.h"
#include "engine.h"
bool Engine::_check_pad_ball_collision(Paddle &p) {

View File

@@ -1,5 +1,3 @@
#include <Arduino.h>
#include "config.h"
#include "paddle.h"
void Paddle::move_pad_up() {

View File

@@ -1,75 +0,0 @@
#include <Arduino.h>
#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]) {
for (int x=0; x < MATRIX_WIDTH; x++) {
for (int y=0; y < MATRIX_HEIGHT; y++) {
frame[y][x]= 0;
}
}
}
void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], Paddle &p1, Paddle &p2, Ball &ball) {
clear_matrix(frame);
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;
}
for (int i= p2pos; i < p2pos+PADDLE_LENGTH; i++) {
frame[i][MATRIX_WIDTH-1]= 1;
}
// ball coords
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], Paddle &p1, Paddle &p2) {
clear_matrix(frame);
// player score separator (-)
frame[4][5]= 1;
frame[4][6]= 1;
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];
}
}
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];
}
}
}
void render_timer(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int seconds) {
clear_matrix(frame);
for (int h=0; h < 8; h++) {
for (int w=0; w < 3; w++) {
frame[h][w+5]= font_pong[seconds][h][w];
}
}
}
void render_winner(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix, Paddle &p1, Paddle &p2) {
clear_matrix(frame);
// check winner
if (p1.get_score() > p2.get_score()) {
Serial.println("Player 1 wins!!!");
matrix.loadSequence(pone_wins);
}
else {
Serial.println("Player 2 wins!!!");
matrix.loadSequence(ptwo_wins);
}
matrix.play(true);
}

View File

@@ -1,15 +0,0 @@
#ifndef PONG_RENDER_H
#define PONG_RENDER_H
#include <Arduino.h>
#include "Arduino_LED_Matrix.h"
#include "config.h"
#include "paddle.h"
#include "ball.h"
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, Paddle &p1, Paddle &p2);
#endif