- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- 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;
- const int GHOST_SPEED = 3;
- class Pacman {
- public:
- Pacman(sf::Texture& texture);
- void move(const sf::Vector2f& direction);
- void draw(sf::RenderWindow& window);
- sf::FloatRect getBounds() const;
- void setPosition(const sf::Vector2f& position);
- private:
- sf::Sprite sprite;
- };
- Pacman::Pacman(sf::Texture& texture) {
- sprite.setTexture(texture);
- }
- void Pacman::move(const sf::Vector2f& direction) {
- sprite.move(direction);
- }
- void Pacman::draw(sf::RenderWindow& window) {
- window.draw(sprite);
- }
- sf::FloatRect Pacman::getBounds() const {
- return sprite.getGlobalBounds();
- }
- void Pacman::setPosition(const sf::Vector2f& position) {
- sprite.setPosition(position);
- }
- class Ghost {
- public:
- Ghost(sf::Texture& texture);
- void move(const sf::Vector2f& direction);
- void draw(sf::RenderWindow& window);
- sf::FloatRect getBounds() const;
- void setPosition(const sf::Vector2f& position);
- private:
- sf::Sprite sprite;
- };
- Ghost::Ghost(sf::Texture& texture) {
- sprite.setTexture(texture);
- }
- void Ghost::move(const sf::Vector2f& direction) {
- sprite.move(direction);
- }
- void Ghost::draw(sf::RenderWindow& window) {
- window.draw(sprite);
- }
- sf::FloatRect Ghost::getBounds() const {
- return sprite.getGlobalBounds();
- }
- void Ghost::setPosition(const sf::Vector2f& position) {
- sprite.setPosition(position);
- }
- int main() {
- srand(static_cast<unsigned int>(time(NULL)));
- sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Pacman");
- sf::Texture pacmanTexture;
- pacmanTexture.loadFromFile("pacman.png");
- Pacman pacman(pacmanTexture);
- pacman.setPosition(sf::Vector2f(100, 100));
- sf::Texture ghostTexture;
- ghostTexture.loadFromFile("ghost.png");
- Ghost ghost(ghostTexture);
- ghost.setPosition(sf::Vector2f(200, 200));
- sf::Clock clock;
- int score = 0;
- while (window.isOpen()) {
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed) {
- window.close();
- }
- }
- sf::Time deltaTime = clock.restart();
- float dt = deltaTime.asSeconds();
- // Pacman movement
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
- pacman.move(sf::Vector2f(-PACMAN_SPEED * dt, 0));
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
- pacman.move(sf::Vector2f(PACMAN_SPEED * dt, 0));
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
- pacman.move(sf::Vector2f(0, -PACMAN_SPEED * dt));
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
- pacman.move(sf::Vector2f(0, PACMAN_SPEED * dt));
- // Ghost movement (random)
- int dir = rand() % 4;
- sf::Vector2f ghostDir;
- switch (dir) {
- case 0: // Up
- ghostDir = sf::Vector2f(0, -GHOST_SPEED * dt);
- break;
- case 1: // Down
- ghostDir = sf::Vector2f(0, GHOST_SPEED * dt);
- break;
- case 2: // Left
- ghostDir = sf::Vector2f(-GHOST_SPEED * dt, 0);
- break;
- case 3: // Right
- ghostDir = sf::Vector2f(GHOST_SPEED * dt, 0);
- break;
- }
- ghost.move(ghostDir);
- // Collision detection
- if (pacman.getBounds().intersects(ghost.getBounds())) {
- std::cout << "Game Over! Final Score: " << score << std::endl;
- window.close();
- }
- // Rendering
- window.clear();
- pacman.draw(window);
- ghost.draw(window);
- window.display();
- }
- 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:
File Description
- Pac
- Paste Code
- 17 Apr-2024
- 4 Kb
You can Share it:
Latest Code Pastes