Improve arduino_pong.ino using enum to handle game statuses and switch case for better readibility and little memory improvements
Some checks failed
Some checks failed
This commit is contained in:
@@ -28,15 +28,21 @@ int players_scores[2]= {0, 0};
|
|||||||
int ball_x= BALL_RESET_X;
|
int ball_x= BALL_RESET_X;
|
||||||
int ball_y= BALL_RESET_Y;
|
int ball_y= BALL_RESET_Y;
|
||||||
|
|
||||||
int need_refresh= 1;
|
int need_refresh= true;
|
||||||
|
|
||||||
int ball_delay= INITIAL_BALL_DELAY;
|
int ball_delay= INITIAL_BALL_DELAY;
|
||||||
|
|
||||||
bool game_over= false;
|
|
||||||
bool go= true;
|
|
||||||
|
|
||||||
long exec_t2= millis();
|
long exec_t2= millis();
|
||||||
|
|
||||||
|
enum game_statuses : uint8_t {
|
||||||
|
TIMER,
|
||||||
|
RUN,
|
||||||
|
SCORE,
|
||||||
|
GAMEOVER,
|
||||||
|
WAIT,
|
||||||
|
};
|
||||||
|
game_statuses game_status= TIMER;
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@@ -52,56 +58,65 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
long exec_t1= millis();
|
||||||
|
|
||||||
|
switch (game_status) {
|
||||||
|
case TIMER:
|
||||||
for (int i = START_TIMER; i >= 0; i--) {
|
for (int i = START_TIMER; i >= 0; i--) {
|
||||||
render_timer(frame, i);
|
render_timer(frame, i);
|
||||||
delay(1000);
|
delay(1000);
|
||||||
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
||||||
}
|
}
|
||||||
|
game_status= RUN;
|
||||||
game_over= false;
|
|
||||||
go= true;
|
|
||||||
|
|
||||||
// delay the first ball movement
|
// delay the first ball movement
|
||||||
exec_t2= millis() + FIRST_START_BALL_DELAY;
|
exec_t2= millis() + FIRST_START_BALL_DELAY;
|
||||||
|
break;
|
||||||
|
|
||||||
while (go) {
|
case RUN:
|
||||||
long exec_t1= millis();
|
|
||||||
pong_move_p1(players_coords[0], need_refresh);
|
pong_move_p1(players_coords[0], need_refresh);
|
||||||
pong_move_p2(players_coords[1], need_refresh);
|
pong_move_p2(players_coords[1], need_refresh);
|
||||||
if (exec_t1 - exec_t2 > ball_delay) {
|
if (exec_t1 - exec_t2 > ball_delay) {
|
||||||
bool scored= move_ball(ball_x, ball_y, ball_delay, players_coords, players_scores, need_refresh);
|
need_refresh= true;
|
||||||
|
bool scored= move_ball(ball_x, ball_y, ball_delay, players_coords, players_scores);
|
||||||
if (scored) {
|
if (scored) {
|
||||||
render_score(frame, players_scores);
|
game_status= SCORE;
|
||||||
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
|
||||||
delay(1000);
|
|
||||||
if (players_scores[0] >= MAX_POINTS || players_scores[1] >= MAX_POINTS) {
|
|
||||||
render_winner(frame, matrix, players_scores);
|
|
||||||
need_refresh= 0;
|
|
||||||
game_over= true;
|
|
||||||
}
|
|
||||||
// delay the ball movement after score
|
// delay the ball movement after score
|
||||||
exec_t2= millis() + FIRST_START_BALL_DELAY;
|
exec_t2= millis() + FIRST_START_BALL_DELAY;
|
||||||
} else exec_t2= exec_t1;
|
} else exec_t2= exec_t1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// rerender matrix only if something is changed
|
// rerender matrix only if something is changed
|
||||||
if (need_refresh) {
|
if (need_refresh) {
|
||||||
render_matrix(frame, players_coords, ball_x, ball_y);
|
render_matrix(frame, players_coords, ball_x, ball_y);
|
||||||
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
matrix.renderBitmap(frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
||||||
need_refresh= 0;
|
need_refresh= 0;
|
||||||
}
|
}
|
||||||
if (!game_over) delay(50);
|
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
|
// keep showing the winner waiting for a restart
|
||||||
while (game_over) {
|
|
||||||
// restart game once one button is pressed
|
// 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) {
|
if (digitalRead(P1_BTN_UP) == LOW || digitalRead(P1_BTN_BOTTOM) == LOW || digitalRead(P2_BTN_UP) == LOW || digitalRead(P2_BTN_BOTTOM) == LOW) {
|
||||||
go= false;
|
game_status= TIMER;
|
||||||
game_over= false;
|
|
||||||
players_scores[0]= 0;
|
players_scores[0]= 0;
|
||||||
players_scores[1]= 0;
|
players_scores[1]= 0;
|
||||||
}
|
}
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ void point_scored(int &ball_x, int &ball_y, int &ball_delay, int players_scores[
|
|||||||
ball_delay= INITIAL_BALL_DELAY;
|
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], int &need_refresh) {
|
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) {
|
if (ball_x < 0 || ball_x > MATRIX_WIDTH-1 || ball_y < 0 || ball_y > MATRIX_HEIGHT-1) {
|
||||||
// ball out of matrix limits
|
// ball out of matrix limits
|
||||||
ball_x= BALL_RESET_X;
|
ball_x= BALL_RESET_X;
|
||||||
@@ -41,7 +41,6 @@ bool move_ball(int &ball_x, int &ball_y, int &ball_delay, int players_coords[2],
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
need_refresh= 1;
|
|
||||||
bool scored= false;
|
bool scored= false;
|
||||||
|
|
||||||
// if ball is not moving, get random direction
|
// if ball is not moving, get random direction
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#ifndef PONG_BALL_H
|
#ifndef PONG_BALL_H
|
||||||
#define 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], int &need_refresh);
|
bool move_ball(int &ball_x, int &ball_y, int &loop_delay, int players_coords[2], int players_scores[2]);
|
||||||
|
void foo();
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -13,21 +13,21 @@ int ball_player_collision(int player, int ball_y) {
|
|||||||
int pong_move_p1(int &p1_start, int &need_refresh) {
|
int pong_move_p1(int &p1_start, int &need_refresh) {
|
||||||
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
|
if (digitalRead(P1_BTN_UP) == LOW && p1_start > 0) {
|
||||||
p1_start -= 1;
|
p1_start -= 1;
|
||||||
need_refresh= 1;
|
need_refresh= true;
|
||||||
}
|
}
|
||||||
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
|
else if (digitalRead(P1_BTN_BOTTOM) == LOW && p1_start < 5) {
|
||||||
p1_start += 1;
|
p1_start += 1;
|
||||||
need_refresh= 1;
|
need_refresh= true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int pong_move_p2(int &p2_start, int &need_refresh) {
|
int pong_move_p2(int &p2_start, int &need_refresh) {
|
||||||
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
|
if (digitalRead(P2_BTN_UP) == LOW && p2_start > 0) {
|
||||||
p2_start -= 1;
|
p2_start -= 1;
|
||||||
need_refresh= 1;
|
need_refresh= true;
|
||||||
}
|
}
|
||||||
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
|
else if (digitalRead(P2_BTN_BOTTOM) == LOW && p2_start < 5) {
|
||||||
p2_start += 1;
|
p2_start += 1;
|
||||||
need_refresh= 1;
|
need_refresh= true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user