refactoring

This commit is contained in:
Patrick_Pluto 2024-09-09 07:59:43 +02:00
parent 5a50497cf7
commit 2164102257

View file

@ -1,19 +1,19 @@
/* /*
FlappyBird FlappyBird
Copyright (C) 2024 Patrick_Pluto Copyright (C) 2024 Patrick_Pluto
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
@ -59,7 +59,7 @@ bool sdlPipe(SDL_Rect *pipeRect, SDL_Rect *birdRect, SDL_Renderer *renderer,
return false; return false;
} }
void setPipe(SDL_Rect* pipeRect, int offsetX) { void setPipe(SDL_Rect *pipeRect, int offsetX) {
// Create a random number generator // Create a random number generator
std::random_device rd; // Obtain a random number from hardware 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.w = 48; // Width of the sprite
birdRect.h = 36; // Height of the sprite birdRect.h = 36; // Height of the sprite
// Create a random number generator SDL_Rect *pipeRect[4];
std::random_device rd; // Obtain a random number from hardware int dist = 650;
std::mt19937 eng(rd()); // Seed the generator for (int i = 0; i < 4; i++) {
std::uniform_int_distribution<> distr(-950, -650); // Define the range pipeRect[i] = new SDL_Rect();
setPipe(pipeRect[i], dist);
SDL_Rect pipe1Rect; dist += 210;
setPipe(&pipe1Rect, 650); }
SDL_Rect pipe2Rect;
setPipe(&pipe2Rect, 850);
SDL_Rect pipe3Rect;
setPipe(&pipe3Rect, 1050);
SDL_Rect pipe4Rect;
setPipe(&pipe4Rect, 1250);
// Sprite Velocity // Sprite Velocity
double velocityY = 0; double velocityY = 0;
@ -197,18 +188,11 @@ int main(int argc, char *argv[]) {
// Render the sprite // Render the sprite
SDL_RenderCopy(renderer, flappyBird, nullptr, &birdRect); SDL_RenderCopy(renderer, flappyBird, nullptr, &birdRect);
quit = sdlPipe(&pipe1Rect, &birdRect, renderer, flappyBird, pipe); for (int i = 0; i < 4; i++) {
quit = sdlPipe(pipeRect[i], &birdRect, renderer, flappyBird, pipe);
if (!quit) { if (quit) {
quit = sdlPipe(&pipe2Rect, &birdRect, renderer, flappyBird, pipe); break;
} }
if (!quit) {
quit = sdlPipe(&pipe3Rect, &birdRect, renderer, flappyBird, pipe);
}
if (!quit) {
quit = sdlPipe(&pipe4Rect, &birdRect, renderer, flappyBird, pipe);
} }
// Present the renderer // Present the renderer