-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
208 lines (159 loc) · 5.21 KB
/
Game.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "Game.h"
#include "Button.h"
#include <QTimer>
#include <QGraphicsTextItem>
#include <QFont>
#include <QBrush>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QImage>
#include <QString>
#include <stdlib.h>
#include <QMessageBox>
#include <QDebug>
Game::Game(QWidget *parent)
{
// create the scene
scene = new QGraphicsScene();
scene->setSceneRect(0,0,1200,1000); // make the scene 800x600 instead of infinity by infinity (default)
setBackgroundBrush(QBrush(QImage(":/images/floor.jpg")));
// make the newly created scene the scene to visualize (since Game is a QGraphicsView Widget,
// it can be used to visualize scenes)
setScene(scene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(1200,1000);
//create a file to save the score
QFile file("myFile.txt");
if(!file.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox::warning(this,"title","file not open");
}
//initialize a random variable between 0-10
/*
random = rand() % 20;
for(int i = 0; i<random;i++)
{
dirts.push_back(new Dirt());
scene->addItem(dirts[i]);
}
*/
//create a dirt and add to the scene
dirt = new Dirt();
scene->addItem(dirt);
//create the score for first robot
score = new Score();
scene->addItem(score);
dirt1 = new Dirt();
scene->addItem(dirt1);
//create the score for the romba robot
rombaScore = new Score();
rombaScore->setDefaultTextColor(Qt::darkRed);
rombaScore->setPos(rombaScore->x()+930,rombaScore->y());
rombaScore->setFont(QFont("Helvetica",15));
scene->addItem(rombaScore);
dirt2 = new Dirt();
scene->addItem(dirt2);
//create the health
health = new Health();
health->setPos(health->x()+500,health->y());
scene->addItem(health);
dirt3 = new Dirt();
scene->addItem(dirt3);
//create the robot in the scene
robot = new Robot();
robot->setPos(0,60);
//make the robot focusable
//robot->setFlag(QGraphicsItem::ItemIsFocusable);
//robot->setFocus();
// add the robot to the scene
scene->addItem(robot);
//add romba to the scene
romba = new Romba();
romba->setPos(1100,900);
romba->setFlag(QGraphicsItem::ItemIsFocusable);
romba->setFocus();
scene->addItem(romba);
show();
}
void Game::gameOver()
{
robot->setVisible(false);
romba->setVisible(false);
QString message;
//count
if(health->getHealth() == 0)
{
message = "Robots have been destroyed!";
writeToFile(message);
}
else if(rombaScore->getScore() > score->getScore())
{
message = "Red robot has won!";
writeToFile( message + "\n" + QString("Red Robot score: ") + QString::number(rombaScore->getScore()));
}
else if(score->getScore() > rombaScore->getScore())
{
message = "Black robot has won!";
writeToFile( message + "\n" + QString("Black Robot score: ") + QString::number(score->getScore()));
}
else
{
message = "Tie game!";
writeToFile(message);
}
displayGameOverWindow(message);
}
void Game::displayGameOverWindow(QString textToDisplay)
{
//disable all scene items
for(size_t i = 0, n = scene->items().size(); i < n; i++)
{
scene->items()[i]->setEnabled(false);
}
//pop up semi transparent panel
drawPanel(0,0,1200,1000,Qt::black,0.65);
//draw panel
drawPanel(400,300,400,400,Qt::lightGray,0.85);
//quit button
Button *quit = new Button(QString("Quit"));
quit->setPos(500,575);
scene->addItem(quit);
connect(quit,SIGNAL(clicked()),this,SLOT(close()));
QGraphicsTextItem *overText = new QGraphicsTextItem(textToDisplay);
overText->setPos(430,350);
scene->addItem(overText);
}
void Game::writeToFile(QString textToWrite)
{
//create a file to save the score
try
{
QFile file("C:\\Users\\latif\\OneDrive - Borough of Manhattan Community College\\Desktop\\Spring 2020\\CSC 211H\\QT Projects\\romba\\myFile.txt");
if(!file.open(QFile::WriteOnly | QFile::Text))
{
QMessageBox::warning(this,"title","file not open");
throw QString("Could not open the file");
}
QTextStream out(&file);
out << textToWrite << endl;
file.flush();
file.close();
} catch (QString s)
{
drawPanel(400,300,400,100,Qt::lightGray,0.65);
QGraphicsTextItem *displayMessage = new QGraphicsTextItem(s);
displayMessage->setPos(420,320);
scene->addItem(displayMessage);
}
}
void Game::drawPanel(int x, int y, int width, int height, QColor color, double opacity){
// draws a panel at the specified location with the specified properties
QGraphicsRectItem* panel = new QGraphicsRectItem(x,y,width,height);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(color);
panel->setBrush(brush);
panel->setOpacity(opacity);
scene->addItem(panel);
}