Skip to content

Commit 054a99c

Browse files
NeroBurnerJF002
authored andcommitted
Paddle: add a little randomization in the dy speed
To make the game a bit more challenging an less predictable add a little bit of randomness to the `dy` value. When hitting the right wall add a random number (one of [-1, 0, 1]) to the `dy` value. To keep the difficulty level managable limit the dy value to be in the range from -5 to 5.
1 parent ada96cc commit 054a99c

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

src/displayapp/screens/Paddle.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "displayapp/DisplayApp.h"
33
#include "displayapp/LittleVgl.h"
44

5+
#include <cstdlib> // for rand()
6+
57
using namespace Pinetime::Applications::Screens;
68

79
Paddle::Paddle(Pinetime::Applications::DisplayApp* app, Pinetime::Components::LittleVgl& lvgl) : Screen(app), lvgl {lvgl} {
@@ -50,6 +52,13 @@ void Paddle::Refresh() {
5052
// checks if it has touched the side (right side)
5153
if (ballX >= LV_HOR_RES - ballSize - 1) {
5254
dx *= -1;
55+
dy += rand() % 3 - 1; // add a little randomization in wall bounce direction, one of [-1, 0, 1]
56+
if (dy > 5) { // limit dy to be in range [-5 to 5]
57+
dy = 5;
58+
}
59+
if (dy < -5) {
60+
dy = -5;
61+
}
5362
}
5463

5564
// checks if it is in the position of the paddle

0 commit comments

Comments
 (0)