This program is mainly about how to let the enemy in the game to walk themselves, but will not be fixed at two points back and forth, I use the method is random direction, and take a step can not and the previous direction of the contrary, I hope the experts a lot of guidance. Also hope that the experts can be instructed to write a simple game of some technology.
#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 Wall, 1 can move the place, 3 own, 4 enemy * *
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};/* array is the map * *
StrUCt Play/* The structure of the characters in the game * *
{
int x;
int y;
};
struct play you,them[5];
int sum=0;/* to count the number of beans to eat, eat 50 even if the victory * *
INT xx[5][2];/* structure for judging enemy direction
int false=0;
void Timedelay (unsigned long microsec)/* Delay function Incoming microsecond number * *
{
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)/* Pacman Function * *
Begain ()/* Start function * *
{int i,j;
Sleep (1);
for (i=0;i<15;i++)
for (j=0;j<20;j++)
if (a[i][j]==2)/* Represents the wall */
{
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)/* On behalf of oneself * *
{
SetColor (RED);
Circle (100+j*20,100+i*20,9);
}
else if (a[i][j]==4)/* On behalf of the enemy * *
{
SetColor (GREEN);
Circle (100+j*20,100+i*20,9);
}
else if (a[i][j]==0)/* Stands for beans * *
{
SetColor (yellow);
Circle (100+j*20,100+i*20,3);
}
you.x=5;you.y=9;/* the enemy's own starting coordinates.
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) * * The process of moving the enemy * *
{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)//* As long as the controller is around you immediately.
them[i].x--;
Else
{
Loop
Xx[i][0]=rand () The direction of the%4+1;/* here takes a random assignment, and the principle is that the new direction must not be the opposite of the original direction.
if (xx[i][0]==1&&xx[i][1]==2xx[i][0]==2&&xx[i][1]==1)
Goto Loop;
if (xx[i][0]==3&&xx[i][1]==4xx[i][0]==4&&xx[i][1]==3)
Goto Loop;
XX[I][1]=XX[I][0];
if (xx[i][0]==1)/* Four Directions * *
{them[i].x--;
if (a[them[i].x][them[i].y]==2)/* If you touch the wall, go back to the original place and wait for the random direction.
{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)/* The judgment in moving
{
int i;
SetColor (0)/* Remove the enemy's old position * *
for (i=0;i<5;i++)
Circle (them[i].y*20+100,them[i].x*20+100,9);
Movethem (them)/* depending on the position of the controller to determine the direction of movement of the enemy * *
}
Win () * * *
{
Cleardevice ();
Settextstyle (0,0,4);
while (!kbhit ())
{
SetColor (rand ()%13+1);
Outtextxy (200,200, "you win!");
Delay (1000);
}
}
FALSE1 ()///Failure Screen * *
{
Cleardevice ();
Settextstyle (0,0,4);
while (!kbhit ())
{
SetColor (rand ()%13+1);
Outtextxy (180,200, "GAME over!");
Delay (1000);
}
}
Loseyes () * * To determine whether failure/
{int i;
for (i=0;i<5;i++)
if (THEM[I].X==YOU.X&&THEM[I].Y==YOU.Y)
false=1;/* if it fails.
}
Main ()
{int gd=detect,gm;
int key,i;
Initgraph (&GD,&GM, "C:\TC");
Cleardevice ();
Begain ()/* Start Screen * *
while (1)
{
while (!kbhit ())
{
SetColor (GREEN)/* Re-draw enemy * *
for (i=0;i<5;i++)
Circle (them[i].y*20+100,them[i].x*20+100,9);
Timedelay (280000);
Fun (them)/* Handling the enemy *
for (i=0;i<5;i++)
if (THEM[I].X==YOU.X&&THEM[I].Y==YOU.Y)
false=1;/* if it fails.
Loseyes ()/* To determine whether failure/
if (false)
Break
}
if (false)
Break
Key=bioskey (0);
SetColor (0)/* Remove the person from your original position.
Circle (100+you.y*20,100+you.x*20,9);
if (KEY==ESC)
Break
else if (key==up)/* The first judgment here is whether to eat beans and touch walls.
{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);/* Perform a keyboard and then draw your own position.
Circle (100+you.y*20,100+you.x*20,9);
Loseyes ()///////////
if (false)
Break
}
if (sum==50) * * Eat beans full * *
if (false)
Closegraph ();
}