- #include <SFML/Graphics.hpp>
- class Pacman {
- public:
- Pacman();
- void setPosition(const sf::Vector2f& position);
- void move(const sf::Vector2f& offset);
- sf::FloatRect getBounds() const;
- void draw(sf::RenderWindow& window);
- private:
- sf::CircleShape mShape;
- };
- Pacman::Pacman()
- : mShape(20.f) { // Set the radius of Pacman
- mShape.setFillColor(sf::Color::Yellow);
- }
- void Pacman::setPosition(const sf::Vector2f& position) {
- mShape.setPosition(position);
- }
- void Pacman::move(const sf::Vector2f& offset) {
- mShape.move(offset);
- }
- sf::FloatRect Pacman::getBounds() const {
- return mShape.getGlobalBounds();
- }
- void Pacman::draw(sf::RenderWindow& window) {
- window.draw(mShape);
- }
- class Ghost {
- public:
- Ghost(const sf::Vector2f& position);
- void setPosition(const sf::Vector2f& position);
- void move(const sf::Vector2f& offset);
- sf::FloatRect getBounds() const;
- void draw(sf::RenderWindow& window);
- private:
- sf::CircleShape mShape;
- };
- Ghost::Ghost(const sf::Vector2f& position)
- : mShape(20.f) { // Set the radius of the ghost
- mShape.setPosition(position);
- mShape.setFillColor(sf::Color::Red);
- }
- void Ghost::setPosition(const sf::Vector2f& position) {
- mShape.setPosition(position);
- }
- void Ghost::move(const sf::Vector2f& offset) {
- mShape.move(offset);
- }
- sf::FloatRect Ghost::getBounds() const {
- return mShape.getGlobalBounds();
- }
- void Ghost::draw(sf::RenderWindow& window) {
- window.draw(mShape);
- }
- 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) {
- // Initialize Pacman and Ghosts
- mPacman.setPosition(sf::Vector2f(100.f, 100.f));
- 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));
- // Initialize wall
- mWall.setSize(sf::Vector2f(40.f, 40.f));
- mWall.setFillColor(sf::Color::Blue);
- }
- void Game::run() {
- while (mWindow.isOpen()) {
- if (mInMenu) {
- // Handle menu
- } 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() {
- // Move Pacman and Ghosts (demo)
- 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)
- // For now, let's just stop Pacman
- mPacman.move(sf::Vector2f(0.f, 0.f));
- }
- // Ghost-Pacman collision detection
- for (int i = 0; i < 4; ++i) {
- if (checkCollision(mPacman.getBounds(), mGhosts[i].getBounds())) {
- // Game over
- mGameOver = true;
- break;
- }
- }
- }
- void Game::render() {
- mWindow.clear();
- mWindow.draw(mWall); // Draw walls
- mPacman.draw(mWindow); // Draw Pacman
- for (int i = 0; i < 4; ++i) {
- mGhosts[i].draw(mWindow); // Draw ghosts
- }
- mWindow.display();
- }
- bool Game::checkCollision(const sf::FloatRect& rect1, const sf::FloatRect& rect2) {
- return rect1.intersects(rect2);
- }
- void Game::resetGame() {
- // Reset game state
- }
- int main() {
- Game game;
- game.run();
- return 0;
- }
[text] Pac
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: