标题:贪吃蛇:看哈啊
只看楼主
Antwind
Rank: 2
等 级:论坛游民
帖 子:82
专家分:53
注 册:2010-7-11
结帖率:100%
 问题点数:0 回复次数:4 
贪吃蛇:看哈啊
#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[300];
struct
{
   int x;
   int y;
}heart[500];
int k=0;
int n=0;
int score=0;
void prin();
void loadheart();
void gameover();
void load();
void find();
void eat();
void game ();
void pringame();
void main()
{
    prin();
    clrscr();
    game();
    getch();
}
void prin()
{  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;
  int key=0;
  load();

  loadheart();
  randomize();
  pringame();
  gotoxy(heart[n-1].x,heart[n-1].y);
                   putch(3);

    for(i=0;i<k;i++)
                    {
                     gotoxy(body[i].x,body[i].y);
                     putch(4);
                      }




  do
  {



        while(!kbhit())

          {
           switch(key)
                {

                    case    ESC :
                                     exit(1);break;
                    case    LEFT:
                            for(i=0;i<k-1;i++)
                            {
                                body[i].x=body[i+1].x;
                                body[i].y=body[i+1].y;
                            }
                            body[k-1].x--;
                                      break;
                     case     RIGHT :
                            for(i=0;i<k-1;i++)
                            {
                                body[i].x=body[i+1].x;
                                body[i].y=body[i+1].y;
                            }
                            body[k-1].x++;
                                   break;
                      case   UP:                            for(i=0;i<k-1;i++)
                                         {
                                         body[i].x=body[i+1].x;
                                             body[i].y=body[i+1].y;
                                              }
                            body[k-1].y--;
                                           break;
               
                     case  DOWN  :          for(i=0;i<k-1;i++)
                            {
                                body[i].x=body[i+1].x;
                                body[i].y=body[i+1].y;
                            }
                            body[k-1].y++;

                                           break;
                     case  ENTER:
                                       getch(); break;
                  }
                 find();
                        eat();
                     pringame();

                     delay(60000);

                      clrscr();
                     for(i=0;i<k;i++)
                    {
                     gotoxy(body[i].x,body[i].y);
                     putch(4);
                      }
                   gotoxy(heart[n-1].x,heart[n-1].y);
                   putch(3);


           }

         key=bioskey(0);

    }while(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;
  k=3;
}
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  find()
{     int i;
     for(i=0;i<k;i++)
           {
                  if(body[i].x==0||body[i].x==85||body[i].y==2||body[i].y==26)
                       gameover();



           }
          for(i=0;i<k-2;i++)
                   {
                       if(body[k-1].x==body[i].x&&body[k-1].y==body[i].y)
                        gameover();

                    }


}
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);

   getch();


}

void eat()
{
    if(body[k-1].x==heart[n-1].x&&body[k-1].y==heart[n-1].y)
           { score=score+5;

        body[k].x=heart[n-1].x;
        body[k].y=heart[n-1].y;
        k++;
            loadheart();

            }
}
void loadheart()
{



  heart[n].x=(rand()%83)+2;
  heart[n].y=(rand()%22)+3;
  n++ ;



}


下次再注释:先看哈:
搜索更多相关主题的帖子: 贪吃 
2010-08-27 06:51
卧龙孔明
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:59
帖 子:3872
专家分:684
注 册:2006-10-13
得分:0 
以下是引用sunyh1999在2010-8-27 08:52:50的发言:

这一段:switch (kb)中最好把case中的数据换成ascii码
左:75
右:77
上:72
下:80
ESC:27
误人子弟

My Blog: www.aiexp.info
虽然我的路是从这里开始的,但是这里不再是乐土.感谢曾经影响过,引导过,帮助过我的董凯,飞燕,leeco,starwing,Rockcarry,soft_wind等等等等.别了,BCCN.
2010-08-27 18:40
Antwind
Rank: 2
等 级:论坛游民
帖 子:82
专家分:53
注 册:2010-7-11
得分:0 
#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[300];     /* 蛇的坐标   */
struct
{
   int x;
   int y;
}heart[500];   /*食物的坐标*/
int k=0;            /*蛇的长度*/
int n=0;            /*该吃地几个食物*/
int score=0;
void prin();
void loadheart();
void gameover();
void load();
void find();
void eat();
void game ();
void pringame();
void main()
{
    prin();
    clrscr();
    game();
    getch();
}
void prin()                            /*打印提示*/
{  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;
  int key=0;
  load();                                 /*初始化蛇的坐标*/
                                           /*产生食物*/
  loadheart();
  randomize();
  pringame();                              /*打印环境*/
  gotoxy(heart[n-1].x,heart[n-1].y);        /*打印食物*/
                   putch(3);

    for(i=0;i<k;i++)
                    {
                     gotoxy(body[i].x,body[i].y);      /*打印蛇*/
                     putch(4);
                      }




  do
  {



        while(!kbhit())

          {
           switch(key)
                {

                    case    ESC :
                                     exit(1);break;
                    case    LEFT:
                            for(i=0;i<k-1;i++)
                            {
                                body[i].x=body[i+1].x;
                                body[i].y=body[i+1].y;
                            }
                            body[k-1].x--;
                                      break;
                     case     RIGHT :
                            for(i=0;i<k-1;i++)
                            {
                                body[i].x=body[i+1].x;
                                body[i].y=body[i+1].y;
                            }
                            body[k-1].x++;
                                   break;
                      case   UP:                            for(i=0;i<k-1;i++)
                                         {
                                         body[i].x=body[i+1].x;
                                             body[i].y=body[i+1].y;
                                              }
                            body[k-1].y--;
                                           break;
               
                     case  DOWN  :          for(i=0;i<k-1;i++)
                            {
                                body[i].x=body[i+1].x;
                                body[i].y=body[i+1].y;
                            }
                            body[k-1].y++;

                                           break;
                     case  ENTER:
                                       getch(); break;
                  }
                 find();
                        eat();
                     pringame();

                     delay(60000);

                      clrscr();
                     for(i=0;i<k;i++)
                    {
                     gotoxy(body[i].x,body[i].y);
                     putch(4);
                      }
                   gotoxy(heart[n-1].x,heart[n-1].y);
                   putch(3);


           }

         key=bioskey(0);

    }while(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;
  k=3;
}
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  find()
{     int i;
     for(i=0;i<k;i++)                              /*是否撞墙*/
           {
                  if(body[i].x==0||body[i].x==85||body[i].y==2||body[i].y==26)
                       gameover();



           }
          for(i=0;i<k-2;i++)                    /*是否撞到自己*/
                   {
                       if(body[k-1].x==body[i].x&&body[k-1].y==body[i].y)
                        gameover();

                    }


}
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);

   getch();


}

void eat()                                                        /*吃食物长大,得分,再加食物*/
{
    if(body[k-1].x==heart[n-1].x&&body[k-1].y==heart[n-1].y)
           { score=score+5;

        body[k].x=heart[n-1].x;
        body[k].y=heart[n-1].y;
        k++;
            loadheart();

            }
}
void loadheart()                /*初始化食物*/
{



  heart[n].x=(rand()%83)+2;
  heart[n].y=(rand()%22)+3;
  n++ ;



}
我加了注释:看哈啊,给点意见:
指导下啊。


/*************我就是程序;程序就是我!******************/
2010-08-27 20:06
以中
Rank: 3Rank: 3
来 自:长沙
等 级:论坛游侠
帖 子:108
专家分:129
注 册:2010-4-13
得分:0 
打不开bios.h文件。

道之所存,师之所存。
2010-08-27 23:11
Antwind
Rank: 2
等 级:论坛游民
帖 子:82
专家分:53
注 册:2010-7-11
得分:0 
用win-tc 啊
应该可以啊


/*************我就是程序;程序就是我!******************/
2010-08-28 09:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-316927-1-1.html




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

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