| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 718 人关注过本帖, 1 人收藏
标题:贪吃蛇!小游戏!!
取消只看楼主 加入收藏
Antwind
Rank: 2
等 级:论坛游民
帖 子:82
专家分:53
注 册:2010-7-11
结帖率:100%
收藏(1)
 问题点数:0 回复次数:3 
贪吃蛇!小游戏!!
003.zip (22.38 KB)

#include<conio.h>
#include<bios.h>
#include<stdlib.h>
#define  LEFT  0x4b00
#define  RIGHT  0x4d00
#define  UP     0x4800
#define  DOWN   0x5000
#define  ENTER  0x000d
#define  ESC    0x0011b
struct
{
    int x;
    int y;
}body[500];     /* 蛇的坐标   */
struct

{
    int x;
    int y;
}heart;   /*食物的坐标*/
int length=0;            /*蛇的长度*/
int getit=0;          /***************食物是否被吃的标志************************/
int key=0;                   /********************按键*******************/
int score=0;                     /******************得分情况*********************/
void begin();                       /******************开始界面*********************/
void game ();                     /*************游戏主函数**************************/
void keyboard();                  /********键盘扫描函数*******************************/
void pringame();                     /************游戏界面***************************/
void find();                              /**************蛇是否出界*************************/
void prin();                         /************打印蛇和食物***************************/
void eat();                           /*********蛇吃食物******************************/
void choose();                         /*******************选择按键*******************/
void load();                              /*********初始化蛇*************************/
void onesmore();                       /**************随机食物的坐标*************************/
void explain();                          /*************操作解释*************************/
void gameover();                         /*********游戏结束界面******************************/
void main()
{
     begin();
     explain();
     onesmore();
     getch();
}
void begin()                            /*打印提示*/
{
        textmode(C80);
        window(0,0,80,25);
        textbackground(GREEN);
        textcolor(BLUE);
        clrscr();
        gotoxy(30,9);
        cprintf("Welcome to my game!");
        gotoxy(30,10);
        cprintf("Scores");
        gotoxy(30,11);
        cprintf("Are you ready" );
        gotoxy(30,12);
        cprintf("Press anykey to beagin!");
        getch();
}
void game()
{                                        /**********************游戏主函数*****************/
        int i;
        while(!getit)                                /****************判断食物是否被吃***********************/
        {
                prin();
                for(i=150;i>0;i--)
                        delay(1000);
                keyboard();
                eat();
                find();

        }
}
void prin()
{

        int i;
        clrscr();
        pringame();                              /*打印环境*/
        gotoxy(heart.x,heart.y);        /*打印食物*/
                    putch(3);
         for(i=0;i<length;i++)
        {
                gotoxy(body[i].x,body[i].y);      /*打印蛇*/
                putch(4);
        }
}
void  pringame()                   /******************打印环境*********************/
{
        int i;
        gotoxy(35,1);
        cputs("score:");
        gotoxy(43,1);
        cprintf("%d",score);
        for(i=1;i<=80;i++)
        {
                gotoxy(i,2);
                putch(23);
        }
}
void gameover()                                       /* 游戏结束*/
{
        int i;
        window(0,0,80,25);
        textbackground(GREEN);
        textcolor(BLUE);
        clrscr();
        gotoxy(33,10);
        cputs("Game Over");
        gotoxy(35,12);
        cputs("Score");
        gotoxy(42,12);
        cprintf("%d",score);
        while(1);
}
void keyboard()                         /***********键盘扫描函数*********************/
{
        int i;

        if( kbhit())
        {
                        key=bioskey(0);
                        choose();

         }/*检查是否有按键if */
         else
                        choose();
}/*子函数keyboard               */
void onesmore()                               /**************随机食物坐标*************************/
{
        int x,y;
        long i;
        load();
        srand((int)time(0));
        for(i=0;i<10000;i++)
        {
                x=(int)(80.0*rand()/(RAND_MAX+1.0))+1;
                y=(int)(23.0*rand()/(RAND_MAX+1.0))+3;
                heart.x=x;
                heart.y=y;
                getit=0;
                game();



        }

}
void eat()                                                        /*吃食物长大,得分,再加食物*/
{
        if(body[length-1].x==heart.x&&body[length-1].y==heart.y)
        {
                score=score+5;
                body[length].x=heart.x;
                body[length].y=heart.y;
                length++;
                getit=1;
        }
}
 void load()                               /**************初始化蛇*************************/
{
        body[0].x=1;
        body[0].y=3;
        body[1].x=2;
        body[1].y=3;
        body[2].x=3;
        body[2].y=3;
        length=3;
}
void  find()
{                                            /***********蛇是否出界****************************/
        int i;
        for(i=0;i<length;i++)                              /*是否撞墙*/
        {
                if(body[i].x<1||body[i].x>80||body[i].y<3||body[i].y>25)
                gameover();
        }
        for(i=0;i<length-2;i++)                    /*是否撞到自己*/
        {
                if(body[length-1].x==body[i].x&&body[length-1].y==body[i].y)
                        gameover();
        }
}

void choose()                           /*****************选择按键**********************/
{
        int i;
        switch(key)
        {
                case   ESC:
                                gameover();
                                break;
                case    LEFT:
                                for(i=0;i<length-1;i++)
                                {
                                        body[i].x=body[i+1].x;
                                        body[i].y=body[i+1].y;
                                }
                                body[length-1].x--;
                                break;
                case     RIGHT :
                                for(i=0;i<length-1;i++)
                                {
                                        body[i].x=body[i+1].x;
                                        body[i].y=body[i+1].y;
                                }
                                body[length-1].x++;
                                break;
                case   UP:
                                for(i=0;i<length-1;i++)
                                {
                                        body[i].x=body[i+1].x;
                                        body[i].y=body[i+1].y;
                                }
                                body[length-1].y--;
                                break;
                 
                case  DOWN  :
                                for(i=0;i<length-1;i++)
                                {
                                        body[i].x=body[i+1].x;
                                        body[i].y=body[i+1].y;
                                }
                                body[length-1].y++;
                                break;
        }
}
void explain()                                  /******游戏解释函数,按键的操作********/
{

        window(1,1,80,25) ;
        textmode(C80);
        textattr(GREEN+BLUE<<4);
        clrscr();
        gotoxy(24,9);
        cprintf("ESC--------------------quit");
        gotoxy(38,11);
        putch(30);
        gotoxy(37,12)  ;
        putch(17);
        gotoxy(39,12);
        putch(16);
        gotoxy(38,13);
        putch(31);
        gotoxy(27,14);
        cprintf("Press anykey to begin!!");
        getch();

}
看看啊!
指点指点!!
搜索更多相关主题的帖子: 小游戏 贪吃蛇 
2011-07-13 14:01
Antwind
Rank: 2
等 级:论坛游民
帖 子:82
专家分:53
注 册:2010-7-11
收藏
得分:0 

先抢了啊!!
嘿嘿!!!!!!!!!!!


/*************我就是程序;程序就是我!******************/
2011-07-13 14:01
Antwind
Rank: 2
等 级:论坛游民
帖 子:82
专家分:53
注 册:2010-7-11
收藏
得分:0 
呵呵呵!!以后给你留个啊!!


/*************我就是程序;程序就是我!******************/
2011-07-13 18:43
Antwind
Rank: 2
等 级:论坛游民
帖 子:82
专家分:53
注 册:2010-7-11
收藏
得分:0 
谢谢了啊!我会继续努力的!!
嘿嘿


/*************我就是程序;程序就是我!******************/
2011-07-14 00:06
快速回复:贪吃蛇!小游戏!!
数据加载中...
 
   



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

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