签到

06月21日
尚未签到

共有回帖数 0

    十万个为什么

    等级:
    #include "graphics.h"
    #include "stdlib.h"
    #include "dos.h"
    #include "bios.h"
    #define LEFT 0x4b00
    #define RIGHT 0x4d00
    #define DOWN 0x5000
    #define UP 0x4800
    #define ESC 0x011b
    #define ENTER 0x1c0d
    /*2墙壁,1可以移动地方,3自己,4敌人*/
    int a[15][20]={2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
                  2,1,1,0,0,0,1,0,1,0,0,0,1,1,1,1,1,1,0,2,
                  2,1,2,2,2,1,1,2,1,1,0,0,0,1,1,4,1,1,0,2,
                  2,1,1,0,2,1,1,2,0,1,1,2,2,2,2,2,0,0,0,2,
                  2,4,1,0,2,1,1,2,1,1,1,0,1,1,1,1,0,1,1,2,
                  2,1,2,1,2,1,1,2,1,3,2,2,1,1,1,1,2,2,1,2,
                  2,1,2,1,2,1,1,1,1,1,1,1,1,0,0,0,1,1,1,2,
                  2,1,2,1,0,1,1,1,1,2,1,0,1,2,2,2,1,1,1,2,
                  2,1,0,1,0,1,2,1,1,2,1,0,1,2,1,1,4,1,1,2,
                  2,1,0,2,0,1,2,1,1,2,1,0,1,2,1,1,1,1,1,2,
                  2,1,0,2,1,1,2,1,1,2,1,0,2,2,1,0,0,0,1,2,
                  2,1,1,2,1,1,2,1,1,2,1,0,2,1,1,2,2,1,1,2,
                  2,1,2,2,1,2,2,1,1,1,1,0,1,4,1,2,0,0,1,2,
                  2,1,0,0,0,0,0,4,0,1,1,0,1,1,1,1,0,0,1,2,
           2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};/*数组就是地图*/
    struct play /*游戏中人物的结构体*/
    {
    int x;
    int y;
    };
    struct play you,them[5];
    int sum=0;/*统计吃的豆子个数,吃满50颗就算胜利*/
    int xx[5][2];/*判断敌人方向用的结构体*/
    int false=0;
    void TimeDelay(unsigned long microsec) /*延时函数 传入微秒数*/
    {
    union REGS r;
    r.h.ah=0x86;
    r.x.cx=microsec16;
    r.x.dx=microsec;
    int86(0x15,&r,&r);
    }
    drawblackdou(int x,int y)/*吃豆子的函数*/
    {
    setcolor(0);
    circle(100+y*20,100+x*20,3);
    sum++;/*吃到豆子后就加一*/
    a[x][y]=1;/*吃到后这里就成为普通平地*/
    }

    begain()/*开始函数*/
    {int i,j;
    sleep(1);
    for(i=0;i15;i++)
     for(j=0;j20;j++)
      if(a[j]==2)/*代表墙壁*/
       {
        setfillstyle(SOLID_FILL,BLUE);
        bar(100+j*20-10,100+i*20+10,100+j*20+10,100+i*20-10);
       }
      else if(a[j]==3)/*代表自己*/
       {
        setcolor(RED);
        circle(100+j*20,100+i*20,9);
       }
      else if(a[j]==4)/*代表敌人*/
       {
        setcolor(GREEN);
        circle(100+j*20,100+i*20,9);
       }
      else if(a[j]==0)/*代表豆子*/
      {
       setcolor(YELLOW);
       circle(100+j*20,100+i*20,3);
      }
    you.x=5;you.y=9;/*敌人也自己的开始坐标*/
    them[0].x=2;them[0].y=15;
    them[1].x=4;them[1].y=1;
    them[2].x=8;them[2].y=16;
    them[3].x=12;them[3].y=13;
    them[4].x=13;them[4].y=7;
    }
    void movethem(struct play *them)/*敌人移动的过程*/
    {int i,loop;
    randomize();
    for(i=0;i5;i++)
    {
    if(you.x==them.x&&(them.y+1)==you.y)
      them.y++;
    else if(you.x==them.x&&(them.y-1)==you.y)
      them.y--;
    else if(you.y==them.y&&(them.x+1)==you.x)
     them.x++;
    else if(you.y==them.y&&(them.x-1)==you.x)/*只要控制者在身边就立即靠上去*/
     them.x--;
    else
    {
    loop:
      xx[0]=rand()%4+1;/*这里的方向采取随机赋值,原则是新的方向不可以和原来的方向相反*/
      if(xx[0]==1&&xx[1]==2||xx[0]==2&&xx[1]==1)
       goto loop;
      if(xx[0]==3&&xx[1]==4||xx[0]==4&&xx[1]==3)
       goto loop;
      xx[1]=xx[0];
     if(xx[0]==1)/*四个方向*/
      {them.x--;


    if(a[them.x][them.y]==2)/*如果碰墙壁的话就回到原来的地方等待随机的方向*/
       {them.x++;goto loop;}
      }
     else if(xx[0]==2)
      {them.x++;
       if(a[them.x][them.y]==2)
       {them.x--;goto loop;}
      }
     else if(xx[0]==3)
      {them.y++;
       if(a[them.x][them.y]==2)
       {them.y--;goto loop;}
      }
     else if(xx[0]==4)
      {them.y--;
       if(a[them.x][them.y]==2)
       {them.y++;goto loop;}
      }
     }
    }
    }
    fun(struct play *them)/*移动中的判断*/
    {
    int i;
    setcolor(0);/*把敌人的老位置删除*/
      for(i=0;i5;i++)
       circle(them.y*20+100,them.x*20+100,9);
     movethem(them);/*根据控制者的位置来决定敌人的移动方向*/
    }
    win()/*胜利的话*/
    {
    cleardevice();
    settextstyle(0,0,4);
    while(!kbhit())
    {
     setcolor(rand()%13+1);
      outtextxy(200,200,"YOU WIN!");
     delay(1000);
    }
    }
    false1()/*失败画面*/
    {
    cleardevice();
    settextstyle(0,0,4);
    while(!kbhit())
    {
     setcolor(rand()%13+1);
      outtextxy(180,200,"GAME OVER!");
     delay(1000);
    }
    }
    loseyes()/*判断是否失败*/
    {int i;
    for(i=0;i5;i++)
      if(them.x==you.x&&them.y==you.y)
      false=1;/*如果失败的话*/
    }

    main()
    {int gd=DETECT,gm;
    int key,i;
    initgraph(&gd,&gm,"c:\tc");
     cleardevice();
    begain();/*开始画面*/  
    while(1)
    {
     while(!kbhit())
      {
     setcolor(GREEN);/*重画敌人*/
     for(i=0;i5;i++)
      circle(them.y*20+100,them.x*20+100,9);
     TimeDelay(280000);
      fun(them);/*处理敌人*/
     for(i=0;i5;i++)
      if(them.x==you.x&&them.y==you.y)
      false=1;/*如果失败的话*/
     loseyes();/*判断是否失败*/
     if(false)
       break;
      }
     if(false)
       break;
     key=bioskey(0);
     setcolor(0);/*把自己原来位置的人给删除掉*/
     circle(100+you.y*20,100+you.x*20,9);
      if(key==ESC)
       break;
      else if(key==UP)/*这里开始的判断主要是是否吃到豆子和碰到墙壁*/
       {you.x--;
        if(a[you.x][you.y]==2) you.x++;
        else if(a[you.x][you.y]==0)
        drawblackdou(you.x,you.y);}
      else if(key==DOWN)
       {you.x++;if(a[you.x][you.y]==2) you.x--;
        else if(a[you.x][you.y]==0)
        drawblackdou(you.x,you.y);}
      else if(key==RIGHT)
       {you.y++;if(a[you.x][you.y]==2) you.y--;
        else if(a[you.x][you.y]==0)

        drawblackdou(you.x,you.y);}
      else if(key==LEFT)
       {you.y--;if(a[you.x][you.y]==2) you.y++;
        else if(a[you.x][you.y]==0)
        drawblackdou(you.x,you.y);}
        if(sum==50)
         break;
     setcolor(RED);/*执行了一次键盘后再画出自己的位置*/
     circle(100+you.y*20,100+you.x*20,9);
     loseyes();/*自己走上去碰到敌人的可能*/
      if(false)
       break;
    }
    if(sum==50)/*吃满豆子了*/
     {win();getch();}
    if(false)
     {false1();getch();}
    closegraph();
    }




    楼主 2016-02-12 15:38 回复

共有回帖数 0
  • 回 帖
  • 表情 图片 视频
  • 发表

登录直线网账号

Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号 意见反馈 | 关于直线 | 版权声明 | 会员须知