From d27214ae5e998241ba451ba86e2e6afea1fcab0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Fri, 27 Dec 2024 14:02:24 +0100 Subject: [PATCH] Pong: move --- src/mode/Pong/Player.h | 3 ++- src/mode/Pong/Pong.h | 28 ++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/mode/Pong/Player.h b/src/mode/Pong/Player.h index 768a6f6..6687d2c 100644 --- a/src/mode/Pong/Player.h +++ b/src/mode/Pong/Player.h @@ -15,6 +15,8 @@ public: bool moveUp = false; + bool random = true; + void randomMove(uint8_t height) { if (moveUp) { y--; @@ -28,7 +30,6 @@ public: } } } - }; #endif diff --git a/src/mode/Pong/Pong.h b/src/mode/Pong/Pong.h index 0a7943b..3b30ff9 100644 --- a/src/mode/Pong/Pong.h +++ b/src/mode/Pong/Pong.h @@ -40,6 +40,24 @@ public: return "Pong"; } + void move(int index, int x, int y) override { + if (index == 0) { + player0.random = false; + player0.y = min(height - player0.size, max(0, player0.y + y)); + } else if (index == 1) { + player1.random = false; + player1.y = min(height - player1.size, max(0, player1.y + y)); + } + } + + void fire(int index) override { + if (index == 0) { + player0.random = false; + } else if (index == 1) { + player1.random = false; + } + } + protected: void tick(uint8_t index, microseconds_t microseconds) override { @@ -52,8 +70,12 @@ protected: break; case PLAY: ball = ball.plus(velocity); - player0.randomMove(height); - player1.randomMove(height); + if (player0.random) { + player0.randomMove(height); + } + if (player1.random) { + player1.randomMove(height); + } topBottomBounce(); paddleBounce(); checkScoring(); @@ -105,9 +127,11 @@ private: player0.size = 3; player0.score = 0; player0.y = (height - player0.size) / 2; + player0.random = true; player1.size = 3; player1.score = 0; player1.y = (height - player1.size) / 2; + player1.random = true; } void topBottomBounce() {