推箱子小游戏实现源码 -- Java

备注: 这里 wolf 表示推的人物 sheep表示被推的箱子
代码我已经执行玩了好几遍了,哈哈哈哈
图片等相关资源可以留下邮箱,我单独压缩下发给你…

public static void main(String[] args) {
new MainFrame();
}

public class MainFrame extends Frame implements KeyListener {

JLabel lab_wolf;
int wx; // 代表狼横向位置
int wy; // 代表狼纵向位置

int num = 0; // 代表有几只羊进了笼中
int total = 3; // 代表有几只羊

public MainFrame() {
	targetInit();
	wolfInit();
	sheepInit();
	treeInit();
	backGroundInit(); // 最好放在设置窗体的上面
	setMainFrameUI();
	this.addKeyListener(this);
}

JLabel[][] sheeps = new JLabel[12][16];

// 1 表示障碍 0 代表空地
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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 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 } };

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 targetInit() {
	Icon i = new ImageIcon("target.png");
	JLabel lab_target1 = new JLabel(i);
	lab_target1.setBounds(712, 236, 50, 50);
	this.add(lab_target1);
	datas[4][14] = 8;

	JLabel lab_target2 = new JLabel(i);
	lab_target2.setBounds(712, 286, 50, 50);
	this.add(lab_target2);
	datas[5][14] = 8;

	JLabel lab_target3 = new JLabel(i);
	lab_target3.setBounds(712, 336, 50, 50);
	this.add(lab_target3);
	datas[6][14] = 8;

}

private void sheepInit() {
	Icon i = new ImageIcon("sheep-no.png");

	JLabel lab_sheep1 = new JLabel(i);
	lab_sheep1.setBounds(12 + 6 * 50, 36 + 4 * 50, 50, 50);
	this.add(lab_sheep1);
	datas[4][6] = 4;
	sheeps[4][6] = lab_sheep1;

	JLabel lab_sheep2 = new JLabel(i);
	lab_sheep2.setBounds(12 + 6 * 50, 336, 50, 50);
	this.add(lab_sheep2);
	datas[6][6] = 4;
	sheeps[6][6] = lab_sheep2;

	JLabel lab_sheep3 = new JLabel(i);
	lab_sheep3.setBounds(12 + 6 * 50, 436, 50, 50);
	this.add(lab_sheep3);
	datas[8][6] = 4;
	sheeps[8][6] = lab_sheep3;
}

private void wolfInit() {
	wx = 4;
	wy = 5;

	Icon i = new ImageIcon("wolf-zm.png");
	lab_wolf = new JLabel(i);
	lab_wolf.setBounds(12 + 50 * wx, 36 + 50 * wy, 50, 50);
	this.add(lab_wolf);
}

private void backGroundInit() {

	Icon i = new ImageIcon("bg.png");
	JLabel lab_bg = new JLabel(i);
	lab_bg.setBounds(12, 36, 800, 600);
	this.add(lab_bg);

}

private void setMainFrameUI() {
	this.setLayout(null); // 自由布局
	this.setTitle("推箱子 V1.0");
	this.setLocation(110, 30);
	this.setSize(826, 650);
	this.setResizable(false);
	this.setVisible(true);
}

private void victory() {
	if (num == total) {
		System.out.println("恭喜你,过关啦!");
	}
}

public void keyReleased(KeyEvent e) {
	// 37 38 39 40 对应键盘 左 上 右 下 键码值
	int key = e.getKeyCode();
	if (key == 37) {
		if (datas[wy][wx - 1] == 0) {
			wx = wx - 1;
			int x = (int) lab_wolf.getLocation().getX();
			int y = (int) lab_wolf.getLocation().getY();
			lab_wolf.setLocation(x - 50, y);
			Icon i = new ImageIcon("wolf-zuom.png");
			lab_wolf.setIcon(i);
			return;
		}
		if (datas[wy][wx - 1] == 1) {
			return;
		}
		if (datas[wy][wx - 1] == 4 && datas[wy][wx - 2] == 1) {
			return;
		}
		if (datas[wy][wx - 1] == 4 && datas[wy][wx - 2] == 4) {
			return;
		}
		if (datas[wy][wx - 1] == 4 && datas[wy][wx - 2] == 12) {
			return;
		}
		if (datas[wy][wx - 1] == 12 && datas[wy][wx - 2] == 1) {
			return;
		}
		if (datas[wy][wx - 1] == 12 && datas[wy][wx - 2] == 4) {
			return;
		}
		if (datas[wy][wx - 1] == 12 && datas[wy][wx - 2] == 12) {
			return;
		}
		if (datas[wy][wx - 1] == 8) {
			wx = wx - 1;
			int x = (int) lab_wolf.getLocation().getX();
			int y = (int) lab_wolf.getLocation().getY();
			lab_wolf.setLocation(x - 50, y);
			Icon i = new ImageIcon("wolf-zuom.png");
			lab_wolf.setIcon(i);
			return;
		}
		if (datas[wy][wx - 1] == 4 && datas[wy][wx - 2] == 0) {
			datas[wy][wx - 1] = 0;
			datas[wy][wx - 2] = 4;
		}
		if (datas[wy][wx - 1] == 4 && datas[wy][wx - 2] == 8) {
			datas[wy][wx - 1] = 0;
			datas[wy][wx - 2] = 12;
			num++;
		}
		if (datas[wy][wx - 1] == 12 && datas[wy][wx - 2] == 0) {
			datas[wy][wx - 1] = 8;
			datas[wy][wx - 2] = 4;
			num--;
		}
		if (datas[wy][wx - 1] == 12 && datas[wy][wx - 2] == 8) {
			datas[wy][wx - 1] = 8;
			datas[wy][wx - 2] = 12;
		}
		sheeps[wy][wx - 1].setLocation(12 + wx * 50 - 100, 36 + wy * 50);
		sheeps[wy][wx - 2] = sheeps[wy][wx - 1];
		sheeps[wy][wx - 1] = null; // 将原来组件上对象清除
		wx = wx - 1;
		int x = (int) lab_wolf.getLocation().getX();
		int y = (int) lab_wolf.getLocation().getY();
		lab_wolf.setLocation(x - 50, y);
		Icon i = new ImageIcon("wolf-zuom.png");
		lab_wolf.setIcon(i);
		victory();
		return;
	}
	// ------------------------
	if (key == 38) {
		if (datas[wy - 1][wx] == 0) {
			wy = wy - 1;
			int x = (int) lab_wolf.getLocation().getX();
			int y = (int) lab_wolf.getLocation().getY();
			lab_wolf.setLocation(x, y - 50);
			Icon i = new ImageIcon("wolf-sm.png");
			lab_wolf.setIcon(i);
			return;
		}
		if (datas[wy - 1][wx] == 1) {
			return;
		}
		if (datas[wy - 1][wx] == 4 && datas[wy - 2][wx] == 1) {
			return;
		}
		if (datas[wy - 1][wx] == 4 && datas[wy - 2][wx] == 4) {
			return;
		}
		if (datas[wy - 1][wx] == 4 && datas[wy - 2][wx] == 12) {
			return;
		}
		if (datas[wy - 1][wx] == 12 && datas[wy - 2][wx] == 1) {
			return;
		}
		if (datas[wy - 1][wx] == 12 && datas[wy - 2][wx] == 4) {
			return;
		}
		if (datas[wy - 1][wx] == 12 && datas[wy - 2][wx] == 12) {
			return;
		}
		if (datas[wy - 1][wx] == 8) {
			wy = wy - 1;
			int x = (int) lab_wolf.getLocation().getX();
			int y = (int) lab_wolf.getLocation().getY();
			lab_wolf.setLocation(x, y - 50);
			Icon i = new ImageIcon("wolf-sm.png");
			lab_wolf.setIcon(i);
			return;
		}
		if (datas[wy - 1][wx] == 4 && datas[wy - 2][wx] == 0) {
			datas[wy - 1][wx] = 0;
			datas[wy - 2][wx] = 4;
		}
		if (datas[wy - 1][wx] == 4 && datas[wy - 2][wx] == 8) {
			datas[wy - 1][wx] = 0;
			datas[wy - 2][wx] = 12;
			num++;
		}
		if (datas[wy - 1][wx] == 12 && datas[wy - 2][wx] == 0) {
			datas[wy - 1][wx] = 8;
			datas[wy - 2][wx] = 4;
			num--;
		}
		if (datas[wy - 1][wx] == 12 && datas[wy - 2][wx] == 8) {
			datas[wy - 1][wx] = 8;
			datas[wy - 2][wx] = 12;
		}
		sheeps[wy - 1][wx].setLocation(12 + wx * 50, 36 + wy * 50 - 100);
		sheeps[wy - 2][wx] = sheeps[wy - 1][wx];
		sheeps[wy - 1][wx] = null; // 将原来组件上对象清除
		wy = wy - 1;
		int x = (int) lab_wolf.getLocation().getX();
		int y = (int) lab_wolf.getLocation().getY();
		lab_wolf.setLocation(x, y - 50);
		Icon i = new ImageIcon("wolf-sm.png");
		lab_wolf.setIcon(i);
		victory();
		return;
	}
	// ------------------------
	if (key == 39) {
		if (datas[wy][wx + 1] == 0) {
			wx = wx + 1;
			int x = (int) lab_wolf.getLocation().getX();
			int y = (int) lab_wolf.getLocation().getY();
			lab_wolf.setLocation(x + 50, y);
			Icon i = new ImageIcon("wolf-ym.png");
			lab_wolf.setIcon(i);
			return;
		}
		if (datas[wy][wx + 1] == 1) {
			return;
		}
		if (datas[wy][wx + 1] == 4 && datas[wy][wx + 2] == 1) {
			return;
		}
		if (datas[wy][wx + 1] == 4 && datas[wy][wx + 2] == 4) {
			return;
		}
		if (datas[wy][wx + 1] == 4 && datas[wy][wx + 2] == 12) {
			return;
		}
		if (datas[wy][wx + 1] == 12 && datas[wy][wx + 2] == 1) {
			return;
		}
		if (datas[wy][wx + 1] == 12 && datas[wy][wx + 2] == 4) {
			return;
		}
		if (datas[wy][wx + 1] == 12 && datas[wy][wx + 2] == 12) {
			return;
		}
		if (datas[wy][wx + 1] == 8) {
			wx = wx + 1;
			int x = (int) lab_wolf.getLocation().getX();
			int y = (int) lab_wolf.getLocation().getY();
			lab_wolf.setLocation(x + 50, y);
			Icon i = new ImageIcon("wolf-ym.png");
			lab_wolf.setIcon(i);
			return;
		}
		if (datas[wy][wx + 1] == 4 && datas[wy][wx + 2] == 0) {
			datas[wy][wx + 1] = 0;
			datas[wy][wx + 2] = 4;
		}
		if (datas[wy][wx + 1] == 4 && datas[wy][wx + 2] == 8) {
			datas[wy][wx + 1] = 0;
			datas[wy][wx + 2] = 12;
			num++;
		}
		if (datas[wy][wx + 1] == 12 && datas[wy][wx + 2] == 0) {
			datas[wy][wx + 1] = 8;
			datas[wy][wx + 2] = 4;
			num--;
		}
		if (datas[wy][wx + 1] == 12 && datas[wy][wx + 2] == 8) {
			datas[wy][wx + 1] = 8;
			datas[wy][wx + 2] = 12;
		}
		sheeps[wy][wx + 1].setLocation(12 + wx * 50 + 100, 36 + wy * 50);
		sheeps[wy][wx + 2] = sheeps[wy][wx + 1];
		sheeps[wy][wx + 1] = null; // 将原来组件上对象清除
		wx = wx + 1;
		int x = (int) lab_wolf.getLocation().getX();
		int y = (int) lab_wolf.getLocation().getY();
		lab_wolf.setLocation(x + 50, y);
		Icon i = new ImageIcon("wolf-ym.png");
		lab_wolf.setIcon(i);
	}
	// ------------------------
	if (key == 40) {
		if (datas[wy + 1][wx] == 0) {
			wy = wy + 1;
			int x = (int) lab_wolf.getLocation().getX();
			int y = (int) lab_wolf.getLocation().getY();
			lab_wolf.setLocation(x, y + 50);
			Icon i = new ImageIcon("wolf-zm.png");
			lab_wolf.setIcon(i);
			return;
		}
		if (datas[wy + 1][wx] == 1) {
			return;
		}
		if (datas[wy + 1][wx] == 4 && datas[wy + 2][wx] == 1) {
			return;
		}
		if (datas[wy + 1][wx] == 4 && datas[wy + 2][wx] == 4) {
			return;
		}
		if (datas[wy + 1][wx] == 4 && datas[wy + 2][wx] == 12) {
			return;
		}
		if (datas[wy + 1][wx] == 12 && datas[wy + 2][wx] == 1) {
			return;
		}
		if (datas[wy + 1][wx] == 12 && datas[wy + 2][wx] == 4) {
			return;
		}
		if (datas[wy + 1][wx] == 12 && datas[wy + 2][wx] == 12) {
			return;
		}
		if (datas[wy + 1][wx] == 8) {
			wy = wy + 1;
			int x = (int) lab_wolf.getLocation().getX();
			int y = (int) lab_wolf.getLocation().getY();
			lab_wolf.setLocation(x, y + 50);
			Icon i = new ImageIcon("wolf-zm.png");
			lab_wolf.setIcon(i);
			return;
		}
		if (datas[wy + 1][wx] == 4 && datas[wy + 2][wx] == 0) {
			datas[wy + 1][wx] = 0;
			datas[wy + 2][wx] = 4;
		}
		if (datas[wy + 1][wx] == 4 && datas[wy + 2][wx] == 8) {
			datas[wy + 1][wx] = 0;
			datas[wy + 2][wx] = 12;
			num++;
		}
		if (datas[wy + 1][wx] == 12 && datas[wy + 2][wx] == 0) {
			datas[wy + 1][wx] = 8;
			datas[wy + 2][wx] = 4;
			num--;
		}
		if (datas[wy + 1][wx] == 12 && datas[wy + 2][wx] == 8) {
			datas[wy + 1][wx] = 8;
			datas[wy + 2][wx] = 12;
		}
		sheeps[wy + 1][wx].setLocation(12 + wx * 50, 36 + wy * 50 + 100);
		sheeps[wy + 2][wx] = sheeps[wy + 1][wx];
		sheeps[wy + 1][wx] = null; // 将原来组件上对象清除
		wy = wy + 1;
		int x = (int) lab_wolf.getLocation().getX();
		int y = (int) lab_wolf.getLocation().getY();
		lab_wolf.setLocation(x, y + 50);
		Icon i = new ImageIcon("wolf-zm.png");
		lab_wolf.setIcon(i);
		victory();
		return;
	}
}

public void keyTyped(KeyEvent e) {

}

public void keyPressed(KeyEvent e) {

}

}

你可能感兴趣的:(学习日记)