-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainScreen.h
249 lines (203 loc) · 7.61 KB
/
MainScreen.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#pragma once
#include <SFML/Graphics.hpp>
#include "Player.h"
#include"Enemy.h"
#include<fstream>
#include <iostream>
#include <ctime> // For random enemies location
using namespace std;
using namespace sf;
class MainScreen
{
private:
Font font;
Text playerLivesText; // Text to display player's lives
Text playerScoreText; // Text to display player's score
Text menuOptions[4]; // Array to hold the menu options
const int menuSize = 4; // Number of menu options
int selectedOption;
Texture backgroundTexture; // Texture for the background image
Sprite backgroundSprite; // Sprite for the background image
// Starting screen
Texture startTexture; // Texture for the starting screen image
Sprite startSprite; // Sprite for the starting screen image
// Exit screen
Texture exitTexture; // Texture for the image to display for a few seconds
Sprite exitSprite; // Sprite for the image to display
Clock clock; // Clock to track elapsed time
// How to Play screen
Texture howToPlayTexture;
Sprite howToPlaySprite;
// Level screen
Texture levelTexture;
Sprite levelSprite;
Texture blockTexture;
Texture woodTexture;
Sprite blockSprite;
Sprite woodSprite;
// Player-related
Texture playerTexture; // Texture for the player
Player* player; // Pointer to the player object
Enemy* enemies[10]; // Array of pointers to enemies
Texture gateTexture;
Sprite gateSprite;
bool gateVisible = false; // Initially, the gate is not visible
Texture winTexture;
Sprite winSprite;
Text highScoresText[5];
public:
enum class ScreenState { Start, Menu, Exit, HowToPlay, Level, Win, HighScore };
ScreenState screenState; // Current screen state
MainScreen()
{
// Load the font
if (!font.loadFromFile("./fonts/arial.ttf"))
{
cout << "\nMenu Screen Fonts not Loaded!" << endl;
}
if (!backgroundTexture.loadFromFile("./images/background.png"))
{
cout << "\nBackground Image not Loaded!" << endl;
}
// Set the texture to the sprite
backgroundSprite.setTexture(backgroundTexture);
// Load the texture for the starting screen image
if (!startTexture.loadFromFile("./images/start1.png"))
{
cout << "\nStarting Screen Image not Loaded!" << endl;
}
startSprite.setTexture(startTexture);
// Load the texture for the image to display for a few seconds
if (!exitTexture.loadFromFile("./images/exit.png"))
{
cout << "\nExit Screen Image not Loaded!" << endl;
}
// Set the texture to the sprite
exitSprite.setTexture(exitTexture);
// Load the texture for the level screen image
if (!levelTexture.loadFromFile("./images/level.png"))
{
cout << "\nLevel Screen Image not Loaded!" << endl;
}
levelSprite.setTexture(levelTexture);
// Load the texture for the How to Play screen image
if (!howToPlayTexture.loadFromFile("./images/howtoplay.png"))
{
cout << "\nHow to Play Screen Image not Loaded!" << endl;
}
howToPlaySprite.setTexture(howToPlayTexture);
// Initialize the player with the loaded texture
player = new Player(20, 20, 1.f);
for (int i = 0; i < 10; ++i)
{
enemies[i] = new Enemy();
enemies[i]->generateRandomPosition(player->getPosition(), enemies, 10);
}
// Load the texture for block
if (!blockTexture.loadFromFile("./images/block.png"))
{
cout << "\nblock Image not Loaded!" << endl;
}
// Set the texture to the sprite for block
blockSprite.setTexture(blockTexture);
// Load the texture for wood
if (!woodTexture.loadFromFile("./images/wood.png"))
{
cout << "\nwood Image not Loaded!" << endl;
}
// Set the texture to the sprite for wood
woodSprite.setTexture(woodTexture);
if (!gateTexture.loadFromFile("./images/gate.png"))
{
cout << "\nGate Image not Loaded!" << endl;
}
gateSprite.setTexture(gateTexture);
gateSprite.setPosition(280.f, 540.f);
for (int i = 0; i < 5; ++i)
{
highScoresText[i].setFont(font);
highScoresText[i].setCharacterSize(36);
highScoresText[i].setFillColor(Color::White);
}
if (!winTexture.loadFromFile("./images/win.png"))
{
cout << "\nWin Screen Image not Loaded!" << endl;
}
winSprite.setTexture(winTexture);
selectedOption = 0;
for (int i = 0; i < menuSize; ++i)
{
menuOptions[i].setFont(font);
menuOptions[i].setCharacterSize(48);
menuOptions[i].setFillColor(Color::White);
menuOptions[i].setOutlineThickness(3);
if (i == 0)
menuOptions[i].setString("START GAME");
if (i == 1)
menuOptions[i].setString("HOW TO PLAY");
if (i == 2)
menuOptions[i].setString("HIGH SCORE");
if (i == 3)
menuOptions[i].setString("EXIT GAME");
menuOptions[i].setPosition(150.f, 200.f + i * 70.f);
}
menuOptions[0].setFillColor(Color::Green); // Highlight the first option
screenState = ScreenState::Start; // Initialize the screen state to menu
clock.restart();
// player score and live text
playerLivesText.setFont(font);
playerLivesText.setCharacterSize(20);
playerLivesText.setFillColor(Color::White);
playerLivesText.setOutlineThickness(1);
playerLivesText.setPosition(40.f, 0.f);
playerScoreText.setFont(font);
playerScoreText.setCharacterSize(20);
playerScoreText.setFillColor(Color::White);
playerScoreText.setOutlineThickness(1);
playerScoreText.setPosition(300.f, 0.f);
}
void moveUp();
void moveDown();
void handleMouseClick(float x, float y);
void handleMouseMove(float x, float y);
void update();
void draw(RenderWindow& window);
bool shouldClose() const;
void goBackToMenu();
ScreenState getScreenState() const;
void handleInput(float deltaTime); // Handle player input
Enemy** getEnemies()
{
return &enemies[0];
}
void loadHighScores()
{
ifstream inputFile("highscores.txt");
if (!inputFile.is_open())
{
cout << "\nFailed to open high scores file!" << endl;
return;
}
string line;
int index = 0;
while (getline(inputFile, line) && index < 5)
{
highScoresText[index].setFont(font);
highScoresText[index].setCharacterSize(36); // Set size of the font
highScoresText[index].setFillColor(Color::White);
highScoresText[index].setString(line);
// Center the text horizontally on the screen
highScoresText[index].setPosition(300.f - highScoresText[index].getLocalBounds().width / 2, 200.f + index * 50.f);
index++;
}
inputFile.close();
}
~MainScreen()
{
for (int i = 0; i < 10; ++i)
{
delete enemies[i];
}
delete player;
}
};