| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 659 人关注过本帖
标题:用C写的一个贪吃蛇小游戏(tc下可运行)
取消只看楼主 加入收藏
ming10086
Rank: 2
等 级:论坛游民
帖 子:5
专家分:15
注 册:2013-9-11
收藏
 问题点数:0 回复次数:0 
用C写的一个贪吃蛇小游戏(tc下可运行)
SNAKE.zip (26.29 KB)




代码如下

#include "Conio.h"
#include "graphics.h"
#define closegr closegraph
#include<stdlib.h>
#include <dos.h>

#define N 200
#define up 0x4800
#define down 0x5000
#define left 0x4b00
#define right 0x4d00
#define esc 0x011b
#define Y 0x1579
#define n 0x316e
int gamespeed; /* 游戏速度 */
int i,key,color;
int score=0; /* 游戏分数 */
int color,i,m;



#include"display.c"


struct Food/*定义结构体存储食物的属性*/
{
     int x; /* 食物的坐标 */
     int y;
     int yes; /* 值为0表示屏幕上没有食物,值为1表示屏幕上有食物 */
     int color; /* 食物颜色 */
}food;





struct Snake/*定义结构体存储蛇的属性*/
{
     int x[N]; /* 每一节蛇的坐标 */
     int y[N];
     int color[N];/*存储每一节蛇的颜色*/
     int node; /* 蛇的节数 */
     int direction; /* 蛇移动的方向 */
     int life; /* 蛇的生命,如果为1,蛇死,游戏结束 */
}snake;
void init(void)
{
  int gd = DETECT, gm = 0;
  registerbgidriver(EGAVGA_driver);
  initgraph(&gd, &gm, "");
}

void timer(void)
{
    struct date d;
    struct  time t;
    getdate(&d);
    gettime(&t);

    printf("%d-",d.da_year);
    printf("%d-", d.da_day);
    printf("%d/",d.da_mon);
    printf("%2d:%02d:%02d\r",t.ti_hour, t.ti_min,t.ti_sec);
}



void drawmat(char *mat,int matsize,int x,int y,int color)/*汉字点阵*/
{
     int i,j,k,m;
     m=(matsize-1)/8+1;
     for(j=0;j<matsize;j++)
          for(i=0;i<m;i++)
               for(k=0;k<8;k++)
                    if(mat[j*m+i]&(0x80>>k))
                         putpixel(x+i*8+k,y+j,color);
}




void showword(void)
{/* 调用汉字点阵输出程序,显示标题和作者信息 */
     drawmat(ben48H,48,129,-4,7);
     drawmat(ben48H,48,209,-4,7);
     drawmat(tan48H,48,289,-4,7);
     drawmat(chi48H,48,369,-4,7);
     drawmat(she48H,48,459,-4,7);

     drawmat(ben48H,48,130,-5,4);
     drawmat(ben48H,48,210,-5,4);
     drawmat(tan48H,48,290,-5,4);
     drawmat(chi48H,48,370,-5,4);
     drawmat(she48H,48,460,-5,4);


     drawmat(zuo16H,16,415,465,7);
     drawmat(zhe16H,16,430,465,7);

     drawmat(han16H,16,450,465,7);
     drawmat(ming16H,16,465,465,7);

     drawmat(tu128H,128,500,20,10);
      drawmat(tuu128H,128,500,300,14);

     /*drawmat(xi32H,32,510,160,4);
      drawmat(wang32H,32,540,160,4);
      drawmat(wo32H,32,570,160,4);
      drawmat(men32H,32,600,160,4);

      drawmat(mei32H,32,510,195,4);
      drawmat(tian32H,32,540,195,4);
      drawmat(kuai32H,32,570,195,4);
      drawmat(le32H,32,600,195,4);

      drawmat(kai32H,32,510,230,4);
      drawmat(xin32H,32,540,230,4);
        drawmat(huan32H,32,570,230,4);
      drawmat(you32H,32,600,230,4);
          drawmat(xing32H,32,510,265,4);
      drawmat(fu32H,32,540,265,4);  */



}

void draw(void)/*画出四周的墙*/
{

    setfillstyle(SOLID_FILL, 8);

     for(i=30;i<=500;i+=10)
     {
          bar(i,40,i+10,49);
         bar(i,451,i+10,460);
     }
     for(i=40;i<450;i+=10)
     {
          bar(30,i,39,i+10);
          bar(501,i,510,i+10);
     }
}

void drawshiwu(int x,int y)
{

    setfillstyle(SOLID_FILL, 4);
    bar(x, y, x+10, y+10);

}

 void gameover(void)
{
     cleardevice(); /* 清屏函数 */

     for(i=0;i<snake.node;i++)/* 画出蛇死时的位置 */
     {
          setcolor(snake.color[i]);
          rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10);
     }

     draw();

     settextstyle(0,0,6);
     setcolor(7);
     outtextxy(103,203,"GAME OVER");
     setcolor(RED);
     outtextxy(100,200,"GAME OVER");

}
void prscore(void)
{/* 打印游戏分数 */
     char str[10];
     setfillstyle(SOLID_FILL,YELLOW);
     bar(490,5,670,25);
     setcolor(6);
     settextstyle(0,0,2);
     sprintf(str,"score:%d",score);
     outtextxy(490,10,str);
}


