贪吃蛇

本文介绍了一个使用C语言实现的简单贪吃蛇游戏。游戏包括开始界面、游戏规则说明及游戏进行过程。通过键盘控制蛇的移动方向,吃到食物增加长度并得分。文章详细展示了如何利用Windows API进行屏幕刷新和坐标定位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h> 
#include<windows.h>
#define CX coord.X=x
#define CY coord.Y=y
#define SCP SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord)
char map[60][30];
struct node{
int x,y;
}shen[1000];
char xiang[4]={'w','a','s','d'}; 
void dong();
void gui();
int kai();
int youxi();
void xiao();
int main()
{
system("color 74");
COORD coord;
int x,y;
gui();
dong();
while (1)
{
int lp=kai();
if (lp==2)
{
printf("\n\n\n谢谢来玩贪吃蛇!!\n欢迎下次再玩-.-\n");
return 0;
}
int kp=youxi();
x=51;y=14;
   CX;CY;SCP;
   printf("            ");
xiao();
if (kp==-1)
{
x=51;y=12;
CX;CY;SCP;
printf("游戏中退出-.-请重新选择...");
}
else
{
x=51;y=12;
CX;CY;SCP;
printf("本次游戏得分为:%d",kp*10);
}
}
Sleep(100000);
return 0;
}
void dong()
{
COORD coord;
int x,y,i,j,R=24;
coord.X=20;
coord.Y=11;
SCP;
printf("贪吃蛇!"); 
for (i=0;i<10;i++)
{
x=i;
for (y=x;y<R-x;y++)
{
coord.X=x*2;
coord.Y=y;
SCP;
printf("0 ");
}
x=(23-i);
for (y=i;y<=x;y++)
{
coord.X=x*2;
coord.Y=y;
SCP;
printf("0 "); 
}
x=i;
for (y=x;y<R-x;y++)
{
coord.Y=x;
coord.X=y*2;
SCP;
printf("0 ");
}
x=(23-i);
for (y=i;y<=x;y++)
{
coord.Y=x;
coord.X=y*2;
SCP;
printf("0 ");
}
Sleep(100);
}
return ;
}
void gui()
{
COORD coord;
int x=50,y;
for (y=0;y<26;y++)
{
CX;CY;SCP;
printf("|");
}
x=52;y=0;
CX;CY;
SCP;
printf("游戏规则:");
x=52;y=2;
CX;CY;SCP;
printf("1. w a s d 键控制贪吃蛇的方向.");
x=52;y=4;
CX;CY;SCP;
printf("2.当贪吃蛇碰到结界或自身会死亡.");
x=52;y=6;
CX;CY;SCP;
printf("3.游戏中间按空格键会暂停.");
x=52;y=8;
CX;CY;SCP;
printf("4.游戏暂停后,按'1'键游戏继续,按'0'键会回到刚开始游戏时的选择界面.");
}
int kai()
{
COORD coord;
int x,y;
for (x=0;x<50;x++)
for (y=0;y<24;y++)
{
CX;CY;SCP;
printf(" ");
}
x=0;y=0;CX;CY;SCP;
printf("1.游戏开始\n");
printf("2.游戏结束\n");
printf("请选择:");
int lp;
scanf("%d",&lp);
return lp;
}
int youxi()
{
memset(map,0,sizeof(map));
COORD coord;
int x,y,i;
for (x=0;x<50;x++)
for (y=0;y<25;y++)
{
CX;CY;SCP;
printf(" ");
}
y=25;//蛇x为0-49,,y为0-24; 
int R=50,D=25;
for (x=0;x<50;x++)
{
CX;CY;SCP;
printf("="); 
}
int ll=0,fen=0;
shen[ll].x=rand()%8+20;
shen[ll++].y=rand()%6+10;
x=shen[ll-1].x;y=shen[ll-1].y;
CX;CY;SCP;
map[x][y]='+';
printf("+");
int p=rand()%4;
if (xiang[p]=='w')
{
for (i=0;i<3;i++)
{
shen[ll].x=shen[ll-1].x;
shen[ll].y=shen[ll-1].y-1;
ll++;x=shen[ll-1].x;y=shen[ll-1].y;
CX;CY;SCP;map[x][y]='+';
printf("+");
}
}
else if (xiang[p]=='a')
{
for (i=0;i<3;i++)
{
shen[ll].x=shen[ll-1].x-1;
shen[ll].y=shen[ll-1].y;
ll++;x=shen[ll-1].x;y=shen[ll-1].y;
CX;CY;SCP;map[x][y]='+';
printf("+");
}
}
else if (xiang[p]=='s')
{
for (i=0;i<3;i++)
{
shen[ll].x=shen[ll-1].x;
shen[ll].y=shen[ll-1].y+1;
ll++;x=shen[ll-1].x;y=shen[ll-1].y;
CX;CY;SCP;map[x][y]='+';
printf("+");
}
}
else if (xiang[p]=='d')
{
for (i=0;i<3;i++)
{
shen[ll].x=shen[ll-1].x+1;
shen[ll].y=shen[ll-1].y;
ll++;x=shen[ll-1].x;y=shen[ll-1].y;
CX;CY;SCP;map[x][y]='+';
printf("+");
}
}
int wux,wuy;
int fafe=0;//用1和0模拟 bool 类型 
int wu=0;
char zhiling,kp;
while (1)
{
wux=rand()%50;
wuy=rand()%25;
if (map[wux][wuy]!='+')
{
x=wux;y=wuy;
CX;CY;SCP;map[x][y]='#';
printf("#");
break;
}
}
while (1)
{
x=51;y=12;
CX;CY;SCP;
printf("按任意方向键开始游戏...");
if(kbhit())
{
kp=getch();
if (kp=='w'||kp=='a'||kp=='s'||kp=='d')
{
x=51;y=12;
   CX;CY;SCP;
   printf("                            ");
   break;
}
}
Sleep(100);
    }
while (1)
{
x=51;y=14;
   CX;CY;SCP;
   printf("得分:%d",fen*10);
if(kbhit())//kbhit函数判断是否有键盘输入 
   zhiling=getch();
if (zhiling!=xiang[p])
{
if (p<2&&zhiling!=xiang[p+2]||p>1&&zhiling!=xiang[p-2])//不然方向直接反向时,会直接死亡。。。 
{
if (zhiling=='w')
p=0;
else if (zhiling=='a')
p=1;
else if (zhiling=='s')
p=2;
else if (zhiling=='d')
p=3;
else if (zhiling==' ')
{
zhiling='1'; 
while (1)
{
if(kbhit())
{
kp=getch();
                   if (kp=='1')
                   break;
                   else if (kp=='0')
                   {
                    return -1;
}
}
                   
Sleep(100);
}
}
/* else if (zhiling==' ')//有点小难 
{
system("pause");
while (kp=getch())
{
if (kp=='1')
break;
if (kp=='0')
return; 
}
}
/* else
system("pause");*/
}

}
if (xiang[p]=='w')
{
shen[ll].x=shen[ll-1].x;
shen[ll].y=shen[ll-1].y-1;
ll++;x=shen[ll-1].x;y=shen[ll-1].y;
if (x==-1||x==R||y==-1||y==D||map[x][y]=='+')
{
fafe=1;
break;
}
if (map[x][y]=='#')
{
fen++;
wu=1;
}
CX;CY;SCP;map[x][y]='+';
printf("+");
if (!wu)
{
x=shen[0].x;y=shen[0].y;
CX;CY;SCP;map[x][y]=' ';
       printf(" ");
for (i=0;i<ll-1;i++)
shen[i]=shen[i+1];
ll--;
}
}
else if (xiang[p]=='a')
{
shen[ll].x=shen[ll-1].x-1;
shen[ll].y=shen[ll-1].y;
ll++;x=shen[ll-1].x;y=shen[ll-1].y;
if (x==-1||x==R||y==-1||y==D||map[x][y]=='+')
{
fafe=1;
break;
}
if (map[x][y]=='#')
{
fen++;
wu=1;
}
CX;CY;SCP;map[x][y]='+';
printf("+");
if (!wu)
{
x=shen[0].x;y=shen[0].y;
CX;CY;SCP;map[x][y]=' ';
       printf(" ");
for (i=0;i<ll-1;i++)
shen[i]=shen[i+1];
ll--;
}
}
else if (xiang[p]=='s')
{
shen[ll].x=shen[ll-1].x;
shen[ll].y=shen[ll-1].y+1;
ll++;x=shen[ll-1].x;y=shen[ll-1].y;
if (x==-1||x==R||y==-1||y==D||map[x][y]=='+')
{
fafe=1;
break;
}
if (map[x][y]=='#')
{
fen++;
wu=1;
}
CX;CY;SCP;map[x][y]='+';
printf("+");
if (!wu)
{
x=shen[0].x;y=shen[0].y;
CX;CY;SCP;map[x][y]=' ';
       printf(" ");
for (i=0;i<ll-1;i++)
shen[i]=shen[i+1];
ll--;
}
}
else if (xiang[p]=='d')
{
shen[ll].x=shen[ll-1].x+1;
shen[ll].y=shen[ll-1].y;
ll++;x=shen[ll-1].x;y=shen[ll-1].y;
if (x==-1||x==R||y==-1||y==D||map[x][y]=='+')
{
fafe=1;
break;
}
if (map[x][y]=='#')
{
fen++;
wu=1;
}
CX;CY;SCP;map[x][y]='+';
printf("+");
if (!wu)
{
x=shen[0].x;y=shen[0].y;
CX;CY;SCP;map[x][y]=' ';
       printf(" ");
for (i=0;i<ll-1;i++)
shen[i]=shen[i+1];
ll--;
}
}
if (wu)
{
while (1)
{
wux=rand()%50;
wuy=rand()%25;
if (map[wux][wuy]!='+')
{
x=wux;y=wuy;
CX;CY;SCP;map[x][y]='#';
printf("#");
break;
}
}
wu=0;
}
if (fafe)
break;
    Sleep(200);//sleep函数功能: 执行 挂起(暂停)一段时间     分为sleep (秒)  Sleep  (毫秒)   和 usleep (微秒)  这点最重要 
}
return fen;
}
void xiao()
{
COORD coord;
int x,y;
for (x=0;x<50;x++)
for (y=0;y<24;y++)
{
CX;CY;SCP;
printf(" ");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值