-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
301 lines (244 loc) · 9.89 KB
/
Source.cpp
File metadata and controls
301 lines (244 loc) · 9.89 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
class Game {
public:
Game() : window(sf::VideoMode(800, 600), "Spaceship Game"), spaceship(sf::Vector2f(50, 50)), gameOver(false), score(0), startScreen(true) {
window.setFramerateLimit(60);
// Load background texture
if (!backgroundTexture.loadFromFile("images/space.jpg")) {
// Handle error loading texture
std::cerr << "Error loading background texture!" << std::endl;
}
// Set texture for background
background.setTexture(backgroundTexture);
// Load spaceship texture
if (!spaceshipTexture.loadFromFile("images/spaceship.png")) {
// Handle error loading texture
std::cerr << "Error loading spaceship texture!" << std::endl;
}
// Set texture for spaceship
spaceship.setTexture(&spaceshipTexture);
// Load asteroid texture
if (!asteroidTexture.loadFromFile("images/rock2.png")) {
// Handle error loading texture
std::cerr << "Error loading asteroid texture!" << std::endl;
}
// Set texture for asteroid sprite
asteroidSprite.setTexture(asteroidTexture);
// Load font for buttons
if (!font.loadFromFile("font/BAUHS93.ttf")) {
// Handle error loading font
std::cerr << "Error loading font!" << std::endl;
}
// Set up start button
startButton.setSize(sf::Vector2f(145, 50));
startButton.setFillColor(sf::Color::Yellow);
startButtonText.setFont(font);
startButtonText.setString("Start Game");
startButtonText.setCharacterSize(20);
startButtonText.setFillColor(sf::Color::Black);
// Set up restart button
restartButton.setSize(sf::Vector2f(175, 50));
restartButton.setFillColor(sf::Color::Yellow);
restartButtonText.setFont(font);
restartButtonText.setString("Restart Game");
restartButtonText.setCharacterSize(20);
restartButtonText.setFillColor(sf::Color::Black);
// Create initial asteroids
createAsteroids(3); // Change the number of asteroids as needed
}
void run() {
while (window.isOpen()) {
if (startScreen) {
processStartScreenEvents();
renderStartScreen();
}
else {
processEvents();
update();
render();
}
}
}
private:
sf::RenderWindow window;
sf::RectangleShape spaceship;
sf::Texture spaceshipTexture; // Texture for spaceship
sf::Sprite asteroidSprite; // Use sf::Sprite for asteroids
sf::Texture asteroidTexture; // Texture for asteroids
sf::Sprite background; // Background sprite
sf::Texture backgroundTexture; // Texture for background
sf::RectangleShape startButton; // Start button
sf::Text startButtonText; // Text on the start button
sf::RectangleShape restartButton; // Restart button
sf::Text restartButtonText; // Text on the restart button
bool gameOver;
int score;
bool startScreen;
sf::Clock asteroidSpawnClock; // Clock for tracking asteroid spawn time
sf::Clock scoreClock; // Clock for tracking score
std::vector<sf::Sprite> asteroids; // Vector to store asteroid sprites
sf::Font font;
void processStartScreenEvents() {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::MouseButtonPressed) {
if (event.mouseButton.button == sf::Mouse::Left) {
sf::Vector2i mousePosition = sf::Mouse::getPosition(window);
sf::Vector2f mousePositionF(static_cast<float>(mousePosition.x), static_cast<float>(mousePosition.y));
if (startButton.getGlobalBounds().contains(mousePositionF)) {
startScreen = false;
resetGame();
}
}
}
}
}
void processEvents() {
sf::Event event;
// Handle continuous input outside the event loop
if (!gameOver) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && spaceship.getPosition().x > 0) {
spaceship.move(-5, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && spaceship.getPosition().x < 750) {
spaceship.move(5, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && spaceship.getPosition().y > 0) {
spaceship.move(0, -5);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && spaceship.getPosition().y < 550) {
spaceship.move(0, 5);
}
}
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::MouseButtonPressed) {
if (event.mouseButton.button == sf::Mouse::Left) {
sf::Vector2i mousePosition = sf::Mouse::getPosition(window);
sf::Vector2f mousePositionF(static_cast<float>(mousePosition.x), static_cast<float>(mousePosition.y));
if (startScreen && startButton.getGlobalBounds().contains(mousePositionF)) {
startScreen = false;
resetGame();
}
if (gameOver && restartButton.getGlobalBounds().contains(mousePositionF)) {
resetGame();
}
}
}
}
}
void update() {
if (!gameOver) {
// Update asteroid positions
for (auto& asteroid : asteroids) {
asteroid.move(0, 6);
// Check for collision with each asteroid
if (spaceship.getGlobalBounds().intersects(asteroid.getGlobalBounds())) {
gameOver = true;
}
// Check if asteroid is out of bounds
if (asteroid.getPosition().y > window.getSize().y) {
// Remove the asteroid from the vector
asteroids.erase(asteroids.begin());
}
}
// Check if it's time to spawn a new asteroid
if (asteroidSpawnClock.getElapsedTime().asSeconds() > 0.5) { // Adjust the time interval as needed
createAsteroid();
asteroidSpawnClock.restart();
}
// Update the score
if (scoreClock.getElapsedTime().asSeconds() >= 1.0) {
score++; // Increment score every second
scoreClock.restart();
}
}
}
void renderStartScreen() {
window.clear();
// Draw the background first
window.draw(background);
// Draw the game name
sf::Text gameNameText("Astro-dodge", font, 50);
gameNameText.setFillColor(sf::Color::Red);
gameNameText.setPosition(275, 200);
window.draw(gameNameText);
// Draw the start button
startButton.setPosition(335, 400);
window.draw(startButton);
startButtonText.setPosition(360, 410);
window.draw(startButtonText);
window.display();
}
void render() {
window.clear();
// Draw the background first
window.draw(background);
if (!gameOver) {
window.draw(spaceship);
for (const auto& asteroid : asteroids) {
window.draw(asteroid); // Draw each asteroid sprite
}
// Draw the score
sf::Text scoreText("Score: " + std::to_string(score), font, 20);
scoreText.setFillColor(sf::Color::White);
scoreText.setPosition(10, 10);
window.draw(scoreText);
}
else {
// Display game over message and final score
sf::Text gameOverText("Game Over!", font, 50);
gameOverText.setFillColor(sf::Color::Red);
gameOverText.setPosition(250, 250);
window.draw(gameOverText);
sf::Text finalScoreText("Final Score: " + std::to_string(score), font, 30);
finalScoreText.setFillColor(sf::Color::White);
finalScoreText.setPosition(300, 320);
window.draw(finalScoreText);
// Display restart button
restartButton.setPosition(300, 400);
window.draw(restartButton);
restartButtonText.setPosition(325, 410);
window.draw(restartButtonText);
}
window.display();
}
void createAsteroid() {
sf::Sprite newAsteroid = asteroidSprite;
newAsteroid.setPosition(std::rand() % 750, 0);
asteroids.push_back(newAsteroid);
}
void createAsteroids(int count) {
asteroids.clear();
for (int i = 0; i < count; ++i) {
sf::Sprite asteroidCopy = asteroidSprite;
asteroidCopy.setPosition(std::rand() % 750, i * -150); // Initial positions staggered for each asteroid
asteroids.push_back(asteroidCopy);
}
}
void resetGame() {
gameOver = false;
score = 0;
// Reset the clocks
asteroidSpawnClock.restart();
scoreClock.restart();
// Recreate initial asteroids
createAsteroids(3);
// Reset spaceship position
spaceship.setPosition(375, 500);
}
};
int main() {
Game game;
game.run();
return 0;
}