-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spidersaur.cpp
217 lines (188 loc) · 7.03 KB
/
Spidersaur.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
#include <SFML/Graphics.hpp>
using namespace sf;
const int windowWidth = 1280;
const int windowHeight = 720;
Sprite dinoSprite;
Sprite groundSprite;
Sprite skySprite1;
Sprite skySprite2;
Sprite cactuses[5];
Sprite bricks[10];
FloatRect skySpriteRect;
const int x = 100;
int y;
float dy;
int lastCactus;
int lastBrick;
bool gameOver;
void startGame() {
dinoSprite.setPosition(x, windowHeight - 200);
groundSprite.setPosition(0, windowHeight - 100);
skySprite1.setPosition(0, 0);
skySprite2.setPosition(skySpriteRect.width, 0);
for (int i = 0; i < 5; i++) {
cactuses[i].setPosition(Vector2f(-1000, -1000));
}
for (int i = 0; i < 10; i++) {
bricks[i].setPosition(Vector2f(-1000, -1000));
}
y = 100;
dy = 0;
lastCactus = 0;
lastBrick = 0;
gameOver = false;
}
int main() {
// create the window
RenderWindow app(VideoMode(windowWidth, windowHeight), "\"Spidersaur: A Game\" by Archawin Wongkittiruk");
app.setFramerateLimit(100);
Font font;
font.loadFromFile("fonts/consola.ttf");
Text highScore;
highScore.setFont(font);
highScore.setCharacterSize(50);
highScore.setPosition(50, 0);
Text score;
score.setFont(font);
score.setCharacterSize(50);
score.setPosition(50, 50);
Texture dinoTextures[4];
Texture dinoTexture0; dinoTexture0.loadFromFile("images/dinosprite1.png"); dinoTextures[0] = dinoTexture0;
Texture dinoTexture1; dinoTexture1.loadFromFile("images/dinosprite3.png"); dinoTextures[1] = dinoTexture1;
Texture dinoTexture2; dinoTexture2.loadFromFile("images/dinosprite2.png"); dinoTextures[2] = dinoTexture2;
Texture dinoTexture3; dinoTexture3.loadFromFile("images/dinosprite3.png"); dinoTextures[3] = dinoTexture3;
Texture groundTexture; groundTexture.loadFromFile("images/ground.png");
Texture skyTexture; skyTexture.loadFromFile("images/sky.jpg");
Texture cactusTexture; cactusTexture.loadFromFile("images/cactus.png");
Texture brickTexture; brickTexture.loadFromFile("images/brick.png");
dinoSprite.setTexture(dinoTexture0);
int dinoTextureIndex = 0;
groundSprite.setTexture(groundTexture);
skySprite1.setTexture(skyTexture);
skySprite2.setTexture(skyTexture);
skySpriteRect = skySprite1.getGlobalBounds();
for (int i = 0; i < 5; i++) {
cactuses[i] = Sprite(cactusTexture);
}
for (int i = 0; i < 10; i++) {
bricks[i] = Sprite(brickTexture);
}
VertexArray line(LineStrip, 2);
bool shootLine = false;
startGame();
Clock clock;
int highScoreInt = 0;
// run the program as long as the window is open
while (app.isOpen()) {
// check all the window's events that were triggered since the last iteration of the loop
Event event;
while (app.pollEvent(event)) {
// "close requested" event: we close the window
if (event.type == Event::Closed) app.close();
}
if (!gameOver) {
// get and update scores, and increase the game speed
int time = (int) clock.getElapsedTime().asSeconds();
if (time > highScoreInt) {
highScoreInt = time;
}
highScore.setString("High score: " + std::to_string(highScoreInt));
score.setString("Score: " + std::to_string(time));
float speed = -5.f - clock.getElapsedTime().asSeconds() / 10;
// jump when pressing space and slow descent when holding
if (Keyboard::isKeyPressed(Keyboard::Space)) {
if (y > windowHeight - 200) {
dy = -12;
dinoSprite.setTexture(dinoTexture0);
}
if (dy > 0) {
dy -= 0.3;
}
}
// spawn next cactus randomly
if (rand() % 75 == 0 && cactuses[lastCactus-1].getPosition().x < windowWidth / 2) {
cactuses[lastCactus].setPosition(windowWidth, windowHeight - 220);
lastCactus = (lastCactus + 1) % 5;
}
// spawn next brick randomly
if (rand() % 50 == 0) {
bricks[lastBrick].setPosition(windowWidth, 150);
lastBrick = (lastBrick + 1) % 10;
}
// gravity
dy += 0.35;
y += dy;
if (y > windowHeight - 200) {
dy = 0;
}
dinoSprite.setPosition(x, y);
// check for collision with cactuses
for (int i = 0; i < 5; i++) {
if (dinoSprite.getGlobalBounds().intersects(cactuses[i].getGlobalBounds())) {
gameOver = true;
}
}
// move the sky background
skySprite1.move(Vector2f(speed/2, 0.f));
skySprite2.move(Vector2f(speed/2, 0.f));
if (skySprite1.getPosition().x < -skySpriteRect.width) {
skySprite1.setPosition(skySpriteRect.width, 0);
}
if (skySprite2.getPosition().x < -skySpriteRect.width) {
skySprite2.setPosition(skySpriteRect.width, 0);
}
// move the cactuses and bricks
for (int i = 0; i < 5; i++) {
cactuses[i].move(Vector2f(speed, 0.f));
}
for (int i = 0; i < 10; i++) {
bricks[i].move(Vector2f(speed, 0.f));
}
// shoot and pull a sling to a brick when pressing left mouse button
Vector2i mousePos = Mouse::getPosition(app);
shootLine = false;
line[0].position = Vector2f(x + 75, y + 50);
line[1].position = Vector2f(mousePos.x, mousePos.y);
for (int i = 0; i < 10; i++) {
FloatRect brickRect = bricks[i].getGlobalBounds();
if (brickRect.intersects(line.getBounds()) && Mouse::isButtonPressed(Mouse::Left)) {
line[1].position = Vector2f(brickRect.left+50, brickRect.top+50);
shootLine = true;
if (y > 300) {
dy -= 0.8;
}
}
}
// 'animate' running
if (((int)clock.getElapsedTime().asMilliseconds() / 10) % 8 == 0 && y > windowHeight - 200) {
dinoTextureIndex = (dinoTextureIndex + 1) % 4;
dinoSprite.setTexture(dinoTextures[dinoTextureIndex]);
}
}
else if (Keyboard::isKeyPressed(Keyboard::Space)) {
startGame();
clock.restart();
}
// clear the window
app.clear();
// draw everything here...
app.draw(skySprite1);
app.draw(skySprite2);
app.draw(dinoSprite);
app.draw(groundSprite);
for (int i = 0; i < 5; i++) {
app.draw(cactuses[i]);
}
for (int i = 0; i < 10; i++) {
app.draw(bricks[i]);
}
app.draw(highScore);
app.draw(score);
if (shootLine) {
app.draw(line);
}
// end the current frame
app.display();
}
return 0;
}