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
| #include <SFML/Graphics.hpp>
#include <iostream>
#define _USE_MATH_DEFINES
#include "ForceBar.h"
#include <cmath>
#define M_PI 3.14159265
enum class SpearState
{
initial,
flying,
dropped
};
void resetGame(SpearState& spear_state, float& total_distance, float& best_distance, int& attempt_count)
{
spear_state = SpearState::initial;
total_distance = 0.0f;
attempt_count = 0;
}
int main()
{
float total_distance = 0.0f;
float best_distance = 0.0f;
int attempt_count = 0;
bool restartGame = false;
int force{ 50 };
ForceBar forceBar;
forceBar.update(force);
sf::RenderWindow window(sf::VideoMode(1280, 600), "SPEAR", sf::Style::Close);
Vector2f window_size = window.getView().getSize();
Image stad_image;
stad_image.loadFromFile("https://www.cyberforum.ru/images/stad.png");
Texture stad_texture;
stad_texture.loadFromImage(stad_image);
Sprite stad_sprite;
stad_sprite.setTexture(stad_texture);
Image spear_image;
spear_image.loadFromFile("https://www.cyberforum.ru/images/spear.png");
spear_image.createMaskFromColor(Color(255, 255, 255));
Texture spear_texture;
spear_texture.loadFromImage(spear_image);
Sprite spear_sprite;
spear_sprite.setTexture(spear_texture);
spear_sprite.scale(sf::Vector2f(0.5f, 0.5f));
SpearState spear_state{ SpearState::initial };
Vector2f word_base{ 0, window_size.y - 10 };
int word_spear_start_angle{ 45 };
Vector2f word_spear_start_position{ 0, 0 };
float word_spear_start_velocity{};
Time flying_time{};
float spear_throw_velocity{};
float flying_distance{};
Clock clock;
float prev_T{};
float prev_X{};
float prev_Y{};
int spear_rotate{};
Vector2f spear_position{};
sf::Font font;
if (font.loadFromFile("fonts/arial.ttf"))
{
sf::Text bestDistanceText;
bestDistanceText.setFont(font);
bestDistanceText.setCharacterSize(20);
bestDistanceText.setFillColor(sf::Color::White);
bestDistanceText.setPosition(1000, 50);
sf::Text distanceText;
distanceText.setFont(font);
distanceText.setCharacterSize(20);
distanceText.setFillColor(sf::Color::White);
distanceText.setPosition(1000, 20);
sf::Text attemptsText;
attemptsText.setFont(font);
attemptsText.setCharacterSize(20);
attemptsText.setFillColor(sf::Color::White);
attemptsText.setPosition(1000, 80);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (spear_state == SpearState::initial && event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Right)
{
if (++force > 99)
force = 99;
forceBar.update(force);
}
else if (event.key.code == sf::Keyboard::Left)
{
if (--force < 1)
force = 1;
forceBar.update(force);
}
else if (event.key.code == sf::Keyboard::Down)
{
if (--word_spear_start_angle < 10)
word_spear_start_angle = 10;
}
else if (event.key.code == sf::Keyboard::Up)
{
if (++word_spear_start_angle > 80)
word_spear_start_angle = 80;
}
else if (event.key.code == sf::Keyboard::Space)
{
if (attempt_count >= 3)
resetGame(spear_state, total_distance, best_distance, attempt_count);
else
{
spear_state = SpearState::flying;
clock.restart();
flying_time = Time::Zero;
spear_throw_velocity = force + 10;
attempt_count++;
}
}
else if (event.key.code == sf::Keyboard::R)
{
resetGame(spear_state, total_distance, best_distance, attempt_count);
restartGame = true;
}
}
}
if (restartGame)
{
resetGame(spear_state, total_distance, best_distance, attempt_count);
restartGame = true;
}
switch (spear_state)
{
case SpearState::initial:
{
spear_rotate = 360 - word_spear_start_angle;
spear_position = { word_base.x + word_spear_start_position.x, word_base.y - word_spear_start_position.y };
break;
}
case SpearState::flying:
{
constexpr float G = 9.81;
flying_time += clock.restart();
float T = flying_time.asSeconds();
if ((T - prev_T) > 1 / 60)
{
prev_T = T;
float Y = word_spear_start_position.y + spear_throw_velocity * std::sin(word_spear_start_angle * M_PI / 180) * T - G * T * T / 2.0;
float X = word_spear_start_position.x + spear_throw_velocity * std::cos(word_spear_start_angle * M_PI / 180) * T;
float dx = X - prev_X;
float dy = Y - prev_Y;
float k = dy / dx;
spear_rotate = 360 - std::atan(k) * 180 / M_PI;
prev_X = X;
prev_Y = Y;
total_distance += dx;
flying_distance = X;
spear_position = { word_base.x + X, word_base.y - Y };
if (Y < 0)
spear_state = SpearState::dropped;
}
break;
}
case SpearState::dropped:
{
spear_position = { word_base.x + prev_X, word_base.y - prev_Y };
if (total_distance > best_distance)
{
best_distance = total_distance;
}
break;
}
}
window.clear();
bestDistanceText.setString("Best Distance: " + std::to_string(static_cast<int>(best_distance)) + " m");
window.draw(bestDistanceText);
distanceText.setString("Distance: " + std::to_string(static_cast<int>(total_distance)) + " m");
window.draw(distanceText);
attemptsText.setString("Attempts: " + std::to_string(attempt_count) + "/3");
window.draw(attemptsText);
window.draw(stad_sprite);
forceBar.draw(window);
Vector2f size = window.getView().getSize();
spear_sprite.setPosition(spear_position);
spear_sprite.setRotation(spear_rotate);
window.draw(spear_sprite);
window.draw(bestDistanceText);
window.draw(distanceText);
window.draw(attemptsText);
window.display();
}
}
return 0;
} |