-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameProject.cpp
209 lines (188 loc) · 5.16 KB
/
GameProject.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
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <chrono>
#include <iostream>
#include "Player.h"
#include "Animation.h"
#include "Obstacles.h"
#include "Food.h"
#include "Status.h"
#include <time.h>
#include <sstream>
using namespace std;
struct GameOverMessage
{
sf::Font font;
sf::Text text;
GameOverMessage() {
if (!font.loadFromFile("Monogram.ttf"))
{
cout << "Error loading arial.ttf" << endl;
}
text.setFont(font);
text.setString("Game Over");
text.setCharacterSize(80); // in pixels, not points!
// set the color
// set the text style
text.setStyle(sf::Text::Bold | sf::Text::Underlined);
text.setPosition(sf::Vector2f(800 / 2, 600 / 2 ));
}
void draw(sf::RenderWindow& window) {
window.draw(text);
}
};
int main()
{
// Create the main window
GameOverMessage gameovermsg;
Status status;
sf::RenderWindow window(sf::VideoMode(status.window_width, status.window_height), "VitaTheDino");
sf::Texture bg;
bg.loadFromFile("background2.png");
sf::Sprite sBackground(bg);
Player vita({ status.window_width / 2 - 55, status.window_height / 2 - 30});
int o1 = 1,o2 = 2;
Obstacles object({ 1200.0f ,600.0f }, o1);
Obstacles object2({ 800.0f ,1000.0f }, o2);
//Coin Objects:
std::vector<Food*> coinVec;
Food coin1({ 20, 20 });
coinVec.push_back(&coin1);
srand(time(NULL));
coin1.setPos({ 700, 600 });
int score = 0;
sf::Font font;
if (!font.loadFromFile("Monogram.ttf"))
return EXIT_FAILURE;
std::ostringstream ssScore;
ssScore << "Score: " << score;
font.loadFromFile("Monogram.ttf");
sf::Text lblScore;
lblScore.setCharacterSize(60);
lblScore.setPosition({ 750, 0 });
lblScore.setFont(font);
lblScore.setString(ssScore.str());
sf::Vector2f _dir = { 1.0f, 1.0f };
sf::Vector2f _dir2 = { -1.0f, 1.0f };
float life = 2;
//Font
sf::Text text("VitaTheDino", font, 50);
// timepoint for delta time calculation
auto tp = chrono::steady_clock::now();
// Load a music to play
sf::Music music;
if (!music.openFromFile("13.ogg"))
return EXIT_FAILURE;
sf::SoundBuffer buffer;
if (!buffer.loadFromFile("coininsert.wav"))
return -1;
sf::Sound sound;
sound.setBuffer(buffer);
// Play the music
music.setVolume(2.f);
music.setLoop(true);
music.play();
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed)
window.close();
}
// get delta time
float dt;
{
const auto new_tp = chrono::steady_clock::now();
dt = chrono::duration<float>(new_tp - tp).count();
tp = new_tp;
}
//Handle input //TODO: move to status class
if (life > 0) {
sf::Vector2f dir = { 0.0f,0.0f };
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
if (vita.getY() + 16 <= 0) dir.y = 0.0f;
else dir.y -= 2.0f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
if (vita.getY() + 88 >= status.window_height) dir.y = 0.0f;
else dir.y += 2.0f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
if (vita.getX() + 20 <= 0) dir.x = 0.0f;
else dir.x -= 2.0f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
if (vita.getX() + 50 >= status.window_width) dir.x = 0.0f;
else dir.x += 2.0f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
if (vita.getX() + 50 >= status.window_width) dir.x = 0.0f;
else dir.x += 5.0f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
if (vita.getX() + 20 <= 0) dir.x = 0.0f;
else dir.x -= 5.0f;
}
if (object.getX() - 250 >= status.window_width) _dir.x = -1.0f;
if (object.getX() + 250 <= 0) _dir.x = 1.0f;
if (object.getY() - 250 >= status.window_height) _dir.y = -1.0f;
if (object.getY() + 250 <= 0) _dir.y = 1.0f;
if (object2.getX() - 350 >= status.window_width) _dir2.x = -1.0f;
if (object2.getX() + 350 <= 0) _dir2.x = 1.0f;
if (object2.getY() - 350 >= status.window_height) _dir2.y = -1.0f;
if (object2.getY() + 350 <= 0) _dir2.y = 1.0f;
if (vita.isCollidingwithObject(object) || vita.isCollidingwithObject(object2)) life -= 1;
//Coin Logic:
for (int i = 0; i < coinVec.size(); i++) {
if (vita.isCollidingwithFood(coinVec[i])) {
sound.play();
float x = rand() % 800 + 100;
float y = rand() % 600 + 100;
coinVec[i]->setPos({ x, y });
score++;
ssScore.str("");
ssScore << "Score " << score;
lblScore.setString(ssScore.str());
}
}
//if (score == 1) {
//}
//Set direction
vita.SetDirection(dir);
//update
vita.Update(dt);
// Clear screen
window.clear();
window.draw(sBackground); //Draw background
window.draw(text); //Draw text
window.draw(lblScore);
coin1.drawTo(window);
// Draw the sprite
vita.Draw(window);
if (score >= 3) {
object.SetDirection(_dir);
object.Update(dt, o1);
object.Draw(window);
}
if (score >= 10) {
object2.SetDirection(_dir2);
object2.Update(dt, o2);
object2.Draw(window);
}
}
// Update the window
else gameovermsg.draw(window);
window.display();
}
return EXIT_SUCCESS;
}