void gameplay(void)/* 玩游戏的具体过程 */
{
     int flag,flag1;
     randomize();

     gamespeed=50000;
     food.yes=0;/* food.yes=0表示屏幕上没有食物 */
     snake.life=1;
     snake.direction=4;
     snake.node=2;
     snake.color[0]=2;
     snake.color[1]=2;
     snake.x[0]=100;snake.y[0]=100;
     snake.x[1]=110;snake.y[1]=100;
     food.color=4;
      while(1)
      {
        while(1)
        {
            if(food.yes==0) /* 如果蛇活着 */
               {
                    while(1)
                    {
                         flag=1;
                         food.yes=1;
                         food.x=random(46)*10+40;
                         food.y=random(40)*10+50;
                         for(i=0;i<snake.node;i++)
                         {
                              if(food.x==snake.x[i]&&food.y==snake.y[i])
                                   flag=0;
                         }
                         if(flag) break;
                    }
                }
            if(food.yes)
               {
                    drawshiwu(food.x,food.y);
               }
            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.y[0]-=10;break;
                    case 2: snake.y[0]+=10;break;
                    case 3: snake.x[0]-=10;break;
                    case 4: snake.x[0]+=10;break;
               }
            for(i=3;i<snake.node;i++)
               {
                    if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
                    {
                         gameover();
                         snake.life=0;
                         break;
                    }
               }
             if(snake.x[0]<40||snake.x[0]>490||snake.y[0]<50||snake.y[0]>440)
               {
                    gameover();
                    snake.life=0;
               }
             if(snake.life==0)
                    break;


             if(snake.x[0]==food.x&&snake.y[0]==food.y)/*蛇吃掉食物*/
               {

                   setfillstyle(SOLID_FILL, 0);
                    bar(food.x,food.y,food.x+10,food.y+10);
                    snake.x[snake.node]=-20;
                    snake.y[snake.node]=-20;
                    snake.color[snake.node]=food.color;
                    snake.node++;
                    food.yes=0;
                    food.color=4;
                    score+=10;
                    m++;
                    if(m==15)m=0;

                    prscore();
                     if(score%100==0&&score!=0)
                    {
                         for(i=0;i<snake.node;i++)/* 画出蛇 */
                         {
                              setcolor(snake.color[i]);
                              rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10);
                         }
                         sound(200);
                         delay(50000);delay(50000);delay(50000);
                         delay(50000);delay(50000);delay(50000);
                         nosound();
                         gamespeed-=5000;
                         draw();
                    }
                    else
                    {
                         sound(500);
                         delay(500);
                         nosound();
                    }


               }

               for(i=0;i<snake.node;i++)/* 画出蛇 */
               {
                    setcolor(snake.color[i]);
                    rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10);
                    switch(m)
                     {
                        case 1: drawmat(xi32H,32,510,160,4);break;
                        case 2:drawmat(wang32H,32,540,160,4); break;
                        case 3:drawmat(wo32H,32,570,160,4);    break;
                        case 4:drawmat(men32H,32,600,160,4);   break;
                        case 5:drawmat(mei32H,32,510,195,4); break;
                        case 6:drawmat(tian32H,32,540,195,4); break;
                        case 7:drawmat(kuai32H,32,570,195,4); break;
                        case 8:drawmat(le32H,32,600,195,4); break;
                        case 9:drawmat(kai32H,32,510,230,4);break;
                        case 10:drawmat(xin32H,32,540,230,4);  break;
                        case 11:drawmat(huan32H,32,570,230,4); break;
                        case 12:drawmat(you32H,32,600,230,4);  break;
                        case 13:drawmat(xing32H,32,510,265,4); break;
                        case 14:drawmat(fu32H,32,540,265,4);   break;
                        default:   break;
                     }
               }

               delay(gamespeed);
               delay(gamespeed);
               flag1=1;
               setcolor(0);
               rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
               snake.x[snake.node-1]+10,snake.y[snake.node-1]+10);

               if(kbhit()&&flag1==1)  /*如果没按有效键就重新开始循环*/
               {
                    flag1=0;
                    key=bioskey(0);
                    if(key==esc)
                         exit(0);
                    else if(key==up&&snake.direction!=2)
                         snake.direction=1;
                    else if(key==down&&snake.direction!=1)
                         snake.direction=2;
                    else if(key==left&&snake.direction!=4)
                         snake.direction=3;
                    else if(key==right&&snake.direction!=3)
                         snake.direction=4;
               }
               timer();


          }

          if(snake.life==0)/*如果蛇死了就退出循环*/
               break;

        }
}

void main(void)
{
     while(1)
     {
          color=0;
          init();
          cleardevice();
          draw();
          showword();
          gameplay();
          setcolor(15);
          settextstyle(0,0,2);
          outtextxy(200,400,"CONTINUE(Y/N)?");
          while(1)
          {
               key=bioskey(0);
               if(key==Y||key==n||key==esc)
                    break;
          }
          if(key==n||key==esc)
               break;
     }
     closegraph();
}
搜索更多相关主题的帖子: include 小游戏 贪吃蛇 结构体 color 
2013-09-22 22:46
快速回复:用C写的一个贪吃蛇小游戏(tc下可运行)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021424 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved