失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > c语言写的贪吃蛇在哪能运行 有没有可以帮我写c语言贪吃蛇的代码 cmd运行的

c语言写的贪吃蛇在哪能运行 有没有可以帮我写c语言贪吃蛇的代码 cmd运行的

时间:2023-09-29 03:30:32

相关推荐

c语言写的贪吃蛇在哪能运行 有没有可以帮我写c语言贪吃蛇的代码 cmd运行的

匿名用户

1级

-01-25 回答

//codebywlfryq71693456@

#include

#include

#include

#include

#include

#include

#include

#defineW80//屏幕宽度

#defineH37//屏幕高度

#defineSNAKE_ALL_LENGTH200//蛇身最长为

voidCALLBACKTimerProc(

HWNDhwnd,

UINTmessage,

UINTidTimer,

DWORDdwTime);

voidstart();

structMYPOINT

{

intx;

inty;

}s[SNAKE_ALL_LENGTH],head,end,food;

intmax_count=0;//历史最高分,如果count>max_count,则max_count=count

intold_max_count=0;//历史最高分,不会变动,用于死亡时判断max_count是否大于old_max_count,如果大于,则写入文件

intcount=0;//得分

intlen=20;//当前蛇身长度

intdirect=0;//方向:0-向右,1-向下,2-向左,3-向上

intspeed=200;//速度:毫秒

boolisfood=false;//食物是否存在

inttimerID;

boolstop=false;//暂停

char*ini_path;//数据文件绝对路径

voidsetxy(intx,inty)//设置CMD窗口光标位置

{

COORDcoord={x,y};

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);

}

voidhide_cursor()//隐藏CMD窗口光标

{

CONSOLE_CURSOR_INFOcci;

cci.bVisible=FALSE;

cci.dwSize=sizeof(cci);

HANDLEhandle=GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleCursorInfo(handle,&cci);

}

voidset_food()//设置食物坐标

{

if(isfood==true)

{

return;

}

intx,y,i;

boolflag=false;

while(1)

{

flag=false;

x=rand()%(W-14)+6;

y=rand()%(H-12)+6;

for(i=0;i

{

if(s[i].x==x&&s[i].y==y)

{

flag=true;

break;

}

}

if(flag==true)

{

continue;

}

else

{

food.x=x;

food.y=y;

break;

}

}

setxy(food.x,food.y);

printf("*");

isfood=true;

}

voidcheck_board()//检测蛇身是否越界和相交

{

inti;

if(s[0].x>=W-3||s[0].x<=2||s[0].y>=H-1||s[0].y<=2)

{

setxy(W/2-5,0);

printf("gameover\n");

stop=true;

if(old_max_count

{

chart[5]={'\0'};

sprintf(t,"%d",max_count);

WritePrivateProfileString("MAX_COUNT","max_count",t,ini_path);

}

}

for(i=1;i

{

if(s[i].x==s[0].x&&s[i].y==s[0].y)

{

setxy(W/2-5,0);

printf("gameover\n");

stop=true;

if(old_max_count

{

chart[5]={'\0'};

sprintf(t,"%d",max_count);

WritePrivateProfileString("MAX_COUNT","max_count",t,ini_path);

}

break;

}

}

if(stop==true)

{

KillTimer(NULL,timerID);

intc;

while(1)

{

fflush(stdin);

c=_getch();

if(c=='n'||c=='N')

{

start();

}

elseif(c=='q'||c=='Q')

{

exit(0);

}

elsecontinue;

}

}

}

voidprintf_body(boolis_first)//打印蛇身

{

if(is_first==true)//如果是第一次打印蛇身

{

inti;

for(i=0;i

{

setxy(s[i].x,s[i].y);

printf("O");

}

}

else//如果不是第一次打印蛇身

{

setxy(end.x,end.y);

printf("");

setxy(s[0].x,s[0].y);

printf("O");

}

if(food.x==s[0].x&&food.y==s[0].y)//如果吃到食物

{

count++;

isfood=false;//重置食物

set_food();

len++;

KillTimer(NULL,timerID);

if(speed>100)speed-=10;

elseif(speed>50)speed-=5;

elseif(speed>30)speed-=2;

elseif(speed>16)speed-=1;

else;

setxy(0,0);

if(max_count

printf("speed:%dmsscore:%dbestscore:%d",speed,count,max_count);

timerID=SetTimer(NULL,001,speed,TimerProc);

}

}

voidchange_body_pos(intx,inty)//改变蛇身的坐标数据

{

end.x=s[len-1].x;

end.y=s[len-1].y;

inti;

for(i=len-1;i>0;i--)

{

s[i].x=s[i-1].x;

s[i].y=s[i-1].y;

}

s[0].x=x;

s[0].y=y;

}

voidCALLBACKTimerProc(

HWNDhwnd,

UINTmessage,

UINTidTimer,

DWORDdwTime)

{

switch(direct)

{

case0:

head.x++;

change_body_pos(head.x,head.y);

printf_body(false);

check_board();

break;

case1:

head.y++;

change_body_pos(head.x,head.y);

printf_body(false);

check_board();

break;

case2:

head.x--;

change_body_pos(head.x,head.y);

printf_body(false);

check_board();

break;

case3:

head.y--;

change_body_pos(head.x,head.y);

printf_body(false);

check_board();

break;

}

}

voidstart()

{

inti;

KillTimer(NULL,timerID);

count=0;//得分

len=20;//当前蛇身长度

direct=0;//方向:0-向右,1-向下,2-向左,3-向上

speed=200;//速度:毫秒

isfood=false;//食物是否存在

stop=false;//停止

system("cls");

setxy(1,4);

printf("┌─────────────────────────────────────┐\n");

for(i=0;i<33;i++)

{

printf("││\n");

}

printf("└─────────────────────────────────────┘");

head.x=len-1+5;

head.y=H/2;

for(i=0;i

{

s[i].x=head.x-i;

s[i].y=head.y;

}

setxy(0,0);

printf("speed:%d:msscore:%dbestscore:%d",speed,count,max_count);

printf_body(true);

set_food();

timerID=SetTimer(NULL,001,speed,TimerProc);

intc;

MSGmsg;

while(GetMessage(&msg,NULL,0,0))

{

if(stop==true)break;

if(_kbhit())//如果按下的是方向键或功能键,_getch()要调用两次,第一次返回0XE0或0

{

fflush(stdin);

c=_getch();//上:72下:80左:75右:77

if(c==0XE0||c==0)

{

c=_getch();

if(c==72&&direct!=1&&direct!=3)

{

direct=3;

}

elseif(c==80&&direct!=1&&direct!=3)

{

direct=1;

}

elseif(c==75&&direct!=0&&direct!=2)

{

direct=2;

}

elseif(c==77&&direct!=0&&direct!=2)

{

direct=0;

}

}

elseif(c=='')

{

setxy(W/2-10,0);

system("pause");

setxy(W/2-10,0);

printf("");

}

}

if(msg.message==WM_TIMER)

{

DispatchMessage(&msg);

}

}

}

intmain()

{

ini_path=(char*)malloc(sizeof(char)*50);

srand((unsigned)time(0));

getcwd(ini_path,50);//取得当前程序绝对路径

ini_path=strcat(ini_path,"snake.dat");

max_count=GetPrivateProfileInt("MAX_COUNT","max_count",0,ini_path);

old_max_count=max_count;

charcmd[50];

sprintf(cmd,"modeconcols=%dlines=%d\0",W,H);

system(cmd);//改变CMD窗口大小

hide_cursor();

start();

return0;

}

追问:

可以运行吗

追答:

你试下呗, DEV C++下测试通过

如果觉得《c语言写的贪吃蛇在哪能运行 有没有可以帮我写c语言贪吃蛇的代码 cmd运行的》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。