{"id":17923943,"url":"https://github.com/blueskyson/qt-pac-man","last_synced_at":"2025-03-24T02:33:24.443Z","repository":{"id":45235170,"uuid":"208532465","full_name":"blueskyson/Qt-pac-man","owner":"blueskyson","description":"make a pacman game with Qt","archived":false,"fork":false,"pushed_at":"2021-12-29T11:23:02.000Z","size":69,"stargazers_count":14,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-19T02:38:26.998Z","etag":null,"topics":["cpp","pacman-game","qt"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/blueskyson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-15T02:44:43.000Z","updated_at":"2024-06-21T10:26:33.000Z","dependencies_parsed_at":"2022-08-04T13:00:15.508Z","dependency_job_id":null,"html_url":"https://github.com/blueskyson/Qt-pac-man","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blueskyson%2FQt-pac-man","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blueskyson%2FQt-pac-man/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blueskyson%2FQt-pac-man/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blueskyson%2FQt-pac-man/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blueskyson","download_url":"https://codeload.github.com/blueskyson/Qt-pac-man/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245198954,"owners_count":20576474,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","pacman-game","qt"],"created_at":"2024-10-28T20:46:21.472Z","updated_at":"2025-03-24T02:33:24.130Z","avatar_url":"https://github.com/blueskyson.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Qt pacman\n\nA pacman game made with Qt Creator.  \n\nPlay with w, a, s, d keys.\n\n![](./screenshots/1.png)\n\n## Qt spec\n\nThis is developed on Qt 6.2.1. If you want to develop under Qt 6, you may need to uncomment some lines in `Qt-pac-man.pro` file.\n\n**Name:** Qt 6.2.1 GCC 64bit  \n**ABI:** x86-linux-generic-elf-64bit  \n**Source:** /opt/Qt/6.2.1/gcc_64  \n**mkspec:** linux-g++  \n\n## How to call pacman game API\n\nYou can take reference from [mainwindow.cpp](https://github.com/blueskyson/Qt-pac-man/blob/master/source/mainwindow.cpp) and [mainwindow.h](https://github.com/blueskyson/Qt-pac-man/blob/master/source/mainwindow.h) for detailed code. Assume that the following steps are implemented in a `QMainWindow`.\n\n### Step 1: declare objects and functions  \n- Add a `QGraphicsView` named `graphicsView` for displaying game. (In the source code, `graphicsView` is defined in [mainwindow.ui](https://github.com/blueskyson/Qt-pac-man/blob/master/mainwindow.ui).)  \n- Add a `QLabel` named `score` for displaying score.  \n- Add two `QLabel`s named `win_label` and `lose_label` for displaying when game is over.  \n- Add a `QTimer` named `score_timer` for updating current score.  \n- Declare `keyPressEvent` override function.  \n- Declare `update_score` slot function.  \n- Include [game.h](https://github.com/blueskyson/Qt-pac-man/blob/master/source/game.h).\n\n```cpp\n#include \u003cQMainWindow\u003e\n#include \u003cQLabel\u003e\n#include \u003cQKeyEvent\u003e\n#include \u003cQTimer\u003e\n#include \"game.h\"\n\nQT_BEGIN_NAMESPACE\nnamespace Ui { class MainWindow; }\nQT_END_NAMESPACE\n\nclass MainWindow : public QMainWindow\n{\n    Q_OBJECT\n\npublic:\n    MainWindow(QWidget *parent = nullptr);\n    ~MainWindow();\n    void keyPressEvent(QKeyEvent*) override;\n\nprivate slots:\n    void update_score();\n\nprivate:\n    Ui::MainWindow *ui;\n    QLabel *score, *win_label, *lose_label;\n    QTimer *score_timer;\n    Game *game;\n};\n```\n\n### Step 2: initialize objects\n\nSetup `graphicsView` in the constructor of `MainWindow`.\n\n```cpp\n#include \"mainwindow.h\"\n#include \"ui_mainwindow.h\"\n\nMainWindow::MainWindow(QWidget *parent)\n    : QMainWindow(parent)\n    , ui(new Ui::MainWindow)\n{\n    ui-\u003egraphicsView-\u003esetStyleSheet(\"QGraphicsView {border: none;}\");\n    ui-\u003egraphicsView-\u003esetBackgroundBrush(Qt::black);\n    ui-\u003egraphicsView-\u003esetFocusPolicy(Qt::NoFocus);\n```\n\nSet the geometry of `graphicsView` and `game`. Read game map from [map.txt](https://github.com/blueskyson/Qt-pac-man/blob/master/game_objects/map_objects/map.txt).\n\n```cpp\n    int map_height = 20, map_width = 29;            // 20x29 game map\n    int x = 50, y = 50;                             // coordinate in mainwindow\n    int w = (map_width * GameObject::Width);        // width pixel\n    int h = (map_height * GameObject::Width);       // height pixel\n    ui-\u003egraphicsView-\u003esetGeometry(x, y, w, h);\n    game = new Game(x, y, map_width, map_height, \":/game_objects/map_objects/map.txt\");\n    ui-\u003egraphicsView-\u003esetScene(game);\n```\n\nInitialize labels and the timer.\n\n```cpp\n    score = new QLabel(this);\n    win_label = new QLabel(this);\n    lose_label = new QLabel(this);\n    score_timer = new QTimer(this);\n    // setup labels' properties here...\n```\n\nStart timer and game.\n\n```cpp\n    score_timer-\u003estart(25);\n    connect(score_timer, SIGNAL(timeout()), this , SLOT(update_score()));\n    game-\u003estart();\n}\n```\n\n### Step 3: Implement update_score and keyPressEvent\n\nUpdate score when `score_timer` ticks. If the pacman eats all points, `game-\u003estat` will change to `Game::Win`. If a ghost catches the pacman, `game-\u003estat` will change to `Game::Lose`. Stop timer and show `win_label` or `lose_label` when game is over.\n\n```cpp\nvoid MainWindow::update_score()\n{\n    score-\u003esetText(QString::number(game-\u003eget_score()));\n    if (game-\u003estat == Game::Win) {\n        win_label-\u003eshow();\n        score_timer-\u003estop();\n    } else if (game-\u003estat == Game::Lose) {\n        lose_label-\u003eshow();\n        score_timer-\u003estop();\n    }\n}\n```\n\nDetect key press events from w, a, s, d keys and make pacman move.\n\n```cpp\nvoid MainWindow::keyPressEvent(QKeyEvent *e)\n{\n    switch (e-\u003ekey()) {\n    case Qt::Key_W:\n        game-\u003epacman_next_direction(GameObject::Up);\n        break;\n    case Qt::Key_A:\n        game-\u003epacman_next_direction(GameObject::Left);\n        break;\n    case Qt::Key_S:\n        game-\u003epacman_next_direction(GameObject::Down);\n        break;\n    case Qt::Key_D:\n        game-\u003epacman_next_direction(GameObject::Right);\n        break;\n    }\n}\n```\n\n## Changeable game parameters\n\nHere are some changeable macro in [game.h](https://github.com/blueskyson/Qt-pac-man/blob/master/source/game.h).\n\n```cpp\n#define BALL_SCORE       10     // score of balls\n#define POWERBALL_SCORE  30     // score of powerballs\n#define GHOST_SCORE      50     // score of ghosts\n#define INTERVAL         10     // move interval of pacman\n#define NORMAL_INTERVAL  10     // move interval of normal ghosts\n#define PANNIC_INTERVAL  15     // move interval of pannic ghosts\n#define RUNNING_INTERVAL 5      // move interval of running ghosts\n#define PANNIC_TIME      1000   // interval number of pannic ghosts\n#define FLASH_INTERVAL   200    // flash interval of powerballs\n```\n\nHere's a changeable array in [game.cpp](https://github.com/blueskyson/Qt-pac-man/blob/master/source/game.cpp).\n\n```cpp\n// interval number before ghosts going out the cage\nint GHOST_RELEASE_TIME[] = {0, 200, 400, 600};\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblueskyson%2Fqt-pac-man","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblueskyson%2Fqt-pac-man","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblueskyson%2Fqt-pac-man/lists"}