java推箱子游戏源代码

合集下载

推箱子之完美游戏程序清单

推箱子之完美游戏程序清单

//.推箱子源代码.#include <stdio.h> //标准输入输出头文件#include <stdlib.h> //system函数的头文件#include <conio.h> //getch函数的头文件#include <windows.h>int i,j;void drawmap(int map[10][12]); //声明画图函数void setback();int main(){char input;int count=0; //定义记分变量/*下面定义一个二维数组,其中的元素是不同图像的ID号,不同数字分别代表的图像元素*/ setback ();int map[10][12] = {{2,2,2,2,2,1,1,1,1,1,2,2},{1,1,1,1,2,1,0,0,0,1,1,2},{1,0,0,1,1,1,0,1,0,0,1,2},{1,0,4,3,3,3,3,3,1,0,1,1},{1,0,0,1,1,3,3,3,4,0,0,1},{1,0,0,0,0,4,1,0,4,1,0,1},{1,0,4,1,4,0,0,0,4,0,0,1},{1,1,0,6,0,1,1,1,4,1,0,1},{2,1,1,1,1,1,2,1,0,0,0,1},{2,2,2,2,2,2,2,1,1,1,1,1}};while (1) //死循环,等待用户命令{system("cls");printf(" 由范占伟,刘艳威制:\n\n\n");drawmap(map);printf(" 你的当前得分=%d\n\n",count);//找初始位置for (i=0;i<10;i++){for (j=0;j<12;j++){if (map[i][j]==6||map[i][j]==9) break;}if (map[i][j]==6||map[i][j]==9) break;}printf(" 您的当前坐标(%d,%d)\n",i,j);input = getch(); //用getch()函数无需回车确认地获取用户输入,用于控制行走方向。

Java实现经典游戏推箱子的示例代码

Java实现经典游戏推箱子的示例代码

Java实现经典游戏推箱⼦的⽰例代码⽬录前⾔主要设计功能截图代码实现核⼼类声⾳播放类总结前⾔《推箱⼦》推箱⼦是⼀个古⽼的游戏,⽬的是在训练你的逻辑思考能⼒。

在⼀个狭⼩的仓库中,要求把⽊箱放到指定的位置,稍不⼩⼼就会出现箱⼦⽆法移动或者通道被堵住的情况,所以需要巧妙的利⽤有限的空间和通道,合理安排移动的次序和位置,才能顺利的完成任务。

游戏是⽤java语⾔实现,采⽤了swing技术进⾏了界⾯化处理,设计思路⽤了⾯向对象思想。

主要需求控制搬运⼯上下左右移动,来将箱⼦推到指定地点主要设计1、游戏⾯板⽣成显⽰2、地图⽣成算法3、⼈物移动算法4、播放背景⾳乐5、箱⼦移动算法6、全部箱⼦移动到指定位置,才算游戏过关功能截图游戏开始移动效果游戏过关代码实现核⼼类public class GameFrame extends JFrame implementsActionListener, MouseListener, KeyListener {// 实现动作事件监听器、⿏标事件监听器、键盘事件监听器// 当前的关卡数,默认为第⼀关,从1开始计数private int grade = 1;// row,column记载⼈的位置,分别表⽰⼆维数组中的⾏号和列号,即map[row][column]确定⼈的位置private int row = 7, column = 7;// leftX,leftY记载左上⾓图⽚的位置,避免图⽚从(0,0)坐标开始,因为是图⽚填充,从(0,0)开始不⾏private int leftX = 50, leftY = 50;// 记载地图的总共有多少⾏、多少列private int mapRow = 0, mapColumn = 0;// 记载屏幕窗⼝的宽度和⾼度private int width = 0, height = 0;private boolean acceptKey = true;// 程序所需要⽤到的图⽚private Image pics[] = null;// 图⽚数据private byte[][] map = null;// 地图数据private ArrayList list = new ArrayList();private SoundPlayerUtil soundPlayer;// 播放声⾳⼯具类/* 常量,即游戏中的资源 */private final static int WALL = 1;// 墙private final static int BOX = 2;// 箱⼦private final static int BOX_ON_END = 3;// 放到⽬的地的箱⼦private final static int END = 4;// ⽬的地private final static int MAN_DOWN = 5;// 向下的⼈private final static int MAN_LEFT = 6;// 向左的⼈private final static int MAN_RIGHT = 7;// 向右的⼈private final static int MAN_UP = 8;// 向上的⼈private final static int GRASS = 9;// 通道private final static int MAN_DOWN_ON_END = 10;// 站在⽬的地向下的⼈private final static int MAN_LEFT_ON_END = 11;// 站在⽬的地向左的⼈private final static int MAN_RIGHT_ON_END = 12;// 站在⽬的地向右的⼈private final static int MAN_UP_ON_END = 13;// 站在⽬的地向上的⼈private final static int MOVE_PIXEL = 30;// 表⽰每次移动30像素/*** 在构造⽅法GameFrame0中,调⽤initMap()法来初始化本关grade游戏地图,清空悔棋信* 息列表list,同时播放MIDI背景⾳乐。

java_推箱子讲解

java_推箱子讲解

总共四个包,因为不能上传图片所以在第2个包中,导入图片(图片可以去网上下载一些)或者高手帮我解决一下。

不用导入图片也能玩!(求大神)下面是整个游戏的代码:1:package com.sj.xzq;class BackUpInfo {private int[][] map;private int manX;private int manY;public BackUpInfo(int[][] map, int manX, int manY) {this.map = map;this.manX = manX;this.manY = manY;}public int[][] getMap() {return map;}public int getManX() {return manX;}public int getManY() {return manY;}}2:package com.sj.xzq;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.Toolkit;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.util.Stack;import javax.swing.JOptionPane;import javax.swing.JPanel;public class Game extends JPanel {private LoadMap lm;private int level;private int manX, manY;private int lastImg = 2;private int[][] map; // 地图所对应的二维数组private int[][] backMap; // 地图所对应的二维数组,这个数组用于保存上一步的地图信息private static final int LEN = 30; // 图片的长private static final int WIDTH = 30; // 图片的宽private static final int SPEED = 30; // 移动速度,每次移动一格private static Toolkit tk = Toolkit.getDefaultToolkit();private static Image[] imgs = null;private Stack<BackUpInfo> myStack = new Stack<BackUpInfo>();private BackUpInfo bp;static {imgs = new Image[] {tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/0.gif")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/1.gif")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/2.GIF")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/3.GIF")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/4.gif")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/5.GIF")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/6.GIF")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/7.GIF")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/8.GIF")),tk.getImage(GameFrame.class.getClassLoade r().getResource("imgs/9.GIF")) };}public Game(int level) {this.setBounds(0, 0, 600, 600);this.setVisible(true);lm = new LoadMap(level);map = lm.getMap();this.manX = lm.getManX();this.manY = lm.getManY();this.level = level;}public void paint(Graphics g) {for (int i = 0; i < 20; i++) {for (int j = 0; j < 20; j++) {g.drawImage(imgs[map[i][j]], i * LEN, j * WIDTH, this);}}g.setColor(Color.BLUE);g.setFont(new Font("仿宋", Font.BOLD, 18));g.drawString("关卡:" + level, 50, 50);}// 响应键盘事件,松开键盘才响应// 上下左右四个键,实现人物的移动。

推箱子游戏的代码)

推箱子游戏的代码)

