-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.cpp
197 lines (160 loc) · 4.32 KB
/
box.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
#include <stdlib.h>
#include <QTimer>
#include "box.h"
#include "game.h"
extern Box * BoxArray[9];
extern Box * LivesArray[3];
extern Game * theGame;
static int newValue = 4;
static int oldValue = 4;
Box::Box(QObject *parent) : QObject(parent)
{
}
Box::Box(int x, int y, int width, int heigth)
{
setRect(x,y,width,heigth);
}
void Box::keyPressEvent(QKeyEvent *event)
{ int t;
if(theGame->acceptUserInput == true){
if(event->key() == Qt::Key_7){
t = 0;
selectRandomBox(t);
}
if(event->key() == Qt::Key_8){
t = 1;
selectRandomBox(t);
}
if(event->key() == Qt::Key_9){
t = 2;
selectRandomBox(t);
}
if(event->key() == Qt::Key_4){
t = 3;
selectRandomBox(t);
}
if(event->key() == Qt::Key_5){
t = 4;
selectRandomBox(t);
}
if(event->key() == Qt::Key_6){
t = 5;
selectRandomBox(t);
}
if(event->key() == Qt::Key_1){
t = 6;
selectRandomBox(t);
}
if(event->key() == Qt::Key_2){
t = 7;
selectRandomBox(t);
}
if(event->key() == Qt::Key_3){
t = 8;
selectRandomBox(t);
}
}
}
void Box::selectRandomBox(int tester)
{
//start the timer at first press.
if(theGame->getTimerHasStarted() == false){
theGame->setTimerHasStarted(true);
theGame->connect(theGame->theTimer,SIGNAL(timeout()),theGame,SLOT(timerTestFunction()));
theGame->theTimer->start(10000);
}
if(BoxArray[tester]->getColor()){
if(theGame->getStopCounting() == false)
theGame->increaseScore();
BoxArray[tester]->setBrush(QBrush(Qt::white));
BoxArray[tester]->setColor(false);
oldValue = tester;
}else{
BoxArray[newValue]->setBrush(QBrush(Qt::white));
BoxArray[newValue]->setColor(false);
RemoveLifePoint();
}
if(theGame->acceptUserInput == true){
while(newValue == oldValue)
newValue = rand() % 9;
oldValue = newValue;
if(BoxArray[newValue]->ItemIsFocusable){
BoxArray[newValue]->setFocus();
BoxArray[newValue]->setBrush(QBrush(Qt::red));
BoxArray[newValue]->setColor(true);
}
}
}
void Box::CreateTheArray()
{
int rectWidth = 50;
int rectHeight = 50;
int positionX = 0;
int positionY = 0;
int counter = 0;
for(unsigned int i = 0; i < 9; i++){
Box * testbox = new Box(positionX,positionY,rectWidth,rectHeight);;
BoxArray[i] = testbox;
//make item focusable (making it possible to focus the item)
BoxArray[i]->setFlag(QGraphicsItem::ItemIsFocusable);
if(i == 4){
BoxArray[i]->setFocus();
BoxArray[i]->setColor(true);
BoxArray[i]->setBrush(QBrush(Qt::red));
}else{
BoxArray[i]->setColor(false);
BoxArray[i]->setBrush(QBrush(Qt::white));
}
positionX += 75;
if(counter == 2){
positionY+=75;
positionX = 0;
counter = -1;
}
counter++;
}
}
void Box::CreateLivesArray()
{
int rectWidth = 50;
int rectHeight = 50;
int X = 0;
int Y = 275;
for(int i = 0; i < 3; i++){
Box * thebox = new Box(X,Y,rectWidth,rectHeight);
LivesArray[i] = thebox;
X+=75;
LivesArray[i]->setFlag((QGraphicsItem::ItemIsFocusable));
LivesArray[i]->setBrush(QBrush(Qt::darkGreen));
}
}
void Box::RemoveLifePoint()
{
theGame->setLifePoint(theGame->getLifePoint()-1);
LivesArray[theGame->getLifePoint()]->setBrush(QBrush(Qt::white));
LivesArray[theGame->getLifePoint()]->setColor(false);
if(theGame->getLifePoint() == 0){
BoxArray[newValue]->setColor(true); //new
emit theGame->gameIsOver();
}
}
void Box::AddToScene(QGraphicsScene *thescene, Box * theArray[])
{
for(int i = 0; i < 9; i++){
thescene->addItem(theArray[i]);
}
}
void Box::AddToScene(Box *theArray[], QGraphicsScene *thescene)
{
for(int i = 0; i < 3; i++){
thescene->addItem(theArray[i]);
}
}
bool Box::getColor() const
{
return color;
}
void Box::setColor(bool value)
{
color = value;
}