/*开始时,按数字 1 键进入人工控制模式;按w,s,
graphics not initialized(use 'initgraph')
a,d分别代表上,下,左,右方向。
开始时,按除数字 1 以外的任意键进入自动模式;
小人由电脑控制。
按 Q键结束程序*/
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <graphics.h>
#define N 22
#define M 22
int bg[M][N];
void makebg(int,int);
void drawbg(int[][],int,int,int,int,int);
void drawman(int,int,int);
void rect(int,int,int,int);
void main(){/* main()开始 */
int step=20;
int len=10;
int size=20;
int x=0,y=0;
int i=0,j=0;
int gdriver=DETECT,gmode;
char ch;
int direc;
makebg(M,N);
/* registerbgidriver(EGAVGA_driver);*/
/* initgraph(&gdriver,&gmode,"c:\\turboc2"); */
initgraph(&gdriver,&gmode,"c:\\turboc2\tc20\\bgi");
cleardevice();
setwritemode(XOR_PUT);
settextstyle(1,0,3);
setcolor(GREEN);
outtextxy(100,180,"Press <Q> to quit");
setcolor(BLUE);
setfillstyle(LINE_FILL,BLUE);
drawbg(bg,M,N,size,0,0);
setcolor(WHITE);
x+=len;y+=len;
drawman(x,y,len);
setcolor(GREEN);
outtextxy(60,120,"PRESS KEY <1> :YOU ,");
outtextxy(70,150,"OTHER KEY :AUTOMATIC");
setcolor(WHITE);
if((ch=getch())=='1'){
/* 人工控制 */
while((ch=getch())!='q'){
drawman(x,y,len);
switch(ch){
case 'a':
if(j>0&&bg[i][j-1]==0){
if(x>step){x-=step;j--;};
}
break;
case 's':
if(i<M-1&&bg[i+1][j]==0){
if(y<479-step){y+=step;i++;};
}
break;
case 'd':
if(j<N-1&&bg[i][j+1]==0){
if(x<639-step){x+=step;j++;}
}
break;
case 'w':
if(i>0&&bg[i-1][j]==0){
if(y>step){y-=step;i--;}
}
break;
default :break;
}
drawman(x,y,len);
delay(800);
if(i>=M-1&&j>=N-1){
settextstyle(4,0,3);
setcolor(RED);
outtextxy(150,260,"YOU WIN!");
}
setcolor(WHITE);
}
closegraph();
}/* 人工控制结束 */
else{
/* 电脑控制 */
/* direc表示上一步运动方向 */
/* 并表示下一步运动方向 */
/* 0~3分别表示 西、北、东、南 */
direc=2;
i=j=0;
while(i<M-1||j<N-1){
delay(80000);
drawman(x,y,len);
switch(direc){
case 0:
/* 以3,0,1的次序尝试 */
if(i<M-1&&bg[i+1][j]==0){
y+=step;i++;
direc=3;
}
else if(j>0&&bg[i][j-1]==0){
x-=step;j--;
direc=0;
}
else if(i>0&&bg[i-1][j]==0){
y-=step;i--;
direc=1;
}
else {
x+=step;j++;
direc=2;
}
break;
case 1:
if(j>0&&bg[i][j-1]==0){
x-=step;j--;
direc=0;
}
else if(i>0&&bg[i-1][j]==0){
y-=step;i--;
direc=1;
}
else if(j<N-1&&bg[i][j+1]==0){
x+=step;j++;
direc=2;
}
else{
y+=step;i++;
direc=3;
}
break;
case 2:
if(i>0&&bg[i-1][j]==0){
y-=step;i--;
direc=1;
}
else if(j<N-1&&bg[i][j+1]==0){
x+=step;j++;
direc=2;
}
else if(i<M-1&&bg[i+1][j]==0){
y+=step;i++;
direc=3;
}
else {
x-=step;j--;
direc=0;
}
break;
case 3:
if(j<N-1&&bg[i][j+1]==0){
x+=step;j++;
direc=2;
}
else if(i<M-1&&bg[i+1][j]==0){
y+=step;i++;
direc=3;
}
else if(j>0&&bg[i][j-1]==0){
x-=step;j--;
direc=0;
}
else {
y-=step;i--;
direc=1;
}
break;
default :break;
}
drawman(x,y,len);
}
getch();
closegraph();
}/* 电脑控制结束 */
}/* main()结束 */
/* 绘制小人 */
void drawman(int x,int y,int len){
int r=len/4;
rect(x-r,y-len,x+r,y-len+2*r);
line(x,y-len+2*r,x,y);
line(x-len,y,x+len,y);
line(x,y,x-len,y+len);
line(x,y,x+len,y+len);
}
/* 绘制迷宫地图 */
void drawbg(int bg[][N],int a,int b,int size,int x,int y){
int startx=x;
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
if(bg[i][j]==1)
rect(x,y,x+size-1,y+size-1);
x+=size;
}
x=startx;
y+=size;
}
rectangle(0,0,size*b,size*a);
line(0,0,size,0);line(0,0,0,size);
line(size*b,size*(a-1),size*b,size*a);
line(size*(b-1),size*a,size*b,size*a);
}
/* 绘制实心矩形 */
void rect(int x0,int y0,int x1,int y1){
int i,j;
for(i=x0;i<=x1;i++)
line(i,y0,i,y1);
}
/* 随机生成代表迷宫地图的数组 */
void makebg(int a,int b){
int i,j;
int ran;
int direc;
/* 初始化迷宫地图 */
for(i=0;i<a;i++)
for(j=0;j<b;j++)
bg[i][j]=1;
/* 随机生成迷宫通路 */
randomize();
i=j=0;direc=2;
while(1){
bg[i][j]=0;
if(i>=M-1&&j>=N-1)break;
ran=(int)rand()*4;
if(ran<1){
if(direc!=1&&i<a-1){
i++;
direc=3;
}
}
else if(ran<2){
if(direc!=2&&j>0){
j--;
direc=0;
}
}
else if(ran<3){
if(direc!=3&&i>0){
i--;
direc=1;
}
}
else {
if(direc!=0&&j<b-1){
j++;
direc=2;
}
}
}
/* 随机生成迷宫其余部分 */
for(i=0;i<a;i++)
for(j=0;j<b;j++)
if(bg[i][j]==1){
ran=(int)rand()*10;
if(ran<7)bg[i][j]=0;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于ASP的售后服务管理系统源码.zip (117个子文件)
inbox.asp 16KB
inbox1.asp 16KB
outbox.asp 15KB
edit_member.asp 14KB
member_list.asp 13KB
add_member.asp 12KB
member_info.asp 12KB
config_admin.asp 12KB
view_service.asp 9KB
product_type_list.asp 8KB
item_list.asp 8KB
dispose_service.asp 8KB
config.asp 8KB
product_list.asp 8KB
edit_product.asp 7KB
index.asp 7KB
count1.asp 7KB
add_product.asp 7KB
add_product1.asp 7KB
count4.asp 7KB
count3.asp 7KB
count5.asp 6KB
count2.asp 6KB
sent.asp 6KB
add_service.asp 5KB
service.asp 5KB
sent_mess1.asp 4KB
service1.asp 4KB
save_dispose_service.asp 4KB
edit_product_type.asp 3KB
save_edit_member.asp 3KB
edit_item.asp 3KB
sent_mess.asp 3KB
view_mess.asp 3KB
login.asp 3KB
sent_mess2.asp 3KB
save_mess.asp 3KB
save_edit_product_type.asp 2KB
save_add_product_type.asp 2KB
save_edit_product.asp 2KB
save_edit_item.asp 2KB
save_add_product1.asp 2KB
save_add_product.asp 2KB
save_add_item.asp 2KB
add_product_type.asp 2KB
add_item.asp 2KB
save_add_service.asp 2KB
save_mess2.asp 2KB
upfile.asp 2KB
uppicfile.asp 2KB
save_add_member.asp 2KB
SEARCH.ASP 2KB
active_member.asp 911B
uploadpic.asp 485B
upload.asp 479B
CONN.ASP 409B
COUNTS.ASP 396B
CONN1.ASP 383B
dele_product_type.asp 312B
dele_product.asp 302B
dele_item_name.asp 301B
dele_member.asp 300B
cg.asp 273B
logout.asp 114B
dispose_service.asp.bak 8KB
CONN1.ASP.bak 410B
CONN.ASP.bak 400B
1.bmp 3KB
2.bmp 3KB
STYLE.CSS 2KB
Thumbs.db 34KB
Thumbs.db 15KB
Thumbs.db 9KB
Thumbs.db 5KB
20083912442511502.exe 1.2MB
fsc_items.gif 918B
T2.GIF 857B
L2.GIF 850B
gif014.gif 339B
ITEM13.GIF 143B
ITEM12.GIF 132B
DELE.GIF 122B
EDIT.GIF 121B
I.GIF 114B
UNLOCK.GIF 100B
LOCK.GIF 99B
T.gif 64B
L.gif 61B
SPACER.GIF 43B
DEFAULT.HTM 1KB
FOOT.HTM 466B
HEAD.HTM 225B
upload.inc 5KB
desktop.ini 114B
byservice.jpg 10KB
20083917233476811.jpg 2KB
2008391731885828.jpg 2KB
byservice.ldf 2MB
byservice.mdf 2MB
20083911493427084.txt 6KB
共 117 条
- 1
- 2
资源评论
入伍击寇
- 粉丝: 129
- 资源: 4706
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功