本文目录一览:
- 1、C语言小程序推箱子问题
- 2、c语言推箱子代码
- 3、js编写推箱子的时候黄色箱子到目的地时变成红色怎样弄?(黄,红箱子是图片)
- 4、推箱子游戏java代码如何判断下一步是墙还是空地
- 5、谁有推箱子的C语言游戏代码
- 6、js 写推箱子用了多少内部函数
C语言小程序推箱子问题
你好,这是推箱子的全部代码
/*图形模式下的推箱子*/
#define MAX 2 /*游戏中总关数*/
#define STARTX 180
#define STARTY 80
#define BKCOLOR BLACK
#define MANCOLOR RED
#define OBJECTCOLOR YELLOW
#define TIMEINT 2
#define STARNUM 300
#define STEPMAX 5
#includestring.h
#includebios.h
#includestdio.h
#includedos.h
#includegraphics.h
#includeconio.h
#includetime.h
#includestdlib.h
#includestdio.h
#define Key_R 0x1372
#define Key_Up 0x4800
#define Key_Enter 0x1c0d
#define Key_Down 0x5000
#define Key_P 0x1970
#define Key_Esc 0x11b
#define Key_Right 0x4d00
#define Key_Left 0x4b00
typedef struct star
{
int x;
int y;
int c;
}Star;
Star s[STARNUM];
typedef struct record
{
char name[20];
int second;
struct time t;
struct date d;
}Record;
Record r[MAX];
typedef struct c
{
int x;
int y;
}Add;
typedef struct a
{
int x;
int y;
}Player;
Player p;
char name[20]; /*进入游戏时记录玩家的姓名*/
time_t t1,t2; /*游戏结束时的时间*/
int Ide; /*开始进入游戏时,选择菜单时返的功能号(1,2,3)*/
int MissionNum; /*玩家正在玩的关数*/
int BoxNum; /*目的地的个数*/
int Key; /*玩家按键*/
int map[10][10]; /*地图.(空=0),(人=1),(箱子=2),(墙=3),(目的地=4),(人+目的地=5),(箱子+目的地=6)*/
int StepNum;
int DirectionKey; /*用来表示方向键最后一次按键*/
int BoxMove[STEPMAX];
int Step[STEPMAX];
/*函数定义*/
void InputName();
void Init();
void MainMenu();
void JudgeRecord();
void WriteRecord();
void JudgeIde();
void DrawMenu(int );
void Game();
void InitMission(int );
void NextMission();
void InitPic(int ,int ,int );
int Move(Add );
void DrawWall(int ,int );
void DrawBack(int ,int );
void DrawBox(int ,int );
void DrawObject(int ,int );
void DrawMan(int ,int );
void DrawStar();
int JudgeWin();
void InitMission1();
void InitMission2();
int TimeCome();
void ChangeStar();
void InputName();
void ViewRecords();
void DeleteRecords();
void RegisterStep();
void ReverselyMove();
void MoveBack(Add );
void main()
{
InputName();
Init(); /*驱动显卡*/
srand(time(t1));
MainMenu(); /*画开始菜单(1.start game 2.view records 3.delete records 4.exit game)*/
}
void InputName()
{
char b;
char c;
do
{
printf("\n\nPlease input your password:");
scanf("%s",b);
}while(b!='l');
clrscr();
do
{
printf("\n\nPlease input your name:");
scanf("%s",name);
printf("Are you sure the name right(Y/N):");
do
{
c=getch();
}while(c!='Y'c!='y'c!='N'c!='n');
}while(c!='Y'c!='y');
}
void Init()
{
int gd=DETECT,gm;
initgraph(gd,gm,"d:\\tc");
}
void MainMenu()
{
setbkcolor(BKCOLOR);
cleardevice();
DrawStar(); /*画开始菜单的背景画面*/
DrawStar();
Ide=0,Key=0;
DrawMenu(Ide);
do
{
if(bioskey(1)) /*有键按下则处理按键*/
{
Key=bioskey(0);
switch(Key)
{
case Key_Down: {Ide++;Ide=Ide%4;DrawMenu(Ide);break;}
case Key_Up: {Ide--;Ide=(Ide+4)%4;DrawMenu(Ide);break;}
}
}
else {if(TimeCome()) ChangeStar();} /*改变背景*/
}while(Key!=Key_Enter);
JudgeIde(); /*根据Ide运行不同的程序*/
}
void JudgeIde()
{
switch(Ide)
{
case 0:Game();break;
case 1:{ViewRecords();bioskey(0);MainMenu();break;}
case 2:{DeleteRecords();ViewRecords();bioskey(0);MainMenu();break;}
case 3:exit(0);
}
}
void Game()
{
int i,j,flag;
Add ad;
MissionNum=0;
NextMission();
do
{
flag=0;
Key=bioskey(0);
switch(Key)
{
case Key_Enter:{NextMission();time(t1);break;}
case Key_Up:{ad.x=-1;ad.y=0;flag=1;DirectionKey=Key;break;}
case Key_Down:{ad.x=1;ad.y=0;flag=1;DirectionKey=Key;break;}
case Key_Left:{ad.x=0;ad.y=-1;flag=1;DirectionKey=Key;break;}
case Key_Right:{ad.x=0;ad.y=1;flag=1;DirectionKey=Key;break;}
case Key_R:{MissionNum--;NextMission();break;}
case Key_Esc:{MainMenu();break;}
case Key_P:{ReverselyMove();break;}
}
if(flag==1)
{if(Move(ad)) {RegisterStep(); if(JudgeWin()) {JudgeRecord();NextMission();}}}
}while(1);
}
void InitMission(int n)
{
int i,j;
for(i=0;i10;i++)
for(j=0;j10;j++)
map[i][j]=0;
switch(n)
{
case 1:InitMission1();break; /*第一关*/
case 2:InitMission2();break; /*第二关*/
}
}
void InitPic(int n,int i,int j)
{
switch(n)
{
case 0:DrawBack(i,j);break;
case 1:DrawMan(i,j);break;
case 2:DrawBox(i,j);break;
case 3:DrawWall(i,j);break;
case 4:DrawObject(i,j);break;
case 5:DrawMan(i,j);break;
case 6:DrawBox(i,j);break;
}
}
void NextMission()
{
int i,j;
if(MissionNum+1MAX) MissionNum=1;
else MissionNum++;
InitMission(MissionNum);
setbkcolor(BKCOLOR);
cleardevice();
for(i=0;i10;i++)
for(j=0;j10;j++)
InitPic(map[i][j],i,j);
switch(MissionNum)
{
case 1:outtextxy(200,230,"Mission 1");break;
case 2:outtextxy(200,230,"Mission 2");break;
}
time(t1);
for(i=0;iSTEPMAX;i++)
{Step[i]=BoxMove[i]=0;}
StepNum=0;
}
int Move(Add a)
{
int flag;
int i=StepNum%STEPMAX;
switch(map[p.x+a.x][p.y+a.y]) /*看下一位置为什么*/
{
case 0:{map[p.x][p.y]-=1;InitPic(map[p.x][p.y],p.x,p.y);
p.x=p.x+a.x;p.y=p.y+a.y;
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);flag=1;break;}
case 2:{if(map[p.x+2*a.x][p.y+2*a.y]==0||map[p.x+2*a.x][p.y+2*a.y]==4)
{map[p.x][p.y]-=1;map[p.x+a.x][p.y+a.y]=1;map[p.x+2*a.x][p.y+2*a.y]+=2;
InitPic(map[p.x][p.y],p.x,p.y);
InitPic(map[p.x+a.x][p.y+a.y],p.x+a.x,p.y+a.y);
InitPic(map[p.x+2*a.x][p.y+2*a.y],p.x+2*a.x,p.y+2*a.y);
p.x=p.x+a.x;p.y=p.y+a.y;flag=1;BoxMove[i]=1;}
else flag=0;
break;}
case 3:flag=0;break;
case 4:{map[p.x][p.y]-=1;InitPic(map[p.x][p.y],p.x,p.y);
p.x=p.x+a.x;p.y=p.y+a.y;
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);flag=1;break;}
case 6:{if(map[p.x+2*a.x][p.y+2*a.y]==0||map[p.x+2*a.x][p.y+2*a.y]==4)
{map[p.x][p.y]-=1;map[p.x+a.x][p.y+a.y]=5;map[p.x+2*a.x][p.y+2*a.y]+=2;
InitPic(map[p.x][p.y],p.x,p.y);
InitPic(map[p.x+a.x][p.y+a.y],p.x+a.x,p.y+a.y);
InitPic(map[p.x+2*a.x][p.y+2*a.y],p.x+2*a.x,p.y+2*a.y);
p.x=p.x+a.x;p.y=p.y+a.y;flag=1;BoxMove[i]=1;}
else flag=0;
break;}
}
return flag;
}
void DrawWall(int i,int j)
{
DrawBack(i,j);
setfillstyle(9,1);
bar(STARTX+20*j-9,STARTY+20*i-9,STARTX+20*j+9,STARTY+20*i+9);
}
void DrawMan(int i,int j)
{
DrawBack(i,j);
setcolor(MANCOLOR);
circle(STARTX+20*j,STARTY+20*i,9);
arc(STARTX+20*j-3,STARTY+20*i-2,20,160,3);
arc(STARTX+20*j+4,STARTY+20*i-2,20,160,3);
arc(STARTX+20*j,STARTY+20*i-2,220,320,7);
}
void DrawBack(int i,int j)
{
setfillstyle(1,BKCOLOR);
bar(STARTX+20*j-9,STARTY+20*i-9,STARTX+20*j+9,STARTY+20*i+9);
}
void DrawObject(int i,int j)
{
DrawBack(i,j);
setcolor(OBJECTCOLOR);
line(STARTX+20*j-9,STARTY+20*i,STARTX+20*j+9,STARTY+20*i);
line(STARTX+20*j-9,STARTY+20*i-9,STARTX+20*j+9,STARTY+20*i+9);
line(STARTX+20*j-9,STARTY+20*i+9,STARTX+20*j+9,STARTY+20*i-9);
}
void DrawBox(int i,int j)
{
DrawBack(i,j);
setfillstyle(9,3);
bar(STARTX+20*j-9,STARTY+20*i-9,STARTX+20*j+9,STARTY+20*i+9);
}
void DrawMenu(int j)
{
int n;
char *s[4]={"1.Start Game","2.View Records","3.Delete Records","4.Exit Game"};
settextstyle(0,0,1);
setcolor(GREEN);
for(n=0;n4;n++)
outtextxy(250,170+n*20,s[n]);
setcolor(RED);
outtextxy(250,170+j*20,s[j]);
}
void DrawStar()
{
int w,h,i,dotx,doty,color,maxcolor;
w=getmaxx();
h=getmaxy();
maxcolor=getmaxcolor();
for(i=0;iSTARNUM;i++)
{
s[i].x=1+random(w-1);
s[i].y=1+random(h-1);
s[i].c=random(maxcolor);
putpixel(s[i].x,s[i].y,s[i].c);
}
}
void ChangeStar()
{
int i,maxcolor;
maxcolor=getmaxcolor();
for(i=0;iSTARNUM;i++)
{
s[i].c=random(maxcolor);
putpixel(s[i].x,s[i].y,s[i].c);
}
}
int TimeCome()
{
static long tm, old;
tm=biostime(0,tm);
if(tm-oldTIMEINT) return 0;
else
{
old=tm; return 1;
}
}
int JudgeWin()
{
int n=0,i,j;
for(i=0;i10;i++)
for(j=0;j10;j++)
if(map[i][j]==6) n++;
if(n==BoxNum) return 1;
else return 0;
}
void InitMission1() /*第九关*/
{
int i,j;
for(i=0;i10;i++)
for(j=0;j10;j++)
map[i][j]=0;
for(i=0;i=5;i++)
map[0][i]=3;
for(i=5;i=7;i++)
{map[2][i]=map[i-1][1]=3;}
for(i=1;i=4;i++)
{map[6][i]=map[5][i+3]=map[i][0]=3;}
map[3][7]=map[4][7]=map[1][5]=3;
for(i=2;i=4;i++)
map[2][i]=2;
map[3][4]=map[4][5]=2;
for(i=2;i=3;i++)
{map[3][i]=map[4][i]=4;}
map[4][4]=4;
p.x=3;p.y=5;
map[3][5]=1;
BoxNum=5;
}
void InitMission2()
{
int i,j;
for(i=0;i10;i++)
for(j=0;j10;j++)
map[i][j]=0;
for(i=1;i=5;i++)
{map[0][i]=map[6][i]=3;}
for(i=2;i=4;i++)
{map[1][i+3]=map[i][7]=map[i+2][5]=map[i][0]=3;}
map[1][1]=map[2][1]=map[5][0]=map[5][5]=map[4][6]=map[6][0]=3;
map[2][4]=map[3][3]=map[4][2]=map[4][3]=2;
map[2][3]=map[3][2]=map[3][4]=map[4][4]=4;
p.x=1;p.y=3;
map[1][3]=1;
BoxNum=4;
}
void ViewRecords()
{
FILE *fp;
int i;
setbkcolor(BKCOLOR);
cleardevice();
if((fp=fopen("record","r"))==NULL)
{
printf("\nerror on open file!");
getch();
exit(1);
}
gotoxy(1,1);
printf("\n\t\t\tRecord Information\n");
printf("Record-holder Achievement(s)\t Time(h:m:s)\t\tDate(y/m/d)");
for(i=0;iMAX;i++)
{fseek(fp,i*sizeof(Record),0);
fread(r[i],sizeof(Record),1,fp);
printf("\n%-10s\t%d\t\t %02d:%02d:%02d\t\t%02d/%02d/%02d",r[i].name,r[i].second,r[i].t.ti_hour,r[i].t.ti_min,r[i].t.ti_sec,r[i].d.da_year,r[i].d.da_mon,r[i].d.da_day);}
fclose(fp);
gotoxy(10,25);
printf("Press any key to return mainmenu...");
}
void DeleteRecords()
{
int i;
FILE *fp;
fp=fopen("record","w");
for(i=0;iMAX;i++)
{
strcpy(r[i].name,"nameless");
r[i].second=0;
gettime(r[i].t);
getdate(r[i].d);
}
for(i=0;iMAX;i++)
fwrite(r[i],sizeof(Record),1,fp);
fclose(fp);
}
void JudgeRecord()
{
int i=MissionNum-1;
time(t2);
if(r[i].second==0||difftime(t2,t1)r[i].second)
{
gotoxy(10,3);printf("\t\tYou have broken the record");
r[i].second=difftime(t2,t1);
strcpy(r[i].name,name);
gettime(r[i].t);
getdate(r[i].d);
WriteRecord();
}
else
{gotoxy(10,3);printf("\t\tYou have pass this mission");}
gotoxy(10,4);
printf("\t\tpress any key continue...");
getch();
getch();
}
void WriteRecord()
{
FILE *fp;
int i=MissionNum-1;
fp=fopen("record","rt+");
fseek(fp,i*sizeof(Record),0);
fwrite(r[MissionNum-1],sizeof(Record),1,fp);
fclose(fp);
}
void RegisterStep()
{
int i;
StepNum++;
i=(StepNum-1)%STEPMAX;
Step[i]=DirectionKey;
}
void ReverselyMove()
{
int i;
Add ad;
i=(StepNum-1)%STEPMAX;
if(Step[i]==0) return;
else
{
switch(Step[i])
{
case Key_Up:{ad.x=1;ad.y=0;MoveBack(ad);break;}
case Key_Down:{ad.x=-1;ad.y=0;MoveBack(ad);break;}
case Key_Left:{ad.x=0;ad.y=1;MoveBack(ad);break;}
case Key_Right:{ad.x=0;ad.y=-1;MoveBack(ad);break;}
}
StepNum--;Step[i]=0;BoxMove[i]=0;
}
}
void MoveBack(Add a) /*一定可以移动*/
{
int i=(StepNum-1)%STEPMAX;
if(BoxMove[i]==0)
{
map[p.x][p.y]-=1;InitPic(map[p.x][p.y],p.x,p.y);
p.x=p.x+a.x;p.y=p.y+a.y;
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);
}
else if(BoxMove[i]==1)
{
map[p.x-a.x][p.y-a.y]-=2;InitPic(map[p.x-a.x][p.y-a.y],p.x-a.x,p.y-a.y);
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);
p.x=p.x+a.x;p.y=p.y+a.y;
map[p.x][p.y]+=1;InitPic(map[p.x][p.y],p.x,p.y);
}
}
c语言推箱子代码
Here you are!
编译通过。
/* 推箱子游戏 */
#include dos.h
#include stdio.h
#include stdlib.h
#include ctype.h
#include conio.h
#include bios.h
#include alloc.h
/* 定义二维数组ghouse来记录屏幕上各点的状态,
其中:0表示什么都没有,'b'表示箱子,'w'表示墙壁,'m'表示目的地,'i'表示箱子在目的地。 */
char ghouse[20][20];
/* 以下函数为直接写屏函数,很酷的函数哦!是我朋友告诉我的。 */
char far *screen=(char far* )0xb8000000;
void putchxy(int y,int x,char ch,char fc,char bc)
{
screen[(x*160)+(y1)+0]=ch;
screen[(x*160)+(y1)+1]=(bc*16)+fc;
}
/* 定义判断是否胜利的数据结构 */
typedef struct winer {
int x,y;
struct winer *p;
}winer;
/* 箱子位置的数据结构 */
typedef struct boxs {
int x,y;
struct boxs *next;
}boxs;
/* 在特定的坐标上画墙壁并用数组记录状态的函数 */
void printwall(int x,int y)
{
putchxy(y-1,x-1,219,MAGENTA,BLACK);
ghouse[x][y]='w';
}
/* 在特定的坐标上画箱子并用数组记录状态的函数 */
void printbox(int x,int y)
{
putchxy(y-1,x-1,10,WHITE,BLACK);
ghouse[x][y]='b';
}
/* 在特定的坐标上画目的地并用数组记录状态的函数 */
void printwhither1(int x,int y,winer **win,winer **pw)
{
winer *qw;
putchxy(y-1,x-1,'*',YELLOW,BLACK);
ghouse[x][y]='m';
if(*win==NULL)
{
*win=*pw=qw=(winer* )malloc(sizeof(winer));
(*pw)-x=x;(*pw)-y=y;(*pw)-p=NULL;
}
else
{
qw=(winer* )malloc(sizeof(winer));
qw-x=x;qw-y=y;(*pw)-p=qw;(*pw)=qw;qw-p=NULL;
}
}
/* 在特定的坐标上画目的地并用数组记录状态的函数 */
void printwhither(int x,int y)
{
putchxy(y-1,x-1,'*',YELLOW,BLACK);
ghouse[x][y]='m';
}
/* 在特定的坐标上画人的函数 */
void printman(int x,int y)
{
gotoxy(y,x);
_AL=02;_CX=01;_AH=0xa;
geninterrupt(0x10);
}
/* 在特定的坐标上画箱子在目的地上并用数组记录状态的函数 */
void printboxin(int x,int y)
{
putchxy(y-1,x-1,10,YELLOW,BLACK);
ghouse[x][y]='i';
}
/* 初始化函数,初始化数组和屏幕 */
void init()
{
int i,j;
system("cls");
for(i=0;i20;i++)
for(j=0;j20;j++)
ghouse[i][j]=0;
_AL=3;
_AH=0;
geninterrupt(0x10);
gotoxy(40,4);
printf("Welcome to push box world!");
gotoxy(40,6);
printf("Press up,down,left,right to play.");
gotoxy(40,8);
printf("Press Esc to quit it.");
gotoxy(40,10);
printf("Press space to reset the game.");
gotoxy(40,12);
printf("April 30th 2004.");
}
/* 第一关的图象初始化 */
winer *inithouse1()
{
int x,y;
winer *win=NULL,*pw;
gotoxy(8,2);
printf("Level No.1");
for(x=1,y=5;y=9;y++)
printwall(x+4,y+10);
for(y=5,x=2;x=5;x++)
printwall(x+4,y+10);
for(y=9,x=2;x=5;x++)
printwall(x+4,y+10);
for(y=1,x=3;x=8;x++)
printwall(x+4,y+10);
for(x=3,y=3;x=5;x++)
printwall(x+4,y+10);
for(x=5,y=8;x=9;x++)
printwall(x+4,y+10);
for(x=7,y=4;x=9;x++)
printwall(x+4,y+10);
for(x=9,y=5;y=7;y++)
printwall(x+4,y+10);
for(x=8,y=2;y=3;y++)
printwall(x+4,y+10);
printwall(5+4,4+10);
printwall(5+4,7+10);
printwall(3+4,2+10);
printbox(3+4,6+10);
printbox(3+4,7+10);
printbox(4+4,7+10);
printwhither1(4+4,2+10,win,pw);
printwhither1(5+4,2+10,win,pw);
printwhither1(6+4,2+10,win,pw);
printman(2+4,8+10);
return win;
}
/* 第三关的图象初始化 */
winer *inithouse3()
{int x,y;
winer *win=NULL,*pw;
gotoxy(8,3);
printf("Level No.3");
for(x=1,y=2;y=8;y++)
printwall(x+4,y+10);
for(x=2,y=2;x=4;x++)
printwall(x+4,y+10);
for(x=4,y=1;y=3;y++)
printwall(x+4,y+10);
for(x=5,y=1;x=8;x++)
printwall(x+4,y+10);
for(x=8,y=2;y=5;y++)
printwall(x+4,y+10);
for(x=5,y=5;x=7;x++)
printwall(x+4,y+10);
for(x=7,y=6;y=9;y++)
printwall(x+4,y+10);
for(x=3,y=9;x=6;x++)
printwall(x+4,y+10);
for(x=3,y=6;y=8;y++)
printwall(x+4,y+10);
printwall(2+4,8+10);
printwall(5+4,7+10);
printbox(6+4,3+10);
printbox(4+4,4+10);
printbox(5+4,6+10);
printwhither1(2+4,5+10,win,pw);
printwhither1(2+4,6+10,win,pw);
printwhither1(2+4,7+10,win,pw);
printman(2+4,4+10);
return win;
}
/* 第二关的图象初始化 */
winer *inithouse2()
{int x,y;
winer *win=NULL,*pw;
gotoxy(8,2);
printf("Level No.2");
for(x=1,y=4;y=7;y++)
printwall(x+4,y+10);
for(x=2,y=2;y=4;y++)
printwall(x+4,y+10);
for(x=2,y=7;x=4;x++)
printwall(x+4,y+10);
for(x=4,y=1;x=8;x++)
printwall(x+4,y+10);
for(x=8,y=2;y=8;y++)
printwall(x+4,y+10);
for(x=4,y=8;x=8;x++)
printwall(x+4,y+10);
for(x=4,y=6;x=5;x++)
printwall(x+4,y+10);
for(x=3,y=2;x=4;x++)
printwall(x+4,y+10);
for(x=4,y=4;x=5;x++)
printwall(x+4,y+10);
printwall(6+4,3+10);
printbox(3+4,5+10);
printbox(6+4,6+10);
printbox(7+4,3+10);
printwhither1(5+4,7+10,win,pw);
printwhither1(6+4,7+10,win,pw);
printwhither1(7+4,7+10,win,pw);
printman(2+4,6+10);
return win;
}
/* 第四关的图象初始化 */
winer *inithouse4()
{int x,y;
winer *win=NULL,*pw;
gotoxy(8,2);
printf("Level No.4");
for(x=1,y=1;y=6;y++)
printwall(x+4,y+10);
for(x=2,y=7;y=8;y++)
printwall(x+4,y+10);
for(x=2,y=1;x=7;x++)
printwall(x+4,y+10);
for(x=7,y=2;y=4;y++)
printwall(x+4,y+10);
for(x=6,y=4;y=9;y++)
printwall(x+4,y+10);
for(x=3,y=9;x=5;x++)
printwall(x+4,y+10);
for(x=3,y=3;y=4;y++)
printwall(x+4,y+10);
printwall(3+4,8+10);
printbox(3+4,5+10);
printbox(4+4,4+10);
printbox(4+4,6+10);
printbox(5+4,5+10);
printbox(5+4,3+10);
printwhither1(3+4,7+10,win,pw);
printwhither1(4+4,7+10,win,pw);
printwhither1(5+4,7+10,win,pw);
printwhither1(4+4,8+10,win,pw);
printwhither1(5+4,8+10,win,pw);
printman(2+4,2+10);
return win;
}
/* 移动在空地上的箱子到空地上 */
movebox(int x,int y,char a)
{switch(a)
{
case 'u':ghouse[x-1][y]=0;printf(" ");
printbox(x-2,y);printman(x-1,y);
ghouse[x-2][y]='b';break;
case 'd':ghouse[x+1][y]=0;printf(" ");
printbox(x+2,y);printman(x+1,y);
ghouse[x+2][y]='b';break;
case 'l':ghouse[x][y-1]=0;printf(" ");
printbox(x,y-2);printman(x,y-1);
ghouse[x][y-2]='b';break;
case 'r':ghouse[x][y+1]=0;printf(" ");
printbox(x,y+2);printman(x,y+1);
ghouse[x][y+2]='b';break;
default: break;
}
}
/* 移动在目的地上的箱子到空地上 */
moveinbox(int x,int y,char a)
{switch(a)
{
case 'u':ghouse[x-1][y]='m';printf(" ");
printbox(x-2,y);printman(x-1,y);
ghouse[x-2][y]='b';break;
case 'd':ghouse[x+1][y]='m';printf(" ");
printbox(x+2,y);printman(x+1,y);
ghouse[x+2][y]='b';break;
case 'l':ghouse[x][y-1]='m';printf(" ");
printbox(x,y-2);printman(x,y-1);
ghouse[x][y-2]='b';break;
case 'r':ghouse[x][y+1]='m';printf(" ");
printbox(x,y+2);printman(x,y+1);
ghouse[x][y+2]='b';break;
default: break;
}
}
/* 移动在空地上的箱子到目的地上 */
moveboxin(int x,int y,char a)
{switch(a)
{
case 'u':ghouse[x-1][y]=0;printf(" ");
printboxin(x-2,y);printman(x-1,y);
ghouse[x-2][y]='i';break;
case 'd':ghouse[x+1][y]=0;printf(" ");
printboxin(x+2,y);printman(x+1,y);
ghouse[x+2][y]='i';break;
case 'l':ghouse[x][y-1]=0;printf(" ");
printboxin(x,y-2);printman(x,y-1);
ghouse[x][y-2]='i';break;
case 'r':ghouse[x][y+1]=0;printf(" ");
printboxin(x,y+2);printman(x,y+1);
ghouse[x][y+2]='i';break;
default: break;
}
}
/* 移动在目的地上的箱子到目的地 */
moveinboxin(int x,int y,char a)
{switch(a)
{
case 'u':ghouse[x-1][y]='m';printf(" ");
printboxin(x-2,y);printman(x-1,y);
ghouse[x-2][y]='i';break;
case 'd':ghouse[x+1][y]='m';printf(" ");
printboxin(x+2,y);printman(x+1,y);
ghouse[x+2][y]='i';break;
case 'l':ghouse[x][y-1]='m';printf(" ");
printboxin(x,y-2);printman(x,y-1);
ghouse[x][y-2]='i';break;
case 'r':ghouse[x][y+1]='m';printf(" ");
printboxin(x,y+2);printman(x,y+1);
ghouse[x][y+2]='i';break;
default: break;
}
}
/* 判断特定的坐标上的状态 */
int judge(int x,int y)
{int i;
switch(ghouse[x][y])
{
case 0: i=1;break;
case 'w': i=0;break;
case 'b': i=2;break;
case 'i': i=4;break;
case 'm': i=3;break;
default: break;
}
return i;
}
/* 处理按下键盘后,人物移动的主函数 */
move(int x,int y,char a)
{switch(a)
{
case 'u':if(!judge(x-1,y)) {gotoxy(y,x);break;}
else if(judge(x-1,y)==1||judge(x-1,y)==3)
{if(judge(x,y)==3)
{ printwhither(x,y);printman(x-1,y);break;}
else
{printf(" ");printman(x-1,y);break;}
}
else if(judge(x-1,y)==2)
{ if(judge(x-2,y)==1)
{movebox(x,y,'u');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y,x-1);
}
else if(judge(x-2,y)==3)
{ moveboxin(x,y,'u');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y,x-1);
}
else gotoxy(y,x);
break;
}
else if(judge(x-1,y)==4)
{ if(judge(x-2,y)==1)
{moveinbox(x,y,'u');
if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x-1);
}
else if(judge(x-2,y)==3)
{ moveinboxin(x,y,'u');
if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x-1);
}
else gotoxy(y,x);
break;
}
case 'd':if(!judge(x+1,y)) {gotoxy(y,x);break;}
else if(judge(x+1,y)==1||judge(x+1,y)==3)
{if(judge(x,y)==3)
{ printwhither(x,y);printman(x+1,y);break;}
else
{printf(" ");printman(x+1,y);break;}
}
else if(judge(x+1,y)==2)
{ if(judge(x+2,y)==1)
{movebox(x,y,'d');
if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x+1);
}
else if(judge(x+2,y)==3)
{moveboxin(x,y,'d');
if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x+1);
}
else gotoxy(y,x);
break;
}
else if(judge(x+1,y)==4)
{ if(judge(x+2,y)==1)
{moveinbox(x,y,'d');
if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x+1);
}
else if(judge(x+2,y)==3)
{moveinboxin(x,y,'d');
if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x+1);
}
else gotoxy(y,x);
break;
}
case 'l':if(!judge(x,y-1)) {gotoxy(y,x);break;}
else if(judge(x,y-1)==1||judge(x,y-1)==3)
{if(judge(x,y)==3)
{ printwhither(x,y);printman(x,y-1);break;}
else
{printf(" ");printman(x,y-1);break;}
}
else if(judge(x,y-1)==2)
{ if(judge(x,y-2)==1)
{movebox(x,y,'l');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y-1,x);
}
else if(judge(x,y-2)==3)
{moveboxin(x,y,'l');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y-1,x);
}
else gotoxy(y,x);
break;
}
else if(judge(x,y-1)==4)
{ if(judge(x,y-2)==1)
{moveinbox(x,y,'l');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y-1,x);
}
else if(judge(x,y-2)==3)
{moveinboxin(x,y,'l');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y-1,x);
}
else gotoxy(y,x);
break;
}
case 'r':if(!judge(x,y+1)) {gotoxy(y,x);break;}
else if(judge(x,y+1)==1||judge(x,y+1)==3)
{if(judge(x,y)==3)
{printwhither(x,y);printman(x,y+1);break;}
else
{printf(" ");printman(x,y+1);break;}
}
else if(judge(x,y+1)==2)
{ if(judge(x,y+2)==1)
{movebox(x,y,'r');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y+1,x);
}
else if(judge(x,y+2)==3)
{moveboxin(x,y,'r');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y+1,x);
}
else gotoxy(y,x);
break;
}
else if(judge(x,y+1)==4)
{ if(judge(x,y+2)==1)
{moveinbox(x,y,'r');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y+1,x);
}
else if(judge(x,y+2)==3)
{moveinboxin(x,y,'r');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y+1,x);
}
else gotoxy(y,x);
break;
}
default: break;
}
}
/* 按下空格键后,回到本关开头的函数 */
void reset(int i)
{switch(i)
{
case 0: init();
inithouse1();break;
case 1: init();
inithouse2();break;
case 2: init();
inithouse3();break;
case 3: init();
inithouse4();break;
default:break;
}
}
/* 主函数main */
main()
{int key,x,y,s,i=0;
winer *win,*pw;
_AL=3;_AH=0;
geninterrupt(0x10);
init();
win=inithouse1();
do{
_AH=3;
geninterrupt(0x10);
x=_DH+1;y=_DL+1;
while(bioskey(1)==0);
key=bioskey(0);
switch(key)
{
case 0x4800:move(x,y,'u');break; /* 按下向上键后 */
case 0x5000:move(x,y,'d');break; /* 按下向下键后 */
case 0x4b00:move(x,y,'l');break; /* 按下向左键后 */
case 0x4d00:move(x,y,'r');break; /* 按下向右键后 */
case 0x3920:reset(i);break; /* 按下空格键后 */
default:break;
}
s=0;
pw=win;
while(pw)
{
if(ghouse[pw-x][pw-y]=='m') s++;
pw=pw-p;
}
if(s==0)
{
free(win);
gotoxy(25,2);
printf("Congratulate! You have passed Level %d!",i+1);
getch();
i++;
switch(i)
{
case 1: init();
win=inithouse2();break;
case 2: init();
win=inithouse3();break;
case 3: init();
win=inithouse4();break;
case 4: gotoxy(8,14);
printf("Great! You have passed all the levels! Press any key to quit!");
key=0x011b;getch();break;
default: break;
}
}
}while(key!=0x011b);
_AL=3;
_AH=0;
geninterrupt(0x10);
}
js编写推箱子的时候黄色箱子到目的地时变成红色怎样弄?(黄,红箱子是图片)
给你个思路,就是每推格子(横x±1,竖y±1),然后每次你判断一下,是否达到你要的位置(x,y),达到的话,你就让它的图片替换成红色的就行啦
推箱子游戏java代码如何判断下一步是墙还是空地
1. 确定的功能:让玩家通过按上下左右键推箱子,当箱子们都推到了目的地后出现过关信息,并显示下一关。推错了玩家还按空格键从新玩过这关。直到过完全部关卡。
2. 定义的核心数据结构:我们定义一个二维数组ghouse来记录屏幕上各点的状态。char ghouse[20][20]; 其中:0表示什么都没有,'b'表示箱子,'w'表示墙壁,'m'表示目的地,'i'表示箱子在目的地。
3. 对整个进行功能模块划分。
(1)。初始化:在屏幕上输出欢迎信息,把ghouse数组的元素初始化为0。并根据各关的要求在屏幕上输出墙、箱子、目的地和人。并用ghouse 数组记录各点的状态。
(2)。进入游戏循环:这个游戏主循环是等待按键。当接受到上下左右键时执行相关操作:接受ESC键时退出游戏;接受空格键时返回本关开头;接受无效按键时做忽略处理。重点介绍按上下左右键时如何执行相关操作。
(3)。判断是否过关:用一个链表win由每关的初始化函数传给main函数。Win链表主要记录屏幕上的哪些点是目的地,并记录目的地的位置。Main函数每执行一次操作后就判断屏幕上的目的地是不是都有箱子了。
谁有推箱子的C语言游戏代码
#include dos.h
#include stdio.h
#include ctype.h
#include conio.h
#include bios.h
#include alloc.h
/* 定义二维数组ghouse来记录屏幕上各点的状态,
其中:0表示什么都没有,'b'表示箱子,'w'表示墙壁,'m'表示目的地,'i'表示箱子在目的地。 */
char ghouse[20][20];/* 以下函数为直接写屏函数,很酷的函数哦!是我朋友告诉我的。 */
char far *screen=(char far* )0xb8000000;
void putchxy(int y,int x,char ch,char fc,char bc)
{
screen[(x*160)+(y1)+0]=ch;
screen[(x*160)+(y1)+1]=(bc*16)+fc;
}/* 定义判断是否胜利的数据结构 */
typedef struct winer {
int x,y;
struct winer *p;
}winer;/* 箱子位置的数据结构 */
typedef struct boxs {
int x,y;
struct boxs *next;
}boxs;/* 在特定的坐标上画墙壁并用数组记录状态的函数 */
void printwall(int x,int y)
{
putchxy(y-1,x-1,219,MAGENTA,BLACK);
ghouse[x][y]='w';
}/* 在特定的坐标上画箱子并用数组记录状态的函数 */
void printbox(int x,int y)
{
putchxy(y-1,x-1,10,WHITE,BLACK);
ghouse[x][y]='b';
}/* 在特定的坐标上画目的地并用数组记录状态的函数 */
void printwhither1(int x,int y,winer **win,winer **pw)
{
winer *qw;
putchxy(y-1,x-1,'*',YELLOW,BLACK);
ghouse[x][y]='m';
if(*win==NULL)
{
*win=*pw=qw=(winer* )malloc(sizeof(winer));
(*pw)-x=x;(*pw)-y=y;(*pw)-p=NULL;
}
else
{
qw=(winer* )malloc(sizeof(winer));
qw-x=x;qw-y=y;(*pw)-p=qw;(*pw)=qw;qw-p=NULL;
}
}
/* 在特定的坐标上画目的地并用数组记录状态的函数 */
void printwhither(int x,int y)
{
putchxy(y-1,x-1,'*',YELLOW,BLACK);
ghouse[x][y]='m';
}
/* 在特定的坐标上画人的函数 */
void printman(int x,int y)
{
gotoxy(y,x);
_AL=02;_CX=01;_AH=0xa;
geninterrupt(0x10);
}/* 在特定的坐标上画箱子在目的地上并用数组记录状态的函数 */
void printboxin(int x,int y)
{
putchxy(y-1,x-1,10,YELLOW,BLACK);
ghouse[x][y]='i';
}/* 初始化函数,初始化数组和屏幕 */
void init()
{
int i,j;
clrscr();
for(i=0;i20;i++)
for(j=0;j20;j++)
ghouse[i][j]=0;
_AL=3;
_AH=0;
geninterrupt(0x10);
gotoxy(40,4);
printf("Welcome to push box world!");
gotoxy(40,6);
printf("Press up,down,left,right to play.");
gotoxy(40,8);
printf("Press Esc to quit it.");
gotoxy(40,10);
printf("Press space to reset the game.");
gotoxy(40,12);
printf("April 30th 2004.");
}/* 第一关的图象初始化 */
winer *inithouse1()
{
int x,y;
winer *win=NULL,*pw;
gotoxy(8,2);
printf("Level No.1");
for(x=1,y=5;y=9;y++)
printwall(x+4,y+10);
for(y=5,x=2;x=5;x++)
printwall(x+4,y+10);
for(y=9,x=2;x=5;x++)
printwall(x+4,y+10);
for(y=1,x=3;x=8;x++)
printwall(x+4,y+10);
for(x=3,y=3;x=5;x++)
printwall(x+4,y+10);
for(x=5,y=8;x=9;x++)
printwall(x+4,y+10);
for(x=7,y=4;x=9;x++)
printwall(x+4,y+10);
for(x=9,y=5;y=7;y++)
printwall(x+4,y+10);
for(x=8,y=2;y=3;y++)
printwall(x+4,y+10);
printwall(5+4,4+10);
printwall(5+4,7+10);
printwall(3+4,2+10);
printbox(3+4,6+10);
printbox(3+4,7+10);
printbox(4+4,7+10);
printwhither1(4+4,2+10,win,pw);
printwhither1(5+4,2+10,win,pw);
printwhither1(6+4,2+10,win,pw);
printman(2+4,8+10);
return win;
}
js 写推箱子用了多少内部函数
函数直接写在页面相当于是本页调用,所以理所应当可以调用
2.js单独文件不能调用是因为你没有引用js文件,如果引用了的话,也是可以调用的。
引用方式,可以直接拖拽(我一般都是拖拽,因为路径准确),也可以自己写,引用脚本:script src="Scripts/JScript.js" type="text/javascript"/script
3.还有就是,引用的js文件一定要在调用之前,否则就找不到函数了,会报错或undefined
例如:
调用页面:
script src="Scripts/JScript.js" type="text/javascript"/script
script type="text/javascript"
aa();
/script
Script.js中代码:
function aa() {
alert("js文件掉用");
}