nsnake
Classic snake game for the terminal
Loading...
Searching...
No Matches
AnimationSnakes.cpp
1#include <Interface/Animation/AnimationSnakes.hpp>
2#include <Interface/Colors.hpp>
3#include <Misc/Utils.hpp>
4
5AnimationSnakes::AnimationSnakes(Window* window):
6 Animation(window)
7{ }
9{
10 this->addSnake();
11
12 updateTimer.start();
13 addTimer.start();
14}
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!
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}
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}
76void 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
void update()
Updates Animation's internal state.
void load()
Loads all internal things.
void draw()
Shows Animation on the screen.
Abstract interface to any kind of Animation.
Definition Animation.hpp:8
void start()
Sets a starting point for the timer.
Definition Timer.cpp:26
suseconds_t delta_ms()
Returns the milisseconds part of the timer's difference.
Definition Timer.cpp:75
A segment of the terminal screen (2D char matrix).
Definition Window.hpp:17
void printChar(int c, int x, int y, ColorPair pair=0)
Shows #c at x y with color #pair.
Definition Window.cpp:105
bool booleanWithChance(float percent)
Random boolean with chance of #percent.
Definition Utils.cpp:64
int between(int min, int max)
Random number between min and max.
Definition Utils.cpp:48