贪吃蛇 (c++)

这是我花了两个小时才完成的

玩法:

  • 第一次输入是地图的大小、地图是正方形
  • 第二次是速度,即蛇两次移动的时间间隔=输入的数字*0.1s

如发现 bug 请联系我

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

#include <conio.h>
#include <windows.h>
using namespace std;

int n,mid=100,dif,headx,heady,flag,score;
int bx[]={0,-1,1,0,0}, by[]={0,0,0,-1,1};
int dox[15][15], doy[15][15], upx[15][15], upy[15][15], End[15][15];
char che[15][15];

int () {
int res=0;
char ch;
do {
ch=getch ();
switch (ch) {
case 72 : res=1; break ;
case 80 : res=2; break ;
case 75 : res=3; break ;
case 77 : res=4; break ;
default : break ;
}
} while (!res) ;
return res;
}

void Clean () {
system ("cls");
return ;
}

void Out () {
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
Clean ();
printf ("Your score : %dn",score);
for (int i=1;i<=n;i++) printf("__");
puts ("");
for (int i=1;i<=n;i++)
{
printf ("|");
for (int j=1;j<=n;j++)
{
if (che[i][j]=='@') {
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED);
printf("%c",che[i][j]);
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//三色相加,白色
printf("|");
}
else if (che[i][j]=='#') {
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);//绿色和蓝色相加,青色
printf("%c",che[i][j]);
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//三色相加,白色
printf("|");
}
else if (che[i][j]=='+') {
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);//红色和绿色相加,黄色
printf("%c",che[i][j]);
SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//三色相加,白色
printf("|");
}
else printf("%c|",che[i][j]);
}
puts ("");
for (int j=1;j<=n;j++) printf("__");
puts ("");
}
}

/* + @######## */

void Food () {
int x,y;
do {
x=rand()%n+1;
y=rand()%n+1;
} while (che[x][y]!=' ');
che[x][y]='+';
return ;
}

void Be () {
puts ("input a number which as the length of chessboard.");
puts ("warning !!! the number should smaller than ten.");
scanf ("%d",&n);
Clean ();
puts ("Enter a number(1~10) to determine the difficulty of the game.");
puts ("The time interval is the input number *100ms.");
scanf("%d",&dif);
mid*=dif;

for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++) che[i][j]=' ';

int x=rand()%n+1, y=rand()%n+1;
che[x][y]='@';
End[x][y]=1;
headx=x; heady=y;
flag=rand()%4+1;
do {
x=headx+bx[flag];
y=heady+by[flag];
if (x && y && x<=n && y<=n) break ;
flag=(flag+1)%4;
if (!flag) flag=1;
} while (1) ;
Food ();
Out ();
}

void is_fail (int x,int y) {
if (!x || !y || x>n || y>n || (che[x][y]!=' ' && che[x][y]!='+')) {
puts ("Emnnnnnn......");
puts ("Game Over");
system ("pause");
exit(0);
}
return ;
}

void Eat (int x,int y) {
score+=10-dif+1;
che[x][y]='@';
che[headx][heady]='#';
upx[headx][heady]=x;
upy[headx][heady]=y;
dox[x][y]=headx;
doy[x][y]=heady;
headx=x; heady=y;
Food ();
}

void Go (int x) {
int tox=headx+bx[x], toy=heady+by[x];
is_fail (tox,toy);
if (che[tox][toy]=='+') Eat (tox,toy);
else {
upx[headx][heady]=tox;
upy[headx][heady]=toy;
dox[tox][toy]=headx;
doy[tox][toy]=heady;
che[headx][heady]='#';
che[tox][toy]='@';
headx=tox; heady=toy;
while (!End[tox][toy]) {
int nowx=dox[tox][toy];
int nowy=doy[tox][toy];
if (End[nowx][nowy]) {
che[nowx][nowy]=' ';
End[nowx][nowy]=0;
End[tox][toy]=1;
break ;
}
tox=nowx; toy=nowy;
}
}
Out ();
return ;
}

void Move () {
double l=clock ();
int tim=1;
while ((tim=(clock()-l<mid)) && !kbhit ()) ;
if(!tim) {
Go (flag);
return ;
}

int want=Get ();
if (want==1 && flag==2) return ;
if (want==2 && flag==1) return ;
if (want==3 && flag==4) return ;
if (want==4 && flag==3) return ;
flag=want;
Go (flag);
return ;
}

int main ()
{
srand (time(0));
Be ();

while (1) Move ();
}