贪吃蛇源代码
有的话共享一下,谢谢。能在VC6.0下编译运行的
还有注意的是 以 .cpp编译 而不是建立 .c 文件
可以百度 easy x 下载图形库
已经亲测 可以运行
程序代码:
//贪吃蛇Game //模拟板 //环境 : vc 6.0 和 Easy x 图形函数 #include<stdio.h> #include<graphics.h> #include<windows.h> #include<stdlib.h> #include<conio.h> #include<time.h> #define N 500//蛇长度 #define UP 'w'//方向键控制WASD #define DOWN 's' #define LEFT 'a' #define RIGHT 'd' #define ESC 27 #define BKCO BLACK//background #define SNCO YELLOW//snake #define QCO DARKGRAY//RED//墙 #define FOCO GREEN//food int i,score=0,key,s_time=300; struct Food//食物结构体 { int x,y; int yes; }food; struct Snake//蛇的结构体 { int x[N],y[N]; int node;//蛇长度 int direction;//前进方向 int life;//是否存活 }snake; void initgraph(void)//初始化图像界面 { initgraph(840,580); setbkcolor(BKCO); cleardevice(); } void drawq(void)//画墙 { setfillstyle(QCO); bar(50,50,550,60); bar(50,540,550,550); bar(50,50,60,550); bar(540,50,550,550); outtextxy(600,150,"按键控制:"); outtextxy(600,170,"w上 a左 s下 d右"); outtextxy(600,190,"Esc 退出游戏"); outtextxy(600,210,"p 暂停游戏"); outtextxy(600,390,"欢迎找出BUG和提出改进"); outtextxy(600,410,"E-mail:tyf19938@"); } void print_grade(void) { char str1[20],str2[20]; sprintf(str1,"YOUR LENGTH : %d",score/10+2); sprintf(str2,"YOUR SPEED : %5.2f",1000.0/s_time); setfillstyle(DARKGRAY,INTERLEAVE_FILL); bar(560,280,770,360); setcolor(YELLOW); outtextxy(600,300,str1); outtextxy(600,330,str2); } void print_score(void)//成绩输出函数 { char str[20]; sprintf(str,"SCORE: %d ",score);//数字转化为字符数组 setfillstyle(BROWN,INTERLEAVE_FILL); bar(50,20,550,50); setcolor(GREEN); outtextxy(50,20,str); setcolor(YELLOW); outtextxy(200,30,"贪吃蛇----- --------(C语言版)"); } void gameover(void) { cleardevice(); print_score(); print_grade(); outtextxy(300,250,"GAME OVER"); getch(); } void playgame(void)//玩游戏 { int pd=1; snake.x[0]=100; snake.y[0]=100; snake.node=2; snake.direction=3; snake.life=0; food.yes=1; print_score(); print_grade(); outtextxy(600,100,"按任意键开始..."); system("pause"); outtextxy(600,100,"正在游戏中....."); while(1) { srand(time(0)); while(!kbhit() ||pd)//无键盘输入时 执行部分 { if(food.yes==1) { int flag=1; while(flag)//循环使食物与蛇身不重合 { food.x=rand()%470+60; food.y=rand()%470+60; while(food.x%10!=0)//使食物处在整10坐标上 food.x++; while(food.y%10!=0) food.y++; flag=0; for(i=0;i<snake.node;i++) if(food.x==snake.x[i] && food.y==snake.y[i]) { flag=1;break; } } food.yes=0; } if(food.yes==0)//画食物 { setfillstyle(FOCO); bar(food.x,food.y,food.x+10,food.y+10); } for(i=snake.node-1;i>0;i--)//仅移动时除第一节以外的其他坐标变化 { snake.x[i]=snake.x[i-1]; snake.y[i]=snake.y[i-1]; } switch(snake.direction)//第一节根据方向变化 { case 1:snake.x[0]-=10;break;//left case 2:snake.y[0]-=10;break;//up case 3:snake.x[0]+=10;break;//right case 4:snake.y[0]+=10;break;//down } for(i=3;i<snake.node;i++)//是否撞自身 { if(snake.x[0]==snake.x[i] && snake.y[0]==snake.y[i]) { snake.life=1; break; } } if(snake.x[0]<60 || snake.x[0]>530 ||snake.y[0]<60 ||snake.y[0]>530)//是否撞墙 snake.life=1; if(snake.life==1)//撞到退出while(!kbhit())循环 { putchar('\a'); gameover(); break; } if(snake.x[0]==food.x&&snake.y[0]==food.y)//是否吃到食物 { setfillstyle(BKCO); bar(food.x,food.y,food.x+10,food.y+10);//消食物 food.yes=1; snake.node++; score+=10; print_score(); } for(i=0;i<snake.node;i++)//画蛇 { if(i==0)//蛇头颜色亮青 { setfillstyle(LIGHTCYAN); bar(snake.x[0],snake.y[0],snake.x[0]+10,snake.y[0]+10); } else//蛇身颜色浅灰 { setfillstyle(LIGHTGRAY); bar(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10); } } if(score<=50) s_time=300; else if(score<100) s_time=280; else if(score<200) s_time=250; else if(score<300) s_time=210; else if(score<400) s_time=180; else if(score<450) s_time=160; else s_time=160-(score-450)/10; print_grade(); Sleep(s_time);//游戏短暂停息 setfillstyle(BKCO); bar(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]+10); pd=0; }//end while(!kbhit()) if(snake.life==1) break; if(kbhit()) { pd=1; key=getch(); if(key==ESC) break; else if(key==LEFT && snake.direction!=3) snake.direction=1; else if(key==UP && snake.direction!=4) snake.direction=2; else if(key==RIGHT && snake.direction!=1) snake.direction=3; else if(key==DOWN && snake.direction!=2)//can not use "else" snake.direction=4; else if(key=='p') { outtextxy(600,100,"已暂停,任意键继续"); system("pause"); outtextxy(600,100,"正在游戏中...................."); } } }// end while(1) } int main() { initgraph(); drawq(); playgame(); getch(); getch(); closegraph(); return 0; }