#include <dos.h>#include <stdio.h>#include <ctype.h>#include <conio.h>#include <bios.h>#include <alloc.h>typedef struct winer{int x,y;struct winer *p;}winer;char status [20][20];char far *printScreen=(char far* )0xB8000000;void putoutChar(int y,int x,char ch,char fc,char bc);void printWall(int x, int y);void printBox(int x, int y);void printBoxDes(int x, int y);void printDestination(int x, int y);void printDestination1(int x,int y,winer **win,winer **pw); void printMan(int x, int y);void init();winer *initStep1();winer *initStep2();winer *initStep3();winer *initStep4();void moveBoxSpacetoSpace(int x ,int y, char a);void moveBoxDestoSpace(int x ,int y, char a) ;void moveBoxSpacetoDes(int x, int y, char a);void moveBoxDestoDes(int x, int y, char a);int judge(int x, int y);void move(int x, int y, char a);void reset(int i);void putoutChar(int y,int x,char ch,char fc,char bc){printScreen[(x*160)+(y<<1)+0]=ch;printScreen[(x*160)+(y<<1)+1]=(bc*16)+fc;}void printWall(int x,int y){putoutChar(y-1,x-1,219,GREEN,BLACK);status[x][y]='w';}void printBox(int x,int y){putoutChar(y-1,x-1,10,WHITE,BLACK);status[x][y]='b';}void printDestination1(int x,int y,winer **win,winer **pw) {winer *qw;putoutChar(y-1,x-1,003,YELLOW,BLACK);status[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 printDestination(int x,int y){putoutChar(y-1,x-1,003,YELLOW,BLACK);status[x][y]='m';}void printMan(int x,int y){gotoxy(y,x);_AL=02;_CX=01;_AH=0xa;geninterrupt(0x10);}void printBoxDes(int x,int y){putoutChar(y-1,x-1,10,YELLOW,BLACK);status[x][y]='i';}void init(){int i,j;for(i=0;i<20;i++)for(j=0;j<20;j++)status[i][j]=0;_AL=3;_AH=0;geninterrupt(0x10);gotoxy(40,4);printf("Welcome to the box world!");gotoxy(40,6);printf("You can use up, down, left,");gotoxy(40,8);printf("right key to control it, or");gotoxy(40,10);printf("you can press Esc to quit it."); gotoxy(40,12);printf("Press space to reset the game."); gotoxy(40,14);printf("Wish you have a good time !");gotoxy(40,16);printf("April , 2007");}winer *initStep1(){int x;int y;winer *win=NULL;winer *pw;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);printDestination1(4+4,2+10,&win,&pw); printDestination1(5+4,2+10,&win,&pw); printDestination1(6+4,2+10,&win,&pw); printMan(2+4,8+10);return win;}winer *initStep2(){int x;int y;winer *win=NULL;winer *pw;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++)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);printDestination1(5+4,7+10,&win,&pw); printDestination1(6+4,7+10,&win,&pw); printDestination1(7+4,7+10,&win,&pw); printMan(2+4,6+10);return win;}winer *initStep3(){int x;int y;winer *win=NULL;winer *pw;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(5+4,6+10);printDestination1(2+4,5+10,&win,&pw); printDestination1(2+4,6+10,&win,&pw); printDestination1(2+4,7+10,&win,&pw); printMan(2+4,4+10);return win;}winer *initStep4(){int x;int y;winer *win=NULL;winer *pw;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);printDestination1(3+4,7+10,&win,&pw); printDestination1(4+4,7+10,&win,&pw); printDestination1(5+4,7+10,&win,&pw); printDestination1(4+4,8+10,&win,&pw); printDestination1(5+4,8+10,&win,&pw); printMan(2+4,2+10);return win;}void moveBoxSpacetoSpace(int x,int y,char a) {switch(a){case 'u':status[x-1][y]=0;printf(" ");printBox(x-2,y);printMan(x-1,y);status[x-2][y]='b';break;case 'd':status[x+1][y]=0;printf(" ");printBox(x+2,y);printMan(x+1,y);status[x+2][y]='b';break;case 'l':status[x][y-1]=0;printf(" ");printBox(x,y-2);printMan(x,y-1);status[x][y-2]='b';break;case 'r':status[x][y+1]=0;printf(" ");printBox(x,y+2);printMan(x,y+1);status[x][y+2]='b';break;default:break;}}void moveBoxDestoSpace(int x,int y,char a) {switch(a){case 'u':status[x-1][y]='m';printf(" ");printBox(x-2,y);printMan(x-1,y);status[x-2][y]='b';break;case 'd':status[x+1][y]='m';printf(" ");printBox(x+2,y);printMan(x+1,y);status[x+2][y]='b';break;case 'l':status[x][y-1]='m';printf(" ");printBox(x,y-2);printMan(x,y-1);status[x][y-2]='b';break;case 'r':status[x][y+1]='m';printf(" ");printBox(x,y+2);printMan(x,y+1);status[x][y+2]='b';break;default:break;}}void moveBoxSpacetoDes(int x,int y,char a) {switch(a){case 'u':status[x-1][y]=0;printf(" ");printBoxDes(x-2,y);status[x-2][y]='i';break;case 'd':status[x+1][y]=0;printf(" ");printBoxDes(x+2,y);printMan(x+1,y);status[x+2][y]='i';break;case 'l':status[x][y-1]=0;printf(" ");printBoxDes(x,y-2);printMan(x,y-1);status[x][y-2]='i';break;case 'r':status[x][y+1]=0;printf(" ");printBoxDes(x,y+2);printMan(x,y+1);status[x][y+2]='i';break;default:break;}}void moveBoxDestoDes(int x,int y,char a) {switch(a){case 'u':status[x-1][y]='m';printf(" ");printBoxDes(x-2,y);printMan(x-1,y);status[x-2][y]='i';break;case 'd':status[x+1][y]='m';printf(" ");printMan(x+1,y);status[x+2][y]='i'; break;case 'l':status[x][y-1]='m'; printf(" ");printBoxDes(x,y-2); printMan(x,y-1);status[x][y-2]='i'; break;case 'r':status[x][y+1]='m'; printf(" ");printBoxDes(x,y+2); printMan(x,y+1);status[x][y+2]='i'; break;default:break;}}int judge(int x,int y) {int i;switch(status[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;}void 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){printDestination(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){moveBoxSpacetoSpace(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}else if(judge(x-2,y)==3){moveBoxSpacetoDes(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}elsegotoxy(y,x);break;}else if(judge(x-1,y)==4){if(judge(x-2,y)==1){moveBoxDestoSpace(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}else if(judge(x-2,y)==3){moveBoxDestoDes(x,y,'u');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x-1);}elsegotoxy(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){printDestination(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){moveBoxSpacetoSpace(x,y,'d'); if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3){moveBoxSpacetoDes(x,y,'d'); if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}elsegotoxy(y,x);break;}else if(judge(x+1,y)==4){if(judge(x+2,y)==1){moveBoxDestoSpace(x,y,'d'); if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}else if(judge(x+2,y)==3){moveBoxDestoDes(x,y,'d');if(judge(x,y)==3)printDestination(x,y);gotoxy(y,x+1);}elsegotoxy(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){printDestination(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){moveBoxSpacetoSpace(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveBoxSpacetoDes(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}elsegotoxy(y,x);break;}else if(judge(x,y-1)==4){{moveBoxDestoSpace(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}else if(judge(x,y-2)==3){moveBoxDestoDes(x,y,'l');if(judge(x,y)==3)printDestination(x,y);gotoxy(y-1,x);}elsegotoxy(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){printDestination(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){moveBoxSpacetoSpace(x,y,'r');printDestination(x,y); gotoxy(y+1,x);}else if(judge(x,y+2)==3){moveBoxSpacetoDes(x,y,'r'); if(judge(x,y)==3)printDestination(x,y); gotoxy(y+1,x);}elsegotoxy(y,x);break;}else if(judge(x,y+1)==4){if(judge(x,y+2)==1){moveBoxDestoSpace(x,y,'r'); if(judge(x,y)==3)printDestination(x,y); gotoxy(y+1,x);}else if(judge(x,y+2)==3){moveBoxDestoDes(x,y,'r'); if(judge(x,y)==3)printDestination(x,y); gotoxy(y+1,x);}elsegotoxy(y,x);break;}default:break;}}void reset(int i){switch(i){case 0:init();initStep1();break;case 1:init();initStep2();break;case 2:init();initStep3();break;case 3:init();initStep4();break;default:break;}}void main(){int key;int x;int y;int s;int i=0;winer *win;winer *pw;_AL=3;_AH=0;geninterrupt(0x10); init();win=initStep1();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(status[pw->x][pw->y]=='m')s++;pw=pw->p;}if(s==0){free(win);gotoxy(25,2);printf("congratulate! You have done this step!"); getch();i++;switch(i){case 1:init();win=initStep2();break;case 2:init();win=initStep3();break;case 3:init();win=initStep4();break;case 4:gotoxy(15,21);printf("Congratulation! \n");gotoxy(15,23);printf("You have done all the steps, Welcome to play again!"); key=0x011b;getch();break;default:break;}}}while(key!=0x011b);_AL=3;_AH=0;geninterrupt(0x10);}。

推箱子代码

推箱子代码

import javax.swing.*;import java.awt.event.*;import java.awt.*;import java.io.*;import javax.sound.midi.*;import java.util.Stack;public class Tuixiangzi{public static void main(String[] args){new mainFrame();}}class mainFrame extends JFrame implements ActionListener,ItemListener{JLabel lb;JLabel lb2;JButton btnrenew,btnlast,btnnext,btnchoose,btnfirst,btnover,btnmuc,btnback;mainpanel panel;Sound sound;JComboBox jc=new JComboBox();MenuItem renew=new MenuItem(" 重新开始");MenuItem back=new MenuItem(" 悔一步");MenuItem last=new MenuItem(" 上一关");MenuItem next=new MenuItem(" 下一关");MenuItem choose=new MenuItem(" 选关");MenuItem exit=new MenuItem(" 退出");MenuItem qin=new MenuItem(" 琴萧合奏");MenuItem po=new MenuItem(" 泡泡堂");MenuItem guang=new MenuItem(" 灌篮高手");MenuItem nor=new MenuItem(" 默认");MenuItem eye=new MenuItem(" eyes on me");MenuItem about=new MenuItem(" 关于推箱子...");mainFrame(){super("推箱子v2.0");setSize(720,720);setVisible(true);setResizable(false);setLocation(300,20);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Container cont=getContentPane();cont.setLayout(null);cont.setBackground(Color.black);Menu choice=new Menu(" 选项");choice.add(renew);choice.add(last);choice.add(next);choice.add(choose);choice.add(back);choice.addSeparator();choice.add(exit);renew.addActionListener(this);last.addActionListener(this);next.addActionListener(this);choose.addActionListener(this);exit.addActionListener(this);back.addActionListener(this);Menu setmuc=new Menu(" 设置音乐");setmuc.add(nor);setmuc.add(qin);setmuc.add(po);setmuc.add(guang);setmuc.add(eye);nor.addActionListener(this);qin.addActionListener(this);po.addActionListener(this);guang.addActionListener(this);eye.addActionListener(this);Menu help=new Menu(" 帮助");help.add(about);about.addActionListener(this);MenuBar bar=new MenuBar();bar.add(choice);bar.add(setmuc);bar.add(help);setMenuBar(bar);nor.setEnabled(false);lb=new JLabel("JA V A推箱子v2.0版!!!提供友情下载。

基于java的推箱子游戏(含源文件)

基于java的推箱子游戏(含源文件)

基于JAVA的推箱子游戏前言在这个充满竞争的社会里,随着生活节奏的不断加快,人们的生活水平越来越繁忙,因此,越来越多人感到无比的烦躁与巨大的压力,这些压力也带给人们诸多烦恼。

所以如何解除现代人的生活压力,释放心中苦闷已经成为现代人的共同心愿。

而本次的这个基于JAVA的推箱子小游戏正是使用JAVA语言并且结合于EditPlus编译器,为大家开发了一款适合的、休闲娱乐的游戏,在娱乐的同时也给忙碌的上班族们开拓了另一个思考问题的空间。

基于JAVA的推箱子小游戏主要分为初始化模块,画图模块,移动小人模块,移动箱子模块以及功能控制模块。

玩家是通过控制小人的移动来推动箱子,需要避过障碍物与死角才能将其推放到指定位置从而达到过关的目的。

每关的难易程度各异,不仅适合人们休闲娱乐,还有助于开发智力,提高人们思考问题能力与想象能力,改善思考方式,拓展思维。

关键词:JAVA;解压;推箱子;休闲娱乐AbstractIn this competitive society, along with the accelerating pace of life, people's living standard more and busier, therefore, more and more people feel very upset and enormous pressure, the pressure also gives people a lot of trouble. So how to relieve the pressure of modern life, release the pain has become the common aspiration of the people. But this time based on the JAVA Push Box Game is the use of JAVA language and the combination of EditPlus compiler; we developed a suitable, recreational game, in the entertainment but also for busy office workers to develop another thinking space.Based on the JAVA Push Box Game consists of initialization module, drawing module, mobile SIM module, mobile box module and control module. Game player is controlled through a small mobile drive box, need to avoid obstacles and dead angle can be pushed into the specified location so as to achieve the purpose of clearance. Each level of difficulty degree is different, not only for recreation, also contribute to the development of intelligence, improve people's thinking ability and imagination ability, improve the way of thinking, the development of thinking.Key Words:JAVA; Relieve pressure; Push box; Recreation and entertainment目录前言............................................................................................................................................. I Abstract ..................................................................................................................................... II 1.可行性研究 (2)1.1设计目的 (2)1.2可行性研究前提 (2)1.3可行性分析 (2)1.4结论意见 (3)2.需求分析 (3)2.1引言 (3)2.2游戏需求 (3)2.3软硬件需求 (4)硬件环境需求: (4)软件环境: (4)2.4接口控制 (4)2.5方案论证 (4)2.5.1.C++语言的优点 (4)2.5.2.Java语言的优点 (5)2.5.3.方案选择 (6)3.概要设计 (7)3.1游戏设计分析 (7)3.3.1.地图文件的引用 (7)3.3.2.程序运逻辑 (7)3.3.3.绘图 (7)3.3.4.显示平台 (7)3.2 注意事项 (8)3.3 游戏流程图 (8)4.详细设计 (9)4.1游戏总体结构与代码 (9)结论 (20)参考文献 (21)致谢 (22)引言在人类的社会生活当中,游戏占有很大的比重,并且随着社会的发展而不断发展。

Java面向对象推箱子源代码(可扩展)

Java面向对象实现推箱子的源代码目录一、首先: (1)二、以下为工程中各个类的源代码: (1)1、Box (1)2、GameMainTest (3)3、Man (4)4、Map (6)5、MovingException (7)6、Out (8)一、首先:在eclipse中新建一个工程,包名和类名(工程结构)如下:二、以下为工程中各个类的源代码:源代码按对应的类名复制粘贴进去即可。

1、Boxpackage tuixiangzi;import java.util.Random;public class Box {private static Random ran = new Random();private static int x = ran.nextInt(10); //箱子所在的位置(随机) private static int y = ran.nextInt(10); //箱子所在的位置(随机) private int [][]map = Map.getArray();private int h = map.length - 1;private int l = map[h].length - 1;/*** 箱子左移*/public void boxLMove()throws MovingException{if(y-1 < 0) {throw new MovingException("You Can't Moving Left!");}if(Man.getX() == x && Man.getY() == y) {y=(y-1);}/*** 箱子右移*/public void boxRMove()throws MovingException{if(y+1 > l) {throw new MovingException("You Can't Moving Right!");}if(Man.getX() == x && Man.getY() == y) {y=(y+1);}}/*** 箱子上移*/public void boxUMove()throws MovingException{if(x-1 < 0) {throw new MovingException("You Can't Moving Up!");}if(Man.getX() == x && Man.getY() == y) {x=(x-1);}}/*** 箱子下移*/public void boxDMove()throws MovingException{if(x+1 > h) {throw new MovingException("You Can't Moving Down!");}if(Man.getX() == x && Man.getY() == y) {x=(x+1);}}/*** 判断箱子是否能移动(死亡)或者是否通关* @return*/public String judgOver() {String msg = null;if(x == Out.getX() && y == Out.getY()) {msg = "You Win!";return msg;if(x == 0 && y == 0 || x == h && y == l || x == 0 && y == l || x == h && y == 0) { msg = "Game Over!";return msg;}else {msg = " ";return msg;}}public static int getX() {return x;}public static void setX(int x) {Box.x = x;}public static int getY() {return y;}public static void setY(int y) {Box.y = y;}}2、GameMainTestpackage tuixiangzi;import java.util.Scanner;public class GameMainTest {private static Scanner sc;public static void main(String[] args){Map map = new Map();Man man = new Man();Box box = new Box();sc = new Scanner(System.in);map.printMap();//游戏开始,打印地图System.out.println("推箱子游戏开始!");do {System.out.println("w:↑ s:↓ a:← d:→ 回车确认移动");String key = sc.next();switch (key){case"a": //左移try {man.leftMove(key);box.boxLMove();} catch (MovingException e) {System.out.println("走不下去啦!");}map.cleanManAfter(Man.getX(), Man.getY()+1);break;case"d": //右移try {man.rightMove(key);box.boxRMove();} catch (MovingException e) {System.out.println("走不下去啦!");}map.cleanManAfter(Man.getX(), Man.getY()-1);break;case"s": //下移try {man.downMove(key);box.boxDMove();} catch (MovingException e) {System.out.println("走不下去啦!");}map.cleanManAfter(Man.getX()-1, Man.getY());break;case"w": //上移try {man.upMove(key);box.boxUMove();} catch (MovingException e) {System.out.println("走不下去啦!");}map.cleanManAfter(Man.getX()+1, Man.getY());break;}System.out.println("\n\n\n\n");System.out.println(box.judgOver());map.printMap();}while(true);}}3、Manpackage tuixiangzi;public class Man {private static int x = 0; //人所在的行位置private static int y = 0; //人所在的列位置private int [][]map = Map.getArray();private int h = map.length-1;private int l = map[h].length-1;/*** 向左移动* @param key 移动按键* @throws MovingException 无法移动异常*/public void leftMove(String key)throws MovingException{if(y-1 < 0) {throw new MovingException("You Can't Moving Left!");}if(key.equals("a")) {y=(y-1);}}/*** 向右移动* @param key 移动按键* @throws MovingException 无法移动异常*/public void rightMove(String key)throws MovingException{ if(y+1 > l) {throw new MovingException("You Can't Moving Right!");}if(key.equals("d")) {y=(y+1);}}/*** 向上移动* @param key 移动按键* @throws MovingException 无法移动异常*/public void upMove(String key)throws MovingException{if(x-1 < 0) {throw new MovingException("You Can't Moving Up!");}if(key.equals("w")) {x=(x-1);}}/*** 向下移动* @param key 移动按键* @throws MovingException 无法移动异常*/public void downMove(String key)throws MovingException{ if(x+1 > h) {throw new MovingException("You Can't Moving Down!");}if(key.equals("s")) {x=(x+1);}}public static int getX() {return x;}public static void setX(int x) {Man.x = x;}public static int getY() {return y;}public static void setY(int y) {Man.y = y;}}4、Mappackage tuixiangzi;public class Map {private static int array[][] = new int[10][10];/*** 打印地图布局*/public void printMap() {array[Man.getX()][Man.getY()] = 1; //初始化人array[Box.getX()][Box.getY()] = 3; //初始化箱子array[Out.getX()][Out.getY()] = 2; //初始化出口for(int i = 0; i < array.length; i++) {for(int j = 0; j < array[i].length;j++) {if(array[i][j] == array[Man.getX()][Man.getY()]) {System.out.print("♀ ");} else if(array[i][j] == 0) {System.out.print("□ ");} else if(array[i][j] == array[Box.getX()][Box.getY()]) {System.out.print("■ ");} else if(array[i][j] == array[Out.getX()][Out.getY()]) {System.out.print("→ ");}}System.out.println();}}/*** 清除人和箱子移动后上一步的位置* @param x* @param y* @return*/public int cleanManAfter(int x,int y) {return array[x][y]=0;}public static int[][] getArray() {return array;}public static void setArray(int[][] array) {Map.array = array;}}5、MovingExceptionpackage tuixiangzi;public class MovingException extends Exception{private static final long serialVersionUID = 1L;public MovingException() {super();}public MovingException(String message) {super(message);}public MovingException(String message, Throwable cause) {super(message, cause);}public MovingException(Throwable cause) {super(cause);}}6、Outpackage tuixiangzi;public class Out {//后期可扩展为出口出现的位置随机private static int x = 9; //初始化出口的位置private static int y = 9; //初始化出口的位置public static int getX() {return x;}public static void setX(int x) {Out.x = x;}public static int getY() {return y;}public static void setY(int y) {Out.y = y;}}。

2.5D推箱子游戏

import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Toolkit;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.geom.GeneralPath;import javax.swing.JPanel;public class PushBox extends JPanel implements KeyListener{ private Image pic[] = null; // 图片int initX=200,initY=70;//map1为第一层,map2为第二层private int [][]map1={//第一层地图,即地板层{-1,-1,-1,1, 0, 1, 0, 1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},{0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 3, 0, 1},{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},{0, 1, 0, 1, 0, 3, 0, 1, 0, 1, 0, 1, 0, 1},{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},{0, 1, 0, 1, 0, 1, 0, 3, 0, 1, 0, 1, 0, 1},{1, 0, 3, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},{0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},{1, 0, 3, 0, 1, 0, 1, 0, 1, 0, 1, 2, 1, 0},{0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0},{0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,-1,-1,-1},{1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,-1,-1,-1}};private int [][]map2={//第二层地图,建筑物或人{-1,-1,-1,2, 2, 2, 2, 2,-1,-1,-1,-1,-1,-1},{-1,-1,-1,2, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2},{2, 2, 2, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2},{2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2},{2, 0, 0, 0, 1, 0, 2, 0, 0, 2, 0, 0, 0, 2},{2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 2},{2, 0, 2, 2, 0, 0, 0, 0, 0, 2, 0, 1, 0, 2},{2, 0, 0, 0, 1, 2, 2, 0, 0, 0, 1, 0, 0, 2},{2, 0, 0, 0, 0, 2, 2, 0, 0, 0, 2, 2, 0, 2},{2, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 2},{2, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 2},{2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2},{2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2,-1,-1,-1},{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1,-1,-1}};// 定义一些常量,对应地图的元素final byte WALL = 1, BOX = 2, BOXONEND = 3, END = 4, MANDOWN = 5, MANLEFT = 6, MANRIGHT = 7, MANUP = 8, GRASS = 9, MANDOWNONEND = 10,MANLEFTONEND = 11, MANRIGHTONEND = 12, MANUPONEND = 13;private int row = 7, column = 7; //int manx,many;// 加载图片Image box=Toolkit.getDefaultToolkit().getImage("images\\box.png");Image wall =Toolkit.getDefaultToolkit().getImage("images\\wall.png");Image greenBox=Toolkit.getDefaultToolkit().getImage("images\\greenbox.png");Image man =Toolkit.getDefaultToolkit().getImage("images\\a1.png");//人物Image background=Toolkit.getDefaultToolkit().getImage("images\\background.jpg");//人物public PushBox() {System.out.print("Werewr");// 设置焦点setFocusable(true);this.addKeyListener(this);}public void myDrawRect(Graphics g, int x ,int y){//绘制多边形Graphics2D g2D=(Graphics2D)g;if(g2D==null){return;}GeneralPath path = new GeneralPath();p ath.moveTo(x+14, y);p ath.lineTo(x+53, y+10);p ath.lineTo(x+37, y+37);p ath.lineTo(x-2, y+26);p ath.lineTo(x+14, y);g2D.fill(path); //g.draw(myPath);}// 画游戏界面public void paint(Graphics g) {g.clearRect(0,0,this.getWidth(),getHeight());g.setColor(Color.BLACK);g.drawImage(background, 0, 0,800,800,this);//画游戏背景//绘制第一层,即地板层for(int i=0; i<map1.length; i++){for(int j=0; j<map1[i].length; j++){//根据索引值进行坐标转换int X = initX+36*j-15*i;int Y = initY+10*j+25*i;if(map1[i][j] == 0){//白色空地/*设置paint的颜色*/g.setColor(new Color(255, 220, 220, 220));this.myDrawRect(g, X, Y);}else if(map1[i][j] == 1){//灰色空地g.setColor(new Color(255, 170, 170, 170));this.myDrawRect(g, X, Y);}else if(map1[i][j] == 2){//目的地1g.setColor(new Color(255, 127, 255, 130));this.myDrawRect(g, X, Y);}else if(map1[i][j] == 3){//目的地2g.setColor(new Color(255, 60, 255, 120));this.myDrawRect(g, X, Y);}}}//开始绘制第二层,及建筑所在层for(int i=0; i<map2.length; i++){for(int j=0; j<map2[i].length; j++){//根据索引值进行坐标转换int X = initX+36*j-15*i;int Y = initY+10*j+25*i;if(map2[i][j] == 1){//第二层上有箱子处g.drawImage(box, X-1, Y-27,this);}else if(map2[i][j] == 2){//墙g.drawImage(wall, X, Y-25,this);}else if(map2[i][j] == 3){//绿色的箱子g.drawImage(greenBox, X-1, Y-27,this);}//绘制人if(i == row && j == column){g.drawImage(man, X-1, Y-27,this);}}}}public void keyPressed(KeyEvent e) {// TODO Auto-generated method stubif (e.getKeyCode() == KeyEvent.VK_UP) {// 向上moveUp();}if (e.getKeyCode() == KeyEvent.VK_DOWN) {// 向下moveDown();}if (e.getKeyCode() == KeyEvent.VK_LEFT) { // 向左moveLeft();}if (e.getKeyCode() == KeyEvent.VK_RIGHT) { // 向右moveRight();}repaint();}private void moveLeft() {// TODO Auto-generated method stubcolumn--;}private void moveDown() {// TODO Auto-generated method stubrow++;}private void moveRight() {// TODO Auto-generated method stubcolumn++;}private void moveUp() {// TODO Auto-generated method stubrow--;}public void keyReleased(KeyEvent arg0) {// TODO Auto-generated method stub}public void keyTyped(KeyEvent arg0) {// TODO Auto-generated method stub}}import java.awt.Container;import javax.swing.JFrame;public class BoxFrame2 extends JFrame {public BoxFrame2() {// 默认的窗体名称setTitle("2.5D推箱子游戏");// 获得我们自定义面板[PushBox面板]的实例PushBox panel = new PushBox();Container contentPane = getContentPane();contentPane.add(panel);setSize(800, 800);}public static void main(String[] args) {BoxFrame2 e1 = new BoxFrame2();// 设定允许窗体关闭操作e1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 显示窗体e1.setVisible(true);}}。

推箱子游戏代码,非界面版

import java.util.Random;import java.util.Scanner;public class 推箱子 {public static void main(String[] args) {//推箱子int map[][]=new int[10][10]; //固定箱子,人,目的地的位置// int rx=0;// int ry=0;// map[rx][ry]=1;// int xx=5;// int xy=1;// map[xx][xy]=2;// int mx=6;// int my=4;// map[mx][my]=3;//随机生成人,箱子,目的地的位置Random r=new Random();int rx=r.nextInt(10);int ry=r.nextInt(10);map[rx][ry]=1;int xx=r.nextInt(10);int xy=r.nextInt(10);map[xx][xy]=2;int mx=r.nextInt(10);int my=r.nextInt(10);map[mx][my]=3;while(true){for(int i=0;i<map.length;i++){for(int j=0;j<map[i].length;j++){if(map[i][j]==1){System.out.print(" 人 ");}if(map[i][j]==2){//△代表箱子System.out.print(" △ ");}if(map[i][j]==3){//★代表箱子System.out.print(" ★ ");}if(map[i][j]==0){//◇代表箱子System.out.print(" ◇ ");}}System.out.println();}if(xx==mx&&xy==my){break;}if(xx==0&&mx!=0||xy==0&&my!=0||xx==9&&mx!=9||xy==9&&my!=9){// 判断箱子到边上而目的地不在边上的情况System.out.println("永远也到不了了~~~~(>_<)~~~~ ");}Scanner s = new Scanner(System.in);System.out.println("请输入要操作的动作:");System.out.println("w↑,s↓,a←,d→");String t=s.next();if(t.equals("d")){if(ry==9){System.out.println("此路不通!!~~~~(>_<)~~~~ ");}else if(t.equals("d")){ry++;if(map[rx][ry]==0){map[rx][ry]=1;map[rx][ry-1]=0;}else if(map[rx][ry]==2){xy++;map[xx][xy]=2;map[rx][ry]=1;map[rx][ry-1]=0;}}}if(t.equals("a")){if(ry==0){System.out.println("此路不通!!~~~~(>_<)~~~~ ");}else {ry--;if(map[rx][ry]==0){map[rx][ry]=1;map[rx][ry+1]=0;}else if(map[rx][ry]==2){xy--;map[xx][xy]=2;map[rx][ry+1]=0;map[rx][ry]=1;}}}if(t.equals("s")){if(rx==9){System.out.println("此路不通!!~~~~(>_<)~~~~ ");}else {rx++;if(map[rx][ry]==0){map[rx][ry]=1;map[rx-1][ry]=0;}else if(map[rx][ry]==2){xx++;map[xx][xy]=2;map[rx-1][ry]=0;map[rx][ry]=1;}}}if(t.equals("w")){if(rx==0){System.out.println("此路不通!!~~~~(>_<)~~~~ ");}else{rx--;if(map[rx][ry]==0){map[rx][ry]=1;map[rx+1][ry]=0;}else if(map[rx][ry]==2){xx--;map[xx][xy]=2;map[rx+1][ry]=0;map[rx][ry]=1;}}}}System.out.println("游戏结束!");}}。

推箱子java代码

import java.util.*;import java.io.*;public class Main{int r;//地图行数int c;//地图列数int begx, begy;//箱子开始坐标int endx, endy;//目标坐标int begsx, begsy;//人开始坐标char map[][];//地图int[] dx ={-1, 1, 0, 0};//人和箱子都有四个方向可移动int[] dy ={0, 0, 1, -1};char[] P ={'N', 'S', 'E', 'W'};//表示箱子向某个方向移动char[] M ={'n', 's', 'e', 'w'};//表示人向某个方向移动Node f=new Node(0,0,0,0,"");Node g=new Node(0,0,0,0,"");node1 F=new node1(0,0,"");node1 G=new node1(0,0,"");int mark[][];//标志数组,表示地图上某一位置mark[i][j]是否访问过。

public Main(char[][] map,int r,int c,int begx,int begy,int endx,int endy,int begsx,int begsy){this.map=map;this.r=r;this.c=c;this.begx=begx;this.begy=begy;this.endx=endx;this.endy=endy;this.begsx=begsx;this.begsy=begsy;mark=new int[r][c];}public boolean ok(int x,int y) {if (x >= 0 && x < r && y >= 0 && y < c) return true;return false;}public boolean SToB(int bx,int by,int ex, int ey) {//人到箱子BFSint[][] Mark1= new int[r][c]; //标志数组,表示地图上某一位置Mark1[i][j]是否访问过。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第一个Java文件:
import java.awt.Color;
import java.awt.HeadlessException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GameFrame extends JFrame {
JPanel zhuobu = new JPanel();
//工人
JLabel worker = null;
//箱子
JLabel box = null;
//目的地
JLabel goal = null;
JLabel[] walls = null;
//设置图片大小
int imgSize = 48;
public void setImgSize(int imgSize){
this.imgSize = imgSize;
}
public GameFrame(String title) throws HeadlessException { super(title);
//构造方法中调用本类的其它方法
this.initContentPane();
}
/**
* 设置内容面板
*/
void initContentPane() {
zhuobu.setBackground(Color.red);
zhuobu.setLayout(null);
//调用父类的属性和方法
super.setContentPane(zhuobu);
}
/**
* 把某个图片以组件的方式加入窗体
* @param imgPath 图片路径
* @param x x
* @param y y
* @param width 宽度
* @param height 高度
* @return 添加完的组件
*/
void addComponent(int tag, String imgPath, int x, int y) { ImageIcon img = new ImageIcon(imgPath);
//创建JLabel并把ImageIcon通过构造方法传参传入//把食物放到盘子里
JLabel componet = new JLabel(img);
//设置盘子在桌布上的位置和大小
componet.setBounds(x, y, imgSize, imgSize);
//把盘子放到桌布上
zhuobu.add(componet);
switch (tag) {
case 1:
box = componet;
break;
case 2:
goal = componet;
break;
case 3:
worker = componet;
break;
}
}
void addWall(String imgPath, int[][] loactions) {
ImageIcon wallImg = new ImageIcon(imgPath);
walls = new JLabel[66 + loactions.length];
for (int i = 0; i < walls.length; i++) {
//创建没每一个围墙,他们使用的是同一个图片
walls[i] = new JLabel(wallImg);
}
for (int i = 0; i < walls.length; i++) {
//创建没每一个围墙,他们使用的是同一个图片
walls[i] = new JLabel(wallImg);
}
int index = 0;
/*分别设置各个图片位置*/
for (int i = 0; i < 14; i++) {
//左边墙
walls[index].setBounds(0, i * imgSize, imgSize, imgSize);
zhuobu.add(walls[index]);
index++;
//右边墙
walls[index].setBounds(20 * imgSize, i * imgSize, imgSize, imgSize);
zhuobu.add(walls[index]);
index++;
}
for (int i = 0; i < 19; i++) {
//上边墙
walls[index].setBounds((i + 1) * imgSize, 0, imgSize, imgSize);
zhuobu.add(walls[index]);
index++;
//下边墙
walls[index].setBounds((i + 1) * imgSize, 13 * imgSize, imgSize, imgSize);
zhuobu.add(walls[index]);
index++;
}
//添加中间障碍耦合解耦
for (int i = 0; i < loactions.length; i++) {
walls[index].setBounds(loactions[i][0]* imgSize, loactions[i][1]* imgSize, imgSize, imgSize);
zhuobu.add(walls[index]);
index++;
}
}
}
第二个Java文件:
public class Run {
public static void main(String[] args) {
GameFrame gameFrame = new GameFrame("推箱子游戏…");
//设置大小
gameFrame.setBounds(100, 50, 21 * 48 + 5, 14 * 48 + 25);
//窗体大小不可变
gameFrame.setResizable(false);
gameFrame.setImgSize(48);
gameFrame.addComponent(3, "workerUp.png", 400, 100);
gameFrame.addComponent(1, "box.png", 160, 60);
gameFrame.addComponent(2, "goal.png", 80, 520);
int[][] wallLocations ={
{4, 5},{5, 5},{6, 5},{7, 5},
{8, 5},{9, 5},{10, 5},{6, 8},
{7, 8},
{8, 8},
{9, 8},
{10, 8},
{11, 5}
};
gameFrame.addWall("wall.png", wallLocations);
gameFrame.setVisible(true);
}
}。

相关文档
最新文档