move all libs into src
This commit is contained in:
11
src/config.h
Normal file
11
src/config.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#define P1_BTN_UP 13
|
||||
#define P1_BTN_BOTTOM 12
|
||||
#define P2_BTN_UP 11
|
||||
#define P2_BTN_BOTTOM 10
|
||||
|
||||
#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
|
||||
88
src/pong_ball.cpp
Normal file
88
src/pong_ball.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
#include <Arduino.h>
|
||||
#include "config.h"
|
||||
#include "pong_player.h"
|
||||
|
||||
// used to increase speed when game is too easy
|
||||
int hits= 0;
|
||||
|
||||
int p1_score= 0;
|
||||
int p2_score= 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 point_scored(int &ball_x, int &ball_y, int &ball_delay) {
|
||||
ball_x= BALL_RESET_X;
|
||||
ball_y= BALL_RESET_Y;
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
ball_x= BALL_RESET_X;
|
||||
ball_y= BALL_RESET_Y;
|
||||
return;
|
||||
}
|
||||
|
||||
// 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
|
||||
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;
|
||||
}
|
||||
|
||||
else if (ball_x == 0) {
|
||||
// if p1 collision: reverse x, go left
|
||||
if (!ball_player_collision(p1_start, ball_y)) {
|
||||
// else p2 score, reset board
|
||||
p2_score += 1;
|
||||
Serial.println("Player 2 Scores");
|
||||
point_scored(ball_x, ball_y, ball_delay);
|
||||
}
|
||||
else {
|
||||
hits += 1;
|
||||
ball_move_x= ball_move_x * -1;
|
||||
}
|
||||
}
|
||||
else if (ball_x == MATRIX_WIDTH-1) {
|
||||
if (!ball_player_collision(p2_start, ball_y)) {
|
||||
// else p1 score, reset board
|
||||
p1_score += 1;
|
||||
Serial.println("Player 1 Scores");
|
||||
point_scored(ball_x, ball_y, ball_delay);
|
||||
}
|
||||
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;
|
||||
}
|
||||
7
src/pong_ball.h
Normal file
7
src/pong_ball.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef PONG_BALL_H
|
||||
#define PONG_BALL_H
|
||||
|
||||
void point_scored();
|
||||
void move_ball(int &ball_x, int &ball_y, int &loop_delay, int p1_start, int p2_start, int &need_refresh);
|
||||
|
||||
#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= 1;
|
||||
}
|
||||
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
|
||||
p1_start += 1;
|
||||
need_refresh= 1;
|
||||
}
|
||||
}
|
||||
|
||||
int pong_move_p2(int &p2_start, int &need_refresh) {
|
||||
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
|
||||
p2_start -= 1;
|
||||
need_refresh= 1;
|
||||
}
|
||||
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
|
||||
p2_start += 1;
|
||||
need_refresh= 1;
|
||||
}
|
||||
}
|
||||
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
|
||||
22
src/pong_render.cpp
Normal file
22
src/pong_render.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <Arduino.h>
|
||||
#include "config.h"
|
||||
|
||||
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++) {
|
||||
frame[y][x]= 0;
|
||||
}
|
||||
}
|
||||
|
||||
// players coords
|
||||
for (int i= p1_start; i < p1_start+BAR_LENGTH; i++) {
|
||||
frame[i][0]= 1;
|
||||
}
|
||||
for (int i= p2_start; i < p2_start+BAR_LENGTH; i++) {
|
||||
frame[i][MATRIX_WIDTH-1]= 1;
|
||||
}
|
||||
|
||||
// ball coords
|
||||
frame[ball_y][ball_x]= 1;
|
||||
}
|
||||
9
src/pong_render.h
Normal file
9
src/pong_render.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef PONG_RENDER_H
|
||||
#define PONG_RENDER_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "Arduino_LED_Matrix.h"
|
||||
|
||||
void render_matrix(byte frame[MATRIX_HEIGHT][MATRIX_WIDTH], int p1_start, int p2_start, int ball_x, int ball_y);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user