首页>代码>Java swing小游戏:推箱子>/推箱子最终/src/cn/itcast/txz/ui/MainFrame.java
package cn.itcast.txz.ui;

import java.awt.Container;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class MainFrame extends JFrame implements KeyListener{
	private Container panel;
	private JLabel player;
	//人物所处行
	private int playerX;
	//人物所处列
	private int playerY;
	//任务完成计数器
	int count = 0;
	//任务完成总数
	int total = 3;
	public MainFrame(){
		//主窗体UI初始化
		this.mainFrameBaseUI();
		//UI数据初始化
		this.dateInit();
		//UI初始化
		this.dateUI();
		//背景地板UI初始化
		this.backgroundUI();
		//窗体添加键盘监听
		this.addKeyListener(this);
		this.setVisible(true);
	}
	private JLabel[][] uis = new JLabel[12][16];
	private void dateUI() {
		//墙初始化
		for (int i = 0; i < dates.length; i++) {
			for (int j = 0; j < dates[i].length; j++) {
				if(dates[i][j] == 1){
					JLabel temp = new JLabel(new ImageIcon("1.png"));
					temp.setBounds(10+j*50,10+i*50, 50, 50);
					this.panel.add(temp);
				}
			}
		}
		//人物初始位置第3行,第3列(存在第0行第0列)
		this.playerX = 3;
		this.playerY = 3;
		player = new JLabel(new ImageIcon("-10.png"));
		player.setBounds(10+playerX*50,10+playerY*50, 50, 50);
		this.panel.add(player);
		
		
		//目标初始化
		//与total有关
		JLabel tar1 = new JLabel(new ImageIcon("8.png"));
		tar1.setBounds(10+14*50,10+5*50, 50, 50);
		this.panel.add(tar1);
		dates[5][14] = LogicHelper.TARGET;
		uis[5][14] = tar1;
		JLabel tar2 = new JLabel(new ImageIcon("8.png"));
		tar2.setBounds(10+14*50,10+6*50, 50, 50);
		this.panel.add(tar2);
		dates[6][14] = LogicHelper.TARGET;
		uis[6][14] = tar2;
		JLabel tar3 = new JLabel(new ImageIcon("8.png"));
		tar3.setBounds(10+14*50,10+7*50, 50, 50);
		this.panel.add(tar3);
		dates[7][14] = LogicHelper.TARGET;
		uis[7][14] = tar3;
		
		
		//箱子初始化
		//与total有关
		JLabel box1 = new JLabel(new ImageIcon("4.png"));
		box1.setBounds(10+7*50,10+3*50, 50, 50);
		this.panel.add(box1);
		dates[3][7] = LogicHelper.BOX;
		uis[3][7] = box1;
		JLabel box2 = new JLabel(new ImageIcon("4.png"));
		box2.setBounds(10+7*50,10+5*50, 50, 50);
		this.panel.add(box2);
		dates[5][7] = LogicHelper.BOX;
		uis[5][7] = box2;
		JLabel box3 = new JLabel(new ImageIcon("4.png"));
		box3.setBounds(10+7*50,10+7*50, 50, 50);
		this.panel.add(box3);
		dates[7][7] = LogicHelper.BOX;
		uis[7][7] = box3;
		
	}
	private int[][] dates = new int[12][16];
	private void dateInit() {
		dates = new int[][]{
				{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,0,1,1,1,0,1,1,1,1,1,0,0,1},
				{1,0,1,0,0,0,1,0,0,0,0,0,1,0,0,1},
				{1,0,1,0,1,1,1,0,0,0,0,0,1,0,0,1},
				{1,0,1,0,1,0,0,0,0,1,1,1,1,0,0,1},
				{1,0,1,0,1,1,1,0,0,0,0,0,1,0,0,1},
				{1,0,1,0,0,0,0,0,0,0,0,0,1,0,0,1},
				{1,0,1,0,0,0,0,0,1,1,1,1,1,0,0,1},
				{1,0,1,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 backgroundUI() {
		//背景图片信息设置
		JLabel floor  = new JLabel(new ImageIcon("floor.png"));
		floor.setBounds(10, 10, 800, 600);
		this.panel.add(floor);
	}

	private void mainFrameBaseUI() {
		this.panel = this.getContentPane();
		this.setSize(826, 650);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.panel.setLayout(null);
		this.setTitle("推箱子v1.0 ");
		this.setResizable(false);
		this.getContentPane().setLayout(null);
	}
	//移动控制,基于原始位置进行移动,第2参数表示垂直方向移动,即行移动,第3参数表示水平方向移动,即列移动
	private static void move(JLabel c,int vMoveType,int hMoveType){
		c.setLocation(c.getX()-hMoveType*50, c.getY()-vMoveType*50);
	}
	
	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();
		int x = keyCode%2 == 0?39-keyCode:0;
		int y = keyCode%2 == 1?38-keyCode:0;
		
		//碰墙
		if(keyCode >= 37 && keyCode <= 40){
			//人物图片切换
			//上10
			//下-10
			//左01
			//右0-1
			player.setIcon(new ImageIcon(x+""+y+".png"));
			//碰地板检测,碰目标检测
			if(dates[playerX-x][playerY-y] == LogicHelper.FLOOR || dates[playerX-x][playerY-y] == LogicHelper.TARGET){
				move(player,x,y);
				playerX = playerX-x;
				playerY = playerY-y;
				return;
			}
			//碰箱子检测
			if(dates[playerX-x][playerY-y] == LogicHelper.BOX || dates[playerX-x][playerY-y] == LogicHelper.BOX_AND_TARGET){
				//同方向是否为空地板或目标位置
				//获取前方移动障碍物码
				int code1 = dates[playerX-x][playerY-y];
				int code2 = dates[playerX-x*2][playerY-y*2];
				if(code2 == LogicHelper.FLOOR || code2 == LogicHelper.TARGET){
					//箱子移动
					JLabel box = uis[playerX-x][playerY-y];
					move(box,x,y);
					//uis模型更新
					uis[playerX-x*2][playerY-y*2] = uis[playerX-x][playerY-y];
					uis[playerX-x][playerY-y] = null;

					//dates模型更新
					//从箱子到空地
					if(code1 == LogicHelper.BOX && code2 == LogicHelper.FLOOR ){
						dates[playerX-x*2][playerY-y*2] = LogicHelper.BOX;
						dates[playerX-x][playerY-y] = LogicHelper.FLOOR;
					}
					//从箱子到目标
					if(code1 == LogicHelper.BOX && code2 == LogicHelper.TARGET){
						dates[playerX-x*2][playerY-y*2] = LogicHelper.BOX_AND_TARGET;
						dates[playerX-x][playerY-y] = LogicHelper.FLOOR;
						//改变箱子模型
						box.setIcon(new ImageIcon("40.png"));
						count++;
					}
					//从箱子目标到空地
					if(code1 == LogicHelper.BOX_AND_TARGET && code2 == LogicHelper.FLOOR ){
						dates[playerX-x*2][playerY-y*2] = LogicHelper.BOX;
						dates[playerX-x][playerY-y] = LogicHelper.TARGET;
						//改变箱子模型
						box.setIcon(new ImageIcon("4.png"));
						count--;
					}
					//从箱子目标到目标
					if(code1 == LogicHelper.BOX_AND_TARGET && code2 == LogicHelper.TARGET){
						dates[playerX-x*2][playerY-y*2] = LogicHelper.BOX_AND_TARGET;
						dates[playerX-x][playerY-y] = LogicHelper.TARGET;
						//改变箱子模型
						box.setIcon(new ImageIcon("40.png"));
					}
				
					//人物模型移动
					move(player,x,y);
					//人物坐标更新
					playerX = playerX-x;
					playerY = playerY-y;
					
					victory();
				}
				return;
			}
		}
	}
	//胜利判定
	private void victory() {
		if(count == total){
			//移除窗体键盘事件,避免用户多余操作
			this.removeKeyListener(this);
			JDialog victory = new JDialog(this,"恭喜你取得了胜利!",true);
			victory.setSize(400, 300);
			victory.setLocationRelativeTo(null);
			victory.setLayout(null);
			
			JLabel info = new JLabel(new ImageIcon("gg.jpg"));
			info.setBounds(2, 2, 380, 180);
			victory.add(info);
			
			JLabel click = new JLabel(new ImageIcon("gg2.png"));
			click.setBounds(2, 182, 380, 88);
			click.addMouseListener(new MouseAdapter(){
				public void mousePressed(MouseEvent e) {
					try {
						Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler https://www.baidu.com");
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			});
			victory.add(click);
			victory.setVisible(true);
		}
	}
	
	public void keyReleased(KeyEvent e) {
	}

	public void keyTyped(KeyEvent e) {
	}
}
最近下载更多
张德志  LV9 2023年12月27日
求学的熊猫  LV11 2023年12月25日
lili010126  LV8 2023年6月16日
huangzy  LV12 2023年4月6日
一头土猪  LV7 2021年12月22日
chen_jw  LV10 2021年12月13日
HUHUHUHU呼晗  LV2 2021年10月6日
你们的代码都是我的了  LV16 2021年9月18日
123aa21a  LV5 2021年6月23日
zero1234  LV1 2020年6月13日
最近浏览更多
本才子不才  LV1 11月11日
qqqww11  LV2 6月26日
鬼屋报道  LV3 6月4日
kuknkk  LV1 2023年12月27日
张德志  LV9 2023年12月27日
求学的熊猫  LV11 2023年12月25日
bangyiyang  LV2 2023年12月21日
微信网友_6699076084797440  LV7 2023年10月30日
lili010126  LV8 2023年6月16日
sdhfkh  LV4 2023年6月13日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友