文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>c代码 贪吃蛇...

c代码 贪吃蛇...

时间:2010-08-13  来源:gilbertld

#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
#include<conio.h>
#define UP 0x4800
#define DOWN 0x5000
#define RIGHT 0x4d00
#define LEFT 0x4b00
#define ESC 0x11b
#define ENTER 0x1c0d
#define SPACE 0x3920
struct Food        /*食物结构体*/
{
 int x;
 int y;
 int yes;
}food;
struct Snake       /*蛇结构体*/
{
 int x[100];
 int y[100];
 int n;
 int color;
 int direction;
}snake;
int speed=0000;
void main()        /*主函数*/
{
void wall();
void start();
void close();
void game();
void score();
void leavl();
void select();
void sw();
int key;
while(1)
{
 start();
 select();
 cleardevice();
 wall();
 sw();
 game();
 cleardevice();
 setcolor(RED);
 outtextxy(250,220,"Please press ESC Quit");
 outtextxy(250,240,"Press any-key GO ON");
 key=bioskey(0);
 if(key==ESC) break;
}
close();
}
void close()
{
 getch();
 closegraph();
}
void start()                        /*BGI初始化*/
{
 int gd=DETECT,gm;
 registerbgidriver(EGAVGA_driver);
 initgraph(&gd,&gm,"");
}
void wall()
{                                               /*画墙*/
 int i;
 randomize();
 for(i=0;i<=1000;i++)
 putpixel(random(640),random(480),random(14)+1);
 setcolor(random(14)+1);
 rectangle(60,140,580,410);
 setcolor(random(14)+1);
 rectangle(62,142,578,408);
 setcolor(random(14)+1);
 setlinestyle(0,1,3);
 rectangle(68,148,571,401);
 setcolor(LIGHTBLUE);
}
void game()                                  /*主游戏*/
{
 int i,key,gamespeed;
 snake.color=random(14)+1;
 food.yes=1;
 setcolor(GREEN);
 outtextxy(400,50,"Press ESC Qiut");
 outtextxy(400,70,"Press ENTER Stop");
 setcolor(LIGHTBLUE);
 outtextxy(100,100,"score:");
 outtextxy(300,100,"leavl:");
 snake.x[0]=150;
 snake.y[0]=200;
 snake.x[1]=140;
 snake.y[1]=200;
 snake.n=2;
 snake.direction=2;
 setcolor(WHITE);
 setlinestyle(0,1,1);
 setfillstyle(1,snake.color);
 for(i=0;i<snake.n;i++)
 bar(snake.x[i],snake.y[i],snake.x[i]+9,snake.y[i]+9); /*画蛇*/
  while(1)
  {
    key=bioskey(0);                                       /*设置按键*/
    if(bioskey(1)) key=bioskey(0);
    if(key==ENTER) bioskey(0);
    if(key==ESC) break;
    /*if(key==SPACE) select();*/
    if(key==LEFT&&snake.direction!=2) snake.direction=1;
    if(key==RIGHT&&snake.direction!=1) snake.direction=2;
    if(key==UP&&snake.direction!=4) snake.direction=3;
    if(key==DOWN&snake.direction!=3) snake.direction=4;
   while(!kbhit())
   {
    delay(speed);
    gamespeed=60000-(snake.n-2)*1000;         /*蛇每增加一节延时减少1000毫秒*/
    delay(gamespeed);
    leavl();
    if(gamespeed==0)
    {
     setcolor(snake.color);
     settextstyle(0,0,5);
     cleardevice();
     outtextxy(140,250,"You win!!");
     outtextxy(220,200,"GOOD!");
     settextstyle(0,0,1);
     bioskey(0);
     return;
    }
    setfillstyle(1,BLACK);
    if(food.yes==1)                        /*如果蛇头碰到食物,不运行下面语句*/
    bar(snake.x[snake.n-1],snake.y[snake.n-1],snake.x[snake.n-1]+9,snake.y[snake.n-1]+9); /*擦除蛇尾*/
    food.yes=1;
    setfillstyle(1,snake.color);
    for(i=snake.n;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;
     case 2:snake.x[0]+=10; break;
     case 3:snake.y[0]-=10; break;
     case 4:snake.y[0]+=10; break;
    }
    bar(snake.x[0],snake.y[0],snake.x[0]+9,snake.y[0]+9);   /*在蛇头的前面画出新的一节*/
    if(food.x==snake.x[0]&&food.y==snake.y[0])          /*蛇头碰到食物*/
    {
     food.yes=0;
     snake.n++;
     sw();
    }
    if(snake.x[0]>=570||snake.x[0]<=65||snake.y[0]>=400||snake.y[0]<=140)   /*判断蛇是否撞墙*/
    return;
    for(i=snake.n;i>3;i--)
    if(snake.x[0]==snake.x[i]&&snake.y[0]==snake.y[i])   /*判断蛇是否碰到自身*/
    return;
    score();
   }
 }
}
void score()                         /*记分*/
{
 char a[10];
 setcolor(LIGHTBLUE);
 setfillstyle(1,BLACK);
 bar(140,100,300,110);
 sprintf(a,"score:%d",(snake.n-2)*10);
 outtextxy(100,100,a);
}                                   /*等级*/
void leavl()
{
 char str[10];
 setcolor(LIGHTBLUE);
 setfillstyle(1,BLACK);
 bar(340,100,400,110);
 sprintf(str,"leavl:%d",(snake.n-1)/5);
 outtextxy(300,100,str);
 setcolor(RED);
 setfillstyle(1,BLACK);
 bar(200,120,300,130);
 if((snake.n-1)%5==0)
 outtextxy(200,120,"LEAVL UP!"),
 snake.color++;
 if(snake.color>15)
 snake.color=1;
}
void sw()                            /*画食物*/
{
 int i;
 randomize();
 food.x=random(500)+70;
 food.y=random(250)+150;
 for(i=0;i<=snake.n;i++)
 while(snake.x[i]==food.x||snake.y[i]==food.y)   /*判断食物是否出现在蛇身上*/
 food.x=random(500)+70,food.y=random(250)+150;
 while((food.x%10)!=0)
 food.x--;
 while((food.y%10)!=0)
 food.y--;
 setfillstyle(1,YELLOW);
 bar(food.x,food.y,food.x+9,food.y+9);
}
void select()
{
 int key;
 static int x=129,y=148;
 setcolor(LIGHTGREEN);
 outtextxy(140,300,"Press ENTER Select");
 setcolor(GREEN);
 rectangle(x,y,x+30,y+30);
 settextstyle(0,0,4);
 setcolor(RED);
 outtextxy(190,50,"Speed");
 outtextxy(130,150,"5 4 3 2 1");
 settextstyle(0,0,1);
 while(1)
 {
 key=bioskey(0);
 if(key==ENTER) break;
 if(key==RIGHT&&x<=350)
 {
  speed+=10000;
  setcolor(0);
  rectangle(x,y,x+30,y+30);
  setcolor(GREEN);
  x+=64;
  rectangle(x,y,x+30,y+30);
 }
 if(key==LEFT&&x>=160)
 {
  speed-=10000;
  setcolor(0);
  rectangle(x,y,x+30,y+30);
  setcolor(GREEN);
  x-=64;
  rectangle(x,y,x+30,y+30);
 }
 }
 }

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载