- #include <SFML/Graphics.hpp>
- #include <iostream>
- const int TILE_SIZE = 40;
- const int MAP_WIDTH = 15;
- const int MAP_HEIGHT = 15;
- const int WINDOW_WIDTH = TILE_SIZE * MAP_WIDTH;
- const int WINDOW_HEIGHT = TILE_SIZE * MAP_HEIGHT;
- const int PACMAN_SPEED = 4;
- enum class Direction { Up, Down, Left, Right };
- class Pacman {
- public:
- Pacman();
- void move(Direction dir);
- void draw(sf::RenderWindow& window);
- sf::FloatRect getBounds() const;
- private:
- sf::Texture texture;
- sf::Sprite sprite;
- Direction currentDirection;
- };
- Pacman::Pacman() {
- texture.loadFromFile("pacman.png");
- sprite.setTexture(texture);
- sprite.setPosition(100, 100);
- currentDirection = Direction::Right; // Pacman starts moving right by default
- }
- void Pacman::move(Direction dir) {
- currentDirection = dir;
- switch (dir) {
- case Direction::Up:
- sprite.move(0, -PACMAN_SPEED);
- break;
- case Direction::Down:
- sprite.move(0, PACMAN_SPEED);
- break;
- case Direction::Left:
- sprite.move(-PACMAN_SPEED, 0);
- break;
- case Direction::Right:
- sprite.move(PACMAN_SPEED, 0);
- break;
- }
- }
- void Pacman::draw(sf::RenderWindow& window) {
- window.draw(sprite);
- }
- sf::FloatRect Pacman::getBounds() const {
- return sprite.getGlobalBounds();
- }
- int main() {
- sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Pacman");
- Pacman pacman;
- while (window.isOpen()) {
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed) {
- window.close();
- }
- if (event.type == sf::Event::KeyPressed) {
- if (event.key.code == sf::Keyboard::Up)
- pacman.move(Direction::Up);
- else if (event.key.code == sf::Keyboard::Down)
- pacman.move(Direction::Down);
- else if (event.key.code == sf::Keyboard::Left)
- pacman.move(Direction::Left);
- else if (event.key.code == sf::Keyboard::Right)
- pacman.move(Direction::Right);
- }
- }
- window.clear();
- pacman.draw(window);
- window.display();
- }
- return 0;
- }
[text] P
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: