[text] Пакман

Viewer

copydownloadembedprintName: Пакман
  1. #include <SFML/Graphics.hpp>
  2.  
  3. class Menu {
  4. public:
  5.     Menu(sf::RenderWindow& window);
  6.  
  7.     void draw();
  8.     int handleInput();
  9.  
  10. private:
  11.     sf::RenderWindow& mWindow;
  12.     sf::Font mFont;
  13.     sf::Text mTitle;
  14.     sf::Text mStartText;
  15.     sf::Text mExitText;
  16. };
  17.  
  18. Menu::Menu(sf::RenderWindow& window)
  19.     : mWindow(window) {
  20.     if (!mFont.loadFromFile("arial.ttf")) { // Load font
  21.         // Handle font loading error
  22.     }
  23.  
  24.     // Setup menu title
  25.     mTitle.setFont(mFont);
  26.     mTitle.setString("Pacman");
  27.     mTitle.setCharacterSize(50);
  28.     mTitle.setFillColor(sf::Color::Yellow);
  29.     mTitle.setStyle(sf::Text::Bold);
  30.     mTitle.setPosition(300.f, 100.f);
  31.  
  32.     // Setup start text
  33.     mStartText.setFont(mFont);
  34.     mStartText.setString("Start Game");
  35.     mStartText.setCharacterSize(30);
  36.     mStartText.setFillColor(sf::Color::White);
  37.     mStartText.setPosition(350.f, 250.f);
  38.  
  39.     // Setup exit text
  40.     mExitText.setFont(mFont);
  41.     mExitText.setString("Exit");
  42.     mExitText.setCharacterSize(30);
  43.     mExitText.setFillColor(sf::Color::White);
  44.     mExitText.setPosition(400.f, 300.f);
  45. }
  46.  
  47. void Menu::draw() {
  48.     mWindow.clear(sf::Color::Black);
  49.     mWindow.draw(mTitle);
  50.     mWindow.draw(mStartText);
  51.     mWindow.draw(mExitText);
  52.     mWindow.display();
  53. }
  54.  
  55. int Menu::handleInput() {
  56.     sf::Event event;
  57.     while (mWindow.pollEvent(event)) {
  58.         if (event.type == sf::Event::Closed)
  59.             mWindow.close();
  60.         if (event.type == sf::Event::MouseButtonPressed) {
  61.             if (event.mouseButton.button == sf::Mouse::Left) {
  62.                 sf::Vector2f mousePos = mWindow.mapPixelToCoords(sf::Mouse::getPosition(mWindow));
  63.                 if (mStartText.getGlobalBounds().contains(mousePos)) {
  64.                     return 1; // Start game
  65.                 }
  66.                 if (mExitText.getGlobalBounds().contains(mousePos)) {
  67.                     return 2; // Exit game
  68.                 }
  69.             }
  70.         }
  71.     }
  72.     return 0; // No action
  73. }
  74.  
  75. class Game {
  76. public:
  77.     Game();
  78.  
  79.     void run();
  80.  
  81. private:
  82.     void processEvents();
  83.     void update();
  84.     void render();
  85.     bool checkCollision(const sf::FloatRect& rect1, const sf::FloatRect& rect2);
  86.     void resetGame();
  87.  
  88. private:
  89.     sf::RenderWindow mWindow;
  90.     Pacman mPacman;
  91.     Ghost mGhosts[4]; // Four ghosts in Pacman
  92.     sf::RectangleShape mWall;
  93.     int mScore;
  94.     bool mGameOver;
  95.     bool mInMenu;
  96. };
  97.  
  98. Game::Game()
  99.     : mWindow(sf::VideoMode(800, 600), "Pacman"), mScore(0), mGameOver(false), mInMenu(true) {
  100.     mWall.setSize(sf::Vector2f(40.f, 40.f));
  101.     mWall.setFillColor(sf::Color::Blue);
  102.  
  103.     // Initialize ghosts
  104.     mGhosts[0] = Ghost(sf::Vector2f(400.f, 200.f));
  105.     mGhosts[1] = Ghost(sf::Vector2f(300.f, 400.f));
  106.     mGhosts[2] = Ghost(sf::Vector2f(500.f, 400.f));
  107.     mGhosts[3] = Ghost(sf::Vector2f(200.f, 500.f));
  108. }
  109.  
  110. void Game::run() {
  111.     Menu menu(mWindow);
  112.     while (mWindow.isOpen()) {
  113.         if (mInMenu) {
  114.             menu.draw();
  115.             int action = menu.handleInput();
  116.             if (action == 1) { // Start game
  117.                 mInMenu = false;
  118.             } else if (action == 2) { // Exit game
  119.                 mWindow.close();
  120.             }
  121.         } else {
  122.             processEvents();
  123.             update();
  124.             render();
  125.         }
  126.     }
  127. }
  128.  
  129. void Game::processEvents() {
  130.     sf::Event event;
  131.     while (mWindow.pollEvent(event)) {
  132.         if (event.type == sf::Event::Closed)
  133.             mWindow.close();
  134.     }
  135. }
  136.  
  137. void Game::update() {
  138.     if (!mGameOver) {
  139.         // Pacman movement
  140.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  141.             mPacman.move(sf::Vector2f(-1.f, 0.f));
  142.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  143.             mPacman.move(sf::Vector2f(1.f, 0.f));
  144.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  145.             mPacman.move(sf::Vector2f(0.f, -1.f));
  146.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  147.             mPacman.move(sf::Vector2f(0.f, 1.f));
  148.  
  149.         // Basic collision detection with walls
  150.         if (checkCollision(mPacman.getBounds(), mWall.getGlobalBounds())) {
  151.             // Reverse Pacman's movement (prevent collision)
  152.             mPacman.move(-mPacman.getVelocity());
  153.         }
  154.  
  155.         // Ghost-Pacman collision detection
  156.         for (int i = 0; i < 4; ++i) {
  157.             if (checkCollision(mPacman.getBounds(), mGhosts[i].getBounds())) {
  158.                 // Game over
  159.                 mGameOver = true;
  160.                 break;
  161.             }
  162.         }
  163.     } else {
  164.         // Game over handling (reset the game after some time, display game over message, etc.)
  165.         resetGame();
  166.     }
  167. }
  168.  
  169. void Game::render() {
  170.     mWindow.clear();
  171.     if (!mGameOver) {
  172.         mWindow.draw(mWall); // Draw walls
  173.         mPacman.draw(mWindow); // Draw Pacman
  174.         for (int i = 0; i < 4; ++i) {
  175.             mGhosts[i].draw(mWindow); // Draw ghosts
  176.         }
  177.     } else {
  178.         // Render game over message
  179.     }
  180.     mWindow.display();
  181. }
  182.  
  183. bool Game::checkCollision(const sf::FloatRect& rect1, const sf::FloatRect& rect2) {
  184.     return rect1.intersects(rect2);
  185. }
  186.  
  187. void Game::resetGame() {
  188.     // Reset game state (score, positions, etc.)
  189.     mScore = 0;
  190.     mPacman.resetPosition();
  191.     for (int i = 0; i < 4; ++i) {
  192.         mGhosts[i].resetPosition();
  193.     }
  194.     mGameOver = false;
  195. }
  196.  
  197. int main() {
  198.     Game game;
  199.     game.run();
  200.     return 0;
  201. }
  202.  

Editor

You can edit this paste and save as new:


File Description
  • Пакман
  • Paste Code
  • 17 Apr-2024
  • 5.42 Kb
You can Share it: