资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

推箱子地图java源代码 推箱子Java代码

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);

}

用JAVA编写一个小程序 比如拼图游戏 推箱子 猜数字 。。。。。。请附详细注释 因为要发表 谢谢大家!

我写的一个猜数字游戏,希望对你有用,代码如下:

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Random;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class GuessNumber {

static int trys, A, B;

static String r, t;

public static int[] MakeGuessNumber(){//随机生成一个无重复数字的四位数

Random r = new Random();

int guess[] = new int[4];

for(int i=0; i4; i++){

guess[i] = r.nextInt(10);

for(int j=i-1; j=0; j--){

if(guess[i]==guess[j]){

i--;

break;}

}

}

return guess;

}

public static String getRundom(){//将此四位数转化为字符串

int guess[]=MakeGuessNumber();

return ""+guess[0]+guess[1]+guess[2]+guess[3];

}

public static void messageDialog(Object o){

JOptionPane.showMessageDialog(null, o);

}

public static void guessNumber(){//主要算法实现部分

r=getRundom();

//System.out.println(r);

JFrame jf=new JFrame();

JButton b1=new JButton("新游戏");

JLabel l1=new JLabel("输入:");

final JTextField jtf=new JTextField(10);

JButton b2=new JButton("提交");

final JTextArea jta=new JTextArea(10,10);

jta.append(" "+"Guess"+" "+"Result"+"\n");

JScrollPane scrollPane=new JScrollPane(jta);

JPanel jp1=new JPanel();

jp1.add(l1);

jp1.add(jtf);

jp1.add(b2);

jf.add(b1,BorderLayout.NORTH);

jf.add(jp1,BorderLayout.CENTER);

jf.add(scrollPane,BorderLayout.SOUTH);

b1.addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

trys=0;

A=0;

B=0;

jta.setText(" "+"Guess"+" "+"Result"+"\n");

jtf.setText("");

r=getRundom();

//System.out.println(r);

}

});

b2.addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

t=jtf.getText();

A=0;

B=0;

if(t.length()!=4||t.substring(0, 1).equals(t.substring(1, 2))

||t.substring(0, 1).equals(t.substring(2, 3))

||t.substring(0, 1).equals(t.substring(3, 4))

||t.substring(1, 2).equals(t.substring(2, 3))

||t.substring(1, 2).equals(t.substring(3, 4))

||t.substring(2, 3).equals(t.substring(3, 4))

||!t.matches("[0-9]*"))

messageDialog("Wrong Input!");

else{

jtf.setText("");

trys++;

if(t.substring(0, 1).equals(r.substring(0, 1)))

A++;

if(t.substring(0, 1).equals(r.substring(1, 2)))

B++;

if(t.substring(0, 1).equals(r.substring(2, 3)))

B++;

if(t.substring(0, 1).equals(r.substring(3, 4)))

B++;

if(t.substring(1, 2).equals(r.substring(1, 2)))

A++;

if(t.substring(1, 2).equals(r.substring(0, 1)))

B++;

if(t.substring(1, 2).equals(r.substring(2, 3)))

B++;

if(t.substring(1, 2).equals(r.substring(3, 4)))

B++;

if(t.substring(2, 3).equals(r.substring(2, 3)))

A++;

if(t.substring(2, 3).equals(r.substring(0, 1)))

B++;

if(t.substring(2, 3).equals(r.substring(1, 2)))

B++;

if(t.substring(2, 3).equals(r.substring(3, 4)))

B++;

if(t.substring(3, 4).equals(r.substring(3, 4)))

A++;

if(t.substring(3, 4).equals(r.substring(0, 1)))

B++;

if(t.substring(3, 4).equals(r.substring(1, 2)))

B++;

if(t.substring(3, 4).equals(r.substring(2, 3)))

B++;

jta.append(trys+" "+t+" "+A+"A"+B+"B"+"\n");

if(A==4){

if(trys=4)

messageDialog("You win after "+trys+" trys!");

else if(trys=3)

messageDialog("You win after only "+trys+" trys!");

}

}

}

});

jf.setSize(300, 300);

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String args[]){

guessNumber();

}

}

我没有进行详细注释,这个程序挺好理解的,你可以自己再看一下

关于推箱子游戏的程序设计添加

我的回答仅作参考,因为是文曲星编程上学的一点东西。

我当时做地图时用到一些语句:

data 1//表示关卡关数

data 00000000000000//地图第一行

data 00011111111100//地图第二行

data 00012223222100

data 00012224222100

