nsnake
Classic snake game for the terminal
AnimationSnakes.cpp
1 #include <Display/Animations/AnimationSnakes.hpp>
2 #include <Engine/Graphics/Colors.hpp>
3 #include <Engine/Helpers/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("green", "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("green", "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