MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), canvasWidget(new CanvasWidget(this)), gameIsPaused(true), thereIsAnotherDialog(false) { Item::setCanvas(canvasWidget); new Background; // the background put's itself into the canvasWidget gameEngine = new GameEngine(this); // must be called after Item::setCanvas() #if defined ANDROID setAttribute(Qt::WA_AcceptTouchEvents); #endif connect(canvasWidget, SIGNAL(mouseMoved(int)), gameEngine, SLOT(moveBar(int))); connect(canvasWidget, SIGNAL(barMovedLeft()), gameEngine, SLOT(moveBarLeft())); connect(canvasWidget, SIGNAL(barMovedRight()), gameEngine, SLOT(moveBarRight())); connect(canvasWidget, SIGNAL(focusLost()), this, SLOT(pauseGame())); connect(gameEngine, SIGNAL(gamePaused()), canvasWidget, SLOT(handleGamePaused())); connect(gameEngine, SIGNAL(gameResumed()), canvasWidget, SLOT(handleGameResumed())); connect(gameEngine, SIGNAL(gameResumed()), this, SLOT(handleGameResumed())); connect(gameEngine, SIGNAL(resetMousePosition()), canvasWidget, SLOT(handleResetMousePosition())); connect(gameEngine, SIGNAL(gameEnded(int,int,int)), SLOT(handleEndedGame(int,int,int))); // cheating keys, debugging and testing only TODO: REMOVE connect(canvasWidget, SIGNAL(cheatSkipLevel()), gameEngine, SLOT(cheatSkipLevel())); connect(canvasWidget, SIGNAL(cheatAddLife()), gameEngine, SLOT(cheatAddLife())); connect(gameEngine, SIGNAL(levelChanged()), this, SLOT(handleLevelChanged())); connect(Settings::self(), SIGNAL(themeChanged()), canvasWidget, SLOT(reloadSprites())); setCentralWidget(canvasWidget); setupActions(); setFocusProxy(canvasWidget); QSize defaultSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // show here (instead of in main) else the mouse can't be grabbed show(); gameEngine->start(Settings::self()->getLevelset()); }
void GameEngine::moveBar(int newPos) { if (gameIsPaused()) { return; } // width of the game int width = BRICK_WIDTH * WIDTH; // width of the bar int w = m_bar.getRect().width(); int y = m_bar.getRect().y(); int x = newPos - w/2; if (x < 0) { x = 0; emit resetMousePosition(); } else if (x > width - w) { x = width - w; emit resetMousePosition(); } m_bar.moveTo(x, y); }