-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
234 lines (165 loc) · 5.28 KB
/
main.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
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <ctime>
#include <cstdlib>
using namespace sf;
int main()
{
RenderWindow game(VideoMode(1920, 1080), "Retro Drive!");
game.setFramerateLimit(60);
sf::SoundBuffer buffer;
if (!buffer.loadFromFile("critical.wav")) {
return -1;
}
sf::Sound sound;
sound.setBuffer(buffer);
struct Player {
int tileX;
int tileY;
};
int points = 200;
Texture car;
Texture background;
Texture gem;
Texture nogo;
Texture nogoTwo;
nogo.loadFromFile("no.png");
nogoTwo.loadFromFile("go.png");
car.loadFromFile("anotherf1.png");
background.loadFromFile("finaltrack.png");
gem.loadFromFile("f1.png");
Sprite noGoZone(nogo);
Sprite noGoZoneTwo(nogoTwo);
Sprite spriteBack(background);
Sprite spriteCar(car);
Sprite spriteGem(gem);
// 960, 800
noGoZone.setPosition(750, 350);
noGoZone.setOrigin(800 / 2, 100 / 2);
noGoZoneTwo.setPosition(750, 250);
spriteCar.setPosition(100, 100);
spriteCar.setOrigin(32 / 2, 32 / 2);
float x = 300;
float y = 300;
float speed = 0;
float angle = 0;
int maxSpeed = 15;
float accelerate = 0.2;
float decelerate = 0.2;
float corner = 0.08;
sf::Vector2f checkOne = { 400, 200 };
sf::Vector2f checkTwo = { 1200, 200 };
sf::Vector2f checkThree = { 400, 1000 };
sf::Vector2f checkFour = { 1200, 1000 };
float randomX = rand() % 1000 + 1;
sf::Font font;
font.loadFromFile("arial.ttf");
while (game.isOpen()) {
Event e;
while (game.pollEvent(e)) {
if (e.type == Event::Closed)
game.close();
}
bool up = 0;
bool down = 0;
bool right = 0;
bool left = 0;
if (Keyboard::isKeyPressed(Keyboard::Up)) up = 1, points -= 0.1;
if (Keyboard::isKeyPressed(Keyboard::Down)) down = 1;
if (Keyboard::isKeyPressed(Keyboard::Right)) right = 1;
if (Keyboard::isKeyPressed(Keyboard::Left)) left = 1;
if (up && speed < maxSpeed)
if (speed < 0) speed = speed + decelerate;
else speed = speed + accelerate;
if (down && speed > -maxSpeed)
if (speed > 0) speed = speed - decelerate;
else speed = speed - accelerate;
if (!up && !down)
if (speed - decelerate > 0) speed = speed - decelerate;
else if (speed + decelerate < 0) speed = speed + decelerate;
else speed = 0;
if (right && speed != 0) angle = angle + corner * (speed / maxSpeed);
if (left && speed != 0) angle = angle - corner * (speed / maxSpeed);
x = x + sin(angle) * speed;
y = y - cos(angle) * speed;
std::string Speed_str = std::to_string(floor(speed * 10));
sf::Text text("Speed: " + Speed_str, font, 50);
game.clear(Color::Black);
game.draw(spriteBack);
text.setFillColor(Color::Red);
// set track
// count laps
// horn sound
// if points = 0 GAME OVER
spriteCar.setPosition(x, y);
spriteCar.setRotation(angle * 180 / 3.14159);
sf::FloatRect boundingBox = spriteCar.getGlobalBounds();
sf::FloatRect colBox = noGoZone.getGlobalBounds();
// sf::Vector2f pos = { randomX, randomY };
if (colBox.contains(x, y)) {
x = 100;
y = 100;
speed = 0;
spriteCar.setRotation(180);
}
sf::FloatRect lowZone = noGoZoneTwo.getGlobalBounds();
if (lowZone.contains(x, y)) {
x = 100;
y = 100;
speed = 0;
spriteCar.setRotation(180);
}
if (x < 0 || x > 1920 || y < 0 || y > 1080) {
x = 100;
y = 100;
speed = 0;
spriteCar.setRotation(180);
}
if (boundingBox.contains(checkOne))
{
points = points + 2;
sound.play();
}
if (boundingBox.contains(checkTwo))
{
points = points + 2;
sound.play();
}
if (boundingBox.contains(checkThree))
{
points = points + 2;
sound.play();
}
if (boundingBox.contains(checkFour))
{
points = points + 2;
sound.play();
}
if (points > 500) {
points = 500;
}
std::string Points_str = std::to_string(points);
sf::Text pointsText("Fuel: " + Points_str, font, 50);
pointsText.setPosition(0, 100);
spriteCar.setColor(Color::Red);
std::string Rand_str = std::to_string(randomX);
sf::Text randText("Check Point: " + Rand_str, font, 50);
randText.setPosition(0, 200);
spriteGem.setPosition(checkOne);
game.draw(spriteCar);
game.draw(text);
game.draw(pointsText);
game.draw(randText);
game.draw(spriteGem);
spriteGem.setPosition(checkTwo);
game.draw(spriteGem);
spriteGem.setPosition(checkThree);
game.draw(spriteGem);
spriteGem.setPosition(checkFour);
game.draw(noGoZone);
game.draw(noGoZoneTwo);
game.draw(spriteGem);
game.display();
}
return 0;
}