diff --git a/CMakeLists.txt b/CMakeLists.txt index 01cfa48..b256c7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(PROJECT_SOURCES tower.h tower.cpp game.h game.cpp res.qrc + bullet.h bullet.cpp ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) diff --git a/bullet.cpp b/bullet.cpp new file mode 100644 index 0000000..0e2f9c1 --- /dev/null +++ b/bullet.cpp @@ -0,0 +1,20 @@ +#include "bullet.h" +#include +#include + +Bullet::Bullet(QGraphicsItem *parent) { + // set graphics + setPixmap(QPixmap(":/images/missile.png")); + QTimer *move_timer = new QTimer(this); + connect(move_timer, SIGNAL(timeout()), this, SLOT(move())); + move_timer->start(50); +} + +void Bullet::move() { + int STEP_SIZE = 30; + int theta = rotation(); // degrees + double dy = STEP_SIZE * qSin(qDegreesToRadians(theta)); + double dx = STEP_SIZE * qCos(qDegreesToRadians(theta)); + + setPos(x()+dx, y()+dy); +} diff --git a/bullet.h b/bullet.h new file mode 100644 index 0000000..5b79218 --- /dev/null +++ b/bullet.h @@ -0,0 +1,14 @@ +#ifndef BULLET_H +#define BULLET_H +#include +#include + +class Bullet: public QObject, public QGraphicsPixmapItem { + Q_OBJECT +public: + Bullet(QGraphicsItem *parent=0); +public slots: + void move(); +}; + +#endif // BULLET_H diff --git a/game.cpp b/game.cpp index 3c10417..d60b66e 100644 --- a/game.cpp +++ b/game.cpp @@ -1,10 +1,11 @@ #include "game.h" #include #include "tower.h" +#include "bullet.h" Game::Game() { scene = new QGraphicsScene(this); - + scene->setSceneRect(0,0,800,600); setScene(scene); // create a tower @@ -14,4 +15,14 @@ Game::Game() { scene->addItem(t); setFixedSize(800,600); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); +} + +void Game::mousePressEvent(QMouseEvent *event) { + // create a bullet + Bullet *bullet = new Bullet(); + bullet->setPos(event->pos()); + bullet->setRotation(40); + scene->addItem(bullet); } diff --git a/game.h b/game.h index 561bebe..c95f295 100644 --- a/game.h +++ b/game.h @@ -2,11 +2,13 @@ #define GAME_H #include +#include class Game : public QGraphicsView { public: Game(); + void mousePressEvent(QMouseEvent *event); QGraphicsScene *scene; }; diff --git a/missile.png b/missile.png new file mode 100644 index 0000000..8ce58ae Binary files /dev/null and b/missile.png differ diff --git a/res.qrc b/res.qrc index 45bf7a6..6ead141 100644 --- a/res.qrc +++ b/res.qrc @@ -1,5 +1,7 @@ tower.png + missile.png +