Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing high speed movements after clicking resume option #6

Merged
merged 2 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions gamecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
#include <QGraphicsScene>
#include <QKeyEvent>
#include <QMessageBox>
#include <QAction>
#include <iostream>

#include "gamecontroller.h"
#include "food.h"
#include "snake.h"
#include "mainwindow.h"

GameController::GameController(QGraphicsScene &scene, QObject *parent) :
QObject(parent),
Expand All @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions gamecontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

#include <QObject>
#include <QTimer>

#include <QAction>
#include "mainwindow.h"
class QGraphicsScene;
class QKeyEvent;

Expand All @@ -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;
};
Expand Down
7 changes: 4 additions & 3 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ MainWindow::MainWindow(QWidget *parent)

createActions();
createMenus();

initScene();
initSceneBackground();

Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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"
"<p>Space - pause & resume"));
}
}
1 change: 0 additions & 1 deletion snake.pro
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ HEADERS += mainwindow.h \
RESOURCES += \
res.qrc

RC_FILE += myapp.rc