C语言 贪吃蛇游戏

作者在 2016-01-19 12:08:19 发布以下内容

C语言 贪吃蛇游戏     

?
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
//******************************************************* 
//******************************************************* 
//******友情提示:如想速度快点,请改小_sleep(500)函数中参数***** 
//******************************************************* 
//*******************如写的不好,请见谅********************* 
//******************************************************* 
#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 
#include <string.h> 
#include <time.h> 
constintH = 8;  //地图的高 
constintL = 16; //地图的长 
charGameMap[H][L];  //游戏地图 
int key; //按键保存 
int sum = 1, over = 0; //蛇的长度, 游戏结束(自吃或碰墙) 
int dx[4] = {0, 0, -1, 1}; //左、右、上、下的方向 
int dy[4] = {-1, 1, 0, 0}; 
structSnake  //蛇的每个节点的数据类型 
 intx, y; //左边位置 
 intnow;  //保存当前节点的方向, 0,1,2,3分别为左右上下 
}Snake[H*L]; 
constcharShead ='@'; //蛇头 
constcharSbody ='#'; //蛇身 
constcharSfood ='*'; //食物 
constcharSnode ='.'; //'.'在地图上标示为空 
voidInitial(); //地图的初始化 
voidCreate_Food();//在地图上随机产生食物 
voidShow();  //刷新显示地图 
voidButton(); //取出按键,并判断方向 
voidMove();  //蛇的移动 
voidCheck_Border(); //检查蛇头是否越界 
voidCheck_Head(intx,inty);  //检查蛇头移动后的位置情况 
intmain()  
 Initial(); 
 Show(); 
 return0; 
voidInitial() //地图的初始化 
 inti, j; 
 inthx, hy; 
 system("title 贪吃蛇"); //控制台的标题 
 memset(GameMap,'.',sizeof(GameMap)); //初始化地图全部为空'.' 
 system("cls"); 
 srand(time(0));  //随机种子 
 hx =rand()%H;   //产生蛇头 
 hy =rand()%L; 
 GameMap[hx][hy] = Shead; 
 Snake[0].x = hx;  Snake[0].y = hy; 
 Snake[0].now = -1; 
 Create_Food();  //随机产生食物 
 for(i = 0; i < H; i++)  //地图显示 
 {  
  for(j = 0; j < L; j++) 
   printf("%c", GameMap[i][j]); 
  printf("\n"); 
 } 
     
 printf("\n小小C语言贪吃蛇\n"); 
 printf("按任意方向键开始游戏\n"); 
    
 getch();  //先接受一个按键,使蛇开始往该方向走 
 Button(); //取出按键,并判断方向 
voidCreate_Food() //在地图上随机产生食物 
 intfx, fy; 
 while(1) 
 { 
  fx =rand()%H; 
     fy =rand()%L; 
     
  if(GameMap[fx][fy] =='.') //不能出现在蛇所占有的位置 
  {  
   GameMap[fx][fy] = Sfood; 
默认分类 | 阅读 2767 次
文章评论,共1条
zlsxhlp
2016-02-07 19:50
1
666
游客请输入验证码
文章分类
文章归档
最新评论