data 00012225222100

data 00011111111100

data 00000000000000

(数字是随便编的)作为地图时这样理解:

0表示墙外空地,1表示墙,2表示墙内空地,3表示箱子指定地,4表示箱子,5表示小人初始位置。

那么当你设计好地图时,只需在data内对应的写好地图信息编号,那么就能直接读出地图来了,我想文本文档应该是同样的道理,你可以改成:

map 1

line 00000000000000 line

line 00011111111100 line

line 00012223222100 line

line 00012224222100 line

line 00012225222100 line

line 00011111111100 line

line 00000000000000 line

读取关键字进行讨论。

或者用xml之类的语言解析下就好了。

希望对你有帮助。

谁给个VB 推箱子的源代码啊,急求

VB推箱子游戏(完整源代码)

VB推箱子游戏完整源代码,带声音、画面清淅,如codefans.net截图演示,本游戏最大的亮点是可以自动寻找路径,可以选定关卡、设计关卡、打开、关闭声音特效,总体来说是非常不错的小游戏,对学习用VB编写小游戏的朋友很有一定帮助。

下载地址:

我变了一个推箱子的代码,可不知道如何加入图片和音乐,请求高人指点

推箱子小游戏代码,包含音乐,换图功能,同时可以进行鼠标或键盘操作-Sokoban game code, including music, for map features, at the same time can be a mouse or keyboard operation

[MoveNut.rar] - 推箱子游戏,无密码可解压。 经典游戏一个,欢迎大家下载。

[tftp_server_linux.rar] - linux下的tftp源代码,比较经典的一个,好东西大家分享

[ProxyService.rar] - 用socket做的代理程序,对于外网连内网很有用

[tuixz.rar] - 这个程序基本实现了推箱子的所有功能,加入了背景音乐,有5首背景音乐可供 选择,并可以跳关、选关等,并且可以无限悔步,一直悔到当前关卡的开始状态。

[mine60.rar] - Java推箱子游戏(一共有50关卡,带好听的音效),完整版源码,图形界面,看上去够专业。界面和泡泡堂游戏有点相似,学习游戏编程的JAVA朋友值得一看。

求个推箱子的C++代码 要有注解 800到1000行

//*******************************************************

Sokoban.h:  类定义   Sokoban.c:  类成员函数实现

Use_Sokoban.c:  主函数

请用VC6(别编译器的也行)先运行Use_Sokoban.c文件,要编译该文件一下,

再点Project- Add To Project- Files 选择Sokoban.c文件,

即将Sokoban.c加载到工程里,最后运行就OK拉。

//*******************************************************

Sokoban.h

//*******************************************************

#ifndef SOKOBAN_H_  //防止文件重复包含

#define SOKOBAN_H_

#include queue

using std::queue;

//每一步的数据类型

struct node 

{

int bx, by; //箱子的坐标

int px, py; //人的坐标

};

//推箱子类

class Sokoban 

{

private:

enum {L = 15, H = 7};

char GameMap[H][L]; //地图

int Pex, Pey;  //人的位置

int Boxx, Boxy;  //箱子的位置

int Succeed, Prove; //是否成功到目的地, 是否可玩性

int dx[4], dy[4]; //方向数组

protected:

char Empty;

char People;

char Box;

char Block;

char Target;

int dir;  //记录按键方向

node s, e;

public:

Sokoban();  //构建函数

~Sokoban() {} //析构函数,即为inline

//地图初始化函数

void Initial();

//箱子路劲验证函数,参数为箱子坐标(bx,by),人坐标(px,py)

void Box_Bfs(int bx, int by, int px, int py);

//人路劲验证函数,人所到的目的地(ex,ey)

bool People_Bfs(int ex, int ey);

//地图刷新函数

void Show();

//按键判断函数

void Button();

//箱子人移动函数

void Move();

//验证越界函数

bool Check(int x, int y); 

};

#endif

//*******************************************************

Sokoban.cpp

//*******************************************************

#include "Sokoban.h"

#include cstring

#include cstdlib

#include ctime

#include iostream

#include conio.h

using std::cout;

using std::endl;

Sokoban::Sokoban()  //构建函数即对变量初始化

{

dir = -1;

Succeed = Prove = 0;

memset(GameMap, '.', sizeof(GameMap));

Empty = '.';

People = 'P';

Box = '#';

Block = '*';

Target = 'T';

//方向依次为上右下左

dx[0] = -1;  dx[1] = 0;  dx[2] = 1;  dx[3] = 0;  

dy[0] = 0;   dy[1] = 1;  dy[2] = 0;  dy[3] = -1;

//随机种子,使程序每次运行时所产生的随机数不同

srand(time(0));

}

