[text] Pac

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(sf::Texture& texture);
  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(sf::Texture& texture) {
  27.     sprite.setTexture(texture);
  28. }
  29.  
  30. void Pacman::move(const sf::Vector2f& direction) {
  31.     sprite.move(direction);
  32. }
  33.  
  34. void Pacman::draw(sf::RenderWindow& window) {
  35.     window.draw(sprite);
  36. }
  37.  
  38. sf::FloatRect Pacman::getBounds() const {
  39.     return sprite.getGlobalBounds();
  40. }
  41.  
  42. void Pacman::setPosition(const sf::Vector2f& position) {
  43.     sprite.setPosition(position);
  44. }
  45.  
  46. class Ghost {
  47. public:
  48.     Ghost(sf::Texture& texture);
  49.     void move(const sf::Vector2f& direction);
  50.     void draw(sf::RenderWindow& window);
  51.     sf::FloatRect getBounds() const;
  52.     void setPosition(const sf::Vector2f& position);
  53.  
  54. private:
  55.     sf::Sprite sprite;
  56. };
  57.  
  58. Ghost::Ghost(sf::Texture& texture) {
  59.     sprite.setTexture(texture);
  60. }
  61.  
  62. void Ghost::move(const sf::Vector2f& direction) {
  63.     sprite.move(direction);
  64. }
  65.  
  66. void Ghost::draw(sf::RenderWindow& window) {
  67.     window.draw(sprite);
  68. }
  69.  
  70. sf::FloatRect Ghost::getBounds() const {
  71.     return sprite.getGlobalBounds();
  72. }
  73.  
  74. void Ghost::setPosition(const sf::Vector2f& position) {
  75.     sprite.setPosition(position);
  76. }
  77.  
  78. int main() {
  79.     srand(static_cast<unsigned int>(time(NULL)));
  80.  
  81.     sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Pacman");
  82.  
  83.     sf::Texture pacmanTexture;
  84.     pacmanTexture.loadFromFile("pacman.png");
  85.     Pacman pacman(pacmanTexture);
  86.     pacman.setPosition(sf::Vector2f(100, 100));
  87.  
  88.     sf::Texture ghostTexture;
  89.     ghostTexture.loadFromFile("ghost.png");
  90.     Ghost ghost(ghostTexture);
  91.     ghost.setPosition(sf::Vector2f(200, 200));
  92.  
  93.     sf::Clock clock;
  94.  
  95.     int score = 0;
  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! Final Score: " << score << 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
  • Pac
  • Paste Code
  • 17 Apr-2024
  • 4 Kb
You can Share it: