Browse Source

Tut 11

master
Georg Spar 2 years ago
parent
commit
209c80ccbb
  1. 4
      CMakeLists.txt
  2. 3
      game.cpp
  3. 4
      main.cpp
  4. 24
      tower.cpp
  5. 8
      tower.h

4
CMakeLists.txt

@ -13,6 +13,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
find_package(Qt6 REQUIRED COMPONENTS Multimedia)
find_package(Qt6 REQUIRED COMPONENTS Gui)
set(PROJECT_SOURCES
main.cpp
@ -46,6 +48,8 @@ else()
endif()
target_link_libraries(TowerDefense PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(TowerDefense PRIVATE Qt6::Multimedia)
target_link_libraries(TowerDefense PRIVATE Qt6::Gui)
set_target_properties(TowerDefense PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com

3
game.cpp

@ -3,13 +3,14 @@
#include "tower.h"
#include "bullet.h"
Game::Game() {
Game::Game(): QGraphicsView() {
scene = new QGraphicsScene(this);
scene->setSceneRect(0,0,800,600);
setScene(scene);
// create a tower
Tower *t = new Tower();
t->setPos(250,250);
// add tower
scene->addItem(t);

4
main.cpp

@ -2,13 +2,13 @@
#include <QApplication>
Game *game;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Game *game = new Game();
game = new Game();
game->show();
return a.exec();

24
tower.cpp

@ -2,8 +2,13 @@
#include <QPixmap>
#include <QVector>
#include <QPointF>
#include "bullet.h"
#include <QTimer>
#include "game.h"
Tower::Tower(QGraphicsItem *parent) {
extern Game *game;
Tower::Tower(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent) {
setPixmap(QPixmap(":/images/tower.png"));
// create points vectors
@ -30,4 +35,21 @@ Tower::Tower(QGraphicsItem *parent) {
QLineF ln(poly_center, tower_center);
attack_area->setPos(x()+ln.dx(), y()+ln.dy());
// connect timer to attack target
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this,SLOT(attack_target()));
timer->start(1000);
attack_dest = QPointF(800,0);
}
void Tower::attack_target() {
Bullet *bullet = new Bullet();
bullet->setPos(x()+34, y()+64);
QLineF ln(QPointF(x()+34, y()+64), attack_dest);
int angle = -1 * ln.angle();
bullet->setRotation(angle);
game->scene->addItem(bullet);
}

8
tower.h

@ -3,12 +3,18 @@
#include <QGraphicsPixmapItem>
#include <QGraphicsPolygonItem>
#include <QPointF>
#include <QObject>
class Tower : public QGraphicsPixmapItem {
class Tower : public QObject, public QGraphicsPixmapItem {
Q_OBJECT
public:
Tower(QGraphicsItem *parent=0);
public slots:
void attack_target();
private:
QGraphicsPolygonItem *attack_area;
QPointF attack_dest;
};
#endif // TOWER_H

Loading…
Cancel
Save