Pong: move

This commit is contained in:
Patrick Haßel 2024-12-27 14:02:24 +01:00
parent 9ea1eddd57
commit d27214ae5e
2 changed files with 28 additions and 3 deletions

View File

@ -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

View File

@ -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);
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() {