linux下用c语言写吃金豆,求告知如何写个吃金豆的C语言代码

该代码实现了一个Pacman游戏,包括游戏开始、模式选择、颜色选择、地图读取、Pacman和怪物的移动、碰撞检测等功能。玩家可以通过键盘控制Pacman移动,吃掉地图上的点以得分,同时怪物会随机移动,若Pacman被怪物碰到则游戏结束。游戏设有不同难度,速度随难度增加而加快。
摘要由CSDN通过智能技术生成

#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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值