-
Notifications
You must be signed in to change notification settings - Fork 1
/
CSnake.cpp
120 lines (112 loc) · 2.19 KB
/
CSnake.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Developed By : Lorenzo T.Leonardo
* Date : March 23, 2022
* Email : [email protected]
*/
#include "CSnake.h"
CSnake::CSnake(COORD screenSize)
{
m_direction = Snake_Direction::RIGHT;
m_head = { 0,0 };
m_tail = { 0,0 };
COORD coord = { 0,2 };
for (int i = 5; i > 0; i--)
{
coord.X = i;
m_vBody.push_back(coord);
}
m_head = m_vBody[0];
m_tail = m_vBody[m_vBody.size()-1];
m_nlength = (int)m_vBody.size();
m_screenSize = screenSize;
m_bIsAlive = true;
}
CSnake::~CSnake()
{
}
inline void CSnake::checkBodyCollision()
{
for (int i = 1; i < m_nlength; i++)
{
if ((m_vBody[i].X == m_head.X) &&
(m_vBody[i].Y == m_head.Y))
{
m_bIsAlive = false;
break;
}
}
}
void CSnake::growBonus(COORD pos, int nLength)
{
for(int i = 0; i< nLength; i++)
m_vBody.push_back(pos);
m_tail = pos;
m_nlength = (int)m_vBody.size();
if (m_nlength >= (m_screenSize.X * m_screenSize.Y))
m_bIsAlive = false;
}
void CSnake::grow(COORD pos)
{
m_vBody.push_back(pos);
m_tail = pos;
m_nlength = (int)m_vBody.size();
if(m_nlength >= (m_screenSize.X * m_screenSize.Y))
m_bIsAlive = false;
}
void CSnake::crawl()
{
switch (m_direction)
{
case Snake_Direction::RIGHT:
for (int i = m_nlength - 1; i > 0; i--)
{
m_vBody[i] = m_vBody[i - 1];
}
m_vBody[0].X++;
if (m_vBody[0].X >= m_screenSize.X)
{
m_vBody[0].X = 2;
}
break;
case Snake_Direction::LEFT:
for (int i = m_nlength - 1; i > 0; i--)
{
m_vBody[i] = m_vBody[i - 1];
}
m_vBody[0].X--;
if (m_vBody[0].X < 2)
{
m_vBody[0].X = m_screenSize.X - 1;
}
break;
case Snake_Direction::UP:
for (int i = m_nlength - 1; i > 0; i--)
{
m_vBody[i] = m_vBody[i - 1];
}
m_vBody[0].Y--;
if (m_vBody[0].Y < 2)
{
m_vBody[0].Y = m_screenSize.Y - 1;
}
break;
case Snake_Direction::DOWN:
for (int i = m_nlength - 1; i > 0; i--)
{
m_vBody[i] = m_vBody[i - 1];
}
m_vBody[0].Y++;
if (m_vBody[0].Y >= (m_screenSize.Y))
{
m_vBody[0].Y = 2;
}
break;
}
m_head = m_vBody[0];
m_tail = m_vBody[m_vBody.size() - 1];
checkBodyCollision();
}
void CSnake::setDirection(Snake_Direction dir)
{
m_direction = dir;
}