?? 簡單吃豆小游戲.txt
字號:
#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};/*數(shù)組就是地圖*/
struct play /*游戲中人物的結(jié)構(gòu)體*/
{
int x;
int y;
};
struct play you,them[5];
int sum=0;/*統(tǒng)計吃的豆子個數(shù),吃滿50顆就算勝利*/
int xx[5][2];/*判斷敵人方向用的結(jié)構(gòu)體*/
int false=0;
void TimeDelay(unsigned long microsec) /*延時函數(shù) 傳入微秒數(shù)*/
{
union REGS r;
r.h.ah=0x86;
r.x.cx=microsec>>16;
r.x.dx=microsec;
int86(0x15,&r,&r);
}
drawblackdou(int x,int y)/*吃豆子的函數(shù)*/
{
setcolor(0);
circle(100+y*20,100+x*20,3);
sum++;/*吃到豆子后就加一*/
a[x][y]=1;/*吃到后這里就成為普通平地*/
}
begain()/*開始函數(shù)*/
{int i,j;
sleep(1);
for(i=0;i<15;i++)
for(j=0;j<20;j++)
if(a[i][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[i][j]==3)/*代表自己*/
{
setcolor(RED);
circle(100+j*20,100+i*20,9);
}
else if(a[i][j]==4)/*代表敵人*/
{
setcolor(GREEN);
circle(100+j*20,100+i*20,9);
}
else if(a[i][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;i<5;i++)
{
if(you.x==them[i].x&&(them[i].y+1)==you.y)
them[i].y++;
else if(you.x==them[i].x&&(them[i].y-1)==you.y)
them[i].y--;
else if(you.y==them[i].y&&(them[i].x+1)==you.x)
them[i].x++;
else if(you.y==them[i].y&&(them[i].x-1)==you.x)/*只要控制者在身邊就立即靠上去*/
them[i].x--;
else
{
loop:
xx[i][0]=rand()%4+1;/*這里的方向采取隨機賦值,原則是新的方向不可以和原來的方向相反*/
if(xx[i][0]==1&&xx[i][1]==2||xx[i][0]==2&&xx[i][1]==1)
goto loop;
if(xx[i][0]==3&&xx[i][1]==4||xx[i][0]==4&&xx[i][1]==3)
goto loop;
xx[i][1]=xx[i][0];
if(xx[i][0]==1)/*四個方向*/
{them[i].x--;
作者: c閑人 2004-8-20 00:46 回復(fù)此發(fā)言
--------------------------------------------------------------------------------
2 c---簡單吃豆小游戲
if(a[them[i].x][them[i].y]==2)/*如果碰墻壁的話就回到原來的地方等待隨機的方向*/
{them[i].x++;goto loop;}
}
else if(xx[i][0]==2)
{them[i].x++;
if(a[them[i].x][them[i].y]==2)
{them[i].x--;goto loop;}
}
else if(xx[i][0]==3)
{them[i].y++;
if(a[them[i].x][them[i].y]==2)
{them[i].y--;goto loop;}
}
else if(xx[i][0]==4)
{them[i].y--;
if(a[them[i].x][them[i].y]==2)
{them[i].y++;goto loop;}
}
}
}
}
fun(struct play *them)/*移動中的判斷*/
{
int i;
setcolor(0);/*把敵人的老位置刪除*/
for(i=0;i<5;i++)
circle(them[i].y*20+100,them[i].x*20+100,9);
movethem(them);/*根據(jù)控制者的位置來決定敵人的移動方向*/
}
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;i<5;i++)
if(them[i].x==you.x&&them[i].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;i<5;i++)
circle(them[i].y*20+100,them[i].x*20+100,9);
TimeDelay(280000);
fun(them);/*處理敵人*/
for(i=0;i<5;i++)
if(them[i].x==you.x&&them[i].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);/*執(zhí)行了一次鍵盤后再畫出自己的位置*/
circle(100+you.y*20,100+you.x*20,9);
loseyes();/*自己走上去碰到敵人的可能*/
if(false)
break;
}
if(sum==50)/*吃滿豆子了*/
{win();getch();}
if(false)
{false1();getch();}
closegraph();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -