[text] Pac

Viewer

  1. #include <SFML/Graphics.hpp>
  2.  
  3. class Pacman {
  4. public:
  5.     Pacman();
  6.  
  7.     void setPosition(const sf::Vector2f& position);
  8.     void move(const sf::Vector2f& offset);
  9.     sf::FloatRect getBounds() const;
  10.     void draw(sf::RenderWindow& window);
  11.  
  12. private:
  13.     sf::CircleShape mShape;
  14. };
  15.  
  16. Pacman::Pacman()
  17.     : mShape(20.f) { // Set the radius of Pacman
  18.     mShape.setFillColor(sf::Color::Yellow);
  19. }
  20.  
  21. void Pacman::setPosition(const sf::Vector2f& position) {
  22.     mShape.setPosition(position);
  23. }
  24.  
  25. void Pacman::move(const sf::Vector2f& offset) {
  26.     mShape.move(offset);
  27. }
  28.  
  29. sf::FloatRect Pacman::getBounds() const {
  30.     return mShape.getGlobalBounds();
  31. }
  32.  
  33. void Pacman::draw(sf::RenderWindow& window) {
  34.     window.draw(mShape);
  35. }
  36.  
  37. class Ghost {
  38. public:
  39.     Ghost(const sf::Vector2f& position);
  40.  
  41.     void setPosition(const sf::Vector2f& position);
  42.     void move(const sf::Vector2f& offset);
  43.     sf::FloatRect getBounds() const;
  44.     void draw(sf::RenderWindow& window);
  45.  
  46. private:
  47.     sf::CircleShape mShape;
  48. };
  49.  
  50. Ghost::Ghost(const sf::Vector2f& position)
  51.     : mShape(20.f) { // Set the radius of the ghost
  52.     mShape.setPosition(position);
  53.     mShape.setFillColor(sf::Color::Red);
  54. }
  55.  
  56. void Ghost::setPosition(const sf::Vector2f& position) {
  57.     mShape.setPosition(position);
  58. }
  59.  
  60. void Ghost::move(const sf::Vector2f& offset) {
  61.     mShape.move(offset);
  62. }
  63.  
  64. sf::FloatRect Ghost::getBounds() const {
  65.     return mShape.getGlobalBounds();
  66. }
  67.  
  68. void Ghost::draw(sf::RenderWindow& window) {
  69.     window.draw(mShape);
  70. }
  71.  
  72. class Game {
  73. public:
  74.     Game();
  75.  
  76.     void run();
  77.  
  78. private:
  79.     void processEvents();
  80.     void update();
  81.     void render();
  82.     bool checkCollision(const sf::FloatRect& rect1, const sf::FloatRect& rect2);
  83.     void resetGame();
  84.  
  85. private:
  86.     sf::RenderWindow mWindow;
  87.     Pacman mPacman;
  88.     Ghost mGhosts[4]; // Four ghosts in Pacman
  89.     sf::RectangleShape mWall;
  90.     int mScore;
  91.     bool mGameOver;
  92.     bool mInMenu;
  93. };
  94.  
  95. Game::Game()
  96.     : mWindow(sf::VideoMode(800, 600), "Pacman"), mScore(0), mGameOver(false), mInMenu(true) {
  97.     // Initialize Pacman and Ghosts
  98.     mPacman.setPosition(sf::Vector2f(100.f, 100.f));
  99.     mGhosts[0] = Ghost(sf::Vector2f(400.f, 200.f));
  100.     mGhosts[1] = Ghost(sf::Vector2f(300.f, 400.f));
  101.     mGhosts[2] = Ghost(sf::Vector2f(500.f, 400.f));
  102.     mGhosts[3] = Ghost(sf::Vector2f(200.f, 500.f));
  103.     // Initialize wall
  104.     mWall.setSize(sf::Vector2f(40.f, 40.f));
  105.     mWall.setFillColor(sf::Color::Blue);
  106. }
  107.  
  108. void Game::run() {
  109.     while (mWindow.isOpen()) {
  110.         if (mInMenu) {
  111.             // Handle menu
  112.         } else {
  113.             processEvents();
  114.             update();
  115.             render();
  116.         }
  117.     }
  118. }
  119.  
  120. void Game::processEvents() {
  121.     sf::Event event;
  122.     while (mWindow.pollEvent(event)) {
  123.         if (event.type == sf::Event::Closed)
  124.             mWindow.close();
  125.     }
  126. }
  127.  
  128. void Game::update() {
  129.     // Move Pacman and Ghosts (demo)
  130.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  131.         mPacman.move(sf::Vector2f(-1.f, 0.f));
  132.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  133.         mPacman.move(sf::Vector2f(1.f, 0.f));
  134.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  135.         mPacman.move(sf::Vector2f(0.f, -1.f));
  136.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  137.         mPacman.move(sf::Vector2f(0.f, 1.f));
  138.  
  139.     // Basic collision detection with walls
  140.     if (checkCollision(mPacman.getBounds(), mWall.getGlobalBounds())) {
  141.         // Reverse Pacman's movement (prevent collision)
  142.         // For now, let's just stop Pacman
  143.         mPacman.move(sf::Vector2f(0.f, 0.f));
  144.     }
  145.  
  146.     // Ghost-Pacman collision detection
  147.     for (int i = 0; i < 4; ++i) {
  148.         if (checkCollision(mPacman.getBounds(), mGhosts[i].getBounds())) {
  149.             // Game over
  150.             mGameOver = true;
  151.             break;
  152.         }
  153.     }
  154. }
  155.  
  156. void Game::render() {
  157.     mWindow.clear();
  158.     mWindow.draw(mWall); // Draw walls
  159.     mPacman.draw(mWindow); // Draw Pacman
  160.     for (int i = 0; i < 4; ++i) {
  161.         mGhosts[i].draw(mWindow); // Draw ghosts
  162.     }
  163.     mWindow.display();
  164. }
  165.  
  166. bool Game::checkCollision(const sf::FloatRect& rect1, const sf::FloatRect& rect2) {
  167.     return rect1.intersects(rect2);
  168. }
  169.  
  170. void Game::resetGame() {
  171.     // Reset game state
  172. }
  173.  
  174. int main() {
  175.     Game game;
  176.     game.run();
  177.     return 0;
  178. }
  179.  

Editor

You can edit this paste and save as new:


File Description
  • Pac
  • Paste Code
  • 17 Apr-2024
  • 4.34 Kb
You can Share it: