Skip to content

Instantly share code, notes, and snippets.

@KT-Yeh
Created June 13, 2014 13:42
Show Gist options
  • Save KT-Yeh/be5a121beab30b9cf3a9 to your computer and use it in GitHub Desktop.
Save KT-Yeh/be5a121beab30b9cf3a9 to your computer and use it in GitHub Desktop.
#include "Game.h"
Game::Game(QWidget *parent)
: QWidget(parent)
{
setFixedSize(800, 600);
man = new Man(200, 200);
QObject::connect(man, SIGNAL(PositionChange(int,int)), parent, SLOT(ChangeLCD(int, int)));
timer = new QTimer(this);
timer->start(1000/60); // 60fps
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(RefreshFrame()));
}
void Game::RefreshFrame()
{
man->UpdatePosition();
repaint(); // call paintEvent
}
void Game::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
man->PaintMan(painter);
}
void Game::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Right: man->setVelocity(3, 0); break;
case Qt::Key_Left: man->setVelocity(-3, 0); break;
case Qt::Key_Up: man->setVelocity(0, -3); break;
case Qt::Key_Down: man->setVelocity(0, 3); break;
}
}
void Game::keyReleaseEvent(QKeyEvent *event)
{
man->setVelocity(0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment