Here is an example of how to make a simple "Floppy Bird" game in C++ using graphics libraries like SFML or Allegro:
Initialize the game window: Use the graphics library to create a window and set its size, title, and background color.
Create the bird: Draw a simple shape (e.g. a circle) for the bird, set its initial position, and store its velocity and acceleration.
Create the pipes: Draw two rectangles for the pipes, one for the top and one for the bottom. Set their initial position and velocity.
Game loop: Continuously update the position of the bird and pipes based on their velocity and acceleration. Check for collision between the bird and pipes and end the game if a collision occurs.
User input: Listen for user input (e.g. space bar) to make the bird flap its wings and change its velocity.
Render the game: Use the graphics library to render the bird and pipes in their updated positions.
Repeat steps 4-6 until the game ends.
Note that this is just a basic example, and there are many ways to improve the game and add more features.
Here is an example of a simple "Floppy Bird" game in C++ using SFML library:
#include <SFML/Graphics.hpp> #include <iostream> constexpr int windowWidth = 500; constexpr int windowHeight = 500; constexpr float birdRadius = 20.f; constexpr float birdInitialY = windowHeight / 2.f; constexpr float birdGravity = 981.f; constexpr float birdJumpSpeed = 300.f; constexpr float pipeSpeed = 200.f; constexpr int pipeWidth = 50; constexpr int pipeGap = 200; constexpr int pipeCount = 5; struct Pipe { sf::RectangleShape top; sf::RectangleShape bottom; float x; float bottomY; }; int main() { sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "Floppy Bird"); window.setFramerateLimit(60); sf::CircleShape bird(birdRadius); bird.setFillColor(sf::Color::Yellow); bird.setOrigin(birdRadius, birdRadius); bird.setPosition(birdRadius * 2, birdInitialY); std::vector<Pipe> pipes; for (int i = 0; i < pipeCount; ++i) { float bottomY = windowHeight - birdRadius * 2 - pipeGap / 2 - std::rand() % (windowHeight - birdRadius * 2 - pipeGap); pipes.push_back({ sf::RectangleShape({(float)pipeWidth, bottomY}), sf::RectangleShape({(float)pipeWidth, windowHeight - birdRadius * 2 - bottomY - pipeGap}), (float)(windowWidth + i * (pipeWidth + birdRadius * 4)), bottomY }); pipes.back().top.setFillColor(sf::Color::Green); pipes.back().bottom.setFillColor(sf::Color::Green); pipes.back().top.setPosition(pipes.back().x, 0); pipes.back().bottom.setPosition(pipes.back().x, bottomY + pipeGap); } sf::Clock clock; while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } float deltaTime = clock.restart().asSeconds(); if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) bird.move(0, -deltaTime * birdJumpSpeed); bird.move(0, deltaTime * birdGravity); if (bird.getPosition().y + birdRadius > windowHeight) { bird.setPosition(bird.getPosition().x, windowHeight - birdRadius); bird.setFillColor(sf::Color::Red); } for (auto& pipe : pipes) { pipe.x -= deltaTime * pipeSpeed; pipe.top.setPosition(pipe
Source: ChatGPT
😐😐😐😐
No comments:
Post a Comment