- #include <SFML/Graphics.hpp>
- class Menu {
- public:
- Menu(sf::RenderWindow& window);
- void draw();
- int handleInput();
- private:
- sf::RenderWindow& mWindow;
- sf::Font mFont;
- sf::Text mTitle;
- sf::Text mStartText;
- sf::Text mExitText;
- };
- Menu::Menu(sf::RenderWindow& window)
- : mWindow(window) {
- if (!mFont.loadFromFile("arial.ttf")) { // Load font
- // Handle font loading error
- }
- // Setup menu title
- mTitle.setFont(mFont);
- mTitle.setString("Pacman");
- mTitle.setCharacterSize(50);
- mTitle.setFillColor(sf::Color::Yellow);
- mTitle.setStyle(sf::Text::Bold);
- mTitle.setPosition(300.f, 100.f);
- // Setup start text
- mStartText.setFont(mFont);
- mStartText.setString("Start Game");
- mStartText.setCharacterSize(30);
- mStartText.setFillColor(sf::Color::White);
- mStartText.setPosition(350.f, 250.f);
- // Setup exit text
- mExitText.setFont(mFont);
- mExitText.setString("Exit");
- mExitText.setCharacterSize(30);
- mExitText.setFillColor(sf::Color::White);
- mExitText.setPosition(400.f, 300.f);
- }
- void Menu::draw() {
- mWindow.clear(sf::Color::Black);
- mWindow.draw(mTitle);
- mWindow.draw(mStartText);
- mWindow.draw(mExitText);
- mWindow.display();
- }
- int Menu::handleInput() {
- sf::Event event;
- while (mWindow.pollEvent(event)) {
- if (event.type == sf::Event::Closed)
- mWindow.close();
- if (event.type == sf::Event::MouseButtonPressed) {
- if (event.mouseButton.button == sf::Mouse::Left) {
- sf::Vector2f mousePos = mWindow.mapPixelToCoords(sf::Mouse::getPosition(mWindow));
- if (mStartText.getGlobalBounds().contains(mousePos)) {
- return 1; // Start game
- }
- if (mExitText.getGlobalBounds().contains(mousePos)) {
- return 2; // Exit game
- }
- }
- }
- }
- return 0; // No action
- }
- class Game {
- public:
- Game();
- void run();
- private:
- void processEvents();
- void update();
- void render();
- bool checkCollision(const sf::FloatRect& rect1, const sf::FloatRect& rect2);
- void resetGame();
- private:
- sf::RenderWindow mWindow;
- Pacman mPacman;
- Ghost mGhosts[4]; // Four ghosts in Pacman
- sf::RectangleShape mWall;
- int mScore;
- bool mGameOver;
- bool mInMenu;
- };
- Game::Game()
- : mWindow(sf::VideoMode(800, 600), "Pacman"), mScore(0), mGameOver(false), mInMenu(true) {
- mWall.setSize(sf::Vector2f(40.f, 40.f));
- mWall.setFillColor(sf::Color::Blue);
- // Initialize ghosts
- mGhosts[0] = Ghost(sf::Vector2f(400.f, 200.f));
- mGhosts[1] = Ghost(sf::Vector2f(300.f, 400.f));
- mGhosts[2] = Ghost(sf::Vector2f(500.f, 400.f));
- mGhosts[3] = Ghost(sf::Vector2f(200.f, 500.f));
- }
- void Game::run() {
- Menu menu(mWindow);
- while (mWindow.isOpen()) {
- if (mInMenu) {
- menu.draw();
- int action = menu.handleInput();
- if (action == 1) { // Start game
- mInMenu = false;
- } else if (action == 2) { // Exit game
- mWindow.close();
- }
- } else {
- processEvents();
- update();
- render();
- }
- }
- }
- void Game::processEvents() {
- sf::Event event;
- while (mWindow.pollEvent(event)) {
- if (event.type == sf::Event::Closed)
- mWindow.close();
- }
- }
- void Game::update() {
- if (!mGameOver) {
- // Pacman movement
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
- mPacman.move(sf::Vector2f(-1.f, 0.f));
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
- mPacman.move(sf::Vector2f(1.f, 0.f));
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
- mPacman.move(sf::Vector2f(0.f, -1.f));
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
- mPacman.move(sf::Vector2f(0.f, 1.f));
- // Basic collision detection with walls
- if (checkCollision(mPacman.getBounds(), mWall.getGlobalBounds())) {
- // Reverse Pacman's movement (prevent collision)
- mPacman.move(-mPacman.getVelocity());
- }
- // Ghost-Pacman collision detection
- for (int i = 0; i < 4; ++i) {
- if (checkCollision(mPacman.getBounds(), mGhosts[i].getBounds())) {
- // Game over
- mGameOver = true;
- break;
- }
- }
- } else {
- // Game over handling (reset the game after some time, display game over message, etc.)
- resetGame();
- }
- }
- void Game::render() {
- mWindow.clear();
- if (!mGameOver) {
- mWindow.draw(mWall); // Draw walls
- mPacman.draw(mWindow); // Draw Pacman
- for (int i = 0; i < 4; ++i) {
- mGhosts[i].draw(mWindow); // Draw ghosts
- }
- } else {
- // Render game over message
- }
- mWindow.display();
- }
- bool Game::checkCollision(const sf::FloatRect& rect1, const sf::FloatRect& rect2) {
- return rect1.intersects(rect2);
- }
- void Game::resetGame() {
- // Reset game state (score, positions, etc.)
- mScore = 0;
- mPacman.resetPosition();
- for (int i = 0; i < 4; ++i) {
- mGhosts[i].resetPosition();
- }
- mGameOver = false;
- }
- int main() {
- Game game;
- game.run();
- return 0;
- }
[text] Пакман
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: