From 486ee4df97400de42df31183b86f3732bdf24cc9 Mon Sep 17 00:00:00 2001 From: andrea Date: Sun, 15 Mar 2026 10:05:03 +0100 Subject: [PATCH] gradually increase ball speed if noone scores when someone scores a point, speed is resetted to the initial speed --- arduino_pong.ino | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/arduino_pong.ino b/arduino_pong.ino index 5afa689..8cfe6e9 100644 --- a/arduino_pong.ino +++ b/arduino_pong.ino @@ -44,7 +44,11 @@ int p2_score= 0; int need_refresh= 1; -int loop_delay= 220; +int initial_loop_delay= 200; +int loop_delay= initial_loop_delay; +// used to increase speed when game is too easy +int hits= 0; + long exec_t2= millis(); void setup() { @@ -116,13 +120,18 @@ int ball_player_collision(int player) { return 0; } -void print_points() { +void point_scored() { + 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; + loop_delay= initial_loop_delay; } void move_ball() { @@ -148,26 +157,24 @@ void move_ball() { // if p1 collision: reverse x, go left if (!ball_player_collision(p1_start)) { // else p2 score, reset board - ball_x= ball_reset_x; - ball_y= ball_reset_y; p2_score += 1; Serial.println("Player 2 Point"); - print_points(); + point_scored(); } else { + hits += 1; ball_move_x= ball_move_x * -1; } } else if (ball_x == 11) { if (!ball_player_collision(p2_start)) { // else p1 score, reset board - ball_x= ball_reset_x; - ball_y= ball_reset_y; p1_score += 1; Serial.println("Player 1 Point"); - print_points(); + point_scored(); } else { + hits += 1; ball_move_x= ball_move_x * -1; } } @@ -177,6 +184,12 @@ void move_ball() { ball_move_y= ball_move_y * -1; } + if (hits >= 6 && loop_delay >= 80) { + // increase ball speed + hits = 0; + loop_delay -= 20; + } + ball_x+= ball_move_x; ball_y+= ball_move_y; }