Hello! 
I am working on a game, but I can't get the player to move when the WASD keys are pressed. I even tried making a new project and simply animating a square, but it wouldn't even work for me when I did that!
NOTE: In the following code, I removed all my failed attempts at animating, so the code does run and there are no fatal errors.
My code:
dialog.h
#include "player.h"
#include "zombie.h"
#include <QDialog>
#include <QGraphicsScene>
#include <QPropertyAnimation>
namespace Ui {
class Dialog;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
player *p;
zombie *z;
private:
Ui::Dialog *ui;
};
#include "player.h"
#include "zombie.h"
#include <QDialog>
#include <QGraphicsScene>
#include <QPropertyAnimation>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
QGraphicsView *view;
QGraphicsScene *scene;
player *p;
zombie *z;
void keyPressEvent(QKeyEvent *event);
private:
Ui::Dialog *ui;
};
To copy to clipboard, switch view to plain text mode
player.h
#include <QGraphicsItem>
#include <QPropertyAnimation>
{
public:
player();
void moveUp();
void moveLeft();
void moveDown();
void moveRight();
private slots:
};
#include <QGraphicsItem>
#include <QPropertyAnimation>
class player : public QGraphicsItem
{
public:
player();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget);
void moveUp();
void moveLeft();
void moveDown();
void moveRight();
private slots:
};
To copy to clipboard, switch view to plain text mode
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include "player.h"
#include "zombie.h"
#include <QHBoxLayout>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QSizePolicy>
#include <QPropertyAnimation>
#include <QKeyEvent>
#include <iostream>
#include <QPushButton>
using namespace std;
ui(new Ui::Dialog)
{
ui->setupUi(this);
this->setMinimumSize(1000, 700);
this->setMaximumSize(size());
this->setWindowTitle("Game");
this->setLayout(layout);
view
->setRenderHints
(QPainter::Antialiasing);
view->setScene(scene);
QBrush backgroundBrush
(Qt
::white);
scene->setBackgroundBrush(backgroundBrush);
player *p = new player();
scene->addItem(p);
zombie *z = new zombie();
scene->addItem(z);
layout->addWidget(view);
view->show();
}
Dialog::~Dialog()
{
delete ui;
qDebug("Dialog Deleted");
}
void Dialog
::keyPressEvent(QKeyEvent *event
){ switch(event->key()){
case Qt::Key_W:
qDebug("Key Pressed: W");
p->moveUp();
break;
case Qt::Key_A:
qDebug("Key Pressed: A");
p->moveLeft();
break;
case Qt::Key_S:
qDebug("Key Pressed: S");
p->moveDown();
break;
case Qt::Key_D:
qDebug("Key Pressed: D");
p->moveRight();
break;
}
}
#include "dialog.h"
#include "ui_dialog.h"
#include "player.h"
#include "zombie.h"
#include <QHBoxLayout>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QSizePolicy>
#include <QPropertyAnimation>
#include <QKeyEvent>
#include <iostream>
#include <QPushButton>
using namespace std;
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
this->setMinimumSize(1000, 700);
this->setMaximumSize(size());
this->setWindowTitle("Game");
QHBoxLayout *layout = new QHBoxLayout();
this->setLayout(layout);
QGraphicsView *view = new QGraphicsView();
view->setRenderHints(QPainter::Antialiasing);
QGraphicsScene *scene = new QGraphicsScene();
view->setScene(scene);
QBrush backgroundBrush(Qt::white);
scene->setBackgroundBrush(backgroundBrush);
player *p = new player();
scene->addItem(p);
zombie *z = new zombie();
scene->addItem(z);
layout->addWidget(view);
view->show();
}
Dialog::~Dialog()
{
delete ui;
qDebug("Dialog Deleted");
}
void Dialog::keyPressEvent(QKeyEvent *event){
switch(event->key()){
case Qt::Key_W:
qDebug("Key Pressed: W");
p->moveUp();
break;
case Qt::Key_A:
qDebug("Key Pressed: A");
p->moveLeft();
break;
case Qt::Key_S:
qDebug("Key Pressed: S");
p->moveDown();
break;
case Qt::Key_D:
qDebug("Key Pressed: D");
p->moveRight();
break;
}
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
Dialog w;
w.show();
return a.exec();
}
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
player.cpp
#include "player.h"
#include <QGraphicsItem>
#include <QPainter>
#include <QPropertyAnimation>
#include <QObject>
player::player()
{
qDebug("Player Created");
}
QRectF player
::boundingRect() const {
}
{
painter->setBrush(Qt::red);
painter->setPen(Qt::red);
painter->drawEllipse(rec);
}
void player::moveUp(){
}
void player::moveLeft(){
}
void player::moveDown(){
}
void player::moveRight(){
}
#include "player.h"
#include <QGraphicsItem>
#include <QPainter>
#include <QPropertyAnimation>
#include <QObject>
player::player()
{
setFlag(QGraphicsItem::ItemIsMovable);
qDebug("Player Created");
}
QRectF player::boundingRect() const
{
return QRectF(0, 0, 25, 25);
}
void player::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget)
{
QRectF rec = boundingRect();
painter->setBrush(Qt::red);
painter->setPen(Qt::red);
painter->drawEllipse(rec);
}
void player::moveUp(){
}
void player::moveLeft(){
}
void player::moveDown(){
}
void player::moveRight(){
}
To copy to clipboard, switch view to plain text mode
I really wanted to put the respective animations in the functions moveUp(), moveLeft(), moveDown, moveRight() in the class player. Could you please help me by showing my how to do the animations in these locations.
Also, while I'm here, is there anything I should change to my program while I am not to far into the game?
Thanks 
-David
Bookmarks