forked from jcalvarezj/snake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Section.cpp
62 lines (53 loc) · 1.3 KB
/
Section.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
/*
* Snake game program using the SDL library
*
* @author J. Alvarez
*/
#include "Screen.hpp"
#include "Section.hpp"
#include "Snake.hpp"
namespace SnakeGame {
const unsigned int Section::S_SECTION_WIDTH = 20;
Section::Section(): Collideable(0, 0, 0) {}
Section::Section(int x, int y, int t):
Collideable(x, y, t) {}
void Section::draw(Screen & screen) {
for (int i = 0; i < S_SECTION_WIDTH; i++)
for (int j = 0; j < S_SECTION_WIDTH; j++)
screen.setPixel((int) m_x + i, (int) m_y + j, Snake::S_SNK_RED,
Snake::S_SNK_GREEN, Snake::S_SNK_BLUE);
}
void Section::move(int direction) {
switch (direction) {
case Snake::Direction::UP:
m_y -= S_SECTION_WIDTH;
break;
case Snake::Direction::DOWN:
m_y += S_SECTION_WIDTH;
break;
case Snake::Direction::LEFT:
m_x -= S_SECTION_WIDTH;
break;
case Snake::Direction::RIGHT:
m_x += S_SECTION_WIDTH;
break;
}
}
int Section::calculateDirection(Section & other) {
if (other.m_x - m_x == 0) {
if(other.m_y - m_y < 0)
return Snake::Direction::UP;
else
return Snake::Direction::DOWN;
}
else {
if(other.m_x - m_x > 0)
return Snake::Direction::RIGHT;
else
return Snake::Direction::LEFT;
}
}
void Section::toString() { // TODO Remove - Used for debugging
SDL_Log("<%d, %d, %d>", m_x, m_y, type);
}
} // namespace SnakeGame