-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
280 lines (260 loc) · 8.87 KB
/
Source.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
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
#include "Header.h"
float toRadians(float degrees)
{
return degrees / 180 * 3.14;
}
float toDegrees(float radians)
{
return radians / 3.14 * 180;
}
bool collision(sf::RectangleShape rect, sf::CircleShape coin)
{
return rect.getGlobalBounds().intersects(coin.getGlobalBounds());
}
bool collisionUFO(sf::RectangleShape rect, sf::Sprite spr)
{
return rect.getGlobalBounds().intersects(spr.getGlobalBounds());
}
int main()
{
// Window
sf::RenderWindow window;
window.create(sf::VideoMode::getFullscreenModes()[0], "Spaceship", sf::Style::Fullscreen);
// Level
int level = 1;
// TP Timer (cooldown to prevent double uses)
int tpTimer = 0;
// UFO Textures
sf::Texture ufoTex;
ufoTex.loadFromFile("ufo.png");
vector<sf::Sprite> ufos = {};
vector<int> ufoTimers = { 0, 100 };
vector<int> ufoRotations = { rand() % 359, rand() % 359 };
for (int i = 0; i < 2; i++)
{
ufos.push_back(sf::Sprite());
ufos[i].setTexture(ufoTex);
ufos[i].setScale(6, 6);
ufos[i].setPosition(sf::Vector2f(rand() % 3900 + 50, rand() % 3900 + 50));
}
// Spaceship texture
sf::Texture tex;
tex.loadFromFile("spaceship.png");
sf::Sprite spaceship;
spaceship.setTexture(tex);
spaceship.setOrigin(18, 37);
// Background image
sf::Texture background;
background.loadFromFile("background.jpg");
sf::Sprite backSprite;
backSprite.setTexture(background);
backSprite.setPosition(sf::Vector2f(0, 0));
// Actual game rectangle
sf::RectangleShape rect(sf::Vector2f(36, 74));
rect.setPosition(2000, 2000);
rect.setOrigin(18, 37);
bool spaceshipVulnerable = false;
int spaceshipVulnerableTimer = 0;
bool spaceshipFlashing = false;
// Coins
sf::CircleShape coins[10];
for (int i = 0; i < 10; i++)
{
coins[i] = sf::CircleShape(25);
coins[i].setPosition(sf::Vector2f(rand() % 3900 + 50, rand() % 3900 + 50));
coins[i].setFillColor(sf::Color::Yellow);
coins[i].setOutlineColor(sf::Color::Black);
coins[i].setOutlineThickness(2);
}
// Ship velocity vars
float shipVeloX = 0;
float shipVeloY = 0;
// Score
int points = 0;
// Points text
sf::Font TimesNewRoman;
TimesNewRoman.loadFromFile("C:\\Windows\\Fonts\\times.ttf");
sf::Text score("Ponits", TimesNewRoman);
score.setCharacterSize(100);
score.setColor(sf::Color::White);
score.setStyle(sf::Text::Bold);
// Level text
sf::Text lvlText("Level: ", TimesNewRoman);
lvlText.setCharacterSize(120);
lvlText.setColor(sf::Color::White);
lvlText.setStyle(sf::Text::Bold);
// Heart textures
sf::Texture heartTex;
heartTex.loadFromFile("heart.png");
sf::Sprite hearts[3];
for (int i = 0; i < 3; i++)
{
hearts[i].setTexture(heartTex);
}
int lives = 3;
// Game over text
sf::Text lolNoob;
lolNoob.setCharacterSize(120);
lolNoob.setFont(TimesNewRoman);
lolNoob.setColor(sf::Color::Green);
lolNoob.setString("Diagnosis: You're dead!");
// Teleport rectangle
sf::RectangleShape meter;
meter.setSize(sf::Vector2f(600, 50));
meter.setFillColor(sf::Color::Red);
meter.setOutlineColor(sf::Color::Black);
meter.setOutlineThickness(3);
//Teleport charge
int ultCharge = 30000;
// Camera position
window.setView(sf::View(sf::Vector2f(960, 540), sf::Vector2f(1920, 1080)));
// Rerunnables
while (window.isOpen())
{
// Process events
// Not sure what these do but I'm afraid of touching them
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed)
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
shipVeloX += -sin(toRadians(rect.getRotation())) * 3;
shipVeloY += cos(toRadians(rect.getRotation())) * 3;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
rect.rotate(-0.2);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
rect.rotate(0.2);
}
if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S)) && ultCharge >= 30000 && tpTimer == 1000)
{
rect.setPosition(sf::Vector2f(rand() % 3900 + 50, rand() % 3900 + 50));
ultCharge -= 30000;
spaceshipVulnerable = false;
spaceshipVulnerableTimer = 2000;
shipVeloX = 0;
shipVeloY = 0;
tpTimer = 0;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
window.close();
}
rect.move(shipVeloX / 6000, shipVeloY / 6000);
int yPos = rect.getPosition().y;
int xPos = rect.getPosition().x;
if (rect.getPosition().x > 4000) rect.setPosition(sf::Vector2f(0, rect.getPosition().y));
if (rect.getPosition().x < 0) rect.setPosition(sf::Vector2f(4000, rect.getPosition().y));
if (rect.getPosition().y > 4000) rect.setPosition(sf::Vector2f(rect.getPosition().x, 0));
if (rect.getPosition().y < 0) rect.setPosition(sf::Vector2f(rect.getPosition().x, 4000));
window.setView(sf::View(sf::Vector2f(min(max(xPos, 960), 3040), min(max(yPos, 540), 3460)), sf::Vector2f(1920, 1080)));
if (shipVeloX > 1) shipVeloX--;
else if (shipVeloX < 1 && shipVeloX > 0) shipVeloX = 0;
if (shipVeloX < -1) shipVeloX++;
else if (shipVeloX > -1 && shipVeloX < 0) shipVeloX = 0;
if (shipVeloY > 1) shipVeloY--;
else if (shipVeloY < 1 && shipVeloY > 0) shipVeloY = 0;
if (shipVeloY < -1) shipVeloY++;
else if (shipVeloY > -1 && shipVeloY < 0) shipVeloY = 0;
spaceship.setPosition(rect.getPosition());
spaceship.setRotation(rect.getRotation() + 180);
score.setString("Ponits: " + std::to_string(points));
lvlText.setString("Level: " + std::to_string(level));
for (int i = 0; i < 10; i++)
{
if (collision(rect, coins[i]))
{
coins[i].setPosition(sf::Vector2f(rand() % 3900 + 50, rand() % 3900 + 50));
points++;
ultCharge += min(3000, 60000 - ultCharge);
}
}
lvlText.setPosition(sf::Vector2f(window.getView().getCenter().x - 180, window.getView().getCenter().y + 400));
score.setPosition(sf::Vector2f(window.getView().getCenter().x - 160, window.getView().getCenter().y + 300));
meter.setPosition(sf::Vector2f(window.getView().getCenter().x - 300, window.getView().getCenter().y - 475));
meter.setSize(sf::Vector2f(ultCharge / 100, 50));
if (tpTimer < 1000) tpTimer++;
for (int i = 0; i < 3; i++)
{
hearts[i].setPosition(rect.getPosition());
hearts[i].move(sf::Vector2f(i * 40 - 60, -80));
}
for (int i = 0; i < level * 2 - 1; i++)
{
ufoTimers[i]++;
if (ufoTimers[i] == 1000)
{
ufoRotations[i] = rand() % 359;
ufoTimers[i] = 0;
}
}
if (!spaceshipVulnerable) spaceshipVulnerableTimer++;
if (spaceshipVulnerableTimer > 5000) spaceshipVulnerable = true;
if (!spaceshipVulnerable && !spaceshipFlashing) spaceshipFlashing = true;
else spaceshipFlashing = false;
for (int i = 0; i < level * 2 - 1; i++)
{
ufos[i].move(sf::Vector2f(-sin(ufoRotations[i]) * 0.3, cos(ufoRotations[i]) * 0.3));
if (ufos[i].getPosition().x > 4000) ufos[i].setPosition(sf::Vector2f(0, ufos[i].getPosition().y));
if (ufos[i].getPosition().x < 0) ufos[i].setPosition(sf::Vector2f(4000, ufos[i].getPosition().y));
if (ufos[i].getPosition().y > 4000) ufos[i].setPosition(sf::Vector2f(ufos[i].getPosition().x, 0));
if (ufos[i].getPosition().y < 0) ufos[i].setPosition(sf::Vector2f(ufos[i].getPosition().x, 4000));
if (collisionUFO(rect, ufos[i]) && spaceshipVulnerable)
{
rect.setPosition(sf::Vector2f(2000, 2000));
shipVeloX = 0;
shipVeloY = 0;
spaceshipVulnerable = false;
spaceshipVulnerableTimer = 0;
lives--;
}
}
lolNoob.setPosition(sf::Vector2f(window.getView().getCenter().x - 500, window.getView().getCenter().y - 200));
if (ultCharge < 60000) ultCharge++;
if (ultCharge < 30000) meter.setFillColor(sf::Color::Red);
else if (ultCharge >= 30000 && ultCharge < 60000) meter.setFillColor(sf::Color::Blue);
else if (ultCharge == 60000) meter.setFillColor(sf::Color::Green);
if (points >= level)
{
level++;
points = 0;
for (int i = level * 2 - 3; i < level * 2 - 1; i++)
{
ufos.push_back(sf::Sprite());
ufos[i].setTexture(ufoTex);
ufos[i].setPosition(sf::Vector2f(rand() % 3900 + 50, rand() % 3900 + 50));
ufos[i].setScale(6, 6);
ufoRotations.push_back(rand() % 359);
ufoTimers.push_back(rand() % 999);
}
}
//Drawing functions
window.clear();
window.draw(backSprite);
if (!spaceshipFlashing) window.draw(spaceship);
for (int i = 0; i < level * 2 - 1; i++)
{
window.draw(ufos[i]);
}
for (int i = 0; i < 10; i++) window.draw(coins[i]);
window.draw(score);
window.draw(meter);
for (int i = 2; i >= (3 - lives); i--) window.draw(hearts[i]);
window.draw(lvlText);
if (lives <= 0)
{
window.draw(lolNoob);
window.display();
_sleep(1000);
window.close();
}
window.display();
}
}