MAZE(C Language)

#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>

#define ESC 0x1b
#define UP 0x48
#define DOWN 0x50
#define LEFT 0x4b
#define RIGHT 0x4d

void maze(int x,int y,int style);
void draw(void);
int specialkey(void);
void star(int x0,int y0,int r);

main()
{
int graphdriver=VGA,graphmode=VGAHI;
int key,x,y;

/* ---------- set graphics mode ------------ */
initgraph(&graphdriver,&graphmode,"");
cleardevice();

draw();star(335,225,15);

setcolor(WHITE);
outtextxy(10,10,"MAZE 1.01");
outtextxy(500,460,"Electric Peri");

x=160;y=400;
putpixel(x,y,YELLOW);

setcolor(LIGHTBLUE);
outtextxy(x-80,y-3,"Start");
/* draw a -> */
setcolor(WHITE);
line(x-32,y,x-8,y);
line(x-8,y,x-16,y+4);
line(x-8,y,x-16,y-4);

do
{
key=specialkey();
switch(key)
{
case UP:
{
if(getpixel(x,y-8)!=WHITE)
{ if(getpixel(x,y-8)==RED)
setcolor(BLACK);
else
setcolor(RED);
line(x,y,x,y-16); y-=16; }
}break;

case DOWN:
{
if(getpixel(x,y+8)!=WHITE)
{ if(getpixel(x,y+8)==RED)
setcolor(BLACK);
else
setcolor(RED);
line(x,y,x,y+16); y+=16; }
}break;

case LEFT:
{
if(getpixel(x-8,y)!=WHITE)
{ if(getpixel(x-8,y)==RED)
setcolor(BLACK);
else
setcolor(RED);
line(x,y,x-16,y); x-=16; }

}break;

case RIGHT:
{
if(getpixel(x+8,y)!=WHITE)
{ if(getpixel(x+8,y)==RED)
setcolor(BLACK);
else
setcolor(RED);
line(x,y,x+16,y); x+=16;}

}break;

default:break;
}

}while(key!=ESC);

/* ---------- close graphics mode -------- */
closegraph();
}




void maze(int x,int y,int style)
{

x*=16; y*=16;
/*
setcolor(DARKGRAY);
rectangle(x,y,x+16,y+16);

line(x,y+4,x+16,y+4);
line(x,y+8,x+16,y+8);
line(x,y+12,x+16,y+12);

line(x+4,y,x+4,y+16);
line(x+8,y,x+8,y+16);
line(x+12,y,x+12,y+16);
*/

setcolor(WHITE);

switch(style)
{
case 0: break;
case 1: line(x+8,y,x+8,y+16); break;
case 2: line(x,y+8,x+16,y+8); break;
case 3: {line(x+8,y,x+8,y+16);line(x,y+8,x+16,y+8);} break;
case 4: {line(x+8,y,x+8,y+16);line(x+8,y+8,x+16,y+8);} break;
case 5: {line(x+8,y,x+8,y+16);line(x,y+8,x+8,y+8);} break;
case 6: {line(x,y+8,x+16,y+8);line(x+8,y,x+8,y+8);} break;
case 7: {line(x,y+8,x+16,y+8);line(x+8,y+8,x+8,y+16);} break;
case 8: {line(x+8,y+8,x+8,y+16);line(x,y+8,x+8,y+8);} break;
case 9: {line(x+8,y+8,x+8,y+16);line(x+8,y+8,x+16,y+8);} break;
case 10: {line(x,y+8,x+8,y+8);line(x+8,y,x+8,y+8);} break;
case 11: {line(x+8,y+8,x+16,y+8);line(x+8,y,x+8,y+8);} break;
case 12: line(x+8,y,x+8,y+8); break;
case 13: line(x+8,y+8,x+8,y+16); break;
case 14: line(x,y+8,x+8,y+8); break;
case 15: line(x+8,y+8,x+16,y+8); break;

default:printf("Error!/n");break;
}
}




