[text] P

Viewer

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

Editor

You can edit this paste and save as new:


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