#include
#include
#include
#include
#include
#include
#define SIZE 30
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
#define RADIUS 13
struct Pacman
{
int x;
int y;
int direction;
int live;
}MyPacman;
struct Ary
{
int scene[10][10];
int score2[10][10];
int e,f,g,h;
int i;
int j;
int a;
int b;
int count;
int Myscore;
char mode;
char mode2;
char *p;
}MyAry;
struct Monster
{
int x;
int y;
int direction;
}MyMonster;
struct point
{
int x ;
int y ;
}MyPoint;
void ClearPacman();
void MovePacman();
void Init();
void DrawMap();
void map();
void InitMap();
void DrawDou();
void InitPacman();
void DrawPacman();
void MovingPacman();
void DrawArc();
void win();
void InitMonster();
void DrawMonster();
void MoveMonster();
void ClearMonster();
void MovingMonster();
void InitAry();
void lost();
void clean();
void main()
{
char a;
int l;
Init();
InitAry();
again2:
setcolor(YELLOW);
settextstyle(1, 0, 3);
outtextxy(250, 240, "1:GAME START");
outtextxy(250, 270, "2:GAME MODE");
outtextxy(250, 300, "3:SELECT COL");
outtextxy(250, 330, "4:OUT GAME");
outtextxy(100, 81, "THE GAME PRODUCER IS :LIN ZHAO LE");
again:
switch(a=getch())
{
case '1':
cleardevice();
setcolor(BLUE);
settextstyle(1, 0, 14);
outtextxy(150, 150, "READY");
for(l=0;l<30;l++)
{
delay(50000);
}
cleardevice();
outtextxy(150, 150, "GO");
for(l=0;l<30;l++)
{
delay(50000);
}
break;
case '2':
cleardevice();
setcolor(YELLOW);
settextstyle(1, 0, 6);
outtextxy(150, 50, "1:EASY");
outtextxy(150, 150, "2:COMMON");
outtextxy(150, 250, "3:HARD");
outtextxy(150, 350, "4:VERY HARD");
again3:
MyAry.mode=getch();
if(MyAry.mode!='1'&&MyAry.mode!='2'&&MyAry.mode!='3'&&MyAry.mode!='4')
{
goto again3;
}
cleardevice();
goto again2;
case '3':
cleardevice();
setcolor(BLUE);
settextstyle(1, 0, 6);
outtextxy(150, 50, "1:FIRST COL");
outtextxy(150, 150, "2:SECONDLY COL");
outtextxy(150, 250, "3:THRED COL");
outtextxy(150, 350, "4:FOUR COL");
again4:
MyAry.mode2=getch();
if(MyAry.mode2!='1'&&MyAry.mode2!='2'&&MyAry.mode2!='3'&&MyAry.mode2!='4')
{
goto again4;
}
cleardevice();
goto again2;
case'4':goto out;
default :goto again;
}
cleardevice();
setcolor(YELLOW);
settextstyle(1, 0, 3);
outtextxy(400, 100, "Your score is 1");
InitMap();
InitPacman();
InitMonster();
Map();
DrawMonster();
InitPacman();
MovingMonster();
closegraph();
out:
;
}
void InitAry()
{
MyAry.i=1;MyAry.j=1;
MyAry.a=8;MyAry.b=8;
MyAry.e=1;MyAry.f=1;
MyAry.g=8;
MyAry.h=8;
MyAry.mode2='1';
MyAry.mode='1';
MyAry.p="F:\\map.txt";
MyAry.Myscore=1;
MyAry.count=0;
}
void clean()
{
setcolor(BLACK);
setlinestyle(0,0,1);
setfillstyle(1,0);
bar(380, 100, 600, 150);
}
void InitMonster()
{
MyMonster.x=8*SIZE;
MyMonster.y=8*SIZE;
MyMonster.direction=RIGHT;
}
void InitPacman()
{
MyPacman.x=1.5*SIZE;
MyPacman.y=1.5*SIZE;
MyPacman.direction=RIGHT;
MyPacman.live=1;
}
void InitMap()
{
int k,s;
FILE *fp2;
char line[80];
MyPoint.x=0;
MyPoint.y=0;
switch(MyAry.mode2)
{
case '1':
MyAry.p="F:\\map.txt";
break;
case '2':
MyAry.p="F:\\map2.txt";
break;
case '3':
MyAry.p="F:\\map3.txt";
break;
case '4':
MyAry.p="F:\\map4.txt";
break;
}
if((fp2=fopen(MyAry.p,"r"))==NULL)
{
printf("File open error!\n");
exit(1);
}
else
{
for(k=0;k<10;k++)
{
fgets(line,12,fp2);
for(s=0;s<10;s++)
{
MyAry.score2[k][s]=line[s]-'0';
MyAry.scene[k][s]=line[s]-'0';
}
}
}
}
void Init()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
setbkcolor(BLACK);
cleardevice();
}
int Map()
{
char ch;
FILE *fp;
if((fp=fopen(MyAry.p,"r"))==NULL)
{
printf("File open error!\n");
exit(1);
}
else
{
while(ch!=EOF)
{
ch=fgetc(fp);
if(ch=='1')
DrawMap();
if(ch=='0')
{
DrawDou();
DrawPacman();
}
MyPoint.x+=SIZE ;
if(ch=='\n')
{
MyPoint.x=0;
MyPoint.y+=SIZE ;
}
}
}
if(fclose(fp))
{
printf("Can not close the file!\n");
exit(0);
}
}
void DrawDou()
{
setcolor(YELLOW);
rectangle(MyPoint.x+0.4*SIZE ,MyPoint.y+0.4*SIZE ,MyPoint.x+0.6*SIZE ,MyPoint.y+0.6*SIZE);
}
void DrawMap()
{
setcolor(RED);
rectangle(MyPoint.x,MyPoint.y,MyPoint.x+SIZE ,MyPoint.y+SIZE );
}
void MovingPacman()
{
int key;
int k,s;
char * p;
int i;
DrawPacman();
while(bioskey(1))
{
key=bioskey(0);
switch(key)
{
case 0x4800:
MyPacman.direction=UP;
if((MyAry.g==MyAry.e)&&(MyAry.h==MyAry.f))
lost();
if( MyAry.score2[MyAry.i][MyAry.j]==0)
{
MyAry.score2[MyAry.i][MyAry.j]=1;
MyAry.Myscore++;
clean();
setcolor(BLUE);
sprintf(p, "your score is %d",MyAry.Myscore);
settextstyle(1, 0, 3);
outtextxy(400, 100, p);
}
MyAry.count=0;
for(k=0;k<10;k++)
{
for(s=0;s<10;s++)
{
if( MyAry.score2[k][s]==1)
{
MyAry.count++;
}
if(MyAry.count==100)
{
win();
}
}
}
if( MyAry.scene[MyAry.i-1][MyAry.j]==0)
{
MyAry.i--;
MyAry.e=MyAry.i;
ClearPacman();
MovePacman();
DrawPacman();
}break;
case 0x5000:
MyPacman.direction=DOWN;
if((MyAry.g==MyAry.e)&&(MyAry.h==MyAry.f))
lost();
if( MyAry.score2[MyAry.i][MyAry.j]==0)
{
MyAry.score2[MyAry.i][MyAry.j]=1;
MyAry.Myscore++;
clean();
setcolor(BLUE);
sprintf(p, "your score is %d",MyAry.Myscore);
settextstyle(1, 0, 3);
outtextxy(400, 100, p);
}
MyAry.count=0;
for(k=0;k<10;k++)
{
for(s=0;s<10;s++)
{
if( MyAry.score2[k][s]==1)
{
MyAry.count++;
}
if(MyAry.count==100)
{
win();
}
}
}
if( MyAry.scene[MyAry.i+1][MyAry.j]==0)
{
MyAry.i++;
MyAry.e=MyAry.i;
ClearPacman();
MovePacman();
DrawPacman();
}
break;
case 0x4b00:
MyPacman.direction=LEFT;
if((MyAry.g==MyAry.e)&&(MyAry.h==MyAry.f))
lost();
if( MyAry.score2[MyAry.i][MyAry.j]==0)
{
MyAry.score2[MyAry.i][MyAry.j]=1;
MyAry.Myscore++;
clean();
setcolor(BLUE);
sprintf(p, "your score is %d",MyAry.Myscore);
settextstyle(1, 0, 3);
outtextxy(400, 100, p);
}
MyAry.count=0;
for(k=0;k<10;k++)
{
for(s=0;s<10;s++)
{
if( MyAry.score2[k][s]==1)
{
MyAry.count++;
}
if(MyAry.count==100)
{
win();
}
}
}
if( MyAry.scene[MyAry.i][MyAry.j-1]==0)
{
MyAry.j--;
MyAry.f=MyAry.j;
ClearPacman();
MovePacman();
DrawPacman();
}
break;
case 0x4d00:
MyPacman.direction=RIGHT;
if((MyAry.g==MyAry.e)&&(MyAry.h==MyAry.f))
lost();
if( MyAry.score2[MyAry.i][MyAry.j]==0)
{
MyAry.score2[MyAry.i][MyAry.j]=1;
MyAry.Myscore++;
clean();
setcolor(BLUE);
sprintf(p, "your score is %d",MyAry.Myscore);
settextstyle(1, 0, 3);
outtextxy(400, 100, p);
}
MyAry.count=0;
for(k=0;k<10;k++)
{
for(s=0;s<10;s++)
{
if( MyAry.score2[k][s]==1)
{
MyAry.count++;
}
if(MyAry.count==100)
{
win();
}
}
}
if( MyAry.scene[MyAry.i][MyAry.j+1]==0)
{
MyAry.j++;
MyAry.f=MyAry.j;
ClearPacman();
MovePacman();
DrawPacman();
}
break;
case 0x011b:
MyPacman.live=0;
lost();
break;
}
}
}
void win()
{
int l;
cleardevice();
setcolor(YELLOW);
settextstyle(1, 0, 8);
outtextxy(200,200,"YOU WIN");
getch();
closegraph();
}
void lost()
{
cleardevice();
setcolor(YELLOW);
outtextxy(100,100,"GAME OVER");
getch();
closegraph();
}
void DrawPacman()
{
setcolor(YELLOW);
setlinestyle(0,0,1);
setfillstyle(1,14);
switch(MyPacman.direction)
{
case UP:case DOWN:case LEFT:
pieslice(MyPacman.x,MyPacman.y,0,360,RADIUS);
DrawArc();
break;
case RIGHT:
pieslice(MyPacman.x,MyPacman.y,-30,30,RADIUS);
break;
}
}
void MovePacman()
{
switch(MyPacman.direction)
{
case UP:
MyPacman.y-=30;
break;
case DOWN:
MyPacman.y+=30;
break;
case LEFT:
MyPacman.x-=30;
break;
case RIGHT:
MyPacman.x+=30;
break;
}
}
void ClearPacman()
{
setcolor(BLACK);
setlinestyle(0,0,1);
setfillstyle(1,0);
pieslice(MyPacman.x,MyPacman.y,0,360,RADIUS);
}
void DrawArc()
{
setcolor(BLACK);
setlinestyle(0,0,1);
setfillstyle(1,0);
switch(MyPacman.direction)
{
case(UP):
pieslice(MyPacman.x,MyPacman.y,120,60,RADIUS);
break;
case DOWN:
pieslice(MyPacman.x,MyPacman.y,240,-60,RADIUS);
break;
case LEFT:
pieslice(MyPacman.x,MyPacman.y,150,-150,RADIUS);
break;
}
}
void DrawMonster()
{
setcolor(BLUE);
rectangle(MyMonster.x+0.1*SIZE ,MyMonster.y+0.1*SIZE ,MyMonster.x+0.9*SIZE ,MyMonster.y+0.9*SIZE);
}
void MoveMonster()
{
switch(MyMonster.direction)
{
case UP:
MyMonster.y-=30;
break;
case DOWN:
MyMonster.y+=30;
break;
case LEFT:
MyMonster.x-=30;
break;
case RIGHT:
MyMonster.x+=30;
break;
}
}
void ClearMonster()
{
setcolor(BLACK);
rectangle(MyMonster.x+0.1*SIZE ,MyMonster.y+0.1*SIZE ,MyMonster.x+0.9*SIZE ,MyMonster.y+0.9*SIZE);
}
void MovingMonster()
{
int k;
int i;
DrawMonster();
while(MyPacman.live)
{
switch( MyAry.mode)
{
case '1':
for(i=0;i<28;i++)
{
delay(5000);
} break;
case '2':
for(i=0;i<20;i++)
{
delay(5000);
} break;
case '3':
for(i=0;i<13;i++)
{
delay(5000);
} break;
case '4':
for(i=0;i<7;i++)
{
delay(5000);
} break;
}
k=rand()%4;
switch(k)
{
case 0:
if((MyAry.g==MyAry.e)&&(MyAry.h==MyAry.f))
lost();
if( MyAry.scene[MyAry.a-1][MyAry.b]==0)
{
MyMonster.direction=UP;
MyAry.a--;
MyAry.g=MyAry.a;
ClearMonster();
MoveMonster();
DrawMonster();
}
break;
case 1:
if((MyAry.g==MyAry.e)&&(MyAry.h==MyAry.f))
lost();
if( MyAry.scene[MyAry.a+1][MyAry.b]==0)
{
MyMonster.direction=DOWN;
MyAry.a++;
MyAry.g=MyAry.a;
ClearMonster();
MoveMonster();
DrawMonster();
}break;
case 2:
if((MyAry.g==MyAry.e)&&(MyAry.h==MyAry.f))
lost();
if( MyAry.scene[MyAry.a][MyAry.b-1]==0)
{
MyMonster.direction=LEFT;
MyAry.b--;
MyAry.h=MyAry.b;
ClearMonster();
MoveMonster();
DrawMonster();
}break;
case 3:
if((MyAry.g==MyAry.e)&&(MyAry.h==MyAry.f))
lost();
if( MyAry.scene[MyAry.a][MyAry.b+1]==0)
{
MyMonster.direction=RIGHT;
MyAry.b++;
MyAry.h=MyAry.b;
ClearMonster();
MoveMonster();
DrawMonster();
}break;
}
MovingPacman();
}
}