Compare commits
42 Commits
c00eeb2501
...
v1.0.4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c774e11923 | ||
|
|
b3f6aeb3fe | ||
|
|
6e5fc8ea10 | ||
|
|
28f029ce03 | ||
|
|
cb42a45aa7 | ||
|
|
c47291e258 | ||
|
|
fec4db0c5d | ||
|
|
7c065d8799 | ||
|
|
31891bbc0e | ||
|
|
5eeb735364 | ||
|
|
a938adff31 | ||
|
|
1f143520a8 | ||
|
|
cbdfcce354 | ||
|
|
66957b5e19 | ||
|
|
f12af76845 | ||
|
|
8906b2eab5 | ||
|
|
5b94e21f21 | ||
|
|
8a7bd2b192 | ||
|
|
487756978f | ||
|
|
bf2b794421 | ||
|
|
f0e8a7f05f | ||
|
|
2727dfb0ef | ||
|
|
bb79c885a9 | ||
|
|
cd9d4289ef | ||
|
|
d15ae7ba57 | ||
|
|
1337269d32 | ||
|
|
69386ba4da | ||
|
|
455014fdff | ||
|
|
280e681ba6 | ||
|
|
f710fb2a0e | ||
|
|
748cf6a678 | ||
|
|
b1b421c367 | ||
|
|
abc381f397 | ||
|
|
5ebf74e044 | ||
|
|
486ee4df97 | ||
|
|
b9bc138cad | ||
|
|
e7f0c3cc1a | ||
|
|
fa47480aef | ||
|
|
adf2498344 | ||
|
|
fcafc56012 | ||
|
|
ebc6596dce | ||
|
|
f2a6a02005 |
37
.github/workflows/cd.yml
vendored
Normal file
37
.github/workflows/cd.yml
vendored
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
name: Arduino Pong CD
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v*'
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
release:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install Arduino CLI
|
||||||
|
run: |
|
||||||
|
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh
|
||||||
|
echo "./bin" >> $GITHUB_PATH
|
||||||
|
|
||||||
|
- name: Install Board Core and Libs
|
||||||
|
run: |
|
||||||
|
arduino-cli core update-index
|
||||||
|
arduino-cli core install arduino:renesas_uno
|
||||||
|
|
||||||
|
- name: Build Binary
|
||||||
|
run: make compile
|
||||||
|
|
||||||
|
- name: Create Release
|
||||||
|
uses: softprops/action-gh-release@v2
|
||||||
|
with:
|
||||||
|
files: |
|
||||||
|
build/*.bin
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
39
.github/workflows/ci.yml
vendored
Normal file
39
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
name: Arduino Pong CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
paths:
|
||||||
|
- 'arduino_pong.ino'
|
||||||
|
- 'Makefile'
|
||||||
|
- '.github/workflows/ci.yml'
|
||||||
|
- 'src/*.cpp'
|
||||||
|
- 'src/*.h'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||||
|
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout Code
|
||||||
|
uses: actions/checkout@v6
|
||||||
|
|
||||||
|
- name: Setup Arduino CLI
|
||||||
|
#uses: arduino/setup-arduino-cli@v2
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh
|
||||||
|
echo "./bin" >> $GITHUB_PATH
|
||||||
|
|
||||||
|
- name: Install UNO R4 Core
|
||||||
|
run: |
|
||||||
|
arduino-cli core update-index
|
||||||
|
arduino-cli core install arduino:renesas_uno
|
||||||
|
|
||||||
|
- name: Compile Sketch (via Makefile)
|
||||||
|
shell: bash
|
||||||
|
run: make prepare_and_compile
|
||||||
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
.secrets
|
||||||
|
.arduino_cache/
|
||||||
|
bin/
|
||||||
|
build/
|
||||||
63
Makefile
Normal file
63
Makefile
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
PORT ?= /dev/ttyACM0
|
||||||
|
BOARD = arduino:renesas_uno:unor4wifi
|
||||||
|
SKETCH = arduino_pong.ino
|
||||||
|
CACHE_DIR = $(shell pwd)/.arduino_cache
|
||||||
|
BIN_DIR = $(shell pwd)/bin
|
||||||
|
CLI = $(BIN_DIR)/arduino-cli --config-file $(CACHE_DIR)/arduino-cli.yaml
|
||||||
|
|
||||||
|
YELLOW := \033[0;33m
|
||||||
|
GREEN := \033[0;32m
|
||||||
|
CLEAR := \033[0m
|
||||||
|
|
||||||
|
prepare:
|
||||||
|
@mkdir -p $(BIN_DIR)
|
||||||
|
@mkdir -p $(CACHE_DIR)
|
||||||
|
@if [ ! -f $(BIN_DIR)/arduino-cli ]; then \
|
||||||
|
echo -e "📥 $(YELLOW)Downloading arduino-cli...$(CLEAR)"; \
|
||||||
|
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=$(BIN_DIR) sh; \
|
||||||
|
fi
|
||||||
|
@if [ ! -f $(CACHE_DIR)/arduino-cli.yaml ]; then \
|
||||||
|
echo -e "⚙️ $(YELLOW)Initializing local config...$(CLEAR)"; \
|
||||||
|
$(CLI) config init --dest-dir $(CACHE_DIR); \
|
||||||
|
$(CLI) config set directories.data $(CACHE_DIR)/data; \
|
||||||
|
$(CLI) config set directories.downloads $(CACHE_DIR)/downloads; \
|
||||||
|
$(CLI) config set directories.user $(CACHE_DIR)/user; \
|
||||||
|
fi
|
||||||
|
@if ! $(CLI) core list | grep -q "arduino:renesas_uno"; then \
|
||||||
|
echo -e "$(YELLOW)📥 Installing board core...$(CLEAR)"; \
|
||||||
|
$(CLI) core update-index; \
|
||||||
|
$(CLI) core install arduino:renesas_uno; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
compile:
|
||||||
|
@mkdir -p build
|
||||||
|
@echo -e "$(GREEN)🛠️ Compiling $(SKETCH)...$(CLEAR)"
|
||||||
|
@$(CLI) compile -b $(BOARD) --output-dir build $(SKETCH)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@echo -e "$(GREEN)🛠️ Removing build files...$(CLEAR)"
|
||||||
|
@rm -rf build/
|
||||||
|
|
||||||
|
prepare_and_compile:
|
||||||
|
make prepare --no-print-directory
|
||||||
|
make compile --no-print-directory
|
||||||
|
|
||||||
|
upload:
|
||||||
|
@echo -e "$(GREEN)🚀 Uploading $(SKETCH)...$(CLEAR)"
|
||||||
|
@$(CLI) upload -b $(BOARD) -p $(PORT)
|
||||||
|
|
||||||
|
upload_verbose:
|
||||||
|
@echo -e "$(GREEN)🚀 Uploading (Verbose) $(SKETCH)...$(CLEAR)"
|
||||||
|
@$(CLI) upload -b $(BOARD) -p $(PORT) -v
|
||||||
|
|
||||||
|
monitor:
|
||||||
|
@$(CLI) monitor -p $(PORT)
|
||||||
|
|
||||||
|
run:
|
||||||
|
make compile --no-print-directory
|
||||||
|
make upload --no-print-directory
|
||||||
|
make monitor --no-print-directory
|
||||||
|
|
||||||
|
run_init:
|
||||||
|
make prepare --no-print-directory
|
||||||
|
make run --no-print-directory
|
||||||
69
README.md
69
README.md
@@ -1,3 +1,68 @@
|
|||||||
# arduino_pong
|
# 🏓 Arduino UNO R4 WiFi Pong
|
||||||
|
|
||||||
Play Pong on Arduino UNO R4 WiFi LED Matrix
|
A classic implementation of the Pong game developed specifically for the Arduino UNO R4 WiFi, utilizing its built-in 12×8 LED matrix as the game screen.
|
||||||
|
|
||||||
|
[](https://github.com/Dea1993/arduino_pong/actions/workflows/ci.yml)
|
||||||
|
[](https://github.com/Dea1993/arduino_pong/actions/workflows/cd.yml)
|
||||||
|
[](LICENSE)
|
||||||
|
|
||||||
|
# 📹 Preview
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Youtube: https://youtu.be/ouLBTDjpKqc
|
||||||
|
|
||||||
|
# 📝 Description
|
||||||
|
|
||||||
|
This project transforms your Arduino board into a minimalist handheld console. By leveraging the Arduino_LED_Matrix library, the game manages ball physics, collision detection with walls and paddles, and a scoring system displayed via the Serial Monitor.
|
||||||
|
|
||||||
|
Key Features:
|
||||||
|
|
||||||
|
- Integrated Display: No external modules required; it uses the native 12×8 LED matrix.
|
||||||
|
- Local Multiplayer: Two-player support using 4 external pushbuttons.
|
||||||
|
- Dynamic Difficulty: The ball speed periodically increases to keep the gameplay challenging.
|
||||||
|
- Real-time Scoring: Points are tracked and sent via Serial (9600 baud).
|
||||||
|
|
||||||
|
# 🛠️ Hardware Requirements
|
||||||
|
|
||||||
|
- Board: Arduino UNO R4 WiFi.
|
||||||
|
- Buttons: 4x Momentary Pushbuttons.
|
||||||
|
- Resistors: Not required (uses internal INPUT_PULLUP).
|
||||||
|
- Breadboard & Jumper wires.
|
||||||
|
|
||||||
|
# 🖮 Pinout Configuration
|
||||||
|
|
||||||
|
Component Arduino Pin Function
|
||||||
|
|
||||||
|
- P1 Button Up D13 Moves Left Paddle Up
|
||||||
|
- P1 Button Down D12 Moves Left Paddle Down
|
||||||
|
- P2 Button Up D11 Moves Right Paddle Up
|
||||||
|
- P2 Button Down D10 Moves Right Paddle Down
|
||||||
|
- Common GND GND Ground for all buttons
|
||||||
|
|
||||||
|
# 🕹️ Game Logic
|
||||||
|
|
||||||
|
- Bouncing: When the ball hits a paddle, the X direction is reversed and the hits counter increases.
|
||||||
|
- Speed Scaling: Every 6 successful hits (hits >= 6), the loop_delay decreases by 20ms, speeding up the action until a minimum limit of 80ms is reached.
|
||||||
|
- Scoring: If a player misses the ball, the opponent scores a point. The ball then resets to the center with a randomized direction.
|
||||||
|
|
||||||
|
# 🛠️ Development
|
||||||
|
|
||||||
|
This project includes a `Makefile` to automate the workflow using `arduino-cli`.
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
* [Arduino CLI](https://arduino.github.io/arduino-cli/latest/) installed.
|
||||||
|
* Arduino UNO R4 core installed:
|
||||||
|
`arduino-cli core install arduino:renesas_uno`
|
||||||
|
|
||||||
|
### Commands
|
||||||
|
| Command | Description |
|
||||||
|
| :--- | :--- |
|
||||||
|
| `make prepare` | Downloads arduino-cli locally and installs the UNO R4 core. |
|
||||||
|
| `make compile` | Compiles the sketch without uploading. |
|
||||||
|
| `make upload` | Uploads the compiled binary to `/dev/ttyACM0`. |
|
||||||
|
| `make monitor` | Opens the Serial Monitor. |
|
||||||
|
| `make run` | Full cycle: Compile + Upload + Monitor. |
|
||||||
|
| `make run_init` | Full first-time setup: Prepare env + Compile + Upload + Monitor. |
|
||||||
|
|
||||||
|
> **Note:** If your board is on a different port, edit the `Makefile` or override it: `make upload PORT=/dev/ttyUSB0`.
|
||||||
|
|||||||
122
arduino_pong.ino
Normal file
122
arduino_pong.ino
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
#include "Arduino_LED_Matrix.h"
|
||||||
|
|
||||||
|
#include "src/config.h"
|
||||||
|
#include "src/pong_render.h"
|
||||||
|
#include "src/pong_player.h"
|
||||||
|
#include "src/pong_ball.h"
|
||||||
|
|
||||||
|
// create LED matrix object
|
||||||
|
ArduinoLEDMatrix matrix;
|
||||||
|
|
||||||
|
// 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, 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 }
|
||||||
|
};
|
||||||
|
|
||||||
|
// players coordinates
|
||||||
|
int players_coords[2]= {1, 4};
|
||||||
|
int players_scores[2]= {0, 0};
|
||||||
|
|
||||||
|
// initials balls coordinates
|
||||||
|
int ball_x= BALL_RESET_X;
|
||||||
|
int ball_y= BALL_RESET_Y;
|
||||||
|
|
||||||
|
int need_refresh= true;
|
||||||
|
|
||||||
|
int ball_delay= INITIAL_BALL_DELAY;
|
||||||
|
|
||||||
|
long exec_t2= millis();
|
||||||
|
|
||||||
|
enum game_statuses : uint8_t {
|
||||||
|
TIMER,
|
||||||
|
RUN,
|
||||||
|
SCORE,
|
||||||
|
GAMEOVER,
|
||||||
|
WAIT,
|
||||||
|
};
|
||||||
|
game_statuses game_status= TIMER;
|
||||||
|
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(9600);
|
||||||
|
// 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(millis());
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
long exec_t1= millis();
|
||||||
|
|
||||||
|
switch (game_status) {
|
||||||
|
case TIMER:
|
||||||
|
for (int i = START_TIMER; i >= 0; i--) {
|
||||||
|
render_timer(frame, i);
|
||||||
|
delay(1000);
|
||||||
|
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
||||||
|
}
|
||||||
|
game_status= RUN;
|
||||||
|
// delay the first ball movement
|
||||||
|
exec_t2= millis() + FIRST_START_BALL_DELAY;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case RUN:
|
||||||
|
pong_move_p1(players_coords[0], need_refresh);
|
||||||
|
pong_move_p2(players_coords[1], need_refresh);
|
||||||
|
if (exec_t1 - exec_t2 > ball_delay) {
|
||||||
|
need_refresh= true;
|
||||||
|
bool scored= move_ball(ball_x, ball_y, ball_delay, players_coords, players_scores);
|
||||||
|
if (scored) {
|
||||||
|
game_status= SCORE;
|
||||||
|
// delay the ball movement after score
|
||||||
|
exec_t2= millis() + FIRST_START_BALL_DELAY;
|
||||||
|
} else exec_t2= exec_t1;
|
||||||
|
}
|
||||||
|
// rerender matrix only if something is changed
|
||||||
|
if (need_refresh) {
|
||||||
|
render_matrix(frame, players_coords, ball_x, ball_y);
|
||||||
|
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
||||||
|
need_refresh= 0;
|
||||||
|
}
|
||||||
|
delay(50);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SCORE:
|
||||||
|
render_score(frame, players_scores);
|
||||||
|
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
||||||
|
delay(1000);
|
||||||
|
if (players_scores[0] >= MAX_POINTS || players_scores[1] >= MAX_POINTS) {
|
||||||
|
game_status= GAMEOVER;
|
||||||
|
}
|
||||||
|
else game_status= RUN;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GAMEOVER:
|
||||||
|
render_winner(frame, matrix, players_scores);
|
||||||
|
game_status= WAIT;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case WAIT:
|
||||||
|
// keep showing the winner waiting for a restart
|
||||||
|
// restart game once one button is pressed
|
||||||
|
if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW || digitalRead(P2_BTN_UP) == LOW || digitalRead(P2_BTN_BOTTOM) == LOW) {
|
||||||
|
game_status= TIMER;
|
||||||
|
players_scores[0]= 0;
|
||||||
|
players_scores[1]= 0;
|
||||||
|
}
|
||||||
|
delay(100);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
assets/preview.png
Normal file
BIN
assets/preview.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 847 KiB |
15
src/config.h
Normal file
15
src/config.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#define P1_BTN_UP 12
|
||||||
|
#define P1_BTN_BOTTOM 11
|
||||||
|
#define P2_BTN_UP 10
|
||||||
|
#define P2_BTN_BOTTOM 9
|
||||||
|
|
||||||
|
#define MATRIX_WIDTH 12
|
||||||
|
#define MATRIX_HEIGHT 8
|
||||||
|
#define BALL_RESET_X (MATRIX_WIDTH / 2)
|
||||||
|
#define BALL_RESET_Y (MATRIX_HEIGHT / 2)
|
||||||
|
#define BAR_LENGTH 3
|
||||||
|
#define INITIAL_BALL_DELAY 200
|
||||||
|
|
||||||
|
#define MAX_POINTS 9
|
||||||
|
#define START_TIMER 3
|
||||||
|
#define FIRST_START_BALL_DELAY 500
|
||||||
184
src/font.h
Normal file
184
src/font.h
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
#ifndef FONT_H
|
||||||
|
#define FONT_H
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
const uint32_t pone_wins[][4] = {
|
||||||
|
{
|
||||||
|
0x78,
|
||||||
|
0xc4847844,
|
||||||
|
0x440e000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0x11,
|
||||||
|
0x1101501,
|
||||||
|
0x501f0000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0xe,
|
||||||
|
0x400400,
|
||||||
|
0x400e0000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0x9,
|
||||||
|
0xd00b00,
|
||||||
|
0x90090000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0xf,
|
||||||
|
0x800f00,
|
||||||
|
0x100f0000,
|
||||||
|
500
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const uint32_t ptwo_wins[][4] = {
|
||||||
|
{
|
||||||
|
0x79,
|
||||||
|
0xe48279e4,
|
||||||
|
0x1041e000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0x11,
|
||||||
|
0x1101501,
|
||||||
|
0x501f0000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0xe,
|
||||||
|
0x400400,
|
||||||
|
0x400e0000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0x9,
|
||||||
|
0xd00b00,
|
||||||
|
0x90090000,
|
||||||
|
500
|
||||||
|
},
|
||||||
|
{
|
||||||
|
0xf,
|
||||||
|
0x800f00,
|
||||||
|
0x100f0000,
|
||||||
|
500
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const byte font_pong[10][8][3] = {
|
||||||
|
// Number 0
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 1
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 0 },
|
||||||
|
{ 0, 1, 0 },
|
||||||
|
{ 0, 1, 0 },
|
||||||
|
{ 0, 1, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 2
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 3
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 4
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 5
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 6
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 0, 0 },
|
||||||
|
{ 1, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 7
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 8
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
// Number 9
|
||||||
|
{
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 0, 0, 0 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 1, 0, 1 },
|
||||||
|
{ 1, 1, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 1 },
|
||||||
|
{ 0, 0, 0 }
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
97
src/pong_ball.cpp
Normal file
97
src/pong_ball.cpp
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include "config.h"
|
||||||
|
#include "pong_player.h"
|
||||||
|
|
||||||
|
// used to increase speed when game is too easy
|
||||||
|
int hits= 0;
|
||||||
|
|
||||||
|
// initially ball has no movements
|
||||||
|
// once game/round starts, balls gets random x and y movements
|
||||||
|
int ball_move_x= 0;
|
||||||
|
int ball_move_y= 0;
|
||||||
|
|
||||||
|
void random_ball_movement(int &ball_move_x, int &ball_move_y) {
|
||||||
|
if (random(2) == 0) ball_move_x= 1;
|
||||||
|
else ball_move_x= -1;
|
||||||
|
if (random(2) == 0) ball_move_y= 1;
|
||||||
|
else ball_move_y= -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void point_scored(int &ball_x, int &ball_y, int &ball_delay, int players_scores[2], int &ball_move_x, int &ball_move_y) {
|
||||||
|
ball_x= BALL_RESET_X;
|
||||||
|
ball_y= BALL_RESET_Y;
|
||||||
|
random_ball_movement(ball_move_x, ball_move_y);
|
||||||
|
|
||||||
|
Serial.print("P1: ");
|
||||||
|
Serial.print(players_scores[0]);
|
||||||
|
Serial.print(" - ");
|
||||||
|
Serial.print("P2: ");
|
||||||
|
Serial.print(players_scores[1]);
|
||||||
|
Serial.println();
|
||||||
|
|
||||||
|
hits= 0;
|
||||||
|
ball_delay= INITIAL_BALL_DELAY;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool move_ball(int &ball_x, int &ball_y, int &ball_delay, int players_coords[2], int players_scores[2]) {
|
||||||
|
if (ball_x < 0 || ball_x > MATRIX_WIDTH-1 || ball_y < 0 || ball_y > MATRIX_HEIGHT-1) {
|
||||||
|
// ball out of matrix limits
|
||||||
|
ball_x= BALL_RESET_X;
|
||||||
|
ball_y= BALL_RESET_Y;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool scored= false;
|
||||||
|
|
||||||
|
// if ball is not moving, get random direction
|
||||||
|
// this is the initial position
|
||||||
|
if (ball_move_x == 0 || ball_move_y == 0) {
|
||||||
|
// extract random number between 0 or 1 to select the directions
|
||||||
|
random_ball_movement(ball_move_x, ball_move_y);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (ball_x == 0) {
|
||||||
|
// if p1 collision: reverse x, go left
|
||||||
|
if (!ball_player_collision(players_coords[0], ball_y)) {
|
||||||
|
// else p2 score, reset board
|
||||||
|
players_scores[1] += 1;
|
||||||
|
scored= true;
|
||||||
|
Serial.println("Player 2 Scores");
|
||||||
|
point_scored(ball_x, ball_y, ball_delay, players_scores, ball_move_x, ball_move_y);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
hits += 1;
|
||||||
|
ball_move_x= ball_move_x * -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (ball_x == MATRIX_WIDTH-1) {
|
||||||
|
if (!ball_player_collision(players_coords[1], ball_y)) {
|
||||||
|
// else p1 score, reset board
|
||||||
|
players_scores[0] += 1;
|
||||||
|
scored= true;
|
||||||
|
Serial.println("Player 1 Scores");
|
||||||
|
point_scored(ball_x, ball_y, ball_delay, players_scores, ball_move_x, ball_move_y);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
hits += 1;
|
||||||
|
ball_move_x= ball_move_x * -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ball_y == 0 || ball_y == MATRIX_HEIGHT-1) {
|
||||||
|
// reverse y, go down
|
||||||
|
ball_move_y= ball_move_y * -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hits >= 6 && ball_delay >= 80) {
|
||||||
|
// increase ball speed
|
||||||
|
hits= 0;
|
||||||
|
ball_delay-= 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
ball_x+= ball_move_x;
|
||||||
|
ball_y+= ball_move_y;
|
||||||
|
return scored;
|
||||||
|
}
|
||||||
6
src/pong_ball.h
Normal file
6
src/pong_ball.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef PONG_BALL_H
|
||||||
|
#define PONG_BALL_H
|
||||||
|
|
||||||
|
bool move_ball(int &ball_x, int &ball_y, int &loop_delay, int players_coords[2], int players_scores[2]);
|
||||||
|
void foo();
|
||||||
|
#endif
|
||||||
33
src/pong_player.cpp
Normal file
33
src/pong_player.cpp
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
int ball_player_collision(int player, int ball_y) {
|
||||||
|
for (int p= player; p < player + BAR_LENGTH; p++) {
|
||||||
|
if (ball_y == p) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pong_move_p1(int &p1_start, int &need_refresh) {
|
||||||
|
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
|
||||||
|
p1_start -= 1;
|
||||||
|
need_refresh= true;
|
||||||
|
}
|
||||||
|
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
|
||||||
|
p1_start += 1;
|
||||||
|
need_refresh= true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int pong_move_p2(int &p2_start, int &need_refresh) {
|
||||||
|
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
|
||||||
|
p2_start -= 1;
|
||||||
|
need_refresh= true;
|
||||||
|
}
|
||||||
|
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
|
||||||
|
p2_start += 1;
|
||||||
|
need_refresh= true;
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/pong_player.h
Normal file
8
src/pong_player.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef PONG_PLAYER_H
|
||||||
|
#define PONG_PLAYER_H
|
||||||
|
|
||||||
|
int ball_player_collision(int player, int ball_y);
|
||||||
|
int pong_move_p1(int &p1_start, int &need_refresh);
|
||||||
|
int pong_move_p2(int &p2_start, int &need_refresh);
|
||||||
|
|
||||||
|
#endif
|
||||||
75
src/pong_render.cpp
Normal file
75
src/pong_render.cpp
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include "Arduino_LED_Matrix.h"
|
||||||
|
#include "config.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], int players_coords[2], int ball_x, int ball_y) {
|
||||||
|
clear_matrix(frame);
|
||||||
|
int player_one= players_coords[0];
|
||||||
|
int player_two= players_coords[1];
|
||||||
|
// players coords
|
||||||
|
for (int i= player_one; i < player_one+BAR_LENGTH; i++) {
|
||||||
|
frame[i][0]= 1;
|
||||||
|
}
|
||||||
|
for (int i= player_two; i < player_two+BAR_LENGTH; i++) {
|
||||||
|
frame[i][MATRIX_WIDTH-1]= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ball coords
|
||||||
|
frame[ball_y][ball_x]= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int players_scores[2]) {
|
||||||
|
clear_matrix(frame);
|
||||||
|
int player_one= players_scores[0];
|
||||||
|
int player_two= players_scores[1];
|
||||||
|
if (player_one > 9) player_one = 9;
|
||||||
|
if (player_two > 9) player_two = 9;
|
||||||
|
|
||||||
|
// 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[player_one][h][w];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int h=0; h < 8; h++) {
|
||||||
|
for (int w=0; w < 3; w++) {
|
||||||
|
frame[h][w+8]= font_pong[player_two][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, int players_scores[2]) {
|
||||||
|
clear_matrix(frame);
|
||||||
|
// check winner
|
||||||
|
if (players_scores[0] > players_scores[1]) {
|
||||||
|
Serial.println("Player 1 wins!!!");
|
||||||
|
matrix.loadSequence(pone_wins);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("Player 2 wins!!!");
|
||||||
|
matrix.loadSequence(ptwo_wins);
|
||||||
|
}
|
||||||
|
matrix.play(true);
|
||||||
|
}
|
||||||
13
src/pong_render.h
Normal file
13
src/pong_render.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef PONG_RENDER_H
|
||||||
|
#define PONG_RENDER_H
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include "Arduino_LED_Matrix.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int players_coords[2], int ball_x, int ball_y);
|
||||||
|
void render_score(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int players_scores[2]);
|
||||||
|
void render_timer(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int seconds);
|
||||||
|
void render_winner(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix, int players_scores[2]);
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user