吃金子游戏

2025-05-09 13:36:07
推荐回答(1个)
回答1:

#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
/*0-豆子, 1-空地, 2-墙壁, 3-玩家, 4-敌人*/
int beannum=0; /*这个数字不要人工输入,由Statistics函数完成统计地图信息得到*/
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};/*数组就是地图*/
/*你这里定义的地图有点问题 就是0

的个数没有达到50,所以即使全部吃完系统也不会判赢,为此,我加入了一个全局变量beannum表示地图

里豆子的总数,这个数字由Statistics函数完成统计地图信息得到*/

struct play /*游戏中人物的结构体*/
{
int x; /*x坐标*/
int y; /*y坐标*/
};
struct play you, them[5]; /*定义玩家×1,敌人×5*/

int sum = 0; /*统计吃的豆子个数,吃满beannum颗就算胜利*/
int xx[5][2]; /*判断敌人移动方向用的数组*/
int lose = 0; /*判断是否失败的变量 1为失败*/

/*-------------以下为新加入的函数------------*/
long choose_speed(); /*选择游戏速度 */
void printsum(); /*显示当前吃的豆子数*/
void Statistics(); /*统计地图中豆子数目*/
/*-------------以上为新加入的函数------------*/

void TimeDelay (unsigned long microsec) /*延时函数 传入微秒数*/
{
delay(microsec);
}

drawblackdou (int x, int y) /*吃到豆子的函数*/
{
setcolor (0); /*将豆子消除*/
circle (100 + y * 20, 100 + x * 20, 3);
sum++; /*豆子个数加一*/
printsum(); /*显示当前吃的豆子数*/
a[x][y] = 1; /*将豆子改为空地*/
}

begain () /*开始函数, 画出地图*/
{
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 (them[i].x == you.x && (them[i].y + 1) == you.y)
them[i].y++;
else if (them[i].x == you.x && (them[i].y - 1) == you.y)
them[i].y--;
else if (them[i].y == you.y && (them[i].x + 1) == you.x)
them[i].x++;
else if (them[i].y == you.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) /*1,2,3,4 四种可能*/
{
them[i].x--;
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); /*判断玩家位置,并向其移动*/
}

win () /*玩家胜利*/
{
cleardevice ();
settextstyle (0, 0, 4);
while (!kbhit())
{
setcolor (rand () % 13 + 1);
outtextxy (200, 200, "YOU WIN!");
delay (1000);
}
getch();/*加上它对程序界面可见性有帮助*/
}

false1 () /*玩家失败*/
{
cleardevice ();
settextstyle (0, 0, 4);
while (!kbhit())
{
setcolor (rand () % 13 + 1);
outtextxy (180, 200, "GAME OVER!");
delay (1000);
}
getch();/*加上它对程序界面可见性有帮助*/
}

loseyes () /*判断是否失败*/
{
int i;
for (i = 0; i < 5; i++) /*循环判断五个敌人,
如果抓住玩家则玩家失败*/
if (them[i].x == you.x && them[i].y == you.y)
lose = 1;
}

main()
{
int gd = DETECT, gm;
int key, i;
long time;
initgraph (&gd, &gm, "c:\\tc");
cleardevice ();
Statistics(); /*统计地图中豆子数目*/
time=choose_speed(); /*选择速度*/
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 (time);
fun (them); /*处理敌人*/
for (i = 0; i < 5; i++)
if (them[i].x == you.x && them[i].y == you.y)
lose = 1; /*失败*/
loseyes(); /*判断是否失败*/
if (lose)
break;
}
if (lose)
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 == beannum) /*吃够豆子退出,胜利*/
break;
setcolor (RED); /*执行了一次键盘后再画出自己的位置*/
circle (100 + you.y * 20, 100 + you.x * 20, 9);
loseyes (); /*玩家移动后碰到敌人的可能*/
if (lose)
break;
}
if (sum == beannum)/*吃满豆子了*/
{
win ();
getch ();
}
if (lose)
{
false1 ();
getch ();
}
closegraph ();
}

/*---------新增加的函数-------------------*/
long choose_speed() /*选择游戏速度*/
{
long i;
do
{
gotoxy(20,15);
printf(" ");
gotoxy(20,15);
printf("INPUT THE SPEED(1-3):");
scanf("%ld",&i);
}while(i<=0||i>3); /*1-3级*/
return (4-i)*1000000;
}

void printsum() /*显示当前吃的豆子数*/
{/*itoa
【功能】将一个整数转换为字符串。
【原型】char *itoa(int value,char *string,int radix)
【位置】stdlib.h
【说明】返回一个指针,指向目的字符串。
*/
int i;
char c[10];
itoa(sum,c,10);
bar(493,100,635,180);
setcolor(11);
outtextxy(500,120,">>EAT BEAN GAME<<");
outtextxy(500,140,"EAT BEANS NUMBER:");
outtextxy(550,160,c);
}

void Statistics() /*统计地图中豆子数目*/
{
int i,j;
for(i=0;i<15;i++)
for(j=0;j<20;j++)
if(a[i][j]==0)
beannum++;
}