From 2164102257a8935e7a565fd7af635dab30f9f3de Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Mon, 9 Sep 2024 07:59:43 +0200 Subject: [PATCH] refactoring --- main.cpp | 68 ++++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 42 deletions(-) diff --git a/main.cpp b/main.cpp index d3c228a..70943c6 100644 --- a/main.cpp +++ b/main.cpp @@ -1,19 +1,19 @@ - /* - FlappyBird - Copyright (C) 2024 Patrick_Pluto +/* + FlappyBird + Copyright (C) 2024 Patrick_Pluto - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ #include @@ -59,7 +59,7 @@ bool sdlPipe(SDL_Rect *pipeRect, SDL_Rect *birdRect, SDL_Renderer *renderer, return false; } -void setPipe(SDL_Rect* pipeRect, int offsetX) { +void setPipe(SDL_Rect *pipeRect, int offsetX) { // Create a random number generator std::random_device rd; // Obtain a random number from hardware @@ -124,22 +124,13 @@ int main(int argc, char *argv[]) { birdRect.w = 48; // Width of the sprite birdRect.h = 36; // Height of the sprite - // Create a random number generator - std::random_device rd; // Obtain a random number from hardware - std::mt19937 eng(rd()); // Seed the generator - std::uniform_int_distribution<> distr(-950, -650); // Define the range - - SDL_Rect pipe1Rect; - setPipe(&pipe1Rect, 650); - - SDL_Rect pipe2Rect; - setPipe(&pipe2Rect, 850); - - SDL_Rect pipe3Rect; - setPipe(&pipe3Rect, 1050); - - SDL_Rect pipe4Rect; - setPipe(&pipe4Rect, 1250); + SDL_Rect *pipeRect[4]; + int dist = 650; + for (int i = 0; i < 4; i++) { + pipeRect[i] = new SDL_Rect(); + setPipe(pipeRect[i], dist); + dist += 210; + } // Sprite Velocity double velocityY = 0; @@ -197,18 +188,11 @@ int main(int argc, char *argv[]) { // Render the sprite SDL_RenderCopy(renderer, flappyBird, nullptr, &birdRect); - quit = sdlPipe(&pipe1Rect, &birdRect, renderer, flappyBird, pipe); - - if (!quit) { - quit = sdlPipe(&pipe2Rect, &birdRect, renderer, flappyBird, pipe); - } - - if (!quit) { - quit = sdlPipe(&pipe3Rect, &birdRect, renderer, flappyBird, pipe); - } - - if (!quit) { - quit = sdlPipe(&pipe4Rect, &birdRect, renderer, flappyBird, pipe); + for (int i = 0; i < 4; i++) { + quit = sdlPipe(pipeRect[i], &birdRect, renderer, flappyBird, pipe); + if (quit) { + break; + } } // Present the renderer