-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathView.cpp
154 lines (120 loc) · 4.55 KB
/
View.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
#include "View.h"
View::View()
{
//create viewController
viewController = new Controller();
//connect view and controller
connect(viewController, SIGNAL(endRound(int )), this, SLOT(endRoundSlot(int )));
connect(this , SIGNAL(gameStart()) , viewController , SLOT(addBoard()));
// create scene
setScene(viewController->scene);
// set background image
setBackgroundBrush(QBrush(QImage(":/images/homePage.png")));
// create home page buttons
startGameButton = new Button();
startGameButton->setPixmap(QPixmap(":/images/startButton.png"));
startGameButton->setPos(381 , 496);
exitGameButton = new Button();
exitGameButton->setPixmap(QPixmap(":/images/exitButton.png"));
exitGameButton->setPos(381 , 550);
// add button to the scene
viewController->scene->addItem(startGameButton);
viewController->scene->addItem(exitGameButton);
// connect buttons to view slots
connect(startGameButton , SIGNAL(buttonClicked()) , this , SLOT(startGame()) );
connect(exitGameButton , SIGNAL(buttonClicked()) , this , SLOT(exitGame()) );
// set fixed size
setFixedSize(800,600);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// set home page's background music
viewPlayer1 = new QMediaPlayer();
viewPlayer1->setMedia(QUrl("qrc:/musics/MainMenuPvZ1.mp3"));
viewPlayer1->play();
// initialize seconds to zero
seconds = 0;
//set line
line = (rand() % viewController->level) + 1;
}
View::~View() {
delete viewTimer;
delete viewController;
delete viewPlayer1;
}
void View::schedule(){
++seconds;
if (viewController->getRound() == 1) {
setBackgroundBrush(QBrush(QImage(":/images/bgstepone.png")));
if(seconds == 50 || seconds == 54 || seconds == 57 || seconds == 59 || seconds == 60){
viewController->addZombie(5 , 4, 1);
}
}
else if (viewController->getRound() == 2) {
setBackgroundBrush(QBrush(QImage(":/images/bgsteptwo.png")));
if( line == 1){
if(seconds == 45 || seconds == 48 ||seconds == 50 ||seconds == 51 ||seconds == 52){
viewController->addZombie(5 , 4, line);
}
if(seconds == 48 || seconds == 49 ||seconds == 50 ||seconds == 51 ||seconds == 52){
viewController->addZombie(5 , 4, line + 1);
}
}
if(line == 2){
if(seconds == 45 || seconds == 48 ||seconds == 50 ||seconds == 51 ||seconds == 52){
viewController->addZombie(5 , 4, line);
}
if(seconds == 48 || seconds == 49 ||seconds == 50 ||seconds == 51 ||seconds == 52){
viewController->addZombie(5 , 4, line - 1);
}
}
}
else if (viewController->getRound() == 3){
setBackgroundBrush(QBrush(QImage(":/images/bgstepthree.png")));
if(seconds == 40 || seconds == 47 ||seconds == 48 ||seconds == 50 ||seconds == 53){
viewController->addMasterZombie(5 , 10, 1);
}
if(seconds == 44 || seconds == 47 ||seconds == 50 ||seconds == 51 ||seconds == 53){
viewController->addMasterZombie(5 , 10, 2);
}
if(seconds == 46 || seconds == 48 ||seconds == 50 ||seconds == 51 ||seconds == 53){
viewController->addMasterZombie(5 , 10, 3);
}
}
// add sun every 2 seconds
if(seconds % 2 == 0){
viewController->addSun();
}
}
void View::startGame()
{
// stop home page's music
viewPlayer1->stop();
// remove button
viewController->scene->removeItem(startGameButton);
exitGameButton->setPos(695,0);
delete startGameButton;
// set background image
setBackgroundBrush(QBrush(QImage(":/images/bgstepone.png")));
// set background music
viewPlayer2 = new QMediaPlayer();
viewPlayer2->setMedia(QUrl("qrc:/musics/GrasswalkPvZ1.mp3"));
viewPlayer2->play();
// start timer
viewTimer = new QTimer();
viewTimer->start(1000);
connect(viewTimer , SIGNAL(timeout()) , this , SLOT(schedule()));
//connect(viewTimer , SIGNAL(timeout()) , viewController , SLOT(addBoard()));
emit gameStart();
}
void View::exitGame(){
exit(0);
}
void View::endRoundSlot(int level)
{
Controller* viewControl = viewController;
viewController = new Controller( level );
setScene(viewController->scene);
connect(viewController, SIGNAL(endRound(int )), this, SLOT(endRoundSlot(int )));
connect(this , SIGNAL(gameStart()) , viewController , SLOT(addBoard()));
emit gameStart();
}