3 Commits

Author SHA1 Message Date
andrea
c47291e258 minors
Some checks failed
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CD / release (push) Has been cancelled
2026-03-15 20:29:56 +01:00
andrea
fec4db0c5d improve rerender logics
Some checks failed
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
2026-03-15 20:23:52 +01:00
andrea
7c065d8799 updated ci.yml
Some checks failed
Arduino Pong CI / build (macos-latest) (push) Has been cancelled
Arduino Pong CI / build (ubuntu-latest) (push) Has been cancelled
Arduino Pong CI / build (windows-latest) (push) Has been cancelled
2026-03-15 20:09:21 +01:00
8 changed files with 27 additions and 24 deletions

View File

@@ -7,6 +7,8 @@ on:
- 'arduino_pong.ino'
- 'Makefile'
- '.github/workflows/ci.yml'
- '*.cpp'
- '*.h'
jobs:
build:

View File

@@ -8,7 +8,7 @@ A classic implementation of the Pong game developed specifically for the Arduino
# 📹 Preview
![Pong](preview.png)
![Pong](assets/preview.png)
Youtube: https://youtu.be/ouLBTDjpKqc

View File

@@ -8,13 +8,13 @@
// create LED matrix object
ArduinoLEDMatrix matrix;
// initial pong frame
// initial pong frame matrix
byte frame[MATRIX_HEIGHT][MATRIX_WIDTH] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
@@ -30,32 +30,35 @@ int ball_y= BALL_RESET_Y;
int need_refresh= 1;
int loop_delay= INITIAL_LOOP_DELAY;
int ball_delay= INITIAL_BALL_DELAY;
long exec_t2= millis();
void setup() {
//Serial.begin(115200);
Serial.begin(9600);
// stard LED matrix
// start LED matrix
matrix.begin();
pinMode(P1_BTN_UP, INPUT_PULLUP);
pinMode(P1_BTN_BOTTOM, INPUT_PULLUP);
pinMode(P2_BTN_UP, INPUT_PULLUP);
pinMode(P2_BTN_BOTTOM, INPUT_PULLUP);
randomSeed(analogRead(0));
randomSeed(millis());
}
void loop() {
long exec_t1= millis();
pong_move_p1(p1_start, need_refresh);
pong_move_p2(p2_start, need_refresh);
render_matrix(frame, p1_start, p2_start, need_refresh, ball_x, ball_y);
if (exec_t1 - exec_t2 > loop_delay) {
move_ball(ball_x, ball_y, loop_delay, p1_start, p2_start, need_refresh);
if (exec_t1 - exec_t2 > ball_delay) {
move_ball(ball_x, ball_y, ball_delay, p1_start, p2_start, need_refresh);
exec_t2= exec_t1;
}
if (need_refresh) {
render_matrix(frame, p1_start, p2_start, ball_x, ball_y);
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
need_refresh= 0;
}
delay(50);
}

View File

Before

Width:  |  Height:  |  Size: 847 KiB

After

Width:  |  Height:  |  Size: 847 KiB

View File

@@ -8,4 +8,4 @@
#define BALL_RESET_X (MATRIX_WIDTH / 2)
#define BALL_RESET_Y (MATRIX_HEIGHT / 2)
#define BAR_LENGTH 3
#define INITIAL_LOOP_DELAY 200
#define INITIAL_BALL_DELAY 200

View File

@@ -13,7 +13,7 @@ int p2_score= 0;
int ball_move_x= 0;
int ball_move_y= 0;
void point_scored(int &ball_x, int &ball_y, int &loop_delay) {
void point_scored(int &ball_x, int &ball_y, int &ball_delay) {
ball_x= BALL_RESET_X;
ball_y= BALL_RESET_Y;
Serial.print("P1: ");
@@ -24,10 +24,10 @@ void point_scored(int &ball_x, int &ball_y, int &loop_delay) {
Serial.println();
hits= 0;
loop_delay= INITIAL_LOOP_DELAY;
ball_delay= INITIAL_BALL_DELAY;
}
void move_ball(int &ball_x, int &ball_y, int &loop_delay, int p1_start, int p2_start, int &need_refresh) {
void move_ball(int &ball_x, int &ball_y, int &ball_delay, int p1_start, int p2_start, int &need_refresh) {
need_refresh= 1;
if (ball_x < 0 || ball_x > MATRIX_WIDTH-1 || ball_y < 0 || ball_y > MATRIX_HEIGHT-1) {
// ball out of matrix limits
@@ -52,7 +52,7 @@ void move_ball(int &ball_x, int &ball_y, int &loop_delay, int p1_start, int p2_s
// else p2 score, reset board
p2_score += 1;
Serial.println("Player 2 Scores");
point_scored(ball_x, ball_y, loop_delay);
point_scored(ball_x, ball_y, ball_delay);
}
else {
hits += 1;
@@ -64,7 +64,7 @@ void move_ball(int &ball_x, int &ball_y, int &loop_delay, int p1_start, int p2_s
// else p1 score, reset board
p1_score += 1;
Serial.println("Player 1 Scores");
point_scored(ball_x, ball_y, loop_delay);
point_scored(ball_x, ball_y, ball_delay);
}
else {
hits += 1;
@@ -77,10 +77,10 @@ void move_ball(int &ball_x, int &ball_y, int &loop_delay, int p1_start, int p2_s
ball_move_y= ball_move_y * -1;
}
if (hits >= 6 && loop_delay >= 80) {
if (hits >= 6 && ball_delay >= 80) {
// increase ball speed
hits = 0;
loop_delay -= 20;
ball_delay -= 20;
}
ball_x+= ball_move_x;

View File

@@ -1,9 +1,7 @@
#include <Arduino.h>
#include "config.h"
void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int p1_start, int p2_start, int &need_refresh, int ball_x, int ball_y) {
if (!need_refresh) return;
need_refresh= 0;
void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int p1_start, int p2_start, int ball_x, int ball_y) {
// clear
for (int x=0; x < MATRIX_WIDTH; x++) {
for (int y=0; y < MATRIX_HEIGHT; y++) {

View File

@@ -4,6 +4,6 @@
#include <Arduino.h>
#include "Arduino_LED_Matrix.h"
void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int p1_start, int p2_start, int &need_refresh, int ball_x, int ball_y);
void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int p1_start, int p2_start, int ball_x, int ball_y);
#endif