-
Notifications
You must be signed in to change notification settings - Fork 0
/
level1.cpp
179 lines (156 loc) · 4.44 KB
/
level1.cpp
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
175
176
177
178
179
#include "level1.h"
#include <QGraphicsScene>
#include <QTimer>
#include <QDebug>
#include "egg.h"
#include "plane.h"
#include "game.h"
#include "lastp.h"
#include "bulletbouncer.h"
extern Game * game;
Level1::Level1() :itemCount(0)
{
timer3 = new QTimer();
connect(timer3, SIGNAL(timeout()), this, SLOT(advance()));
timer3->start(25);
setSceneRect(0,0,1200,700);
plane = new Plane();
plane->setPos((width() / 2) - plane->pixmap().width()/2,
height() - plane->pixmap().height());
addItem(plane);
//adding score
sc = new Score();
addItem(sc);
//adding hearts to scene
for(int i=0; i<3;i++)
{
QGraphicsPixmapItem *h = new QGraphicsPixmapItem();
h->setPixmap(QPixmap(":/img/heart.png"));
h->setScale(.5);
hearts.push_back(h);
addItem(h);
}
hearts[0]->setPos(1100,17);
hearts[1]->setPos(1130,17);
hearts[2]->setPos(1160,17);
//adding missle count
miss = new Missle();
addItem(miss);
//scene points for chickens
points.push_back(QPointF(120,30));
for(int i=1; i<10; i++)
{
points.push_back(QPointF(points[i-1].x()+95,30));
}
points.push_back(QPointF(120,100));
for(int i=1; i<10; i++)
{
points.push_back(QPointF(points[i-1].x()+95,100));
}
points.push_back(QPointF(120,170));
for(int i=1; i<10; i++)
{
points.push_back(QPointF(points[i-1].x()+95,170));
}
points.push_back(QPointF(120,240));
for(int i=1; i<10; i++)
{
points.push_back(QPointF(points[i-1].x()+95,240));
}
QTimer * timer = new QTimer();
connect(timer , SIGNAL(timeout()) , this , SLOT(spawn()));
timer->start(1000);
QTimer * timer2 = new QTimer();
connect(timer2 , SIGNAL(timeout()), this , SLOT(throwEgg()));
timer2->start(2000);
timer3 = new QTimer();
connect(timer3 , SIGNAL(timeout()), this , SLOT(birdCheck()));
timer3->start(2000);
QTimer *timer4 = new QTimer();
connect(timer4 , SIGNAL(timeout()), this , SLOT(killCheck()));
timer4->start(10);
}
void Level1::spawn()
{
if(itemCount > 39)
return;
Bird * bird = new Bird();
birds.push_back(bird);
addItem(bird);
int x = points.begin()->x();
int y = points.begin()->y();
QVector<QPointF>::Iterator it = points.begin();
bird->moveAnim->setStartValue(bird->pos());
bird->moveAnim->setEndValue(QPointF(x,y));
bird->moveAnim->setDuration(2000);
bird->moveAnim->start();
points.erase(it);
itemCount++;
}
void Level1::throwEgg()
{
if(birds.isEmpty())
return;
Egg * egg = new Egg();
int randN = qrand()%(birds.size());
egg->setPos(birds[randN]->x()+27 , birds[randN]->y()+66);
addItem(egg);
}
void Level1::killCheck()
{
if(birds.isEmpty())
return;
static int lastCall =0;
if(plane->chickensKilled % 12 == 0 && plane->chickensKilled != lastCall)
{
bulletBouncer *bb = new bulletBouncer();
int randN = qrand()%birds.size();
bb->setPos(birds.at(randN)->x()+27,birds.at(randN)->y()+66);
addItem(bb);
lastCall = plane->chickensKilled;
}
}
void Level1::birdCheck()
{
if(birds.isEmpty())
{
updateFile();
lastP *lp = new lastP(QString("you won!!"));
game->lastPage = lp;
game->setScene(game->lastPage);
}
}
void Level1::updateFile()
{
QFile file("/Users/alireza/Documents/QT/chickenInvaders-local5/records.txt");
QMultiMap<int,QString> recs;
QTextStream in(&file);
QString line;
QStringList list;
if(file.open(QFile::Text | QFile::ReadOnly))
{
qDebug() << "ENTERED";
while(!in.atEnd())
{
line = in.readLine();
list = line.split(" ");
recs.insert(list.at(1).toInt(),list.at(0));
}
recs.insert(game->lvl->sc->getScore(),game->player);
}
file.close();
//qDebug()<<recs.keys();
//qDebug()<<recs.values();
QTextStream out(&file);
QMultiMap<int,QString>::iterator it = --(recs.end());
if(file.open(QFile::Text | QFile::WriteOnly|QFile::Truncate))
{
while(it != --(recs.begin()))
{
out << it.value()<<" "<<it.key()<<endl;
--it;
}
}
file.close();
game->lvl->timer3->stop();
}