//地图初始化函数

void Sokoban::Initial()

{

int count = 0, x, y;

//对地图中随机产生25个阻碍物

while(count != 25)

{

x = rand()%H;

y = rand()%L;

if(GameMap[x][y] == Empty)

{

GameMap[x][y] = Block;

count++;

}

}

while(true) //随机产生人开始的位置

{

x = rand()%H;

y = rand()%L;

if(GameMap[x][y] == Empty)

{

GameMap[x][y] = People;

Pex = x;

Pey = y;

break;

}

}

while(true) //随机产生箱子开始的位置

{

x = rand()%H;

y = rand()%L;

//不让箱子在地图的边界处

if(GameMap[x][y] == Empty  x != 0  y != 0

x != H-1  y != L-1)

{

GameMap[x][y] = Box;

Boxx = x;

Boxy = y;

break;

}

}

while(true) //随机产生目标的位置

{

x = rand()%H;

y = rand()%L;

if(GameMap[x][y] == Empty)

{

GameMap[x][y] = Target;

break;

}

}

//对游戏地图检查是否可将箱子推到目的地,即判断游戏可玩性

Sokoban::Box_Bfs(Boxx, Boxy, Pex, Pey); 

//如游戏不可玩,即再随机产生地图

if(!Prove)

{

memset(GameMap, '.', sizeof(GameMap));

Sokoban::Initial();

}

else

Sokoban::Show();

}

//箱子路劲验证函数

//用BFS算法对箱子验证是否可到目的地

void Sokoban::Box_Bfs(int bx, int by, int px, int py)

{

queuenode_Box; //创建箱子队列

//visit对上一步走到下一步的记录,防止箱子走重复路劲

//visit[i][j][z][k]表示箱子从点(i,j)到点(z,k)

//visit[][][][]为0时表示为走过,1时表示已走过

int visit[H][L][H][L];

memset(visit, 0, sizeof(visit)); //visit数组初始化 

s.bx = bx;  s.by = by;  //将起始的箱子、人位置放入队列

s.px = px;  s.py = py;

_Box.push(s);

int pe_x, pe_y;

while(!_Box.empty()) //队列为空时跳出

{

s = _Box.front();

_Box.pop();

if(GameMap[s.bx][s.by] == Target)  //到达目的地

{

Prove = 1;

break;

}

for(int i = 0; i  4; i++)

{

e.bx = s.bx + dx[i];  e.by = s.by + dy[i];

switch(i) //人推箱子的位置

{

case 0:  pe_x = s.bx + dx[2]; pe_y = s.by + dy[2]; break;

case 1:  pe_x = s.bx + dx[3]; pe_y = s.by + dy[3]; break;

case 2:  pe_x = s.bx + dx[0]; pe_y = s.by + dy[0]; break;

case 3:  pe_x = s.bx + dx[1]; pe_y = s.by + dy[1]; break;

}

//验证箱子和人的位置的合法性

if(!Check(e.bx, e.by) || !Check(pe_x, pe_y)

|| GameMap[e.bx][e.by] == Block || GameMap[pe_x][pe_y] == Block

|| visit[s.bx][s.by][e.bx][e.by] )

continue;

//如人可推箱子即进入队列

if(Sokoban::People_Bfs(pe_x, pe_y)) 

{

//保存人推箱子后的位置

e.px = pe_x;  e.py = pe_y;

_Box.push(e);

visit[s.bx][s.by][e.bx][e.by] = 1; //箱子路劲的标记

}

}

}

}

//人路劲验证函数

//用BFS算法对人验证是否可推箱子

bool Sokoban::People_Bfs(int ex, int ey)

{

queuenode_People;

node t, end;

//visit数组对人的路劲进行标记,0为未走过,1为走过

int visit[H][L]; 

//visit数组初始化为0

memset(visit, 0, sizeof(visit));

t.px = s.px;  t.py = s.py;  //人初始位置进入队列

_People.push(t);

visit[t.px][t.py] = 1;

while(!_People.empty()) //对立为空时跳出

{

t = _People.front();

_People.pop();

if(t.px == ex  t.py == ey)  //人可到达(ex,ey)该点

return 1;

for(int i = 0; i  4; i++)

{

end.px = t.px + dx[i];  end.py = t.py + dy[i];

//检查人的位置合法性

if(!Check(end.px, end.py) || GameMap[end.px][end.py] == Block

|| GameMap[end.px][end.py] == Box || visit[end.px][end.py])

continue;

//进入队列

_People.push(end);

visit[end.px][end.py] = 1; //记录

}

}

return 0;

}

