?? 派克曼源程序.txt
字號:
派克曼源程序
本文由鵬程C語言教學網站整理收錄
收錄時間: 2003-6-14 11:41:33
--------------------------------------------------------------------------------
Author: From:
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#define NULL 0
#define BKCOLOR 1
#define TIMER 0x1c
#define VK_ESC 0x11b
#define VK_UP 0x4800
#define VK_DOWN 0x5000
#define VK_LEFT 0x4b00
#define VK_RIGHT 0x4d00
#define VK_FIRE 0x3920
#define VK_Y 0x1579
#define VK_N 0x316e
#define MAX_BULLETS 8
#define STONE_NUM 8
#define TIME_CIRCLE 8
#define R_MAN 3
#define WIDE_STONE 6
#define R_BULLET 1
#define STONE_F_NUM 4
#define MAN_F_NUM 2
#define BULLET_F_NUM 2
#define STONE_AB 2
#define MAN_AB 3
#define BULLET_AB 4
#define FOOD_AB 5
#define TIME_OVER 900
int Sign=0;
int Game_Over=0;
int Stone_Left=20;
int TimeCount=0;
int Time=0;
int Bullet_Num=3;
char p_bullet[4];
char p_time[4];
char p_stone[4];
int Man_Forward=0;
int Array[160][120]={{0},{0}};
int Interval[STONE_NUM];/*random num vs stone[i].*/
struct subjects
{int x;
int y;
int color;
int forward;
}Stone[STONE_NUM],Man;
struct bullets
{int x;
int y;
int color;
int fly;
int forward;
}Bullet[8];
struct foods
{int x;
int y;
int color;
int exist;
}Food;
void interrupt(*oldhandler)();
void interrupt newhandler()
{
TimeCount++;
Time++;
oldhandler();
}
void settimer(void interrupt(*intproc)())
{
oldhandler=getvect(TIMER);
disable();
setvect(TIMER,intproc);
enable();
}
void killtimer()
{
disable();
setvect(TIMER,oldhandler);
enable();
}
void draw1(int x,int y,int color,int ab) /*draw stone*/
{
int i,j;
setfillstyle(SOLID_FILL,color);
bar(x*4,y*4,(x+WIDE_STONE-1)*4,(y+WIDE_STONE-1)*4);
for(i=x;i<x+WIDE_STONE;i++)
for(j=y;j<y+WIDE_STONE;j++)
{
Array[i][j]=ab;
}
}
void draw2(int x,int y,int color,int ab,int forward)/*draw man*/
{
int i,j;
setfillstyle(SOLID_FILL,color);
setcolor(color);
if(forward==1)
pieslice(x*4,y*4,45,315,R_MAN*4);
else if(forward==2)
{
pieslice(x*4,y*4,0,45,12);
pieslice(x*4,y*4,135,360,R_MAN*4);
}
else if(forward==3)
{
pieslice(x*4,y*4,0,135,R_MAN*4);
pieslice(x*4,y*4,225,360,R_MAN*4);
}
else if(forward==4)
{
pieslice(x*4,y*4,0,225,R_MAN*4);
pieslice(x*4,y*4,315,360,R_MAN*4);
}
for(i=x-R_MAN;i<=x+R_MAN;i++)
for(j=y-R_MAN;j<=y+R_MAN;j++)
{
Array[i][j]=ab;
}
}
void draw3(int x,int y,int color,int ab)/*draw bullet*/
{
int i,j;
setfillstyle(SOLID_FILL,color);
setcolor(color);
circle(x*4,y*4,R_BULLET*4);
for(i=x-R_BULLET;i<=x+R_BULLET;i++)
for(j=y-R_BULLET;j<=y+R_BULLET;j++)
Array[i][j]=ab;
}
int move1(int i)
{
int x0=Stone[i].x,y0=Stone[i].y;
int num=STONE_F_NUM,m,n;
int x1,y1;
draw1(x0,y0,BKCOLOR,0);
switch(Stone[i].forward)
{
case 1:x1=x0+num;y1=y0;break;
case 2:x1=x0; y1=y0-num;break;
case 3:x1=x0-num;y1=y0;break;
case 4:x1=x0; y1=y0+num;break;
}
draw1(x1,y1,Stone[i].color,STONE_AB);
Stone[i].x=x1;
Stone[i].y=y1;
}
int move2()
{
int x0=Man.x,y0=Man.y,m,n,num=MAN_F_NUM;
int x1,y1;
draw2(x0,y0,BKCOLOR,0,Man_Forward);
switch(Man.forward)
{
case 1: x1=x0+num;y1=y0;break;
case 2: x1=x0;y1=y0-num;break;
case 3: x1=x0-num;y1=y0;break;
case 4: x1=x0;y1=y0+num;break;
}
draw2(x1,y1,Man.color,MAN_AB,Man.forward);
Man.x=x1;
Man.y=y1;
}
int move3(int i)
{
int x0=Bullet[i].x,y0=Bullet[i].y;
int m,n,num=BULLET_F_NUM;
int x1,y1;
draw3(x0,y0,BKCOLOR,0);
switch(Bullet[i].forward)
{
case 1:x1=x0+num;y1=y0;break;
case 2:x1=x0;y1=y0-num;break;
case 3:x1=x0-num;y1=y0;break;
case 4:x1=x0;y1=y0+num;break;
}
draw3(x1,y1,Bullet[i].color,BULLET_AB);
Bullet[i].x=x1;
Bullet[i].y=y1;
}
stone_init(int i)
{
switch(i)
{
case 4:
Stone[i].x=4;
Stone[i].y=8;
Stone[i].forward=1;
break;
case 5:
Stone[i].x=148;
Stone[i].y=8;
Stone[i].forward=3;
break;
case 6:
Stone[i].x=4;
Stone[i].y=108;
Stone[i].forward=1;
break;
case 7:
Stone[i].x=148;
Stone[i].y=108;
Stone[i].forward=3;
break;
case 0:
Stone[i].x=74;
Stone[i].y=8;
Stone[i].forward=4;
break;
case 1:
Stone[i].x=148;
Stone[i].y=54;
Stone[i].forward=3;
break;
case 2:
Stone[i].x=74;
Stone[i].y=108;
Stone[i].forward=2;
break;
case 3:
Stone[i].x=4;
Stone[i].y=54;
Stone[i].forward=1;
break;
}
}
int * locate_di1(int x,int y)
{
int di[4];
int *dir=di;
if(x+3>Man.x+6)
{
di[0]=3;
di[1]=2;
di[2]=4;
di[3]=1;
}
else if(x+3<Man.x-6)
{
di[0]=1;
di[1]=2;
di[2]=4;
di[3]=3;
}
else
{
if(y+3>Man.y)
{
di[0]=2;
di[1]=1;
di[2]=3;
di[3]=4;
}
else
{
di[0]=4;
di[1]=1;
di[2]=3;
di[3]=2;
}
}
return dir;
}
int *locate_di2(int x,int y)
{
int di[4];
int *dir=di;
if(y+3>Man.y+6)
{
di[0]=2;
di[1]=1;
di[2]=3;
di[3]=4;
}
else if(y+3<Man.y-6)
{
di[0]=4;
di[1]=1;
di[2]=3;
di[3]=2;
}
else
{
if(x+3>Man.x)
{
di[0]=3;
di[1]=2;
di[2]=4;
di[3]=1;
}
else
{ di[0]=1;
di[1]=2;
di[2]=4;
di[3]=3;
}
}
return dir;
}
judge1(int i)
{
int num=STONE_F_NUM;
int j,t,ifroute=0;
int m,n;
int m_b,n_b;
int x0=Stone[i].x,y0=Stone[i].y;
int x1=x0,y1=y0,x2=x0+WIDE_STONE-1,y2=y0+WIDE_STONE-1;
int *dir;
if(Sign%3==0)
dir=locate_di1(x0,y0);
else
dir=locate_di2(x0,y0);
Sign++;
for(t=0;t<4;t++)
{
Stone[i].forward=dir[t];
switch(Stone[i].forward)
{
case 1:x1=x0+WIDE_STONE;x2=x1+num-1;break;
case 2:y2=y0-1;y1=y2-num+1;break;
case 3:x2=x0-1;x1=x2-num+1;break;
case 4:y1=y0+WIDE_STONE;y2=y1+num-1;break;
}
for(m=x1;m<=x2;m++)
{
for(n=y1;n<=y2;n++)
{
if(Array[m][n]==BULLET_AB)
{
move1(i);
for(j=0;j<MAX_BULLETS;j++)
if(Bullet[j].fly==1)
{
for(m_b=Bullet[j].x-R_BULLET;m_b<=Bullet[j].x+R_BULLET;j++)
for(n_b=Bullet[j].y-R_BULLET;n_b<=Bullet[j].y+R_BULLET;j++)
{
if(Array[m_b][n_b]==STONE_AB)
{
draw3(Bullet[j].x,Bullet[j].y,BKCOLOR,0);
Bullet[j].fly=0;
}
}
}
if(Food.exist)
draw1(Food.x,Food.y,BKCOLOR,0);
draw1(Stone[i].x,Stone[i].y,Food.color,FOOD_AB);
Food.x=Stone[i].x;
Food.y=Stone[i].y;
Food.exist=1;
Stone_Left--;
setcolor(BKCOLOR);
outtextxy(600,5,p_stone);
itoa(Stone_Left,p_stone,10);
setcolor(WHITE);
outtextxy(600,5,p_stone);
if(Stone_Left>7)
stone_init(i);
else
{
Stone[i].x=0;
}
return(1);
}
else if(Array[m][n]==MAN_AB)
{
Game_Over=1;
return(0);
}
else if(Array[m][n]==FOOD_AB)
{
draw1(Food.x,Food.y,BKCOLOR,0);
Food.exist=0;
return(0);
}
else if((Array[m][n]==1)||Array[m][n]==STONE_AB)
{
ifroute=1;
break;
}
}
if(ifroute==1)
break;
}
if(ifroute==1)
continue;
else
return 0;
}
if(t==4)
return 1;
}
int judge2()
{
int num=MAN_F_NUM;
int sign=0;
int m,n;
int x0=Man.x,y0=Man.y;
int x1=x0-R_MAN,y1=y0-R_MAN,x2=x0+R_MAN,y2=y0+R_MAN;
switch(Man.forward)
{
case 1:x1=x0+R_MAN+1;x2=x1+num-1;break;
case 2:y2=y0-R_MAN-1;y1=y2-num+1;break;
case 3:x2=x0-R_MAN-1;x1=x2-num+1;break;
case 4:y1=y0+R_MAN+1;y2=y1+num-1;break;
}
for(m=x1;m<=x2;m++)
{
for(n=y1;n<=y2;n++)
{
if(Array[m][n]==STONE_AB)
{
Game_Over=1;
sign=0;
return(sign);
}
else if(Array[m][n]==FOOD_AB)
{
draw1(Food.x,Food.y,BKCOLOR,0);
Food.exist=0;
Bullet_Num+=2;
setcolor(BKCOLOR);
outtextxy(100,5,p_bullet);
itoa(Bullet_Num,p_bullet,10);
setcolor(WHITE);
outtextxy(100,5,p_bullet);
sign=0;
return(sign);
}
else if(Array[m][n]==1)
{
sign=1;
return(1);
}
}
}
}
judge3(int i)
{
int m,n,m_s,n_s;
int j;
int num=BULLET_F_NUM;
int sign=0;
int x0=Bullet[i].x,y0=Bullet[i].y;
int x1=x0-R_BULLET,y1=y0-R_BULLET,x2=x0+R_BULLET,y2=y0+R_BULLET;
switch(Bullet[i].forward)
{
case 1:x1=x0+R_BULLET+1;x2=x1+num-1;break;
case 2:y2=y0-R_BULLET-1;y1=y2-num+1;break;
case 3:x2=x0-R_BULLET-1;x1=x2-num-1;break;
case 4:y1=y0+R_BULLET+1;y2=y1+num+1;break;
}
for(m=x1;m<=x2;m++)
{
for(n=y1;n<=y2;n++)
{
if(Array[m][n]==STONE_AB)
{
move3(i);
for(j=0;j<STONE_NUM;j++)
{
if(Stone[j].x!=0)
{
for(m_s=Stone[j].x;m_s<Stone[j].x+WIDE_STONE;m_s++)
{
for(n_s=Stone[j].y;n_s<Stone[j].y+WIDE_STONE;n_s++)
{
if(Array[m_s][n_s]==BULLET_AB)
{
if(Food.exist==1)
draw1(Food.x,Food.y,BKCOLOR,0);
draw1(Stone[j].x,Stone[j].y,Food.color,FOOD_AB);
Food.x=Stone[j].x;
Food.y=Stone[j].y;
Food.exist=1;
Stone_Left--;
setcolor(BKCOLOR);
outtextxy(600,5,p_stone);
itoa(Stone_Left,p_stone,10);
setcolor(WHITE);
outtextxy(600,5,p_stone);
if(Stone_Left>7)
{
stone_init(j);
}
else
{
Stone[j].x=0;
}
draw3(Bullet[i].x,Bullet[i].y,BKCOLOR,0);
Bullet[i].fly=0;
sign=1;
return sign;
}
}
}
}
}
}
else if(Array[m][n]==FOOD_AB)
{
draw1(Food.x,Food.y,BKCOLOR,0);
Food.exist=0;
sign=0;
return(sign);
}
else if(Array[m][n]==1)
{
draw3(Bullet[i].x,Bullet[i].y,BKCOLOR,0);
Bullet[i].fly=0;
sign=1;
return(sign);
}
}
}
}
keyprocess(int key)
{
int i;
switch(key)
{
case VK_ESC: break;
case VK_UP: Man.forward=2;
if(judge2()!=1)
{
move2();
Man_Forward=2;
}
break;
case VK_DOWN: Man.forward=4;
if(judge2()!=1)
{ move2();
Man_Forward=4;
}
break;
case VK_RIGHT: Man.forward=1;
if(judge2()!=1)
{ move2();
Man_Forward=1;
}
break;
case VK_LEFT: Man.forward=3;
if(judge2()!=1)
{ move2();
Man_Forward=3;
}
break;
case VK_FIRE:if(Bullet_Num>0)
{
for(i=0;i<MAX_BULLETS;i++)
{
if(Bullet[i].fly==0)
{
Bullet[i].x=Man.x;
Bullet[i].y=Man.y;
Bullet[i].forward=Man.forward;
move3(i);
Bullet[i].fly=1;
break;
}
}
Bullet_Num--;
setcolor(BKCOLOR);
outtextxy(100,5,p_bullet);
itoa(Bullet_Num,p_bullet,10);
setcolor(WHITE);
outtextxy(100,5,p_bullet);
}
break;
}
}
void movebullet()
{
int i;
for(i=0;i<MAX_BULLETS;i++)
{
if(Bullet[i].fly==1)
{
if(judge3(i)!=1)
move3(i);
}
}
}
void movestone()
{
int i;
for(i=0;i<STONE_NUM;i++)
if(TimeCount==i)
{
if(Stone[i].x!=0)
{
if(judge1(i)==0)
{
move1(i);
}
}
}
}
void load_map()
{
int i,j;
FILE *fp;
if((fp=fopen("map1","rb"))==NULL)
{
printf("cannot open map.\n");
return;
}
for(i=0;i<160;i++)
for(j=0;j<120;j++)
if(fread(&Array[i][j],2,1,fp)!=1)
{
if(feof(fp))
{
fclose(fp);
return;
}
printf("file read error!\n");
}
fclose(fp);
}
void draw_map()
{
int i,j;
setcolor(YELLOW);
for(i=0;i<160;i++)
for(j=0;j<120;j++)
{
if(Array[i][j]==1)
{
line(i*4,j*4,i*4+3,j*4+3);
}
}
}
void preplay()
{
setbkcolor(CYAN);
settextjustify(1,1);
settextstyle(3,0,2);
outtextxy(320,100,"This game is created by Xu Guang hua with turboc2.0.");
outtextxy(320,150,"Welcome to contect me.");
outtextxy(320,200,"QQ:87360410 e-mail: earthnut@peoplemail.com.cn");
outtextxy(320,250,"press any key to continue.");
getch();
setfillstyle(SOLID_FILL,BKCOLOR);
bar(30,80,600,300);
}
main()
{
int sign;/*test if timecount increase*/
int i,j;
int key=0;
int driver=DETECT,mode;
for(i=0;i<STONE_NUM;i++)
{
stone_init(i);
Stone[i].color=8;
}
Man.x=80;
Man.y=60;
Man.color=6;
Man.forward=2;
Man_Forward=2;
Food.color=2;
Food.exist=0;
for(i=0;i<MAX_BULLETS;i++)
{
Bullet[i].color=4;
Bullet[i].fly=0;
}
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"d:\\turboc2");
preplay();
setbkcolor(BKCOLOR);
load_map();
draw_map();
draw2(Man.x,Man.y,Man.color,MAN_AB,Man.forward);
setcolor(10);
settextstyle(1,0,1);
settextjustify(0,1);
outtextxy(20,5,"bullet :");
itoa(Bullet_Num,p_bullet,10);
outtextxy(100,5,p_bullet);
outtextxy(260,5,"time :");
itoa((TIME_OVER-Time)/18,p_time,10);
outtextxy(340,5,p_time);
outtextxy(520,5,"stone :");
itoa(Stone_Left,p_stone,10);
outtextxy(600,5,p_stone);
settimer(newhandler);
for(;key!=0x11b;)
{
sign=-1;
setcolor(9);
outtextxy(200,200,"I stand alone in the darkness.");
for(TimeCount=0;TimeCount<TIME_CIRCLE;)
{
if(bioskey(1))
{
key=bioskey(0);
keyprocess(key);
}
if(TimeCount>sign)
{
movebullet();
movestone();
sign=TimeCount;
}
if(Time%18==0)
{
setcolor(BKCOLOR);
outtextxy(340,5,p_time);
itoa((TIME_OVER-Time)/18,p_time,10);
setcolor(WHITE);
outtextxy(340,5,p_time);
}
}
if(Game_Over==1)
{
setcolor(13);
settextjustify(1,1);
settextstyle(1,0,3);
outtextxy(320,200,"========== GAME OVER ==========");
outtextxy(320,240," maybe you can win next time.");
outtextxy(320,280," press ESC to exit.");
break;
}
else if(Stone_Left==0)
{
setcolor(14);
settextjustify(1,1);
settextstyle(3,0,3);
outtextxy(320,200,"========== YOU WIN ==========");
outtextxy(320,240,"congratulations!!!");
outtextxy(320,280,"press ESC to exit.");
break;
}
else if(Time>TIME_OVER)
{
setcolor(15);
settextjustify(1,1);
settextstyle(3,0,3);
outtextxy(320,200,"======= TIME OVER =======");
outtextxy(320,240," maybe you can win next time.");
outtextxy(320,280,"press ESC to exit.");
break;
}
}
killtimer();
for(;;)
{
if(bioskey(0)==VK_ESC)
{
break;
}
}
closegraph();
}
網友評論:------我要發表評論
· 努力噢(wuwu)
· 精彩(跳蚤偵探)
什么時候我可以達到這個程度就好了,呵呵
Welcome to 202.112.86.129
--------------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -