-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.cpp
136 lines (118 loc) · 4.26 KB
/
application.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
#include <algorithm> //brauch ich für CLAMP
#include <application.hpp>
#include <button.hpp>
#include <chrono>
application::application() {
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
state.gridwidth = width / (boxlength + linewidth);
state.gridheight = height / (boxlength + linewidth);
}
void application::execute() {
int speed = 300; // sleep time of gameloop in ms
auto starttime = std::chrono::system_clock::now();
while (window.isOpen()) {
process_input();
const auto newtime = std::chrono::system_clock::now();
const auto duration =
std::chrono::duration<float>(newtime - starttime).count();
const float frametime = speed / (float)1000;
if (duration >= frametime) {
starttime = newtime;
state.move_snake();
if (state.foodeaten()) {
// Farbmischung
snakecolor[0] =
(state.snakelist.size() * snakecolor[0] + foodcolor[0]) /
(state.snakelist.size() + 2);
snakecolor[1] =
(state.snakelist.size() * snakecolor[1] + foodcolor[1]) /
(state.snakelist.size() + 2);
snakecolor[2] =
(state.snakelist.size() * snakecolor[2] + foodcolor[2]) /
(state.snakelist.size() + 2);
speed = speed - 50;
foodcolor = {distcolor(rng), distcolor(rng), distcolor(rng)};
}
}
// Break condition
if (state.gamebreak()) {
failalertwindow();
}
render();
}
}
void application::process_input() {
sf::Event event;
while (window.pollEvent(
event)) { // die Eingabe-Liste event wird vom window abgefragt. Das ist
// true, solange es neue events (Benutzereingaben) gibt.
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Escape) window.close();
if (event.key.code == sf::Keyboard::Up) state.movement = 0;
if (event.key.code == sf::Keyboard::Right) state.movement = 1;
if (event.key.code == sf::Keyboard::Down) state.movement = 2;
if (event.key.code == sf::Keyboard::Left) state.movement = 3;
}
}
}
void application::render() {
window.clear(sf::Color(200, 200, 200));
// Draw grid
for (int i = 0; i < state.gridwidth; ++i) {
for (int j = 0; j < state.gridheight; ++j) {
sf::RectangleShape box(sf::Vector2f(boxlength, boxlength));
box.setPosition(i * (boxlength + linewidth), j * (boxlength + linewidth));
box.setFillColor(sf::Color(255, 255, 255));
window.draw(box);
}
}
// Make food
sf::CircleShape food(boxlength / 2);
food.setPosition(state.food.x * (boxlength + linewidth),
state.food.y * (boxlength + linewidth));
food.setFillColor(sf::Color(foodcolor[0], foodcolor[1], foodcolor[2]));
window.draw(food);
// Draw snake
for (const auto p : state.snakelist) {
sf::CircleShape snake(boxlength / 2);
snake.setFillColor(sf::Color(snakecolor[0], snakecolor[1], snakecolor[2]));
snake.setPosition(p.x * (boxlength + linewidth),
p.y * (boxlength + linewidth));
window.draw(snake);
}
window.display();
}
void application::failalertwindow() {
int failwindowwidth = 300;
int failwindowheight = 300;
sf::RenderWindow failalert{sf::VideoMode(failwindowwidth, failwindowheight),
"You failed!"};
// failalert.clear(sf::Color(255, 0, 0, 255));
sf::RectangleShape backgroundcheatsheet(
sf::Vector2f(failwindowwidth, failwindowheight));
backgroundcheatsheet.setFillColor(sf::Color(255, 0, 0, 255));
failalert.draw(backgroundcheatsheet);
while (failalert.isOpen()) {
sf::Event eventfail;
while (failalert.pollEvent(eventfail)) {
if (eventfail.type == sf::Event::Closed)
failalert.close();
else if (eventfail.type == sf::Event::KeyPressed) {
if (eventfail.key.code == sf::Keyboard::Escape) failalert.close();
}
}
button ButtonClose("Close", failalert, (failwindowwidth) / 2.0f, 100);
if (ButtonClose.buttonPressed == true) {
failalert.close();
window.close();
}
button ButtonRestart("Restart", failalert, (failwindowwidth) / 2.0f, 150);
if (ButtonRestart.buttonPressed == true) {
failalert.close();
}
failalert.display();
}
}