code
code
hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
// ============================================================================
// Constants - Game Configuration
// ============================================================================
// ============================================================================
// Structures - Data Representation
// ============================================================================
// ============================================================================
// Function Declarations
// ============================================================================
// ============================================================================
// Main Function - Entry Point
// ============================================================================
int main() {
// Seed the random number generator
std::srand(static_cast<unsigned int>(std::time(nullptr)));
// ========================================================================
// Load Textures - Load game assets from files
// ========================================================================
sf::Texture birdTexture;
if (!birdTexture.loadFromFile("bird.png")) { // Replace with your bird image
file
std::cerr << "Error loading bird texture!" << std::endl;
return -1;
}
sf::Texture pipeTexture;
if (!pipeTexture.loadFromFile("pipe.png")) { // Replace with your pipe image
file
std::cerr << "Error loading pipe texture!" << std::endl;
return -1;
}
sf::Texture backgroundTexture;
if (!backgroundTexture.loadFromFile("background.png")) { // Replace with your
background image
std::cerr << "Error loading background texture!" << std::endl;
return -1;
}
sf::Sprite backgroundSprite(backgroundTexture);
// ========================================================================
// Initialize Game Objects
// ========================================================================
// Bird Initialization
Bird bird;
bird.sprite.setTexture(birdTexture);
bird.sprite.setPosition(100, WINDOW_HEIGHT / 2);
bird.velocity = 0.0f;
bird.sprite.setOrigin(birdTexture.getSize().x / 2.0f, birdTexture.getSize().y /
2.0f); // Set origin to center for rotation
// Pipes Initialization
std::vector<std::pair<Pipe,Pipe>> pipePairs; // Vector to store pairs of top
and bottom pipes
float pipeSpawnTimer = 0.0f;
// ========================================================================
// Game State Variables
// ========================================================================
// ========================================================================
// Load Font - Load a font for displaying text
// ========================================================================
sf::Font font;
if (!font.loadFromFile("arial.ttf")) {
std::cerr << "Error loading font!" << std::endl;
return -1;
}
// ========================================================================
// Initialize Text - Initialize the score and game over text
// ========================================================================
sf::Text scoreText;
scoreText.setFont(font);
scoreText.setString("Score: " + std::to_string(score));
scoreText.setCharacterSize(24);
scoreText.setFillColor(sf::Color::White);
scoreText.setPosition(10, 10);
sf::Text gameOverText;
gameOverText.setFont(font);
gameOverText.setString("Game Over! Press Space to Restart!");
gameOverText.setCharacterSize(30);
gameOverText.setFillColor(sf::Color::Red);
gameOverText.setPosition(WINDOW_WIDTH / 2 - gameOverText.getLocalBounds().width
/ 2,
WINDOW_HEIGHT / 2 - gameOverText.getLocalBounds().height
/ 2);
// ========================================================================
// Game Loop - Main game loop
// ========================================================================
// ====================================================================
// Event Handling - Process user input and window events
// ====================================================================
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed && event.key.code ==
sf::Keyboard::Space) {
if (isGameOver) {
// Restart the game if it's game over
resetGame(bird, pipePairs);
isGameOver = false;
score = 0;
scoreText.setString("Score: " + std::to_string(score));
} else {
// Make the bird jump
bird.velocity = JUMP_VELOCITY;
}
}
}
// ====================================================================
// Update - Update game logic only if the game is not over
// ====================================================================
if (!isGameOver) {
// Update the bird's position
updateBird(bird, deltaTime);
// ====================================================================
// Render - Draw everything to the screen
// ====================================================================
return 0;
}
// ============================================================================
// Function Implementations
// ============================================================================
// Creates a pair of top and bottom pipes with a random gap position
std::pair<Pipe, Pipe> createPipePair(sf::Texture& pipeTexture) {
Pipe topPipe, bottomPipe;
// Top Pipe
topPipe.sprite.setTexture(pipeTexture);
topPipe.sprite.setPosition(WINDOW_WIDTH, gapPosition -
pipeTexture.getSize().y);
topPipe.sprite.setRotation(180); // Rotate top pipe to face down
// Bottom Pipe
bottomPipe.sprite.setTexture(pipeTexture);
bottomPipe.sprite.setPosition(WINDOW_WIDTH, gapPosition + PIPE_GAP_SIZE);
return false;
}