nsnake
Classic snake game for the terminal
Toggle main menu visibility
Loading...
Searching...
No Matches
src
Interface
Animation
AnimationSnakes.cpp
1
#include <Interface/Animation/AnimationSnakes.hpp>
2
#include <Interface/Colors.hpp>
3
#include <Misc/Utils.hpp>
4
5
AnimationSnakes::AnimationSnakes(
Window
* window):
6
Animation
(window)
7
{ }
8
void
AnimationSnakes::load
()
9
{
10
this->addSnake();
11
12
updateTimer.start();
13
addTimer.start();
14
}
15
void
AnimationSnakes::update
()
16
{
17
// Adding yet another snake
18
int
delay =
Utils::Random::between
(1, 3) * 100;
19
20
if
((addTimer.delta_ms() > delay) &&
21
(this->lilsnakes.size() < MAX_SNAKES))
22
{
23
this->addSnake();
24
25
// Random add burst!
26
if
(
Utils::Random::booleanWithChance
(0.2501))
27
{
28
for
(
int
i = 0; i <
Utils::Random::between
(3, 5); i++)
29
this->addSnake();
30
}
31
32
addTimer.start();
33
}
34
35
// Updating all snakes
36
// They all drop once and get deleted as soon as they
37
// leave the Window.
38
if
(updateTimer.delta_ms() > 50)
39
{
40
std::vector<LilSnake>::iterator it = this->lilsnakes.begin();
41
42
while
(it != this->lilsnakes.end())
43
{
44
if
(((*it).y - (*it).size) > (this->window->getH() - 1))
45
{
46
it = this->lilsnakes.erase(it);
47
}
48
else
49
{
50
(*it).y++;
51
++it;
52
}
53
}
54
updateTimer.start();
55
}
56
}
57
void
AnimationSnakes::draw
()
58
{
59
for
(
unsigned
int
i = 0; i < (this->lilsnakes.size()); i++)
60
{
61
window->printChar(
'@'
,
62
this->lilsnakes[i].x,
63
this->lilsnakes[i].y,
64
Colors::pair(COLOR_GREEN, COLOR_DEFAULT,
true
));
65
66
for
(
int
j = 1; j < (this->lilsnakes[i].size); j++)
67
{
68
window->printChar(
'o'
,
69
this->lilsnakes[i].x,
70
this->lilsnakes[i].y - j,
71
Colors::pair(COLOR_GREEN, COLOR_DEFAULT));
72
73
}
74
}
75
}
76
void
AnimationSnakes::addSnake()
77
{
78
int
newx =
Utils::Random::between
(1, this->window->getW() - 1);
79
int
newy =
Utils::Random::between
(0, 3);
80
int
size =
Utils::Random::between
(2, 14);
81
82
this->lilsnakes.push_back(
LilSnake
(newx, newy, size));
83
}
84
AnimationSnakes::update
void update()
Updates Animation's internal state.
Definition
AnimationSnakes.cpp:15
AnimationSnakes::load
void load()
Loads all internal things.
Definition
AnimationSnakes.cpp:8
AnimationSnakes::draw
void draw()
Shows Animation on the screen.
Definition
AnimationSnakes.cpp:57
Animation
Abstract interface to any kind of Animation.
Definition
Animation.hpp:8
Window
A segment of the terminal screen (2D char matrix).
Definition
Window.hpp:17
Utils::Random::booleanWithChance
bool booleanWithChance(float percent)
Random boolean with chance of percent.
Definition
Utils.cpp:64
Utils::Random::between
int between(int min, int max)
Random number between min and max.
Definition
Utils.cpp:48
LilSnake
Definition
AnimationSnakes.hpp:12
Generated on
for nsnake by
1.17.0