最近看到有人发布贪吃蛇的编码实现,想到了自己多年之前也实现过一个,发布在这里,做一下回忆。
C语言面向过程编码,基本功能实现以函数为单位,重点就是各个函数的设计和实现。
本篇使用VC6.0来实现,因为我大二大概就是2012年的时候,上手就使用了这个工具。直接上代码吧:
自定义的函数实现:
复制代码
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//文件名:map.cpp #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <conio.h> #include "map.h" #include <time.h> int a[H][W]; //游戏区域 int s[H*W][2]; //蛇的动态长度 int sLength; //蛇的初始长度 int direction; //蛇的方向 int score=0; //分数记录 bool eated = false; void init() //界面初始化 { srand((unsigned)time(NULL)); //设置光标隐藏 CONSOLE_CURSOR_INFO cursor_info = {1,0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); int i,j; for(i=0;i<H;i++) { a[i][0]=1; a[i][W-1]=1; } for(j=0;j<W;j++) { a[0][j]=1; a[H-1][j]=1; } sLength=4; s[0][0]=H/2; s[0][1]=W/2; for(i=1;i<4;i++) { s[i][0]=s[0][0]+i; s[i][1]=s[0][1]; } direction=UP; } void gotoxy(int i,int j) //移动光标到指定点 { COORD position={j,i}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position); } void drawmap() //画地图 { int i,j; for(i=0;i<H;i++) { for(j=0;j<W;j++) { if(a[i][j]==0) printf(" "); else printf("*"); } printf("n"); } } void drawSnake() //画蛇 { int i; for(i=0;i<sLength;i++) { gotoxy(s[i][0],s[i][1]); printf("@"); } } void move() { int i; gotoxy(s[sLength-1][0],s[sLength-1][1]); printf(" "); if(eated) { sLength++; eated=false; } for(i=sLength-1;i>0;i--) { s[i][0]=s[i-1][0]; s[i][1]=s[i-1][1]; } switch(direction) { case UP: s[0][0]--; break; case DOWN: s[0][0]++; break; case LEFT: s[0][1]--; break; case RIGHT: s[0][1]++; break; } } void key() { if(_kbhit()!=0) { char in; while(!_kbhit()==0) in = _getch(); switch(in) { case 'W': case 'w': if(direction!=DOWN) direction=UP; break; case 'S': case 's': if(direction!=UP) direction=DOWN; break; case 'A': case 'a': if(direction!=RIGHT) direction=LEFT; break; case 'D': case 'd': if(direction!=LEFT) direction=RIGHT; break; case 'P': case 'p': gotoxy(H,0); system("pause"); gotoxy(H,0); printf(" "); break; } } } int check(int ii,int jj) //判断这个点是否可以投放食物 { if(a[ii][jj]==1) return 0; int i; for(i=0;i<sLength;i++) { if(ii==s[i][0]&&jj==s[i][1]) return 0; } if(ii==0||ii==H-1||jj==0||jj==W-1) return 0; return 1; } void food() //投放食物函数 { int i,j; do { i=rand()%H; j=rand()&W; }while(check(i,j)==0); a[i][j]=-1; gotoxy(i,j); printf("$"); } bool gameOver() { bool isGameOver = false; int sX = s[0][0],sY=s[0][1]; // 蛇头的X坐标和Y坐标 if(sX==0||sX==H-1||sY==0||sY==W-1) isGameOver = true; for(int i=1;i<sLength;i++) { if(s[i][0]==sX&&s[i][1]==sY) isGameOver = true; } return isGameOver; } void printfScore() { gotoxy(0,W+2); printf(" EatSnake game!"); gotoxy(1,W+2); printf(" JYH制作"); gotoxy(7,W+2); printf(" 得分:%d",score); gotoxy(3,W+2); printf("操作:W:上 S:下"); gotoxy(4,W+2); printf(" A:左 D:右"); gotoxy(5,W+2); printf(" P:暂停"); }
头文件实现:
复制代码
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//文件名 map.h #ifndef _MAP_H_ #define _MAP_H_ #define H 23 #define W 55 #define WAIT_TIME 500 #define UP 0 #define DOWN 1 #define LEFT 2 #define RIGHT 3 extern int a[H][W]; //游戏区域 extern int s[H*W][2]; //蛇的动态长度 extern int sLength; //蛇的初始长度 extern int direction; //蛇的方向 extern int score; extern bool eated; void init(); void drawmap(); void drawSnake(); void key(); void move(); int check(int ii,int jj); void food(); bool gameOver(); void printfScore(); #endif
主函数实现:
复制代码
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//文件名:main.cpp #include <stdio.h> #include <stdlib.h> #include <windows.h> #include "map.h" int main() { init(); drawmap(); food(); while(1) { drawSnake(); printfScore(); Sleep(WAIT_TIME); key(); move(); if(gameOver()) { system("cls"); printf("Game Over! n"); system("pause"); } if(a[s[0][0]][s[0][1]]==-1) { eated = true; score+=10; food(); a[s[0][0]][s[0][1]]=0; } } getchar(); return 0; }
运行效果:
不足之处,是只能运行一次,game over之后,需要重新ctrl+F5 。
最后
以上就是无聊西牛最近收集整理的关于C语言 VC6.0控制台编写一个贪吃蛇游戏的全部内容,更多相关C语言内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复