-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake_first.cpp
More file actions
191 lines (170 loc) · 4.23 KB
/
Snake_first.cpp
File metadata and controls
191 lines (170 loc) · 4.23 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include"Snake_first.h" //玩家一
using namespace std;
Snake_first::Snake_first(Wall& wall, Food& food) :wall_(wall), food_(food) //构造函数
{
Head = NULL;
//isRool = false;
difficulty = 1;
score = 0;
ability = 0;
}
void Snake_first::DestoryPoint()
{
point* ptr = Head;
while (Head != NULL) //销毁整个蛇身
{
ptr = ptr->next;
delete Head;
Head = ptr;
}
}
void Snake_first::AddPoint(int x, int y)
{
if (ability)
red();
else
green(); //设置蛇身颜色
point* newpoint = new point; //增加新的蛇身
newpoint->x = x;
newpoint->y = y;
newpoint ->next= NULL;
if (Head != NULL)
{
wall_.Setwall(Head->y, Head->x, (char*)"●"); //打印旧蛇头,即旧蛇头变成蛇身
gotoxy(hOut, Head->x*2, Head->y);
cout << "●";
}
newpoint->next = Head; //设置新蛇头
Head = newpoint;
wall_.Setwall(Head->y, Head->x, (char*)"●");
gotoxy(hOut, Head->x*2 ,Head->y);
cout << "●"; //打印新蛇头,长度加一
}
void Snake_first::InitSnake()
{
DestoryPoint(); //初始化蛇身,最初长度为三
AddPoint(3, 1);
AddPoint(4, 1);
AddPoint(5, 1);
}
void Snake_first::DeletePoint() //删除蛇身最后一个节点,即长度减少一
{
if (Head == NULL || Head->next == NULL)
return;
point* ptr1 = Head->next;
point* ptr2 = Head;
while (ptr1->next != NULL)
{
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
wall_.Setwall(ptr1->y, ptr1->x, (char*)" "); //将蛇尾位置恢复空白
gotoxy(hOut, ptr1->x*2, ptr1->y);
cout << " ";
delete ptr1;
ptr1 = NULL;
ptr2->next = NULL;
}
bool Snake_first::Move(char key)
{
int x = Head->x; //蛇头横坐标
int y = Head->y; //蛇头纵坐标
switch (key) //响应键盘输入
{
case UP: //向上
y--;
break;
case DOWN: //向下
y++;
break;
case LEFT: //向左
x--;
break;
case RIGHT: //向右
x++;
break;
default:
break;
}
if (ability == 1) //穿墙状态,只有碰到自身以及边界才死亡
{
if(wall_.Getwall(y, x) == (char*)"●"||y==0||y==39||x==0||x==39)
return false;
}
else if (ability == 0 ) //如果碰到墙或蛇身则死亡
{
if(wall_.Getwall(y, x) == (char*)"■" || wall_.Getwall(y, x) == (char*)"●")
return false;
}
if (wall_.Getwall(y, x) == (char*)"◆") //如果吃到食物则长度加一
{
AddPoint(x, y);
if (food_.Get_attribute() == 2)
{
score += 200;
ability = 1;
start = clock();
}
if (food_.Get_attribute() == 1)
{
score += 300;
}
else if (food_.Get_attribute() == 0)
{
score += 100;
}
food_.Setfood();
}
else //如果不是以上两种情况,则在下一位置增加一个节点,末尾删除一个节点
{
AddPoint(x, y);
DeletePoint();
}
return true;
}
int Snake_first::Getsleeptime()
{
int sleepTime = 0; //延迟时间
int size = Countlist(); //蛇身长度
if (difficulty == 1) //难度等级为简单
{
sleepTime = 400 - 20 * (size - 3); //延迟时间随蛇身长度增加变短
return (sleepTime > 10 ? sleepTime : 10);
}
else if (difficulty == 2) //难度等级为普通
{
sleepTime = 300 - 15 * (size - 3); //延迟时间随蛇身长度增加变短
return (sleepTime > 10 ? sleepTime : 10);
}
else if (difficulty == 3) //难度等级为困难
{ //延迟时间随蛇身长度增加变短
sleepTime = 200 - 10 * (size - 3);
return (sleepTime > 5 ? sleepTime : 5);
}
else if(difficulty == 4) //难度等级为地狱
{
sleepTime = 100 - 5 * (size - 3); //延迟时间随蛇身长度增加变短
return (sleepTime > 5 ? sleepTime : 5);
}
}
int Snake_first::Countlist()
{
int size = 0;
point* ptr = Head;
while (ptr != NULL)
{
size++; //每遍历一个节点,长度加一
ptr = ptr->next;
}
return size;
}
int Snake_first::Getscore()
{
return score;
}
void Snake_first::Resetscore() //重置得分
{
score = 0;
}
int Snake_first::Getability() { return ability; } //查看是否具备穿墙能力
void Snake_first::Resetability() { ability = 0; } //重置穿墙能力