c语言贪吃蛇简易版:规则很简单,每按一下移动一格,用‘wsad’控制方向
第一版(snakemove):
#include
#include
#include
int map[12][12]={0};//游戏地图,全局变量让函数也能修改
//0是空格,1是蛇头,>1是蛇身,-1是边框
void output(void);
void begin(void);
void move(int dir);
int main()
{
srand(time(NULL));
int dir=6;//dir初设化为6,2向下,8向上,4向左,6向右
char act;
begin();
//system("cls"); //清屏
output();
while(scanf("%c",&act)!=EOF){
switch(act) {
case 'a':
dir=4;
move(dir);
output();
break;
case 'd':
dir=6;
move(dir);
output();
break;
case 'w':
dir=8;
move(dir);
output();
break;
case 's':
dir=2;
move(dir);
output();
break;
default:
break;
}
}
return 0;
}
void output(void){
int x,y;
for(x=0;x<12;x++){
for(y=0;y<12;y++){
if(map[x][y]==0){
printf(" ");
}
else if(map[x][y]==1){
printf("H");
}
else if(map[x][y]==-1){
printf("*");
}
else if(map[x][y]>1){
printf("X");
}
}
printf("\n");
}
}
void begin(void){
int x,y,m=5;
for(x=0;x<12;x++){
for(y=0;y<12;y++){
if(x==0||x==11){
map[x][y]=-1;
}
else if(y==0||y==11){
map[x][y]=-1;
}//初始化边框;
else if(x==1&&m>0){
map[x][y]=m--;
}
}
}
}
void move(int dir){
int old_i,old_j,coun,x,max=0,newh_i,newh_j;
for(coun=0;coun<12;coun++){
for(x=0;x<12;x++){
if(map[coun][x]==1){
newh_i=coun;newh_j=x;
}
if(map[coun][x]>0){
map[coun][x]++;
}
if(max<map[coun][x]){
max=map[coun][x];
old_i=coun;
old_j=x;
}
}
}
map[old_i][old_j]=0;
if(dir==8){
newh_i=newh_i-1;
map[newh_i][newh_j]=1;
}
else if(dir==2){
newh_i=newh_i+1;
map[newh_i][newh_j]=1;
}
else if(dir==4){
newh_j=newh_j-1;
map[newh_i][newh_j]=1;
}
else if(dir==6){
newh_j=newh_j+1;
map[newh_i][newh_j]=1;
}
}
第二版(snakeeat):加入了随机放置的食物,每吃到一个食物身体变长(其实只是没有把蛇尾变为0)
#include
#include
#include
int map[12][12]={0};//游戏地图,全局变量让函数也能修改
//0是空格,1是蛇头,>1是蛇身,-1是边框 ,-2是食物
void output(void);
void begin(void);
void move(int dir);
void food();//随机产生食物,在蛇头,蛇身,边框以及食物之外;
int f=1;//初始化生成一个食物;
int main()
{
int dir=6;//dir初设化为6,2向下,8向上,4向左,6向右
char act;
printf("press 'w''s''a''d' to control the snake\n");
begin();
//system("cls"); //清屏
food();
output();
while(scanf("%c",&act)!=EOF){
switch(act) {
case 'a':
dir=4;
move(dir);
output();
break;
case 'd':
dir=6;
move(dir);
output();
break;
case 'w':
dir=8;
move(dir);
output();
break;
case 's':
dir=2;
move(dir);
output();
break;
default:
break;
}
}
return 0;
}
void output(void){
int x,y;
for(x=0;x<12;x++){
for(y=0;y<12;y++){
if(map[x][y]==0){
printf(" ");
}
else if(map[x][y]==1){
printf("H");
}
else if(map[x][y]==-1){
printf("*");
}
else if(map[x][y]>1){
printf("X");
}
else if(map[x][y]==-2){
printf("$");
}
}
printf("\n");
}
}
void begin(void){
int x,y,m=5;
for(x=0;x<12;x++){
for(y=0;y<12;y++){
if(x==0||x==11){
map[x][y]=-1;
}
else if(y==0||y==11){
map[x][y]=-1;
}//初始化边框;
else if(x==1&&m>0){
map[x][y]=m--;
}
}
}
}
void move(int dir){
int old_i,old_j,coun,x,max=0,newh_i,newh_j;
for(coun=0;coun<12;coun++){
for(x=0;x<12;x++){
if(map[coun][x]==1){
newh_i=coun;newh_j=x;
}
if(map[coun][x]>0){
map[coun][x]++;
}
if(max<map[coun][x]){
max=map[coun][x];
old_i=coun;
old_j=x;
}
}
}
if(dir==8){
newh_i=newh_i-1;
if(map[newh_i][newh_j]>0||map[newh_i][newh_j]==-1){
printf("game over\n");
exit(0);
}
else if(map[newh_i][newh_j]==0){
map[newh_i][newh_j]=1;
map[old_i][old_j]=0;
}
else if(map[newh_i][newh_j]==-2){
map[newh_i][newh_j]=1;
f=1;
food();
}
}
else if(dir==2){
newh_i=newh_i+1;
if(map[newh_i][newh_j]>0||map[newh_i][newh_j]==-1){
printf("game over\n");
exit(0);
}
else if(map[newh_i][newh_j]==0){
map[newh_i][newh_j]=1;
map[old_i][old_j]=0;
}
else if(map[newh_i][newh_j]==-2){
map[newh_i][newh_j]=1;
f=1;
food();
}
}
else if(dir==4){
newh_j=newh_j-1;
if(map[newh_i][newh_j]>0||map[newh_i][newh_j]==-1){
printf("game over\n");
exit(0);
}
else if(map[newh_i][newh_j]==0){
map[newh_i][newh_j]=1;
map[old_i][old_j]=0;
}
else if(map[newh_i][newh_j]==-2){
map[newh_i][newh_j]=1;
f=1;
food();
}
}
else if(dir==6){
newh_j=newh_j+1;
if(map[newh_i][newh_j]>0||map[newh_i][newh_j]==-1){
printf("game over\n");
exit(0);
}
else if(map[newh_i][newh_j]==0){
map[newh_i][newh_j]=1;
map[old_i][old_j]=0;
}
else if(map[newh_i][newh_j]==-2){
map[newh_i][newh_j]=1;
f=1;
food();
}
}
}
void food(){
int x,y;
srand(time(NULL));
x=rand()%10+1;
y=rand()%10+1;
while(f==1){
if(map[x][y]==0){
map[x][y]=-2;
f=0;
}
}
}