1、/*2墙壁,1可以移动地方,3自己,4敌人*/int a1520=2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,1,1,1,1,1,1,0,1,0,0,0,1,0,0,0,0,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
2、,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,
3、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,them5;int sum=0;/*统计吃的豆子个数,吃满50颗就算胜利*/int xx52;/*判定敌人方向用的结构体*/int false=0;void init();void begin();void play();void win();void fun(struct play *them);void movethem(struct play *them);void
4、loseyes();void drawblackdou(int x,int y);void lose();/*游戏玩法:当任意按下键时,游戏启动,敌人随机移动,自己开始躲避敌人和墙壁,尽可能多的吃豆豆*/void play() int key,i; while(1) key=bioskey(2); while(!kbhit() /*当无按键时敌人自己移动 */ setcolor(GREEN);/*重画敌人*/ for(i=0;i5;i+) circle(themi.y*20+100,themi.x*20+100,9); sleep(1); fun(them);/*处理敌人*/ if(themi
5、.x=you.x&themi.y=you.y) false=1;/*假如失败的话*/ loseyes();/*判定是否失败*/ if(false) break; /*跳出while(!kbhit()*/ if(false) break; key=bioskey(0); setcolor(0);/*把自己原来位置的人给删除掉*/ circle(100+you.y*20,100+you.x*20,9); if(key=ESC) else if(key=UP)/*这里开始的判定主要是是否吃到豆子和碰到墙壁*/ you.x-; if(ayou.xyou.y=2) you.x+; else if(ayo
6、u.xyou.y=0) drawblackdou(you.x,you.y); else if(key=DOWN) you.x+; you.x-; else if(key=RIGHT) you.y+; if(ayou.xyou.y=2) you.y-; else if(ayou.xyou.y=0) drawblackdou(you.x,you.y); else if(key=LEFT) you.y-; you.y+; if(sum=50) break; setcolor(RED);/*执行了一次键盘后再画出自己的位置*/ loseyes();/*自己走上去碰到敌人的可能*/void fun(st
7、ruct play *them)/*移动中的判定*/ int i;/*把敌人的老位置删除*/ for(i=0; circle(themi.y*20+100,themi.x*20+100,9); movethem(them);/*根据控制者的位置来决定敌人的移动方向*/void lose()/*输的画面*/ cleardevice(); settextstyle(0,0,4); while(!kbhit() setcolor(rand()%13+1); outtextxy(180,200,GAME OVER!); delay(3);void win()/*赢的画面*/ outtextxy(200
8、,200,YOU WIN!void drawblackdou(int x,int y)/*吃豆子*/ circle(100+y*20,100+x*20,3);/*画圆x,y,半径*/ sum+;/*吃到豆子后就加一*/ axy=1;/*吃到后这里就成为普通平地*/void loseyes()/*判断是否失败*/ if(themi.x=you.x& false=1;void movethem(struct play *them)/*them的移动过程*/ int i,loop; randomize();/*随机排列,任意打乱*/ /*只要控制者在身边就立即靠上去*/ if(you.x=themi
9、.x&(themi.y+1)=you.y) themi.y+; else if(you.x=themi.x&(themi.y-1)=you.y) themi.y-; else if(you.y=themi.y&(themi.x+1)=you.x) themi.x+;(themi.x-1)=you.x) themi.x-; else loop: xxi0=rand()%4+1;/*这里的方向采取随机赋值,原则是新的方向不可以和原来的方向相反*/ if(xxi0=1&xxi1=2|xxi0=2&xxi1=1) goto loop; if(xxi0=3&xxi1=4|xxi0=4&xxi1=3) x
10、xi1=xxi0; if(xxi0=1)/*四个方向*/ themi.x-; if(athemi.xthemi.y=2)/*假如碰墙壁的话就回到原来的地方等待随机的方向*/ themi.x+; goto loop; else if(xxi0=2) themi.x+; if(athemi.xthemi.y=2) themi.x-; else if(xxi0=3) themi.y+; themi.y-; else if(xxi0=4) themi.y-; themi.y+; void begin() /*setbkcolor(WHITE);*/ int i,j; sleep(1);15; for(
11、j=0;j20;j+) if(aij=2)/*代表墙壁*/ setfillstyle(SOLID_FILL,9); bar(100+j*20-10,100+i*20+10,100+j*20+10,100+i*20-10); else if(aij=3)/*代表自己*/ setcolor(RED); circle(100+j*20,100+i*20,9); else if(aij=4)/*代表敌人*/ setcolor(GREEN); else if(aij=0)/*代表豆子*/ setcolor(YELLOW); setlinestyle(0,0,1); circle(100+j*20,100
12、+i*20,3); you.x=5;you.y=9;/*自己和敌人的初始坐标*/ them0.x=2;them0.y=15; them1.x=4;them1.y=1; them2.x=8;them2.y=16; them3.x=12;them3.y=13; them4.x=13;them4.y=7;void init()/*是否开始游戏*/ setcolor(3); setlinestyle(DASHED_LINE, 0, 3); /*设置线的宽度和形式*/ rectangle(100, 100, 550, 350); /*画矩形*/ settextstyle(3,0,3); outtextx
13、y(200,200,GAME START! outtextxy(110,300,ESC-exit/press any key to continue getch();main() int gd=DETECT,gm; int key; initgraph(&gd,&gm,/*图形界面初始化*/ init();/*调用初始化函数显示进入游戏初始界面*/*接收键盘按键*/ exit(0); else cleardevice(); begin(); play();/*编写游戏实现过程*/ if(sum=50)/*吃满豆子了*/ win(); getch(); if(false) lose();closegraph(); _
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1