📄 wsl1.txt
字号:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
#include <time.h>
#include <dos.h>
#define TOP 40
#define BOTTOM 440
#define LEFT 120
#define RIGHT 520
#define SIDE 20
int b[20][20]; /*定义一个二维数组画图*/
b[20][20]={{0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,1},
{1,0,0,1,1,1,0,1,1,0,1,0,1,1,0,1,1,0,0,1},
{1,0,1,0,1,1,0,1,1,0,1,0,1,1,0,1,1,0,0,1},
{1,0,0,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,1,1},
{1,1,1,0,1,1,1,0,1,0,1,0,1,1,0,0,0,0,0,1},
{1,1,0,0,0,1,0,0,1,0,0,1,0,1,0,0,1,1,0,1},
{1,0,0,1,1,0,0,1,0,0,1,0,0,0,1,0,1,0,0,1},
{1,0,0,0,0,0,1,1,0,1,1,0,1,1,0,0,1,1,0,1},
{1,0,1,0,0,1,0,0,0,1,0,0,1,1,0,0,1,0,0,1},
{1,0,1,0,1,1,0,1,1,0,0,1,1,0,1,0,1,0,1,1},
{1,0,1,1,0,0,0,1,1,1,0,1,1,0,1,0,1,0,1,1},
{1,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,1},
{1,1,0,1,1,0,1,0,0,1,0,1,1,1,0,1,1,0,1,1},
{1,0,0,0,1,0,1,0,0,0,1,1,0,0,0,1,0,0,0,1},
{1,0,1,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,1},
{1,0,1,0,0,0,1,1,0,0,1,0,1,1,0,1,1,0,0,1},
{1,0,1,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,1},
{1,0,0,0,0,1,0,1,1,1,1,0,0,1,1,0,1,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0}};/*画迷宫地图*/
int x=1,y=1;
void drawwall(int x,int y) /*画网格墙*/
{
setfillstyle(HATCH_FILL,LIGHTGRAY);
bar(LEFT+1+x*SIDE,TOP+1+y*SIDE,RIGHT-1-19*SIDE+x*SIDE,BOTTOM-1-19*SIDE+y*SIDE);
}
void drawpath(int x,int y) /*画路径方格*/
{
setfillstyle(SOLID_FILL,WHITE);
bar(LEFT+1+x*SIDE,TOP+1+y*SIDE,RIGHT-1-19*SIDE+x*SIDE,BOTTOM-1-19*SIDE+y*SIDE);
}
void draw(int x,int y) /*画寻宝人*/
{
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(LEFT+1+x*SIDE,TOP+1+y*SIDE,RIGHT-1-19*SIDE+x*SIDE,BOTTOM-1-19*SIDE+y*SIDE);
setcolor(RED);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(LEFT+10+x*SIDE,TOP+10+y*SIDE,"\2");
}
void cover(int x,int y) /*覆盖人头*/
{
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(LEFT+1+x*SIDE,TOP+1+y*SIDE,RIGHT-1-19*SIDE+x*SIDE,BOTTOM-1-19*SIDE+y*SIDE);
}
void seek() /*寻宝踪迹*/
{ int ch=0;
do{
ch=bioskey(0);
switch(ch)
{
case 18432: /*向上键*/
if(y-1>=0)
if(b[y-1][x]==0)
{
cover(x,y);
y--;
draw(x,y);
break;
}
case 20480: /*向下键*/
if(b[y+1][x]==0)
{
cover(x,y);
y++;
draw(x,y);
break;
}
case 19200: /*向左键*/
if(x-1>=0)
if(b[y][x-1]==0)
{
cover(x,y);
x--;
draw(x,y);
break;
}
case 19712: /*向右键*/
if(b[y][x+1]==0)
{
cover(x,y);
x++;
draw(x,y);
break;
}
/*case 4209:exit(0);*/
}
if(x==18&&y==18)ch=283;
}while(ch!=283);
if(x==18&&y==18)/* 找到宝藏*/
{settextstyle(1,0,2);
setcolor(LIGHTMAGENTA);
outtextxy(330,110,"Good job !");
sleep(1);
settextstyle(1,0,3);
setcolor(GREEN);
outtextxy(330,230,"You're so smart !");
sleep(1);
settextstyle(1,0,4);
setcolor(BLUE);
outtextxy(330,340,"Congratulations !");
}
sleep(1);
}
void figure() /* 成型后整个迷宫的界面*/
{int i,j;
for(i=0;i<20;i++)
for(j=0;j<20;j++)
if(b[i][j]==1)
drawwall(j,i);
else
drawpath(j,i);
}
void blink() /* 等待宝藏闪烁变色*/
{settextjustify(CENTER_TEXT,CENTER_TEXT);
setcolor(GREEN);
outtextxy(491,411,"\5");
sleep(1);
settextjustify(CENTER_TEXT,CENTER_TEXT);
setcolor(YELLOW);
outtextxy(491,411,"\5");
sleep(1);
settextjustify(CENTER_TEXT,CENTER_TEXT);
setcolor(RED);
outtextxy(491,411,"\5");
sleep(1);
}
void init() /* 初始化图形*/
{
int gdriver=DETECT,gmode, errorcode;
initgraph(&gdriver,&gmode,"d:\\borlandc\\bgi");
}
void interface() /* 主界面*/
{ init();
setbkcolor(BLUE);
settextstyle(4,0,5);
outtextxy(50,80," Welcome to the maze world!");
settextstyle(1,0,3);
outtextxy(200,240,"E: Play the game");
outtextxy(200,280,"Q: Exit the game");
}
int qu_re() /* 按q退出,r重玩*/
{
char c,ch;
setcolor(YELLOW);
settextstyle(1,0,4);
outtextxy(324,450,"q: exit r: replay");
while(1)
{
c=getch();
if(c=='q'||c=='Q')
return 0;
else if(c=='r'||c=='R')
{
/* cover(x,y);*/
x=1;
y=1;
draw(x,y);
return 1;
}
/* else
continue;*/
}
}
void g_begin() /*开始迷宫寻宝*/
{int t=1;
init();
/*cleardevice();
setbkcolor(0);*/
while(t)
{
clearviewport();
init();
figure();
draw(x,y);
blink();
seek();
/*cover(18,18);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(491,411,"\5");*/
t=qu_re();
}
}
void main()
{char ch;
interface();
while(1)
{
ch=getch();
switch(ch)
{ case 'E':
case 'e':g_begin(); break;
case 'Q':
case 'q':exit(0);
}
x=1;
y=1;
draw(x,y);
ch=getch();
if(ch=='q')
interface();
continue;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -