-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameWindow.h
217 lines (211 loc) · 8.92 KB
/
GameWindow.h
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
209
210
211
212
213
214
215
216
217
using namespace std;
#include <string>
#include <SFML/Window.hpp>
#include <iostream>
#include <Graphics/Font.hpp>
#include <Graphics/Text.hpp>
#include <SFML/Graphics.hpp>
#include "Board.h"
class GameWindow{
private:
int numOfCols;
int numOfRows;
int numOfBombs;
string PlayerName;
sf::Font font;
bool gameActive;
bool paused;
bool leftButtonPressed = false;
bool rightButtonPressed = false;
sf::Clock clock;
int minutes = 0;
int seconds = 0;
Timer* timer;
sf::Time totalElapsedTime;
public:
GameWindow(int rows, int cols, int bombs, string name): numOfCols(rows), numOfRows(cols), numOfBombs(bombs), PlayerName(name){
font.loadFromFile("../files/font.ttf");
}
void pauseClock(){
totalElapsedTime += clock.getElapsedTime();
}
void resumeClock(){
clock.restart();
}
void gamePlay() {
sf::RenderWindow window(sf::VideoMode(numOfCols * 32, numOfRows * 32 + 100), "GameScreen");
Board board = Board(numOfCols, numOfRows, numOfBombs);
board.setBombs();
board.calculateNearbyBombs();
timer = new Timer((numOfCols*32)-97, 32*(numOfRows+0.5)+16, numOfCols, numOfRows);
DebugButton debugButton = DebugButton((numOfCols * 32) - 304, 32 * (numOfRows + 0.5), "../files/images/debug.png");
PausePlay pausePlayButton = PausePlay((numOfCols * 32) - 240, 32 * (numOfRows + 0.5), "../files/images/pause.png");
LeaderBoard leaderBoard = LeaderBoard( (numOfCols * 32) - 176, 32*(numOfRows + 0.5), "../files/images/leaderboard.png");
leaderBoard.readTopFive("../files/leaderboard.txt");
FaceButton faceButton = FaceButton((numOfCols*16)-32, 32*(numOfRows+0.5), "../files/images/face_happy.png");
while (window.isOpen()) {
sf::Event event{};
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
sf::Time elapsed = clock.getElapsedTime();
totalElapsedTime += elapsed;
if (gameActive && !paused && elapsed.asSeconds() >= 1)
{
clock.restart();
seconds++;
if (seconds >= 60)
{
minutes++;
seconds = 0;
}
}
timer->update(seconds, minutes);
gameActive = !board.checkLose() || debugButton.getDebugActive();
window.clear(sf::Color::White);
window.draw(debugButton.sprite);
window.draw(pausePlayButton.sprite);
window.draw(leaderBoard.sprite);
window.draw(faceButton.sprite);
for (int i = 0; i < 3; i++) {
window.draw(board.getCounter(i));
}
for(int i = 0; i <2; i++){
window.draw(timer->getMinutes(i));
window.draw(timer->getSeconds(i));
}
for(int i = 0; i < numOfCols; i++){
for(int j = 0; j < numOfRows; j++) {
window.draw(board.getBoard()[i][j]->sprite);
if(!paused && board.getBoard()[i][j]->getState() == revealed && board.getBoard()[i][j]->getBombCount() > 0 && !board.getBoard()[i][j]->getIsMine()){
window.draw(board.getBoard()[i][j]->numberSprite.sprite);
}
if(board.getBoard()[i][j]->getState() == flag && !paused)
window.draw(board.getFlag(i, j));
if((!paused && !gameActive || debugButton.getDebugActive()) && board.getBoard()[i][j]->getIsMine())
window.draw(board.getBoard()[i][j]->mineSprite.sprite);
if(!paused && board.getBoard()[i][j]->getState() == revealed && board.getBoard()[i][j]->getIsMine())
window.draw(board.getBoard()[i][j]->mineSprite.sprite);
}
}
if (sf::Mouse().isButtonPressed(Mouse::Right) && !rightButtonPressed) {
rightButtonPressed = true;
if (gameActive && !paused) {
for (int i = 0; i < numOfCols; i++) {
for (int j = 0; j < numOfRows; j++) {
board.mouseRightClicked(sf::Mouse().getPosition(window), i, j);
}
}
}
}
else if(!sf::Mouse().isButtonPressed(Mouse::Right)){
rightButtonPressed = false;
}
if (sf::Mouse().isButtonPressed(Mouse::Left) && !leftButtonPressed){
leftButtonPressed = true;
if(faceButton.buttonClicked(sf::Mouse().getPosition(window))){
paused = false;
pausePlayButton.setPause(paused);
pausePlayButton.updateButtonTexture();
seconds = 0;
minutes = 0;
clock.restart();
board.reset();
board.setBombs();
board.calculateNearbyBombs();
faceButton.gameActive();
gameActive = true;
leaderBoard.setUpdated(false);
}
if(leaderBoard.buttonPressed(sf::Mouse().getPosition(window)))
{
for (int l = 0; l < numOfCols; l++) {
for (int m = 0; m < numOfRows; m++) {
board.openAll(l, m);
window.draw(board.getBoard()[l][m]->sprite);
}
}
window.display();
leaderBoard.drawWindow(numOfCols, numOfRows);
if(!paused) {
for (int i = 0; i < numOfCols; i++) {
for (int j = 0; j < numOfRows; j++) {
board.openTile(sf::Mouse().getPosition(window), i, j);
}
}
}
}
if(debugButton.buttonPressed(sf::Mouse().getPosition(window)) && gameActive && !paused){
debugButton.setDebugActive(!debugButton.getDebugActive());
for(int bombs = 0; bombs < numOfCols; bombs++){
for(int b = 0; b < numOfRows; b++){
if(board.getBoard()[bombs][b]->getIsMine()){
board.setDebug(bombs, b);
}
}
}
}
//TODO: fix pause button as openTile is not working
else if(gameActive && pausePlayButton.ButtonClicked(sf::Mouse().getPosition(window))) {
pausePlayButton.updateButtonTexture();
}
if (pausePlayButton.getPause() && pausePlayButton.getButtonPressed()) {
for (int l = 0; l < numOfCols; l++) {
for (int m = 0; m < numOfRows; m++) {
board.openAll(l, m);
}
}
pauseClock();
paused = true;
} else if(board.getAllOpen() && !pausePlayButton.getPause()) {
for (int l = 0; l < numOfCols; l++) {
for (int m = 0; m < numOfRows; m++) {
board.returnToNormal(l, m);
}
}
resumeClock();
paused = false;
}
if(!paused && gameActive){
for(int i = 0; i < numOfCols; i++) {
for (int j = 0; j < numOfRows; j++) {
board.openTile(sf::Mouse().getPosition(window), i, j);
}
}
}
}
else if(!sf::Mouse().isButtonPressed(Mouse::Left)){
leftButtonPressed = false;
}
if(board.checkWin()) {
board.setAllFlags();
faceButton.gameWon();
gameActive = false;
pauseClock();
if(!leaderBoard.getUpdated()){
leaderBoard.checkTime(minutes, seconds, PlayerName);
}
}
else if(!gameActive && !debugButton.getDebugActive()){
faceButton.gameLose();
board.showAllBombs();
gameActive = false;
pauseClock();
}
else if(gameActive){
faceButton.gameActive();
gameActive = true;
}
window.display();
}
}
sf::Text drawWord(string word, float pos_x, float pos_y){
sf::Text text(word, font);
text.setCharacterSize(30);
text.setFillColor(sf::Color::White);
setText(text, pos_x, pos_y);
return text;
}
};