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

126
main.cpp
View file

@ -21,33 +21,31 @@
#include <iostream>
#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
SDL_RenderCopy(renderer, pipe, nullptr, pipeRect);
// Draw pipes and check for collisions.
if ( SDL_HasIntersection ( pipeRect, birdRect ) )
{
if (SDL_HasIntersection(pipeRect, birdRect)) {
return true;
}
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;
if ( SDL_HasIntersection ( pipeRect, birdRect ) )
{
if (SDL_HasIntersection(pipeRect, birdRect)) {
return true;
}
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::mt19937 eng(rd()); // Seed the generator
std::uniform_int_distribution<> distr(-950, -650); // Define the range
@ -61,33 +59,44 @@ bool sdlPipe ( SDL_Rect* pipeRect, SDL_Rect* birdRect, SDL_Renderer* renderer, S
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
if ( SDL_Init ( SDL_INIT_VIDEO ) < 0 )
{
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
<< std::endl;
return 1;
}
// Create a window
SDL_Window* window = SDL_CreateWindow ( "Flappy Bird",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
800, 600,
SDL_WINDOW_SHOWN );
if ( window == nullptr )
{
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
SDL_Window *window =
SDL_CreateWindow("Flappy Bird", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
if (window == nullptr) {
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError()
<< std::endl;
SDL_Quit();
return 1;
}
// Create a renderer
SDL_Renderer* renderer = SDL_CreateRenderer ( window, -1, SDL_RENDERER_ACCELERATED );
if ( renderer == nullptr )
{
std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl;
SDL_Renderer *renderer =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == nullptr) {
std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError()
<< std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
@ -95,16 +104,16 @@ int main ( int argc, char* argv[] )
// Load the sprite texture
SDL_Texture *flappyBird = IMG_LoadTexture(renderer, "assets/bird.png");
if ( flappyBird == nullptr )
{
std::cerr << "Failed to load texture! SDL_Error: " << SDL_GetError() << std::endl;
if (flappyBird == nullptr) {
std::cerr << "Failed to load texture! SDL_Error: " << SDL_GetError()
<< std::endl;
// Handle error
}
SDL_Texture *pipe = IMG_LoadTexture(renderer, "assets/pipe.png");
if ( pipe == nullptr )
{
std::cerr << "Failed to load texture! SDL_Error: " << SDL_GetError() << std::endl;
if (pipe == nullptr) {
std::cerr << "Failed to load texture! SDL_Error: " << SDL_GetError()
<< std::endl;
// Handle error
}
@ -121,28 +130,16 @@ int main ( int argc, char* argv[] )
std::uniform_int_distribution<> distr(-950, -650); // Define the range
SDL_Rect pipe1Rect;
pipe1Rect.x = 650;
pipe1Rect.y = distr ( eng ) ;
pipe1Rect.w = 64;
pipe1Rect.h = 1024;
setPipe(&pipe1Rect, 650);
SDL_Rect pipe2Rect;
pipe2Rect.x = 850;
pipe2Rect.y = distr ( eng ) ;
pipe2Rect.w = 64;
pipe2Rect.h = 1024;
setPipe(&pipe2Rect, 850);
SDL_Rect pipe3Rect;
pipe3Rect.x = 1050;
pipe3Rect.y = distr ( eng ) ;
pipe3Rect.w = 64;
pipe3Rect.h = 1024;
setPipe(&pipe3Rect, 1050);
SDL_Rect pipe4Rect;
pipe4Rect.x = 1250;
pipe4Rect.y = distr ( eng ) ;
pipe4Rect.w = 64;
pipe4Rect.h = 1024;
setPipe(&pipe4Rect, 1250);
// Sprite Velocity
double velocityY = 0;
@ -157,23 +154,18 @@ int main ( int argc, char* argv[] )
const int FRAME_DELAY = 1000 / TARGET_FPS; // Delay in milliseconds
// Main loop
while ( !quit )
{
while (!quit) {
frameStart = SDL_GetTicks();
// Handle events
while ( SDL_PollEvent ( &e ) != 0 )
{
while (SDL_PollEvent(&e) != 0) {
// User requests quit
if ( e.type == SDL_QUIT )
{
if (e.type == SDL_QUIT) {
quit = true;
}
if ( e.type == SDL_KEYDOWN )
{
switch ( e.key.keysym.sym )
{
if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_SPACE: // Flap
velocityY = -5;
break;
@ -195,7 +187,8 @@ int main ( int argc, char* argv[] )
SDL_Rect bottomRect;
bottomRect.x = 0; // Start from the left edge
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)
// Render the green rectangle
@ -206,18 +199,15 @@ int main ( int argc, char* argv[] )
quit = sdlPipe(&pipe1Rect, &birdRect, renderer, flappyBird, pipe);
if ( !quit )
{
if (!quit) {
quit = sdlPipe(&pipe2Rect, &birdRect, renderer, flappyBird, pipe);
}
if ( !quit )
{
if (!quit) {
quit = sdlPipe(&pipe3Rect, &birdRect, renderer, flappyBird, pipe);
}
if ( !quit )
{
if (!quit) {
quit = sdlPipe(&pipe4Rect, &birdRect, renderer, flappyBird, pipe);
}
@ -226,13 +216,11 @@ int main ( int argc, char* argv[] )
frameTime = SDL_GetTicks() - frameStart;
if ( frameTime < FRAME_DELAY )
{
if (frameTime < FRAME_DELAY) {
SDL_Delay(FRAME_DELAY - frameTime);
}
if ( SDL_HasIntersection ( &bottomRect, &birdRect ) )
{
if (SDL_HasIntersection(&bottomRect, &birdRect)) {
quit = true;
}
}