-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
174 lines (148 loc) · 3.92 KB
/
game.cpp
File metadata and controls
174 lines (148 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "game.h"
#include <QGraphicsScene>
#include "tower.h"
#include "bullet.h"
#include "enemy.h"
#include "buildFirsttowericon.h"
#include "Firsttower.h"
#include "buildsecondtowericon.h"
#include "buildthirdtowericon.h"
#include "QGraphicsLineItem"
#include "score.h"
#include "header.h"
#include "qdebug.h"
#include <QBrush>
#include <QImage>
Game::Game():QGraphicsView()
{
// create a scene
scene = new QGraphicsScene(this);
scene->setSceneRect(0,0,800,600);
//set background
scene->setBackgroundBrush(QBrush(QImage(":/images/background.jpg")));
//set scene
setScene(scene);
//set cursor
cursor = nullptr;
build = nullptr;
setMouseTracking(true);
//change window
setFixedSize(800,600);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
//create a enemy
spawnTimer = new QTimer(this);
enemySpawned = 0;
maxNumberOfEnemies = 0;
pointsToFollow << QPointF(800,0) << QPointF(450,450) <<
QPointF(0,610);
// pointsToFollow2 << QPointF(300,0) << QPointF(350,350) <<
// QPointF(800,610);
createEnemies(5);
//create road
createRoad();
//test code
ft = new BuildFirstTowerIcon();
st = new BuildSecondTowerIcon();
tt = new BuildThirdTowerIcon();
st->setPos(x(),y()+100);
tt->setPos(x(),y()+200);
scene->addItem(ft);
scene->addItem(st);
scene->addItem(tt);
//score and health
score = new Score();
score->setPos(x()+100,y()+15);
scene->addItem(score);
health = new Health();
health->setPos(x()+100,y()+40);
scene->addItem(health);
}
void Game::setCursor(QString filename)
{
if(cursor){
scene->removeItem(cursor);
delete cursor;
}
cursor = new QGraphicsPixmapItem();
cursor->setPixmap(QPixmap(filename));
scene->addItem(cursor);
}
void Game::mouseMoveEvent(QMouseEvent *event)
{
if(cursor){
cursor->setPos(event->pos());
}
}
void Game::mousePressEvent(QMouseEvent *event)
{
if(build){
scene->addItem(build);
build->setPos(event->pos());
cursor = nullptr;
build = nullptr;
//only allow 1 towers
if(ft->count==1)
{
scene->removeItem(ft);
}
if(st->count == 1)
{
scene->removeItem(st);
}
if(tt->count==1)
{
scene->removeItem(tt);
}
}
else{
QGraphicsView::mousePressEvent(event);
}
}
void Game::createEnemies(int numberofEnemies)
{
enemySpawned = 0;
maxNumberOfEnemies = numberofEnemies;
connect(spawnTimer,SIGNAL(timeout()),this,SLOT(spawnEnemy()));
spawnTimer->start(500);
}
void Game::createRoad()
{
for (size_t i = 0, n = pointsToFollow.size()-1; i < n; i++)
{
//create line
QLineF line(pointsToFollow[i],pointsToFollow[i+1]);
QGraphicsLineItem *lineItem = new QGraphicsLineItem(line);
//widht and color
QPen pen;
pen.setWidth(5);
pen.setColor(Qt::darkCyan);
//set pend and add to scene
lineItem->setPen(pen);
scene->addItem(lineItem);
// //points2
// QLineF line1(pointsToFollow2[i],pointsToFollow2[i+1]);
// QGraphicsLineItem *lineItem1 = new QGraphicsLineItem(line1);
// //widht and color
// QPen pen1;
// pen1.setWidth(5);
// pen1.setColor(Qt::darkGray);
// //set pend and add to scene
// lineItem1->setPen(pen1);
// scene->addItem(lineItem1);
}
}
void Game::spawnEnemy()
{
//spwan an enemy
Enemy *enemy = new Enemy(pointsToFollow);
// Enemy *enemy1 = new Enemy(pointsToFollow2);
// enemy1->setPos(pointsToFollow2[0]);
enemy->setPos(pointsToFollow[0]);
scene->addItem(enemy);
//scene->addItem(enemy1);
enemySpawned+=1;
if(enemySpawned >= maxNumberOfEnemies){
spawnTimer->start(500);
}
}