-
Notifications
You must be signed in to change notification settings - Fork 3
/
贪吃蛇(穿墙).cpp
231 lines (181 loc) · 4.04 KB
/
贪吃蛇(穿墙).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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//▇ :用于组成蛇身的符号
# include <stdio.h>
# include <stdlib.h>
# include <windows.h>
# include <time.h>
# include <conio.h>
# define INIT_LENGTH 4 //蛇初始化的长度
# define DEBUG 0
char direction = 'd'; //纪录蛇移动的方向,a——左,w——上,d——右,s——下
int length = 0; //纪录蛇的长度
/* 用于表示组成蛇身的点的结构体 */
typedef struct body
{
int x;
int y;
}body_t, * pBody_t;
body_t snake[300]; //用于储存蛇身的数组
body_t randPoint = { 0, 0 }; //储存随机出现的点
void init_game(void);
void print_snake(void);
void gotoxy(int x, int y);
void rand_point(void);
void move_snake(void);
int main(void)
{
int speed = 0;
char tmpDirection = 0;
body_t tmpBody = { 0, 0 };
system("mode con cols=40 lines=20");
while (1)
{
printf("请选择游戏级别(1~9,9的难度最高):");
scanf("%d", &speed);
if (speed <= 9 || speed >= 1)
break;
}
speed = 1000 - speed * 100;
init_game(); //初始化游戏
/* 游戏的主要部分 */
while (1)
{
/* 用作调试时显示随机点的坐标 */
#if DEBUG
gotoxy(1, 1);
printf("%d,%d", randPoint.x, randPoint.y);
#endif
Sleep(speed); //用于控制蛇移动的速度
/* 键盘操控方向 */
if (kbhit())
{
tmpDirection = getch();
/* 条件判断是否按键为w,a,s,d,且蛇不会回头 */
if (('w' == tmpDirection || 's' == tmpDirection || 'a' == tmpDirection || 'd' == tmpDirection) && 'w' + 's' != tmpDirection + direction && 'a' + 'd' != tmpDirection + direction)
{
direction = tmpDirection;
}
}
/* 蛇头与随机点重合 */
if (randPoint.x == snake[1].x && randPoint.y == snake[1].y)
{
tmpBody = snake[length];
system("cls");
move_snake();
snake[length + 1] = tmpBody;
gotoxy(snake[length + 1].x, snake[length + 1].y);
printf("▇");
length++;
rand_point();
}
else
{
system("cls");
move_snake();
}
}
getchar();
return 0;
}
void init_game(void)
{
int i = 0;
/* 生成一条长度为4的小蛇 */
for(i = 1; i <= 4; i++)
{
snake[i].x = 21 - i * 2;
snake[i].y = 10;
}
length = INIT_LENGTH;
print_snake();
/* 随机生成一个点 */
rand_point();
}
/* 利用gotoxy函数打印出蛇 */
void print_snake(void)
{
int i = 0;
for(i = 1; i <= length; i++)
{
gotoxy(snake[i].x, snake[i].y);
printf("▇");
}
}
/* 直接从别人那儿拿来用了,看不懂~~~~(>_<)~~~~ */
void gotoxy(int x, int y)
{
COORD pos;
pos.X = x - 1;
pos.Y = y - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
/* 生成随机点, 随机种子为 当前时间 x 189 */
void rand_point(void)
{
int i = 0;
srand((int)time(0) * 182);
/* 判断随机生成的点是否与蛇身重合,若重合,则重新生成 */
while (1)
{
randPoint.x = (int)(20 * rand() / RAND_MAX) * 2 + 1;
randPoint.y = (int)(20 * rand() / RAND_MAX) + 1;
for (i = 1; i <= length; i++)
{
if (snake[i].x == randPoint.x && snake[i].y == randPoint.y)
break;
}
if (i > length)
break;
}
gotoxy(randPoint.x, randPoint.y);
printf("▇");
}
/* 移动,程序中最重要的部分 */
void move_snake(void)
{
int i = 0;
/* 将蛇身上每个点的值赋给下一个点 */
for (i = length; i >= 2; i--)
{
snake[i] = snake[i - 1];
}
/* 蛇头向当前方向移动一格 */
switch (direction)
{
case 'w':snake[1].y -= 1; break; //上
case 's':snake[1].y += 1; break; //下
case 'a':snake[1].x -= 2; break; //左
case 'd':snake[1].x += 2; break; //右
}
/* 判断是否遇到墙(开启穿墙模式) */
if (-1 == snake[1].x)
{
snake[1].x = 39;
}
else if (41 == snake[1].x)
{
snake[1].x = 1;
}
else if (0 == snake[1].y)
{
snake[1].y = 20;
}
else if (21 == snake[1].y)
{
snake[1].y = 1;
}
/* 判断是否咬到自己 */
for (i = 5; i <= length; i++)
{
if (snake[i].x == snake[1].x && snake[i].y == snake[1].y)
{
system("cls");
gotoxy(15, 10);
printf("GAME OVER!\n");
getchar();
exit(-1);
}
}
print_snake();
gotoxy(randPoint.x, randPoint.y);
printf("▇");
}