From ecd65b96d191619204a46515d99032b2657c9e5c Mon Sep 17 00:00:00 2001 From: ahmed-debbech Date: Mon, 9 Dec 2019 23:52:43 +0100 Subject: [PATCH 1/2] Fix build issue --- snake.pro | 1 - 1 file changed, 1 deletion(-) diff --git a/snake.pro b/snake.pro index 27d3f7d..0a1d75a 100644 --- a/snake.pro +++ b/snake.pro @@ -29,4 +29,3 @@ HEADERS += mainwindow.h \ RESOURCES += \ res.qrc -RC_FILE += myapp.rc From 5e5bd278d482c4086f8476b3b7d2ee13b1ebb01b Mon Sep 17 00:00:00 2001 From: ahmed-debbech Date: Tue, 10 Dec 2019 19:31:48 +0100 Subject: [PATCH 2/2] Fixing the speed issue --- gamecontroller.cpp | 23 +++++++++++++++++------ gamecontroller.h | 11 ++++++----- mainwindow.cpp | 7 ++++--- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/gamecontroller.cpp b/gamecontroller.cpp index 76c3c90..837cc81 100644 --- a/gamecontroller.cpp +++ b/gamecontroller.cpp @@ -2,10 +2,13 @@ #include #include #include +#include +#include #include "gamecontroller.h" #include "food.h" #include "snake.h" +#include "mainwindow.h" GameController::GameController(QGraphicsScene &scene, QObject *parent) : QObject(parent), @@ -20,8 +23,9 @@ GameController::GameController(QGraphicsScene &scene, QObject *parent) : scene.addItem(snake); scene.installEventFilter(this); - - resume(); + //resume(); + connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance())); + isPause = false; } GameController::~GameController() @@ -106,15 +110,22 @@ void GameController::pause() disconnect(&timer, SIGNAL(timeout()), &scene, SLOT(advance())); isPause = true; + setResume(); } void GameController::resume() { - connect(&timer, SIGNAL(timeout()), - &scene, SLOT(advance())); - isPause = false; + connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance())); + isPause = false; + setResume(); +} +void GameController :: setResume(){ + if(isPause == true){ + resumeAction->setEnabled(true); + }else{ + resumeAction->setEnabled(false); + } } - bool GameController::eventFilter(QObject *object, QEvent *event) { if (event->type() == QEvent::KeyPress) { diff --git a/gamecontroller.h b/gamecontroller.h index 2a54835..4c00deb 100644 --- a/gamecontroller.h +++ b/gamecontroller.h @@ -3,7 +3,8 @@ #include #include - +#include +#include "mainwindow.h" class QGraphicsScene; class QKeyEvent; @@ -21,22 +22,22 @@ class GameController : public QObject void snakeAteFood(Food *food); // void snakeHitWall(Snake *snake, Wall *wall); void snakeAteItself(); - + QAction *getResmueAction(){ return resumeAction;} + void setResumeAction(QAction* r){ resumeAction = r; } public slots: void pause(); void resume(); void gameOver(); - protected: bool eventFilter(QObject *object, QEvent *event); private: void handleKeyPressed(QKeyEvent *event); void addNewFood(); - + void setResume(); + QAction * resumeAction; QTimer timer; QGraphicsScene &scene; - Snake *snake; bool isPause; }; diff --git a/mainwindow.cpp b/mainwindow.cpp index 80d612b..3ca4a97 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -23,7 +23,6 @@ MainWindow::MainWindow(QWidget *parent) createActions(); createMenus(); - initScene(); initSceneBackground(); @@ -58,7 +57,9 @@ void MainWindow::createActions() resumeAction = new QAction(tr("&Resume"), this); resumeAction->setStatusTip(tr("Resume...")); - connect(resumeAction, &QAction::triggered, game, &GameController::resume); + resumeAction->setEnabled(false); + game->setResumeAction(resumeAction); + connect(resumeAction, &QAction::triggered, game, &GameController::resume); gameHelpAction = new QAction(tr("Game &Help"), this); gameHelpAction->setShortcut(tr("Ctrl+H")); @@ -121,4 +122,4 @@ void MainWindow::gameHelp() { QMessageBox::about(this, tr("Game Help"), tr("Using direction keys to control the snake to eat the food" "

Space - pause & resume")); -} \ No newline at end of file +}