From 5dbfdbb12987da38ee574be1fa58c55409e199dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Mon, 27 Dec 2021 13:41:03 +0100 Subject: [PATCH] SpaceInvaders: some randomness for hero --- src/BASICS.h | 4 ++++ src/main.cpp | 4 ++-- src/mode/SpaceInvaders/SpaceInvaders.h | 6 +++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/BASICS.h b/src/BASICS.h index e71cef4..de1af91 100644 --- a/src/BASICS.h +++ b/src/BASICS.h @@ -26,4 +26,8 @@ double step(double valueCurrent, double valueMin, double valueMax, microseconds_ return valueNew; } +bool randomBool(int uncertainty) { + return random(uncertainty) == 0; +} + #endif diff --git a/src/main.cpp b/src/main.cpp index 253bc9a..3a3b66a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,7 +22,7 @@ enum ModeId { Display display(32, 8); -ModeId newModeId = GAME_OF_LIFE_GRAYSCALE; +ModeId newModeId = SPACE_INVADERS; ModeId currentModeId = NONE; @@ -30,7 +30,7 @@ microseconds_t lastMicros = 0; Mode *mode = nullptr; -double speed = 1.0; +double speed = 4.0; bool connected = false; diff --git a/src/mode/SpaceInvaders/SpaceInvaders.h b/src/mode/SpaceInvaders/SpaceInvaders.h index e19306a..479f316 100644 --- a/src/mode/SpaceInvaders/SpaceInvaders.h +++ b/src/mode/SpaceInvaders/SpaceInvaders.h @@ -141,18 +141,18 @@ public: heroRuntime -= 50000; if (heroLeft) { heroX--; - if (heroX <= 1) { + if (heroX <= 1 || randomBool(20)) { heroLeft = false; } } else { heroX++; - if (heroX >= display->width - 2) { + if (heroX >= display->width - 2 || randomBool(20)) { heroLeft = true; } } heroShoot++; - if (heroShoot >= 10) { + if (heroShoot >= 20 || randomBool(5)) { heroShoot = 0; shoot(); }