Shooting game
Here's a simple implementation of a shooting game in C++ using the SFML library for graphics and input handling. **Game Description:** In this game, the player controls a spaceship that can move left and right on the screen. The player can also shoot bullets at enemies that appear on the screen. The game ends when the player's spaceship collides with an enemy. **Code:** ```cpp #include <SFML/Graphics.hpp> #include <SFML/Audio.hpp> #include <iostream> #include <vector> // Define constants const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; const int PLAYER_SIZE = 50; const int BULLET_SIZE = 10; const int ENEMY_SIZE = 50; const int PLAYER_SPEED = 5; const int BULLET_SPEED = 10; const int ENEMY_SPEED = 2; // Define player class class Player { public: sf::RectangleShape shape; sf::Vector2f velocity; Player(sf::Vector2f position) { shape.setSize(sf::Vector2f(PLAYER_SIZE, PLAYER_SIZE)); shape.setFillColor(sf::Color::Red); shape.setPosition(position); velocity = sf::Vector2f(0, 0); } void update() { shape.move(velocity); } }; // Define bullet class class Bullet { public: sf::RectangleShape shape; sf::Vector2f velocity; Bullet(sf::Vector2f position) { shape.setSize(sf::Vector2f(BULLET_SIZE, BULLET_SIZE)); shape.setFillColor(sf::Color::Yellow); shape.setPosition(position); velocity = sf::Vector2f(0, -BULLET_SPEED); } void update() { shape.move(velocity); } }; // Define enemy class class Enemy { public: sf::RectangleShape shape; sf::Vector2f velocity; Enemy(sf::Vector2f position) { shape.setSize(sf::Vector2f(ENEMY_SIZE, ENEMY_SIZE)); shape.setFillColor(sf::Color::Blue); shape.setPosition(position); velocity = sf::Vector2f(ENEMY_SPEED, 0); } void update() { shape.move(velocity); } }; int main() { // Create window sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Shooting Game"); // Create player Player player(sf::Vector2f(SCREEN_WIDTH / 2, SCREEN_HEIGHT - PLAYER_SIZE)); // Create bullets std::vector<Bullet> bullets; // Create enemies std::vector<Enemy> enemies; // Create clock sf::Clock clock; // Create font sf::Font font; font.loadFromFile("arial.ttf"); // Create text sf::Text scoreText; scoreText.setFont(font); scoreText.setCharacterSize(24); scoreText.setFillColor(sf::Color::White); scoreText.setPosition(10, 10); // Create score int score = 0; // Game loop while (window.isOpen()) { // Handle events sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { window.close(); } if (event.type == sf::Event::KeyPressed) { if (event.key.code == sf::Keyboard::Space) { // Shoot bullet bullets.push_back(Bullet(player.shape.getPosition())); } } } // Update player if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { player.velocity.x = -PLAYER_SPEED; } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { player.velocity.x = PLAYER_SPEED; } else { player.velocity.x = 0; } // Update bullets for (auto& bullet : bullets) { bullet.update(); if (bullet.shape.getPosition().y < 0) { // Remove bullet bullets.erase(std::remove(bullets.begin(), bullets.end(), bullet), bullets.end()); } } // Update enemies for (auto& enemy : enemies) { enemy.update(); if (enemy.shape.getPosition().x < 0 || enemy.shape.getPosition().x > SCREEN_WIDTH - ENEMY_SIZE) { // Change enemy direction enemy.velocity.x = -enemy.velocity.x; } } // Check collisions for (auto& bullet : bullets) { for (auto& enemy : enemies) { if (bullet.shape.getGlobalBounds().intersects(enemy.shape.getGlobalBounds())) { // Remove bullet and enemy bullets.erase(std::remove(bullets.begin(), bullets.end(), bullet), bullets.end()); enemies.erase(std::remove(enemies.begin(), enemies.end(), enemy), enemies.end()); score++; } } } // Add new enemies if (clock.getElapsedTime().asSeconds() > 2) { enemies.push_back(Enemy(sf::Vector2f(rand() % (SCREEN_WIDTH - ENEMY_SIZE), 0))); clock.restart(); } // Update player position player.update(); // Update score text scoreText.setString("Score: " + std::to_string(score)); // Draw everything window.clear(); window.draw(player.shape); for (const auto& bullet : bullets) { window.draw(bullet.shape); } for (const auto& enemy : enemies) { window.draw(enemy.shape); } window.draw(scoreText); window.display(); // Cap framerate sf::sleep(sf::milliseconds(1000 / 60)); } return 0; } ``` **How to Run:** 1. Install SFML library by following the instructions on the SFML website. 2. Create a new C++ project in your preferred IDE. 3. Copy the code above and paste it into the project. 4. Replace the font file path in the code with the path to a font file on your system. 5. Compile and run the project. **Gameplay:** * Use the left and right arrow keys to move the player. * Press the space bar to shoot bullets. * Avoid colliding with enemies to increase your score. * The game ends when the player's spaceship collides with an enemy.