Created
October 9, 2025 08:34
-
-
Save kenpower/7bc60474902632b452c86baa8205b783 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Code smell: Long method that does too many things | |
| #include <SFML/Graphics.hpp> | |
| void updateGameWorld(sf::RenderWindow& window, | |
| sf::Sprite& player, | |
| std::vector<sf::Sprite>& enemies, | |
| sf::Texture& explosionTex) | |
| { | |
| // 1. Process player input | |
| if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) | |
| player.move(-5, 0); | |
| if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) | |
| player.move(5, 0); | |
| if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) | |
| player.move(0, -5); | |
| if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) | |
| player.move(0, 5); | |
| // 2. Update all enemies | |
| for (auto& e : enemies) | |
| e.move(0, 0.5f); | |
| // 3. Detect collisions | |
| for (auto& e : enemies) | |
| { | |
| if (player.getGlobalBounds().intersects(e.getGlobalBounds())) | |
| { | |
| // crude “explosion” | |
| e.setTexture(explosionTex); | |
| } | |
| } | |
| // 4. Change game state based on collisions, input, etc. | |
| if (player.getPosition().y < 0) | |
| { | |
| // reached goal – for demo just reset | |
| player.setPosition(400, 550); | |
| } | |
| // 5. Render everything | |
| window.clear(sf::Color::Black); | |
| window.draw(player); | |
| for (auto& e : enemies) | |
| window.draw(e); | |
| window.display(); | |
| // … and you can imagine more: score, audio, UI, physics, etc. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment