当前位置:首页 » 编程语言 » c语言编的贪吃蛇

c语言编的贪吃蛇

发布时间: 2025-08-28 04:33:28

⑴ 求c语言的贪吃蛇的代码 能运行的

//ConsoleApplication1.cpp:定义控制台应用程序的入口点。
//

#include"stdafx.h"

#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<conio.h>


#defineframe_height20//definemapsize
#defineframe_width40
#defineUP'w'//defineoperatekey
#defineDOWN's'
#defineLEFT'a'
#defineRIGHT'd'


inti,j,k;
charch=UP;//initialdirection
intgrow=0;//flag:ifsnakegrow


structFood{
intx;
inty;
}food;
structSnake{
intx[50];
inty[50];
intlen;
intspeed;
}snake;//snake[0]ishead


voidinit_map(void);
voipdate_food(void);
voidmove_snake(void);
intis_alive(void);
voidget_speed(void);
voidgotoxy(intx,inty);


intmain()
{
init_map();//初始化地图
while(1)
{
update_food();
//是否产生食物
get_speed();//获取速度
move_snake();//移动蛇身
Sleep(snake.speed);
//移动速度
if(!(is_alive()))
//蛇的死活(撞墙或自食)
break;
}
printf("GameOver!");
_getch();


return0;
}
//initialize
voidinit_map(void)
{
//initialfood
srand(time(NULL));
food.x=rand()%(frame_height-2)+1;
food.y=rand()%(frame_width-2)+1;
gotoxy(food.x,food.y);
printf("!");
//initialsnake
snake.x[0]=frame_height/2;
snake.y[0]=frame_width/2;
gotoxy(snake.x[0],snake.y[0]);
printf("@");
snake.len=3;
snake.speed=200;
for(k=1;k<snake.len;k++)
{
snake.x[k]=snake.x[k-1]+1;
snake.y[k]=snake.y[k-1];
gotoxy(snake.x[k],snake.y[k]);
printf("@");
}
//initialbar
for(j=0;j<frame_width;j++)
{
gotoxy(0,j);
printf("#");
gotoxy(frame_height-1,j);
printf("#");
}
for(i=1;i<frame_height-1;i++)
{
gotoxy(i,0);
printf("#");
gotoxy(i,frame_width-1);
printf("#");
}
}
//generatefood
voipdate_food()
{
if(snake.x[0]==food.x&&snake.y[0]==food.y)
{
srand(time(NULL));
food.x=rand()%(frame_height-2)+1;
food.y=rand()%(frame_width-2)+1;
gotoxy(food.x,food.y);
printf("!");


snake.len++;
grow=1;
}
}
//movesnake
voidmove_snake()
{
if(_kbhit())
ch=_getch();
if(!grow)
{
gotoxy(snake.x[snake.len-1],snake.y[snake.len-1]);
printf("");
}
for(k=snake.len-1;k>0;k--)
{
snake.x[k]=snake.x[k-1];
snake.y[k]=snake.y[k-1];
}
switch(ch)
{
caseUP:snake.x[0]--;break;
caseDOWN:snake.x[0]++;break;
caseLEFT:snake.y[0]--;break;
caseRIGHT:snake.y[0]++;break;
default:break;
}
gotoxy(snake.x[0],snake.y[0]);
printf("@");
grow=0;
gotoxy(frame_height,0);
}
//isalive
intis_alive(void)
{
if(snake.x[0]==0||snake.x[0]==frame_height-1||snake.y[0]==frame_width-1||snake.y[0]==0)
return0;
for(k=1;k<snake.len;k++)
if(snake.x[k]==snake.x[0]&&snake.y[k]==snake.y[0])
return0;
return1;
}
//speep
voidget_speed(void)
{
if(snake.len<=6)
snake.speed=200;
elseif(snake.len<=10)
snake.speed=100;
elseif(snake.len<=20)
snake.speed=50;
elseif(snake.len<=30)
snake.speed=30;
elsesnake.speed=20;
}
//movecursor
voidgotoxy(intx,inty)
{
HANDLEhout;
COORDcor;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
cor.X=y;
cor.Y=x;
SetConsoleCursorPosition(hout,cor);
}

⑵ 如何用C语言写贪吃蛇

#include<conio.h> #include<graphics.h> #include<time.h> #include<string.h> #include<malloc.h> #include<stdio.h> int grade=5,point=0,life=3; void set(),menu(),move_head(),move_body(),move(),init_insect(),left(),upon(),right(),down(),init_graph(),food_f(),ahead(),crate(); struct bug { int x; int y; struct bug *last; struct bug *next; }; struct fd { int x; int y; int judge; }food={0,0,0}; struct bug *head_f=NULL,*head_l,*p1=NULL,*p2=NULL; void main() { char ch; initgraph(800,600); set(); init_insect(); while(1) { food_f(); Sleep(grade*10); setcolor(BLACK); circle(head_l->x,head_l->y,2); setcolor(WHITE); move_body(); if(kbhit()) { ch=getch(); if(ch==27) { ahead(); set(); } else if(ch==-32) { switch(getch()) { case 72:upon();break; case 80:down();break; case 75:left();break; case 77:right();break; } } else ahead(); } else { ahead(); } if(head_f->x==food.x&&head_f->y==food.y) { Sleep(100); crate(); food.judge=0; point=point+(6-grade)*10; if(food.x<30||food.y<30||food.x>570||food.y>570) life++; menu(); } if(head_f->x<5||head_f->x>595||head_f->y<5||head_f->y>595) { Sleep(1000); life--; food.judge=0; init_graph(); init_insect(); menu(); } for(p1=head_f->next;p1!=NULL;p1=p1->next) { if(head_f->x==p1->x&&head_f->y==p1->y) { Sleep(1000); life--; food.judge=0; init_graph(); init_insect(); menu(); break; } } if(life==0) { outtextxy(280,300,"游戏结束!"); getch(); return; } move(); }; } void init_graph() { clearviewport(); setlinestyle(PS_SOLID,1,5); rectangle(2,2,600,598); setlinestyle(PS_SOLID,1,1); } void set() { init_graph(); outtextxy(640,50,"1、开始 / 返回"); outtextxy(640,70,"2、退出"); outtextxy(640,90,"3、难度"); outtextxy(640,110,"4、重新开始"); switch(getch()) { case '1': menu();setcolor(GREEN);circle(food.x,food.y,2);setcolor(WHITE);return; case '2': exit(0);break; case '3': outtextxy(700,90,":1 2 3 4 5");grade=getch()-48;set();break; case '4': food.judge=0,grade=5;point=0;life=3;init_insect();menu();break; default: outtextxy(250,300,"输入错误!"); set();break; } } void menu() { char str[20],str1[]={"分数:"},str2[]={"难度:"},str3[]={"生命值:"}; init_graph(); sprintf(str,"%d",point); strcat(str1,str); outtextxy(640,50,str1); sprintf(str,"%d",grade); strcat(str2,str); outtextxy(640,70,str2); sprintf(str,"%d",life); strcat(str3,str); outtextxy(640,90,str3); outtextxy(640,110,"设置:ESC"); } void init_insect() { head_f=(struct bug *)malloc(sizeof(struct bug)); head_f->last=NULL; head_f->x=300; head_f->y=300; p2=head_f->next=p1=(struct bug *)malloc(sizeof(struct bug)); p1->last=head_f; p1->x=295; p1->y=300; p1=p1->next=(struct bug *)malloc(sizeof(struct bug)); p1->next=NULL; p1->x=290; p1->y=300; p1->last=p2; head_l=p1; } void move() { for(p1=head_f;p1!=NULL;p1=p1->next) { circle(p1->x,p1->y,2); } } void move_head() { } void move_body() { for(p1=head_l,p2=p1->last;p2!=NULL;p1=p2,p2=p2->last) { p1->x=p2->x; p1->y=p2->y; } } void ahead() { p1=head_f; p2=p1->next; p2=p2->next; if(p1->x==p2->x) { if(p1->y>p2->y) head_f->y+=5; else head_f->y-=5; } else { if(p1->x>p2->x) { head_f->x+=5; } else head_f->x-=5; } } void upon() { p1=head_f->next; p1=p1->next; head_f->y-=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->y+=5; ahead(); } } void down() { p1=head_f->next; p1=p1->next; head_f->y+=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->y-=5; ahead(); } } void left() { p1=head_f->next; p1=p1->next; head_f->x-=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->x+=5; ahead(); } } void right() { p1=head_f->next; p1=p1->next; head_f->x+=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->x-=5; ahead(); } } void food_f() { if(!food.judge) { food.x=(rand()%117+1)*5; food.y=(rand()%117+1)*5; food.judge=1; if(food.x<30||food.y<30||food.x>570||food.y>570) { setcolor(RED); circle(f

⑶ c语言贪吃蛇源代码怎么用

C语言贪吃蛇源代码必须经过相应的C/C++编译器编译成EXE文件后才能运行。

由于我们通常使用的操作系统是Windows系统,而在该系统下最长用的C/C++编译器是VC++编译器,目前在大专院校常用的版本还是VC++6.0

下面就以VC++6.0来说明编译过程:

1.在VC++6.0中通过“File”菜单下的 “Open”子菜单打开贪吃蛇代码

/*
C/C++贪吃蛇游戏,zjlj,2015.3.16
*/
#defineDEBUG0//当程序在调试阶段时DEBUG为1
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
usingnamespacestd;
voidreadini(FILE**fphead,int*score,char*argv[])//创建或打开一个和运行文件对应的ini文件,读取最高纪录
{
charfilename[200],*pfilename;
intflag=-1,i;

strcpy(filename,argv[0]);
for(i=0;filename[i]!='';i++)
{
if('.'==filename[i])flag=1;
}

if(1==flag)
{
filename[i-1]='i';
filename[i-2]='n';
filename[i-3]='i';
}
else
{
filename[i]='.';
filename[i+1]='i';
filename[i+2]='n';
filename[i+3]='i';
filename[i+4]='';
}
for(;filename[i]!='\'&&i>=0;i--)pfilename=&filename[i];
if((*fphead=fopen(pfilename,"rb+"))==NULL)
{
if((*fphead=fopen(pfilename,"wb+"))==NULL)
{
printf("无法创建或打开"%s"文件 ",pfilename);
system("pause");
exit(0);
}
}
else
{
fread(score,sizeof(int),1,*fphead);
}
}
voidwriteini(FILE**fphead,int*score,char*argv[])//打开一个和运行文件对应的ini文件,写入最高纪录
{
charfilename[200],*pfilename;
intflag=-1,i;

strcpy(filename,argv[0]);
for(i=0;filename[i]!='';i++)
{
if('.'==filename[i])flag=1;
}

if(1==flag)
{
filename[i-1]='i';
filename[i-2]='n';
filename[i-3]='i';
}
else
{
filename[i]='.';
filename[i+1]='i';
filename[i+2]='n';
filename[i+3]='i';
filename[i+4]='';
}
for(;filename[i]!='\'&&i>=0;i--)pfilename=&filename[i];
if((*fphead=fopen(pfilename,"wb+"))==NULL)
{
printf("无法写入"%s"文件,磁盘写保护! ",pfilename);
system("pause");
exit(0);
}
else
{
rewind(*fphead);
fwrite(score,sizeof(int),1,*fphead);
fclose(*fphead);
}
}
voidgotoxy(intx,inty)//光标定位,光标定位函数SetConsoleCursorPosition是左上角位置是0,0然后向左向下延伸
{
COORDpos;
pos.X=2*y;
pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
voidcolor(inta)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}

voidRefresh(intq[][22],intgrade,intgamespeed,intlength,intscore)//输出贪吃蛇棋盘
{
inti,j;
for(i=0;i<22;i++)
{
for(j=0;j<22;j++)
{
if(q[i][j]==0)//输出棋盘空白
{
gotoxy(i,j);
color(11);
cout<<"■";
}
if(q[i][j]==1||q[i][j]==2)//输出棋盘墙壁
{
gotoxy(i,j);
color(11);
cout<<"□";
}
if(q[i][j]==3)//输出蛇头
{
gotoxy(i,j);
color(14);
cout<<"★";
}
if(q[i][j]==4)//输出蛇身
{
gotoxy(i,j);
color(12);
cout<<"◆";
}
if(q[i][j]==5)//输出果子
{
gotoxy(i,j);
color(12);
cout<<"●";
}
}
if(i==0)cout<<" ***********************";
if(i==1)cout<<" 等级为:"<<grade;//显示等级
if(i==3)cout<<" 自动前进时间";
if(i==4)cout<<" 间隔为:"<<gamespeed<<"ms";//显示时间
if(i==6)cout<<" 历史最高分为:"<<score<<"分";
if(i==7)cout<<" 你现在得分为:"<<(length+(grade-1)*8)*10<<"分";
if(i==8)cout<<" **********************";
if(i==9)cout<<" 游戏说明:";
if(i==10)cout<<" (1)用小键盘方向键控制";
if(i==11)cout<<" 蛇头运动方向;";
if(i==12)cout<<" (2)蛇每吃一个果子蛇身";
if(i==13)cout<<" 增加一节;";
if(i==14)cout<<" (3)蛇咬到自己或碰到墙";
if(i==15)cout<<" 壁游戏结束。";
if(i==18)cout<<" **********************";
if(i==19)cout<<" C/C++语言作业:";
if(i==20)cout<<" zjlj,2015.03.16";
}
}

intmain(intargc,char*argv[]){
inttcsQipan[22][22];//贪吃蛇棋盘是一个二维数组(如22*22,包括墙壁)
inti,j,score,directiontemp;
FILE*fpini;//*fpini信息文件
readini(&fpini,&score,argv);//读取ini文件的最高纪录
if(score<0)//最高成绩小于零设置为零,初建文件会是负数
score=0;
while(1)
{
for(i=1;i<=20;i++)
for(j=1;j<=20;j++)
tcsQipan[i][j]=0;//贪吃蛇棋盘相应坐标标上中间空白部分的标志0
for(i=0;i<=21;i++)
tcsQipan[0][i]=tcsQipan[21][i]=1;//贪吃蛇棋盘相应坐标标上上下墙壁的标志1
for(i=1;i<=20;i++)
tcsQipan[i][0]=tcsQipan[i][21]=2;//贪吃蛇棋盘相应坐标标上左右墙壁的标志2
inttcsZuobiao[2][500];//蛇的坐标数组
for(i=0;i<4;i++)
{
tcsZuobiao[0][i]=1;//蛇身和蛇头的x坐标
tcsZuobiao[1][i]=i+1;//蛇身和蛇头的y坐标
}
inthead=3,tail=0;//标示蛇头和蛇尾的数组偏移量
for(i=1;i<=3;i++)
tcsQipan[1][i]=4;//蛇身
tcsQipan[1][4]=3;//蛇头
intx1,y1;//随机出果子
srand(time(0));//设置随机种子
do
{
x1=rand()%20+1;
y1=rand()%20+1;
}
while(tcsQipan[x1][y1]!=0);//如果不是在空白处重新出果子
tcsQipan[x1][y1]=5;//贪吃蛇棋盘相应坐标标上果子的标志5
color(12);
cout<<" 贪吃蛇游戏即将开始!"<<endl;//准备开始
longstart,starttemp;
intgrade=1,length=4;//设置初始等级和蛇的初始长度
intgamespeed=500;//设置初始前进时间间隔
for(i=3;i>=0;i--)
{
start=clock();
while(clock()-start<=1000);
system("cls");
if(i>0)
cout<<" 进入倒计时:"<<i<<endl;//倒计时显示
else
Refresh(tcsQipan,grade,gamespeed,length,score);//初始棋盘显示
}
inttimeover=1,otherkey=1;//初始化超时时间和按键判断参数
chardirection=77;//设置初始情况下,向右运动
intx=tcsZuobiao[0][head],y=tcsZuobiao[1][head];//保存蛇头坐标到x,y变量
while(1)//运行一局游戏
{
start=clock();
while((timeover=((starttemp=clock())-start<=gamespeed))&&!kbhit());//如果有键按下或时间超过自动前进时间间隔则终止循环
if(direction==72||direction==80||direction==75||direction==77)
directiontemp=direction;//保留上一次方向按键
//starttemp=gamespeed+start-starttemp;//保留停留时间
if(timeover)
{
#if(DEBUG==1)
direction=getch();//调试代码
#else
if((direction=getch())==-32)
direction=getch();
#endif
}
#if(DEBUG==1)//调试代码
start=clock();
while(clock()-start<=2000);
gotoxy(24,4);
cout<<" 按键ASCII代码"<<(int)direction<<""<<endl;
#endif
if(!(direction==72||direction==80||direction==75||direction==77))
{
otherkey=0;//按键非方向键,otherkey设置为0
}
else
{
otherkey=1;//按键为方向键,otherkey设置为1
}
if(direction==72&&directiontemp==80)//忽略反方向按键
{
direction=32;
otherkey=0;
//start=clock();
//while(clock()-start<=starttemp);
}
elseif(direction==80&&directiontemp==72)
{
direction=32;//设置按键为非方向键
otherkey=0;//按键为非方向键,otherkey设置为0
//start=clock();
//while(clock()-start<=starttemp);//补偿等待时间
}
elseif(direction==75&&directiontemp==77)
{
direction=32;
otherkey=0;
//start=clock();
//while(clock()-start<=starttemp);
}
elseif(direction==77&&directiontemp==75)
{
direction=32;
otherkey=0;
//start=clock();
//while(clock()-start<=starttemp);
}


switch(direction)//判断方向键
{
case72:x=tcsZuobiao[0][head]-1;y=tcsZuobiao[1][head];break;//向上
case80:x=tcsZuobiao[0][head]+1;y=tcsZuobiao[1][head];break;//向下
case75:x=tcsZuobiao[0][head];y=tcsZuobiao[1][head]-1;break;//向左
case77:x=tcsZuobiao[0][head];y=tcsZuobiao[1][head]+1;break;//向右
default:break;
}


if(x==0||x==21||y==0||y==21)//蛇头碰到墙壁,结束本局游戏
{
gotoxy(22,12);
cout<<" 游戏已结束!"<<endl;
if(score>=(length+(grade-1)*8)*10)//判断是否破记录
{
gotoxy(10,7);
color(12);
cout<<"闯关失败加油耶!"<<endl;
fclose(fpini);//关闭ini文件
}
else
{
gotoxy(10,7);
color(12);
cout<<"恭喜您打破记录"<<endl;
score=(length+(grade-1)*8)*10;
writeini(&fpini,&score,argv);//写入ini文件的最高纪录
}
gotoxy(23,12);
cout<<"按回车键重新开始,按ESC退出游戏"<<endl;//显示的提示
break;//退出该局游戏
}
if(tcsQipan[x][y]!=0&&!(x==x1&&y==y1)&&tcsQipan[x][y]!=3)//蛇头碰到蛇身,结束本局游戏
{
gotoxy(22,12);
cout<<" 游戏已结束!"<<endl;
if(score>=(length+(grade-1)*8)*10)//判断是否破记录
{
gotoxy(10,7);
color(12);
cout<<"闯关失败加油耶!"<<endl;
fclose(fpini);//关闭ini文件
}
else
{
gotoxy(10,7);
color(12);
cout<<"恭喜您打破记录"<<endl;
score=(length+(grade-1)*8)*10;
writeini(&fpini,&score,argv);//写入ini文件的最高纪录
}
gotoxy(23,12);
cout<<"按回车键重新开始,按ESC退出游戏"<<endl;//显示的提示
break;//退出该局游戏
}
/*
游戏运行时的核心算法开始
*/
if(x==x1&&y==y1)//吃果子,长度加1
{
length++;
if(length>=8)//长度大于等于8重新计算长度,等级加1
{
length-=8;//重新计算长度
grade++;//等级加1
if(gamespeed>50)//控制最快速度为50
gamespeed=550-grade*50;//改变自动前进时间间隔
}
tcsQipan[x][y]=3;//贪吃蛇棋盘相应坐标现在蛇头标志改为蛇头标志3
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]=4;//贪吃蛇棋盘相应坐标原来蛇头标志改为蛇身标志4
head=(head+1)%400;//防止数组越界
tcsZuobiao[0][head]=x;//蛇头的x坐标
tcsZuobiao[1][head]=y;//蛇头的y坐标
do//随机出果子
{
x1=rand()%20+1;
y1=rand()%20+1;
}
while(tcsQipan[x1][y1]!=0);//如果不是在空白处重新出果子
tcsQipan[x1][y1]=5;//贪吃蛇棋盘相应坐标标上果子的标志5
gotoxy(22,12);
cout<<" 游戏进行中!"<<endl;
Refresh(tcsQipan,grade,gamespeed,length,score);
}
else//不吃果子
{
if(otherkey)
{
tcsQipan[tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=0;
tail=(tail+1)%400;//防止数组越界
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]=4;
head=(head+1)%400;//防止数组越界
tcsZuobiao[0][head]=x;//蛇头的x坐标
tcsZuobiao[1][head]=y;//蛇头的y坐标
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]=3;
gotoxy(22,12);
cout<<" 游戏进行中!"<<endl;
Refresh(tcsQipan,grade,gamespeed,length,score);
}
else
{
gotoxy(22,12);
cout<<" 游戏暂停中!"<<endl;
}
}
/*
游戏运行时的核心算法结束
*/
}
while(1)
{
while(!kbhit());
if((direction=getch())==13)//按回车键开始下一局
break;
if(direction==27)//按ESC退出游戏
exit(0);
}
system("cls");//清除屏幕重新开始
}
return0;
}

热点内容
真实战争安卓手机怎么安装内存卡 发布:2025-08-28 06:22:52 浏览:171
上海云服务器订餐 发布:2025-08-28 06:22:46 浏览:919
购买服务器如何付款 发布:2025-08-28 06:22:10 浏览:966
FTP猎头 发布:2025-08-28 05:55:29 浏览:486
颗粒压压缩机 发布:2025-08-28 05:54:08 浏览:602
数控编程F的意思 发布:2025-08-28 05:31:58 浏览:259
python的平方 发布:2025-08-28 05:20:35 浏览:643
东莞压缩弹簧厂 发布:2025-08-28 05:14:06 浏览:791
c语言inout 发布:2025-08-28 05:02:57 浏览:917
java父类访问子类变量么 发布:2025-08-28 04:54:20 浏览:404