-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.cpp
50 lines (43 loc) · 1.74 KB
/
snake.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
#include "snake.h"
void Snake::initSnakeVars()
{
for (int i = 0; i < Snake::snakesCount; i++)
{
this->snkDir[i] = Direction::right;
this->futureSnkDir[i] = Direction::right;
this->newSnkHead[i] = Point{-1, -1};
this->snkPoints[i].clear();
}
}
void Snake::initSnakesInQueue()
{
int k = (Grid::L / (Snake::snakesCount + 1));
for (int i = 0; i < Snake::snakesCount; i++)
{
for (int j = 0; j < 3; j++)
{
this->snkPoints[i].push(Point{k * (i + 1), j});
}
}
}
void Snake::setSnakeDir(int snakeIdx, byte direction)
{
byte currentSnakeDir = this->snkDir[snakeIdx];
if (currentSnakeDir == Direction::up && direction == Direction::down || currentSnakeDir == Direction::down && direction == Direction::up || currentSnakeDir == Direction::right && direction == Direction::left || currentSnakeDir == Direction::left && direction == Direction::right)
return;
this->futureSnkDir[snakeIdx] = direction;
}
void Snake::takeAndParseJoystickInput(int joystickIdx)
{
int joystickX = analogRead(PinMap::joystick[joystickIdx][0]);
int joystickY = analogRead(PinMap::joystick[joystickIdx][1]);
byte currentSnakeDir = this->snkDir[joystickIdx];
if (currentSnakeDir != Direction::up && joystickX < Joystick::inputThreshold)
setSnakeDir(joystickIdx, Direction::up);
else if (currentSnakeDir != Direction::down && 1024 - joystickX < Joystick::inputThreshold)
setSnakeDir(joystickIdx, Direction::down);
else if (currentSnakeDir != Direction::right && joystickY < Joystick::inputThreshold)
setSnakeDir(joystickIdx, Direction::right);
else if (currentSnakeDir != Direction::left && 1024 - joystickY < Joystick::inputThreshold)
setSnakeDir(joystickIdx, Direction::left);
}