-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timber.cpp
563 lines (440 loc) · 13 KB
/
Timber.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// HelloSFML.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <sstream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
using namespace sf;
void updateBranches(int seed);
const int NUM_BRANCHES = 6;
Sprite branches[NUM_BRANCHES];
enum class side{ LEFT, RIGHT, NONE};
side branchPositions[NUM_BRANCHES];
int main()
{
//Create video object
VideoMode vm(1920, 1080);
//Create and open a window for the game
RenderWindow window(vm, "Timber!!!", Style::Fullscreen);
//Create a texture to hold a graphic on the GPU
Texture textureBackground;
//Load a graphic into the texture
textureBackground.loadFromFile("graphics/background.png");
//Create a Sprite
Sprite spriteBackground;
//Attach the texture to the sprite
spriteBackground.setTexture(textureBackground);
//Set the spriteBackground to cover the screen
spriteBackground.setPosition(0, 0);
//Make tree sprite
Texture textureTree;
textureTree.loadFromFile("graphics/tree.png");
Sprite spriteTree;
spriteTree.setTexture(textureTree);
spriteTree.setPosition(810, 0);
//Make bee sprite
Texture textureBee;
textureBee.loadFromFile("graphics/bee.png");
Sprite spriteBee;
spriteBee.setTexture(textureBee);
spriteBee.setPosition(0, 800);
//Is bee currently moving?
bool beeActive = false;
//How fast can be fly?
float beeSpeed = 0.0f;
//Make 3 clouds
Texture textureCloud;
textureCloud.loadFromFile("graphics/cloud.png");
Sprite spriteCloud1;
Sprite spriteCloud2;
Sprite spriteCloud3;
spriteCloud1.setTexture(textureCloud);
spriteCloud2.setTexture(textureCloud);
spriteCloud3.setTexture(textureCloud);
spriteCloud1.setPosition(0, 0);
spriteCloud2.setPosition(0, 250);
spriteCloud3.setPosition(0, 500);
//Are clouds currently on screem?
bool cloud1Active = false;
bool cloud2Active = false;
bool cloud3Active = false;
//Cloud speed
float cloud1Speed = 0.0f;
float cloud2Speed = 0.0f;
float cloud3Speed = 0.0f;
//Variable to control time
Clock clock;
//Time Bar
RectangleShape timeBar;
float timeBarStartWidth = 400;
float timeBarHeight = 80;
timeBar.setSize(Vector2f(timeBarStartWidth, timeBarHeight));
timeBar.setFillColor(Color::Red);
timeBar.setPosition((1920 / 2) - timeBarStartWidth / 2, 980);
Time gameTimeTotal;
float timeRemaining = 6.0f;
float timeBarWidthPerSecond = timeBarStartWidth / timeRemaining;
//Game pause
bool paused = true;
//Score and Message
int score = 0;
Text messageText;
Text scoreText;
//Font
Font font;
font.loadFromFile("fonts/KOMIKAP_.ttf");
//Set the font to text objects
messageText.setFont(font);
scoreText.setFont(font);
//Assign actual message to text objects
messageText.setString("Press Enter to start");
scoreText.setString("Score = 0");
//Change size
messageText.setCharacterSize(75);
scoreText.setCharacterSize(100);
//Set color
messageText.setFillColor(Color::White);
scoreText.setFillColor(Color::White);
//Positioning the texts
scoreText.setPosition(20, 20);
FloatRect textRect = messageText.getLocalBounds();
messageText.setOrigin(textRect.left + textRect.width / 2.0f, textRect.top + textRect.height / 2.0f);
messageText.setPosition(1920 / 2.0f, 1080 / 2.0f);
//6 Branches sprites
Texture textureBranch;
textureBranch.loadFromFile("graphics/branch.png");
for (int i = 0; i < NUM_BRANCHES; i++) {
branches[i].setTexture(textureBranch);
branches[i].setPosition(-2000, -2000);
branches[i].setOrigin(220, 20);
}
//Prepare the player
Texture texturePlayer;
texturePlayer.loadFromFile("graphics/player.png");
Sprite spritePlayer;
spritePlayer.setTexture(texturePlayer);
spritePlayer.setPosition(580, 720);
//Player side in start
side playerSide = side::LEFT;
//Gravestone
Texture textureRIP;
textureRIP.loadFromFile("graphics/rip.png");
Sprite spriteRIP;
spriteRIP.setTexture(textureRIP);
spriteRIP.setPosition(600, 860);
//Axe
Texture textureAxe;
textureAxe.loadFromFile("graphics/axe.png");
Sprite spriteAxe;
spriteAxe.setTexture(textureAxe);
spriteAxe.setPosition(700, 830);
//Line the axe up with tree
const float AXE_POSITION_LEFT = 700;
const float AXE_POSITION_RIGHT = 1075;
//Flying Log
Texture textureLog;
textureLog.loadFromFile("graphics/log.png");
Sprite spriteLog;
spriteLog.setTexture(textureLog);
spriteLog.setPosition(810, 720);
//Log related variables
bool logActive = false;
float logSpeedX = 1000;
float logSpeedY = -1500;
//Control input
bool acceptInput = false;
//Sounds
SoundBuffer chopBuffer;
chopBuffer.loadFromFile("sound/chop.wav");
Sound chop;
chop.setBuffer(chopBuffer);
SoundBuffer deathBuffer;
deathBuffer.loadFromFile("sound/death.wav");
Sound death;
death.setBuffer(deathBuffer);
SoundBuffer outBuffer;
outBuffer.loadFromFile("sound/out_of_time.wav");
Sound outOfTime;
outOfTime.setBuffer(outBuffer);
while (window.isOpen()) {
/* Handle the players input */
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::KeyReleased && !paused) {
acceptInput = true;
//hide axe
spriteAxe.setPosition(2000, spriteAxe.getPosition().y);
}
}
if (Keyboard::isKeyPressed(Keyboard::Escape)) {
window.close();
}
//Start/Restart the game
if (Keyboard::isKeyPressed(Keyboard::Return)) {
paused = false;
score = 0;
timeRemaining = 6;
//Make all branches disappear
for (int i = 1; i < NUM_BRANCHES; i++) {
branchPositions[i] = side::NONE;
}
//Hide RIP
spriteRIP.setPosition(675, 2000);
//Move the player into position
spritePlayer.setPosition(580, 720);
acceptInput = true;
}
//Take input
if (acceptInput) {
//Handling right key press
if (Keyboard::isKeyPressed(Keyboard::Right)) {
//Make the player right
playerSide = side::RIGHT;
score++;
//Add time remaining
timeRemaining += (2 / score) + 0.15;
//set axe
spriteAxe.setPosition(AXE_POSITION_RIGHT, spriteAxe.getPosition().y);
//set player
spritePlayer.setPosition(1200, 720);
//update branches
updateBranches(score);
//set flying log to the left
spriteLog.setPosition(810, 720);
logSpeedX = -5000;
logActive = true;
acceptInput = false;
chop.play();
}
//Handling left key press
if (Keyboard::isKeyPressed(Keyboard::Left)) {
//Make the player left
playerSide = side::LEFT;
score++;
//Add time remaining
timeRemaining += (2 / score) + 0.15;
//set axe
spriteAxe.setPosition(AXE_POSITION_LEFT, spriteAxe.getPosition().y);
//set player
spritePlayer.setPosition(580, 720);
//update branches
updateBranches(score);
//set flying log to the left
spriteLog.setPosition(810, 720);
logSpeedX = 5000;
logActive = true;
acceptInput = false;
chop.play();
}
}
/*********** Update the scene ***************/
if (!paused) {
//Measure time
Time dt = clock.restart();
//Update time remaining
timeRemaining -= dt.asSeconds();
//Resize the time bar
timeBar.setSize(Vector2f(timeBarWidthPerSecond*timeRemaining, timeBarHeight));
//Time Run Out
if (timeRemaining <= 0.0f) {
//pause the game
paused = true;
//Change message
messageText.setString("Out of Time!");
//reposition message for new size
FloatRect textRect = messageText.getLocalBounds();
messageText.setOrigin(textRect.left + textRect.width / 2.0f, textRect.top + textRect.height / 2.0f);
messageText.setPosition(1920 / 2.0f, 1080 / 2.0f);
outOfTime.play();
}
//Setup the bee
if (beeActive == false) {
//New Bee speed
srand((int)time(0) * 10);
beeSpeed = (rand() % 200) + 200;
//New bee height
srand((int)time(0) * 10);
float height = (rand() % 500) + 500;
spriteBee.setPosition(2000, height);
beeActive = true;
}
else {
//Move the existing bee
spriteBee.setPosition(
spriteBee.getPosition().x - (beeSpeed * dt.asSeconds()),
spriteBee.getPosition().y);
//If bee reaches left end
if (spriteBee.getPosition().x < -100) {
// Delete this one, and set up a new in the right in next loop
beeActive = false;
}
}
//Setup cloud1
if (cloud1Active == false) {
//speed
srand((int)time(0) * 10);
cloud1Speed = (rand() % 200);
//height
srand((int)time(0) * 10);
float height = (rand() % 150);
spriteCloud1.setPosition(-200, height);
cloud1Active = true;
}
else {
//Move the cloud
spriteCloud1.setPosition(
spriteCloud1.getPosition().x +
(cloud1Speed * dt.asSeconds()),
spriteCloud1.getPosition().y);
//If cloud reaches right end
if (spriteCloud1.getPosition().x > 1920) {
//Deactivate this cloud
cloud1Active = false;
}
}
//Setup cloud2
if (cloud2Active == false) {
//speed
srand((int)time(0) * 20);
cloud2Speed = (rand() % 200);
//height
srand((int)time(0) * 20);
float height = (rand() % 300) - 150;
spriteCloud2.setPosition(-200, height);
cloud2Active = true;
}
else {
//Move the cloud
spriteCloud2.setPosition(spriteCloud2.getPosition().x + (cloud2Speed * dt.asSeconds()), spriteCloud2.getPosition().y);
//If cloud reaches right end
if (spriteCloud2.getPosition().x > 1920) {
//Deactivate this cloud
cloud2Active = false;
}
}
//Setup cloud3
if (cloud3Active == false) {
//speed
srand((int)time(0) * 30);
cloud3Speed = (rand() % 200);
//height
srand((int)time(0) * 30);
float height = (rand() % 450) - 150;
spriteCloud3.setPosition(-200, height);
cloud3Active = true;
}
else {
//Move the cloud
spriteCloud3.setPosition(spriteCloud3.getPosition().x + (cloud3Speed * dt.asSeconds()), spriteCloud3.getPosition().y);
//If cloud reaches right end
if (spriteCloud3.getPosition().x > 1920) {
//Deactivate this cloud
cloud3Active = false;
}
}
//Update Score
std::stringstream ss;
ss << "Score = " << score;
scoreText.setString(ss.str());
//Update the branch sprites
for (int i = 0; i < NUM_BRANCHES; i++) {
float height = i * 150;
if (branchPositions[i] == side::LEFT) {
branches[i].setPosition(610, height);
branches[i].setRotation(180);
}
else if (branchPositions[i] == side::RIGHT) {
branches[i].setPosition(1330, height);
branches[i].setRotation(0);
}
else {
//Hide the branch
branches[i].setPosition(3000, height);
}
}
//Handle flying log
if (logActive) {
spriteLog.setPosition(spriteLog.getPosition().x + (logSpeedX * dt.asSeconds()),
spriteLog.getPosition().y + (logSpeedY * dt.asSeconds()));
//Check if log has reached right/left edge
if (spriteLog.getPosition().x < -100 || spriteLog.getPosition().x > 2000) {
logActive = false;
spriteLog.setPosition(810, 720);
}
}
//Squished code
if (playerSide == branchPositions[5]) {
paused = true;
acceptInput = false;
//rip
spriteRIP.setPosition(525, 760);
//Hide player
spritePlayer.setPosition(2000, 660);
//Message
messageText.setString("SQUISHED!");
//Put the message in center
FloatRect textRect = messageText.getLocalBounds();
messageText.setOrigin(textRect.left + textRect.width / 2.0f,
textRect.top + textRect.height / 2.0f);
messageText.setPosition(1920 / 2.0f, 1080 / 2.0f);
death.play();
}
} //paused condition ends
/* Draw the scene */
//Clear everything from the last frame
window.clear();
//Draw the game scene here
window.draw(spriteBackground);
//Draw clouds
window.draw(spriteCloud1);
window.draw(spriteCloud2);
window.draw(spriteCloud3);
//Draw 6 branches
for (int i = 0; i < NUM_BRANCHES; i++) {
window.draw(branches[i]);
}
//Draw tree
window.draw(spriteTree);
//Draw the Player
window.draw(spritePlayer);
//Draw the axe
window.draw(spriteAxe);
//Draw the log
window.draw(spriteLog);
//Draw the RIP
window.draw(spriteRIP);
//Draw bee
window.draw(spriteBee);
//Draw score
window.draw(scoreText);
//Draw timebar
window.draw(timeBar);
if (paused) {
//Draw message
window.draw(messageText);
}
//Show everything we just drew
window.display();
}
return 0;
}
void updateBranches(int seed) {
//Shift all branches down
for (int i = NUM_BRANCHES - 1; i > 0; i--) {
branchPositions[i] = branchPositions[i - 1];
}
//New branch at zeroth position (Top of tree)
srand((int)time(0) + seed);
int r = (rand() % 5);
switch (r) {
case 0:
branchPositions[0] = side::LEFT;
break;
case 1:
branchPositions[0] = side::RIGHT;
break;
default:
branchPositions[0] = side::NONE;
break;
}
}