made a method for setting pipeRect

This commit is contained in:
Patrick_Pluto 2024-09-09 07:44:01 +02:00
parent e518e31fc7
commit 5a50497cf7

180
main.cpp
View file

@ -1,4 +1,4 @@
/* /*
FlappyBird FlappyBird
Copyright (C) 2024 Patrick_Pluto Copyright (C) 2024 Patrick_Pluto
@ -21,39 +21,37 @@
#include <iostream> #include <iostream>
#include <random> #include <random>
bool sdlPipe ( SDL_Rect* pipeRect, SDL_Rect* birdRect, SDL_Renderer* renderer, SDL_Texture* flappyBird, SDL_Texture* pipe ) bool sdlPipe(SDL_Rect *pipeRect, SDL_Rect *birdRect, SDL_Renderer *renderer,
{ SDL_Texture *flappyBird, SDL_Texture *pipe) {
// Render the pipes // Render the pipes
SDL_RenderCopy ( renderer, pipe, nullptr, pipeRect ); SDL_RenderCopy(renderer, pipe, nullptr, pipeRect);
// Draw pipes and check for collisions. // Draw pipes and check for collisions.
if ( SDL_HasIntersection ( pipeRect, birdRect ) ) if (SDL_HasIntersection(pipeRect, birdRect)) {
{
return true; return true;
} }
pipeRect->y += 1150 ; // move sprite for rendering the bottom pipe pipeRect->y += 1150; // move sprite for rendering the bottom pipe
SDL_RenderCopyEx ( renderer, pipe, nullptr, pipeRect, 0.0, nullptr, SDL_FLIP_VERTICAL ); SDL_RenderCopyEx(renderer, pipe, nullptr, pipeRect, 0.0, nullptr,
SDL_FLIP_VERTICAL);
pipeRect->x -= 2; pipeRect->x -= 2;
if ( SDL_HasIntersection ( pipeRect, birdRect ) ) if (SDL_HasIntersection(pipeRect, birdRect)) {
{
return true; return true;
} }
pipeRect->y -= 1150 ; // moving it back pipeRect->y -= 1150; // moving it back
if ( pipeRect->x <= -50 ) if (pipeRect->x <= -50) {
{
std::random_device rd; // Obtain a random number from hardware std::random_device rd; // Obtain a random number from hardware
std::mt19937 eng ( rd() ); // Seed the generator std::mt19937 eng(rd()); // Seed the generator
std::uniform_int_distribution<> distr ( -950, -650 ); // Define the range std::uniform_int_distribution<> distr(-950, -650); // Define the range
// Generate the random number // Generate the random number
pipeRect->y = distr ( eng ); pipeRect->y = distr(eng);
pipeRect->x = 800; pipeRect->x = 800;
} }
@ -61,50 +59,61 @@ bool sdlPipe ( SDL_Rect* pipeRect, SDL_Rect* birdRect, SDL_Renderer* renderer, S
return false; return false;
} }
int main ( int argc, char* argv[] ) void setPipe(SDL_Rect* pipeRect, int offsetX) {
{
// 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
pipeRect->x = offsetX;
pipeRect->y = distr(eng);
pipeRect->w = 64;
pipeRect->h = 1024;
}
int main(int argc, char *argv[]) {
// Initialize SDL // Initialize SDL
if ( SDL_Init ( SDL_INIT_VIDEO ) < 0 ) if (SDL_Init(SDL_INIT_VIDEO) < 0) {
{ std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl; << std::endl;
return 1; return 1;
} }
// Create a window // Create a window
SDL_Window* window = SDL_CreateWindow ( "Flappy Bird", SDL_Window *window =
SDL_WINDOWPOS_UNDEFINED, SDL_CreateWindow("Flappy Bird", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
800, 600, if (window == nullptr) {
SDL_WINDOW_SHOWN ); std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError()
if ( window == nullptr ) << std::endl;
{
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
SDL_Quit(); SDL_Quit();
return 1; return 1;
} }
// Create a renderer // Create a renderer
SDL_Renderer* renderer = SDL_CreateRenderer ( window, -1, SDL_RENDERER_ACCELERATED ); SDL_Renderer *renderer =
if ( renderer == nullptr ) SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
{ if (renderer == nullptr) {
std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl; std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError()
SDL_DestroyWindow ( window ); << std::endl;
SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
return 1; return 1;
} }
// Load the sprite texture // Load the sprite texture
SDL_Texture* flappyBird = IMG_LoadTexture ( renderer, "assets/bird.png" ); SDL_Texture *flappyBird = IMG_LoadTexture(renderer, "assets/bird.png");
if ( flappyBird == nullptr ) if (flappyBird == nullptr) {
{ std::cerr << "Failed to load texture! SDL_Error: " << SDL_GetError()
std::cerr << "Failed to load texture! SDL_Error: " << SDL_GetError() << std::endl; << std::endl;
// Handle error // Handle error
} }
SDL_Texture* pipe = IMG_LoadTexture ( renderer, "assets/pipe.png" ); SDL_Texture *pipe = IMG_LoadTexture(renderer, "assets/pipe.png");
if ( pipe == nullptr ) if (pipe == nullptr) {
{ std::cerr << "Failed to load texture! SDL_Error: " << SDL_GetError()
std::cerr << "Failed to load texture! SDL_Error: " << SDL_GetError() << std::endl; << std::endl;
// Handle error // Handle error
} }
@ -117,32 +126,20 @@ int main ( int argc, char* argv[] )
// 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
std::mt19937 eng ( rd() ); // Seed the generator std::mt19937 eng(rd()); // Seed the generator
std::uniform_int_distribution<> distr ( -950, -650 ); // Define the range std::uniform_int_distribution<> distr(-950, -650); // Define the range
SDL_Rect pipe1Rect; SDL_Rect pipe1Rect;
pipe1Rect.x = 650; setPipe(&pipe1Rect, 650);
pipe1Rect.y = distr ( eng ) ;
pipe1Rect.w = 64;
pipe1Rect.h = 1024;
SDL_Rect pipe2Rect; SDL_Rect pipe2Rect;
pipe2Rect.x = 850; setPipe(&pipe2Rect, 850);
pipe2Rect.y = distr ( eng ) ;
pipe2Rect.w = 64;
pipe2Rect.h = 1024;
SDL_Rect pipe3Rect; SDL_Rect pipe3Rect;
pipe3Rect.x = 1050; setPipe(&pipe3Rect, 1050);
pipe3Rect.y = distr ( eng ) ;
pipe3Rect.w = 64;
pipe3Rect.h = 1024;
SDL_Rect pipe4Rect; SDL_Rect pipe4Rect;
pipe4Rect.x = 1250; setPipe(&pipe4Rect, 1250);
pipe4Rect.y = distr ( eng ) ;
pipe4Rect.w = 64;
pipe4Rect.h = 1024;
// Sprite Velocity // Sprite Velocity
double velocityY = 0; double velocityY = 0;
@ -157,23 +154,18 @@ int main ( int argc, char* argv[] )
const int FRAME_DELAY = 1000 / TARGET_FPS; // Delay in milliseconds const int FRAME_DELAY = 1000 / TARGET_FPS; // Delay in milliseconds
// Main loop // Main loop
while ( !quit ) while (!quit) {
{
frameStart = SDL_GetTicks(); frameStart = SDL_GetTicks();
// Handle events // Handle events
while ( SDL_PollEvent ( &e ) != 0 ) while (SDL_PollEvent(&e) != 0) {
{
// User requests quit // User requests quit
if ( e.type == SDL_QUIT ) if (e.type == SDL_QUIT) {
{
quit = true; quit = true;
} }
if ( e.type == SDL_KEYDOWN ) if (e.type == SDL_KEYDOWN) {
{ switch (e.key.keysym.sym) {
switch ( e.key.keysym.sym )
{
case SDLK_SPACE: // Flap case SDLK_SPACE: // Flap
velocityY = -5; velocityY = -5;
break; break;
@ -185,63 +177,59 @@ int main ( int argc, char* argv[] )
birdRect.y += velocityY; birdRect.y += velocityY;
// Clear the window with a color (e.g., white) // Clear the window with a color (e.g., white)
SDL_SetRenderDrawColor ( renderer, 63, 63, 255, 255 ); // White color SDL_SetRenderDrawColor(renderer, 63, 63, 255, 255); // White color
SDL_RenderClear ( renderer ); SDL_RenderClear(renderer);
// Set the color for the bottom part (green) // Set the color for the bottom part (green)
SDL_SetRenderDrawColor ( renderer, 0, 255, 0, 255 ); // Green color SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); // Green color
// Define the rectangle for the bottom part of the screen // Define the rectangle for the bottom part of the screen
SDL_Rect bottomRect; SDL_Rect bottomRect;
bottomRect.x = 0; // Start from the left edge bottomRect.x = 0; // Start from the left edge
bottomRect.y = 550; // Start from the vertical position (adjust as needed) bottomRect.y = 550; // Start from the vertical position (adjust as needed)
bottomRect.w = 800; // Width of the screen (adjust based on your window size) bottomRect.w =
800; // Width of the screen (adjust based on your window size)
bottomRect.h = 50; // Height of the green area (adjust as needed) bottomRect.h = 50; // Height of the green area (adjust as needed)
// Render the green rectangle // Render the green rectangle
SDL_RenderFillRect ( renderer, &bottomRect ); SDL_RenderFillRect(renderer, &bottomRect);
// 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 ); quit = sdlPipe(&pipe1Rect, &birdRect, renderer, flappyBird, pipe);
if ( !quit ) if (!quit) {
{ quit = sdlPipe(&pipe2Rect, &birdRect, renderer, flappyBird, pipe);
quit = sdlPipe ( &pipe2Rect, &birdRect, renderer, flappyBird, pipe );
} }
if ( !quit ) if (!quit) {
{ quit = sdlPipe(&pipe3Rect, &birdRect, renderer, flappyBird, pipe);
quit = sdlPipe ( &pipe3Rect, &birdRect, renderer, flappyBird, pipe );
} }
if ( !quit ) if (!quit) {
{ quit = sdlPipe(&pipe4Rect, &birdRect, renderer, flappyBird, pipe);
quit = sdlPipe ( &pipe4Rect, &birdRect, renderer, flappyBird, pipe );
} }
// Present the renderer // Present the renderer
SDL_RenderPresent ( renderer ); SDL_RenderPresent(renderer);
frameTime = SDL_GetTicks() - frameStart; frameTime = SDL_GetTicks() - frameStart;
if ( frameTime < FRAME_DELAY ) if (frameTime < FRAME_DELAY) {
{ SDL_Delay(FRAME_DELAY - frameTime);
SDL_Delay ( FRAME_DELAY - frameTime );
} }
if ( SDL_HasIntersection ( &bottomRect, &birdRect ) ) if (SDL_HasIntersection(&bottomRect, &birdRect)) {
{
quit = true; quit = true;
} }
} }
// Clean up // Clean up
SDL_DestroyTexture ( flappyBird ); SDL_DestroyTexture(flappyBird);
SDL_DestroyTexture ( pipe ); SDL_DestroyTexture(pipe);
SDL_DestroyRenderer ( renderer ); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow ( window ); SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
return 0; return 0;