void draw(void)
{
int i,j;
int data[22][22]={
{ 9, 7, 2, 2, 7, 7, 2, 2, 2, 7, 2, 2, 7, 2, 2, 7, 2, 2, 2, 2, 2, 8},
{ 1, 4, 7,14, 1, 1, 9, 2, 8, 4, 2,14, 4, 8,13,12,15, 2, 2, 7,14, 1},
{ 1,12,12, 9,10,12, 1,15,10, 1, 9, 7,10,12, 4, 2, 8,13, 9, 6, 2, 5},
{ 1,13,15, 5,13,15, 6, 7, 8, 1, 1, 4,14,15, 6, 8,11, 5, 1,13,15, 5},
{ 1,11, 8,12, 4, 2,14, 1,12,12, 1,12, 9, 2, 8, 4,14, 1, 1, 4,14, 1},
{ 4, 8,11, 2, 5, 9, 8, 1, 9, 2, 5,13, 1,13,12,12,13, 1, 1, 1, 9, 5},
{ 1,12,13, 9, 5,12, 1, 1, 1,13, 1, 1, 1,11, 2, 7,10, 1, 1, 1,12, 1},
{ 4, 2, 5,12,11, 8, 4, 6, 5, 1,12, 4, 6, 2, 2, 5,15, 6, 5,11, 8, 1},
{ 1,13,11, 2, 8, 1,12,13, 1,11, 7, 6, 2, 2, 8, 1, 9, 8, 4, 8, 1, 1},
{ 4, 5, 9,14,12, 1,15, 5,11, 8, 1, 0, 0,13,12, 1,12, 1,12,12, 1, 1},
{ 1,12, 1, 9, 2, 3,14, 1, 9,10, 1, 0, 0, 1,13, 1,13,11, 7, 2, 5, 1},
{ 1, 9, 6, 5, 9, 6, 8, 1, 1,15, 3, 2, 2,10,11, 5,11, 8,12, 9,10, 1},
{ 1,12,13, 1,12, 9,10, 1, 4,14, 1, 9, 2, 2, 2, 6, 2, 5,13, 1,13, 1},
{ 1, 9, 5,11,14, 1,15, 6, 6, 8, 1,11, 7,14, 9, 2,14,12, 1, 1,11, 5},
{ 4, 5, 1, 9, 2, 6, 2, 7,14, 1,11, 8, 1, 9, 6, 2, 7, 2, 3, 6, 8, 1},
{ 1,12, 1,12,13, 9, 8, 1,15, 6, 8, 1,11, 5, 9,14,12,13, 1,13, 1, 1},
{ 1,13,11, 2, 3,10, 1,11, 2, 8, 1, 4,14, 1,11, 2, 7,10,12, 1, 1, 1},
{ 4, 3, 2,14, 1,13,11,14,13, 1,12, 1, 9, 5,15, 2, 6, 2, 8, 1, 1, 1},
{ 1, 1,15, 8, 1,11, 2, 7,10,11, 2, 5,12,11, 2, 7, 8,13, 1, 1,12, 1},
{ 1, 4,14, 1,12, 9, 8,11, 2, 7, 2, 6, 8, 9,14, 1, 1,11, 6, 6, 2, 5},
{12,12,15, 6, 2,10, 1,15, 2, 6,14,15, 6, 5,15,10,11, 2, 2, 2,14, 1},
{15, 2, 2, 2, 2, 2, 6, 2, 2, 2, 2, 2, 2, 6, 2, 2, 2, 2, 2, 2, 2,10},
};

for(i=0;i<22;i++)
for(j=0;j<22;j++)
maze(j+9,i+4,data[i][j]);

}




int specialkey()
{
int key;
while(bioskey(1)==0);
key=bioskey(0);
key=key&0xff?key&0xff:key>>8;
return(key);
}





void star(int x0,int y0,int r)
{
int i,n=0,x[5],y[5];
float pi;

pi=3.14/180;

for(i=0;i<5;i++)
{
x[i]=cos((n-90)*pi)*r+x0;
y[i]=sin((n-90)*pi)*r+y0;
n+=72;
}
setcolor(YELLOW);

line(x[0],y[0],x[2],y[2]);
line(x[2],y[2],x[4],y[4]);
line(x[4],y[4],x[1],y[1]);
line(x[1],y[1],x[3],y[3]);
line(x[3],y[3],x[0],y[0]);

}

阅读更多
换一批

没有更多推荐了,返回首页