[text] P

Viewer

  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include <vector>
  4. #include <ctime>
  5. #include <cstdlib>
  6.  
  7. const int TILE_SIZE = 40;
  8. const int MAP_WIDTH = 15;
  9. const int MAP_HEIGHT = 15;
  10. const int WINDOW_WIDTH = TILE_SIZE * MAP_WIDTH;
  11. const int WINDOW_HEIGHT = TILE_SIZE * MAP_HEIGHT;
  12. const int PACMAN_SPEED = 4;
  13. const int GHOST_SPEED = 3;
  14.  
  15. class Pacman {
  16. public:
  17.     Pacman();
  18.     void move(const sf::Vector2f& direction);
  19.     void draw(sf::RenderWindow& window);
  20.     sf::FloatRect getBounds() const;
  21.     void setPosition(const sf::Vector2f& position);
  22.  
  23. private:
  24.     sf::Sprite sprite;
  25. };
  26.  
  27. Pacman::Pacman() {
  28.     sf::Texture texture;
  29.     texture.loadFromFile("pacman.png");
  30.     sprite.setTexture(texture);
  31. }
  32.  
  33. void Pacman::move(const sf::Vector2f& direction) {
  34.     sprite.move(direction);
  35. }
  36.  
  37. void Pacman::draw(sf::RenderWindow& window) {
  38.     window.draw(sprite);
  39. }
  40.  
  41. sf::FloatRect Pacman::getBounds() const {
  42.     return sprite.getGlobalBounds();
  43. }
  44.  
  45. void Pacman::setPosition(const sf::Vector2f& position) {
  46.     sprite.setPosition(position);
  47. }
  48.  
  49. class Ghost {
  50. public:
  51.     Ghost();
  52.     void move(const sf::Vector2f& direction);
  53.     void draw(sf::RenderWindow& window);
  54.     sf::FloatRect getBounds() const;
  55.     void setPosition(const sf::Vector2f& position);
  56.  
  57. private:
  58.     sf::Sprite sprite;
  59. };
  60.  
  61. Ghost::Ghost() {
  62.     sf::Texture texture;
  63.     texture.loadFromFile("ghost.png");
  64.     sprite.setTexture(texture);
  65. }
  66.  
  67. void Ghost::move(const sf::Vector2f& direction) {
  68.     sprite.move(direction);
  69. }
  70.  
  71. void Ghost::draw(sf::RenderWindow& window) {
  72.     window.draw(sprite);
  73. }
  74.  
  75. sf::FloatRect Ghost::getBounds() const {
  76.     return sprite.getGlobalBounds();
  77. }
  78.  
  79. void Ghost::setPosition(const sf::Vector2f& position) {
  80.     sprite.setPosition(position);
  81. }
  82.  
  83. int main() {
  84.     srand(static_cast<unsigned int>(time(NULL)));
  85.  
  86.     sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Pacman");
  87.  
  88.     Pacman pacman;
  89.     pacman.setPosition(sf::Vector2f(100, 100));
  90.  
  91.     std::vector<Ghost> ghosts;
  92.     for (int i = 0; i < 4; ++i) {
  93.         Ghost ghost;
  94.         ghost.setPosition(sf::Vector2f(200 + i * 50, 200 + i * 50));
  95.         ghosts.push_back(ghost);
  96.     }
  97.  
  98.     sf::Clock clock;
  99.  
  100.     while (window.isOpen()) {
  101.         sf::Event event;
  102.         while (window.pollEvent(event)) {
  103.             if (event.type == sf::Event::Closed) {
  104.                 window.close();
  105.             }
  106.         }
  107.  
  108.         sf::Time deltaTime = clock.restart();
  109.         float dt = deltaTime.asSeconds();
  110.  
  111.         // Pacman movement
  112.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  113.             pacman.move(sf::Vector2f(-PACMAN_SPEED * dt, 0));
  114.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  115.             pacman.move(sf::Vector2f(PACMAN_SPEED * dt, 0));
  116.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  117.             pacman.move(sf::Vector2f(0, -PACMAN_SPEED * dt));
  118.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  119.             pacman.move(sf::Vector2f(0, PACMAN_SPEED * dt));
  120.  
  121.         // Ghost movement (random)
  122.         for (auto& ghost : ghosts) {
  123.             int dir = rand() % 4;
  124.             sf::Vector2f ghostDir;
  125.             switch (dir) {
  126.                 case 0: // Up
  127.                     ghostDir = sf::Vector2f(0, -GHOST_SPEED * dt);
  128.                     break;
  129.                 case 1: // Down
  130.                     ghostDir = sf::Vector2f(0, GHOST_SPEED * dt);
  131.                     break;
  132.                 case 2: // Left
  133.                     ghostDir = sf::Vector2f(-GHOST_SPEED * dt, 0);
  134.                     break;
  135.                 case 3: // Right
  136.                     ghostDir = sf::Vector2f(GHOST_SPEED * dt, 0);
  137.                     break;
  138.             }
  139.             ghost.move(ghostDir);
  140.         }
  141.  
  142.         // Collision detection
  143.         for (auto& ghost : ghosts) {
  144.             if (pacman.getBounds().intersects(ghost.getBounds())) {
  145.                 std::cout << "Game Over!" << std::endl;
  146.                 window.close();
  147.             }
  148.         }
  149.  
  150.         // Rendering
  151.         window.clear();
  152.         pacman.draw(window);
  153.         for (auto& ghost : ghosts) {
  154.             ghost.draw(window);
  155.         }
  156.         window.display();
  157.     }
  158.  
  159.     return 0;
  160. }
  161.  

Editor

You can edit this paste and save as new:


File Description
  • P
  • Paste Code
  • 17 Apr-2024
  • 4.21 Kb
You can Share it: