用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
zogna的公告
文章分类
    存档

    原创  贪吃猪核心算法TC2.0下源代码 收藏

    /*zogna-07.2.1*/
    #include<graphics.h>
    #include<bios.h>
    #include<stdlib.h>
    #include<math.h>
    #include<stdio.h>
    #include<dos.h>

    /*初始化*/

    #define PIG_RADIUS 10
    #define FOOD_RADIUS 3

    /*这里的数字是用BIOSKEY函数得到的*/
    #define R     4978
    #define UP    18432
    #define DOWN  20480
    #define LEFT  19200
    #define RIGHT 19712
    #define ESC   283

    /*
    RANDX,RANDY,表示随机小圆出现的坐标
    PIGX,PIGY表示大圆-猪的坐标
    KEY是按键
    DISTANCES是表示小圆和大圆间的距离
    PIGBUF表示放大圆图片的内存指针,FOOD是小圆的
    COUNT计算大圆增加的大小,顺便计算玩家获得的成绩
    */

    int count;
    int randx;  /* randy=randx/2 */
    int pigx,pigy;
    int key;
    unsigned size, distances;
    void *pigbuf,*foodbuf;

    /*
    算法是大圆与小圆的圆心距离如果小于他们的2个的半径的和
    DISTANCES<(R+r)就表示他们已经接触,也就吃掉
    大圆的半径就加大,小圆消失
    */

    /*
    算距
    由于坐标表示X,Y轴的坐标
    R=((X-x)平方+(Y-y)平方)根号 
     */

    unsigned distance(void)
    {
     unsigned long int sumx,sumy;
       
     sumx=(abs(pigx+PIG_RADIUS+count-randx-FOOD_RADIUS))*(abs(pigx+PIG_RADIUS+count-randx-FOOD_RADIUS));
            sumy=(abs(pigy+PIG_RADIUS+count-randx/2-FOOD_RADIUS))*(abs(pigy+PIG_RADIUS+count-randx/2-FOOD_RADIUS));
     return (unsigned)sqrt(sumx+sumy);
    }


    /*
    画小圆,XOR_PUT表示剪切,这个是成为动画的必要
    RANDOMIE初始化随机数
    RANDDOM产生随机数
    COPY_PUT表示粘贴
    */

    void drawfooodcircle(void)
    {
     putimage(pigx,pigy,pigbuf,XOR_PUT);
            count+=1;
            randomize();
     randx=random(500)+10+count;
            putimage(randx,randx/2,foodbuf,COPY_PUT);
    }


    /*
    吃掉时,距离小的时候
    大圆会变大,这里将原来的圆删去,清空内存
    创建新的大圆
    */

    void eat(void)
    {    drawfooodcircle();
         free(pigbuf);
         circle(pigx+PIG_RADIUS+count,pigy+PIG_RADIUS+count,PIG_RADIUS+count);
         size=imagesize(pigx,pigy,pigx+2*(PIG_RADIUS+count),pigy+2*(PIG_RADIUS+count));
         pigbuf=malloc(size);
         getimage(pigx,pigy,pigx+2*(PIG_RADIUS+count),pigy+2*(PIG_RADIUS+count),pigbuf);

    }

    void main()
    {
     int driver=VGA,mode=VGAMED;

            initgraph(&driver,&mode,"");

    /*用到GOTO语句,跳到这里*/
    LOOP:
     count=0;
            randx=347;
            pigx=310;
            pigy=165;

     /* draw normal circle */
      /*drawcircle(pigx,pigy,PIG_RADIUS,pigbuf);*/
    /*初始化大小圆*/
     circle(pigx+PIG_RADIUS,pigy+PIG_RADIUS,PIG_RADIUS);
     size=imagesize(pigx,pigy,pigx+2*PIG_RADIUS,pigy+2*PIG_RADIUS);
     pigbuf=malloc(size);
     getimage(pigx,pigy,pigx+2*PIG_RADIUS,pigy+2*PIG_RADIUS,pigbuf);

     /* drawcircle(randx,randx/2,FOOD_RADIUS,foodbuf);*/
     circle(randx+FOOD_RADIUS,randx/2+FOOD_RADIUS,FOOD_RADIUS);
     size=imagesize(randx,randx/2,randx+2*FOOD_RADIUS,randx/2+2*FOOD_RADIUS);
     foodbuf=malloc(size);
     getimage(randx,randx/2,randx+2*FOOD_RADIUS,randx/2+2*FOOD_RADIUS,foodbuf);


        while((key=bioskey(0)))
     {
      switch(key)
      {
      case UP:
       while(!bioskey(1))
       {   
        putimage(pigx,pigy,pigbuf,XOR_PUT);
                             pigy-=10;
        putimage(pigx,pigy,pigbuf,COPY_PUT);
                                    sleep(1);
                                    if((pigy-PIG_RADIUS-count)==0) goto END;
                                    if(distance()<=(PIG_RADIUS+FOOD_RADIUS+count)) eat();
       }
       break;
      case DOWN:
       while(!bioskey(1))
       {   
        putimage(pigx,pigy,pigbuf,XOR_PUT);
                             pigy+=10;
        putimage(pigx,pigy,pigbuf,COPY_PUT);
                                    sleep(1);
                                    if((pigy+PIG_RADIUS+count)==350) goto END;
                                    if(distance()<=(PIG_RADIUS+FOOD_RADIUS+count)) eat();
       }
       break;
      case LEFT:
       while(!bioskey(1))
       {   
        putimage(pigx,pigy,pigbuf,XOR_PUT);
                             pigx-=10;
        putimage(pigx,pigy,pigbuf,COPY_PUT);
                                    sleep(1);
                                    if((pigx-PIG_RADIUS-count)==0) goto END;
                                    if(distance()<=(PIG_RADIUS+FOOD_RADIUS+count)) eat();
       }
       break;
      case RIGHT:
       while(!bioskey(1))
       {   
        putimage(pigx,pigy,pigbuf,XOR_PUT);
                             pigx+=10;
        putimage(pigx,pigy,pigbuf,COPY_PUT);
                                    sleep(1);
                                    if((pigx+PIG_RADIUS+count)==640) goto END;
                                    if(distance()<=(PIG_RADIUS+FOOD_RADIUS+count)) eat();
       }
       break;
      case R:
       putimage(pigx,pigy,pigbuf,XOR_PUT);
                            putimage(randx,randx/2,foodbuf,XOR_PUT);
                     free(pigbuf);
                     free(foodbuf);
                     goto LOOP;
                    case ESC: exit(1);
      default:break;
      }
     }
    /*结束显示,恢复屏幕*/ 
    END:
            restorecrtmode();
            printf("your count=%d\n",count);
            printf("R-restart,UP,DOWN,LEFT,RIGHT\n\n");
            printf("ZOGNA-07.2.1\n");
            sleep(5);

    }
     

    发表于 @ 2007年06月07日 19:27:00 | 评论( loading... ) | 编辑| 举报| 收藏

    新一篇:TC下常见的错误类型

    • 发表评论
    • 评论内容:
    •  
    Copyright © zogna
    Powered by CSDN Blog