这个怎么解决啊 弄了两天啦小白一个 ,做了一个推箱子的做了一半出错做不下去了,求助
public class MainFrame extends Frame implements KeyListener {
public MainFrame() {
tagetinit(); //笼子
personinit();//人物
Boxinit();//箱子
Treeinit();//树
backgroundInde();//背景初始化
setMainFrameUI();//主窗体
this.addKeyListener(this); //键盘监听器
}
//用二维数组定义障碍
int[][] datas = {
{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,0,0,0,0,0,1},
{1,0,1,1,0,0,0,0,0,1,1,0,0,0,0,1},
{1,0,1,1,0,0,0,0,0,1,1,0,0,0,0,1},
{1,0,1,1,0,1,1,0,0,1,1,0,0,0,0,1},
{1,0,1,1,0,1,1,0,0,1,1,0,0,0,0,1},
{1,0,1,1,0,1,1,0,0,1,0,0,0,0,0,1},
{1,0,1,1,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,1,1,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
int wx;
int wy;
private void Treeinit() {
Icon ic =new ImageIcon("tree.png");
//二维数组的遍历
for(int i=0;i<datas.length;i++) {
for(int j=0;j<datas[i].length;j++) {
if(datas[i][j] ==1) {
JLabel lab_tree=new JLabel(ic);
lab_tree.setBounds(12+50*j, 36+50*i, 50, 50);
this.add(lab_tree);
}
}
}
}
//笼子
private void tagetinit() {
Icon i = new ImageIcon("lz.png");
JLabel lab_lz =new JLabel(i);
this.add(lab_lz);
lab_lz.setBounds(612, 436, 50, 50);
JLabel lab_lz2 =new JLabel(i);
lab_lz2.setBounds(612, 486, 50, 50);
this.add(lab_lz2);
JLabel lab_lz3 =new JLabel(i);
lab_lz3.setBounds(612, 386, 50, 50);
this.add(lab_lz3);
}
//背景
private void backgroundInde() {
Icon i =new ImageIcon("bg1.jpg");
JLabel lag_bg = new JLabel(i);
//设置位置与大小
lag_bg.setBounds(12,36,800,600);
this.add(lag_bg);
}
//窗口
private void setMainFrameUI() {
this.setLayout(null);// 设置自由布局
this.setTitle("推箱子1.1");//设置窗口名称
this.setLocation(300, 400); //窗口的位置
this.setSize(850, 650); //窗口大小
this.setVisible(true); //窗口可见
}
// 人物
private void personinit() {
int wx=1;
int wy=8;
Icon i = new ImageIcon("htl-zm.png");
lab_person = new JLabel(i);
lab_person.setBounds(12+wx*50,36+wy*50,50,50 ); //设置位置
this.add(lab_person); //添加
}
JLabel lab_person;//本来该法放在方法里面 放在这里让变量的作用范围变大一级
//箱子
private void Boxinit() {
Icon i=new ImageIcon("lyy.png");
JLabel lab_box1= new JLabel(i);
lab_box1.setBounds(212, 236, 50, 50);
this.add(lab_box1);
JLabel lab_box2= new JLabel(i);
lab_box2.setBounds(412, 236, 50, 50);
this.add(lab_box2);
JLabel lab_box3= new JLabel(i);
lab_box3.setBounds(612, 236, 50, 50);
this.add(lab_box3);
}
//键盘监听器
@Override
public void keyPressed(KeyEvent e) {
//左 37 上38 右39 下40
int key = e.getKeyCode(); //用于获取键码值
//右移
if(key == 39) {
if(datas[wy][wx+1] == 1) {
return;
}
wx=wx+1;
int x=(int)lab_person.getLocation().getX();
int y=(int)lab_person.getLocation().getY();
lab_person.setLocation(x+50,y);
Icon i = new ImageIcon("htl-right.png");
lab_person.setIcon(i);
}
//左移
if(key == 37) {
if(datas[wy][wx-1] == 1) {
return;
}
wx=wx-1;
//让人物移动首先要知道人物的位置
int x=(int)lab_person.getLocation().getX(); //得到的是走上叫
int y=(int)lab_person.getLocation().getY();
//确定一步移动多少
//右移x增加 y不变
lab_person.setLocation(x-50,y);
//人物移动会改变方向 也就是改变一张图片
Icon i = new ImageIcon("htl-left.png");
lab_person.setIcon(i);
}
//上移
if(key == 38) {
if(datas[wy-1][wx] == 1) {
return;
}
wy=wy-1;
int x=(int)lab_person.getLocation().getX();
int y=(int)lab_person.getLocation().getY();
lab_person.setLocation(x,y-50);
Icon i = new ImageIcon("htl-back.png");
lab_person.setIcon(i);
}
//下移
if(key == 40) {
if(datas[wy+1][wx]==1) {
return;
}
wy=wy+1;
int x=(int)lab_person.getLocation().getX();
int y=(int)lab_person.getLocation().getY();
lab_person.setLocation(x,y+50);
Icon i = new ImageIcon("htl-zm.png");
lab_person.setIcon(i);
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
for(int i=0;i<datas.length-1 ;i++) { 便利循环的时候 -1 你试试