//地图刷新函数

void Sokoban::Show()

{

int i, j;

while(true)

{  

//每半秒刷新一次地图

clock_t  s = clock();

while(clock() - s  CLOCKS_PER_SEC/2)

;

//先判断按键在移动

Sokoban::Button();  

Sokoban::Move();

system("cls");

for(i = 0; i  H; i++)

{

for(j = 0; j  L; j++)

cout  GameMap[i][j];

cout  endl;

}

cout  endl;

cout  "\n**********************************"  endl;

cout  "*     小小C++语言推箱子游戏      *"  endl;

cout  "*     游戏规则:                  *"  endl;

cout  "*     P: 人        #: 箱子       *"  endl;

cout  "*     *: 障碍物    T: 目的地     *"  endl;

cout  "**********************************"  endl;

cout  "*       每次游戏地图不一样       *"  endl;

cout  "*    人将箱子推到目的地即过关    *"  endl;

cout  "*所给地图,一定可过关,请慎重移箱子*"  endl;

cout  "*   箱子无路可走时,机器不会提示  *"  endl;

cout  "**********************************"  endl;

//箱子成功到达目的地

if(Succeed)

{

cout  "\n       ^_^  _"  endl;

cout  "恭喜过关成功! 再来一盘吧"  endl;

getchar();

break;

}

}

}

//按键判断函数

void Sokoban::Button()

{

int key;

if(kbhit() != 0) //检查当前是否有键盘输入,若有则返回一个非0值,否则返回0

while(kbhit() != 0)  //可能存在多个按键,要全部取完,以最后一个为主

key = getch(); //将按键从控制台中取出并保存到key中

switch(key)

{  

//上

case 72:  dir = 0;

break;

//右

case 77:  dir = 1;     

break;

//下

case 80:  dir = 2; 

break;

//左

case 75:  dir = 3; 

break;

}

}

}

//人推箱子移动函数

void Sokoban::Move()

{

int x, y;

//有按键时

if(dir != -1) 

{

//人所推向的位置坐标

x = Pex + dx[dir];  y = Pey + dy[dir];

//人所推位置为空,即走向该位置

if(Check(x, y)  GameMap[x][y] == '.')

{

GameMap[Pex][Pey] = '.';  //人的位置改变

GameMap[x][y] = 'P';

Pex = x;  Pey = y;

dir = -1;  //按键记录为无即-1

}

else //人所推位置为箱子,即将箱子推向该方向的前面这点

if(Check(x, y)  GameMap[x][y] == '#'

Check(x+dx[dir], y+dy[dir])

GameMap[ x+dx[dir] ][ y+dy[dir] ] == '.')

{

GameMap[Boxx][Boxy] = '.';  //箱子的位置改变

GameMap[x+dx[dir] ][ y+dy[dir] ] = '#';

Boxx = x + dx[dir];  Boxy = y + dy[dir];

GameMap[Pex][Pey] = '.';  //人的位置改变

GameMap[x][y] = 'P';

Pex = x;  Pey = y;

dir = -1;

}

else  //将箱子推向该方向的前面这点为目的地

if(Check(x, y)  GameMap[x][y] == '#'

Check(x+dx[dir], y+dy[dir])

GameMap[ x+dx[dir] ][ y+dy[dir] ] == 'T')

{

GameMap[Boxx][Boxy] = '.';  //箱子的位置改变

GameMap[x+dx[dir] ][ y+dy[dir] ] = '#';

Boxx = x + dx[dir];  Boxy = y + dy[dir];

GameMap[Pex][Pey] = '.';  //人的位置改变

GameMap[x][y] = 'P';

Pex = x;  Pey = y;

dir = -1;

Succeed = 1;  //记录成功到达目的地

}

}

}

//判断越界情况

bool Sokoban::Check(int x, int y)

{

if(x  0 || x = H || y  0 || y = L)

return 0;

else

return 1;

}

//*************************************************

Use_Sokoban.cpp

//*************************************************

#include iostream

#include "Sokoban.h"

using namespace std;

int main()

{

Sokoban s;

s.Initial();

return 0;

}

//*************************************************


分享名称:推箱子地图java源代码 推箱子Java代码
当前网址:http://cdkjz.cn/article/higjcp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220