推箱子JAVA代码

这篇博客分享了作者参考传智播客推箱子视频并改进的JAVA代码,增加了使用键盘esc键关闭窗口以及ADWS键控制游戏方向的功能。
摘要由CSDN通过智能技术生成

此代码参考传智播客推箱子视频并进行改进 增加esc关闭窗口,用adws或控制方向 的代码

import game.qust.cn.MainFrame;

//启动入口
public class App {
	public static void main(String[] args) {
		new MainFrame();
	}
}

package game.qust.cn;

import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;




public class MainFrame extends Frame implements KeyListener{
	public MainFrame() {
		
		wolfInit();//做人物
		sheepInit();//做箱子
		targetInit();//做笼子
		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,1,0,0,1,1,1,1,0,1,1,1,0,0,1},
			{1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
			{1,0,1,0,0,0,0,0,1,0,0,0,1,0,0,1},
			{1,0,1,0,0,1,1,1,1,0,1,1,1,0,0,1},
			{1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
			{1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,1},
			{1,0,1,0,0,1,1,1,1,0,1,1,1,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}
	};
	int wx ;//代表人物横向位置
	int wy ;//代表人物纵向位置
	//代表当前有多少个箱子移动到了目标中
	int num = 0;
	//代表箱子的总数
	int total = 3;
	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() {
		//用jlabel制作一个笼子
		Icon i=new ImageIcon("target.png");		
		JLabel lab_target1=new JLabel(i);
		lab_target1.setBounds(700+12,200+36,50,50);//设置笼子位置 大小
		this.add(lab_target1);//添加图像到窗口
		datas[4][14] = 8;
		
		JLabel lab_target2=new JLabel(i);
		lab_target2.setBounds(700+12,300+36,50,50);//设置笼子位置 大小
		this.add(lab_target2);//添加图像到窗口
		datas[6][14] = 8;
		
		JLabel lab_target3=new JLabel(i);
		lab_target3.setBounds(700+12,250+36,50,50);//设置笼子位置 大小
		this.add(lab_target3);//添加图像到窗口
		datas[5][14] = 8;
	}

	private void sheepInit() {
		//用jlabel制作一只羊
		Icon i=new ImageIcon("sheep-no.png");
		
		JLabel lab_sheep1=new JLabel(i);
		lab_sheep1.setBounds(512,336,50,50);//设置羊位置 大小
		this.add(lab_sheep1);//添加图像到窗口
		datas[6][10]=4;
		sheeps[6][10]=lab_sheep1;//把这个jlabel组件放入sheeps数组中
		
		JLabel lab_sheep2=new JLabel(i);
		lab_sheep2.setBounds(612,486,50,50);//设置羊位置 大小
		this.add(lab_sheep2);//添加图像到窗口
		datas[9][12]=4;
		sheeps[9][12]=lab_sheep2;//把这个jlabel组件放入sheeps数组中
		
		JLabel lab_sheep3=new JLabel(i);
		lab_sheep3.setBounds(312,186,50,50);//设置羊位置 大小
  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的Java推箱子游戏的源代码实现: ``` import java.util.Scanner; public class BoxPushGame { // 地图的行数和列数 private static final int ROWS = 8; private static final int COLS = 8; // 地图数组 private static char[][] map = new char[ROWS][COLS]; // 玩家的位置 private static int playerRow = 0; private static int playerCol = 0; // 目标位置的数量 private static int targets = 0; // 初始化地图 public static void initMap() { String[] data = { "########", "# #", "# #", "# $ #", "# @ #", "# #", "# #", "########" }; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { map[i][j] = data[i].charAt(j); if (map[i][j] == '@') { playerRow = i; playerCol = j; } else if (map[i][j] == '$') { targets++; } } } } // 输出地图 public static void printMap() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { System.out.print(map[i][j]); } System.out.println(); } } // 移动玩家 public static boolean movePlayer(int row, int col) { if (map[row][col] == '#') { return false; } if (map[row][col] == '$') { int dx = row - playerRow; int dy = col - playerCol; if (map[row+dx][col+dy] == '#' || map[row+dx][col+dy] == '$') { return false; } map[row+dx][col+dy] = '$'; map[row][col] = ' '; targets--; } map[playerRow][playerCol] = ' '; map[row][col] = '@'; playerRow = row; playerCol = col; return true; } // 判断是否完成游戏 public static boolean isGameFinished() { return targets == 0; } // 主函数 public static void main(String[] args) { Scanner scanner = new Scanner(System.in); initMap(); while (true) { printMap(); if (isGameFinished()) { System.out.println("Congratulations! You win!"); break; } System.out.print("Please enter your move (up/down/left/right): "); String input = scanner.nextLine(); int dx = 0; int dy = 0; if (input.equalsIgnoreCase("up")) { dx = -1; } else if (input.equalsIgnoreCase("down")) { dx = 1; } else if (input.equalsIgnoreCase("left")) { dy = -1; } else if (input.equalsIgnoreCase("right")) { dy = 1; } if (dx != 0 || dy != 0) { int newRow = playerRow + dx; int newCol = playerCol + dy; if (movePlayer(newRow, newCol)) { System.out.println("Move successful."); } else { System.out.println("Move failed."); } } } } } ``` 这个程序使用一个二维字符数组来表示地图,其中 '#' 表示墙,' ' 表示空地,'$' 表示目标位置,'@' 表示玩家位置。玩家可以使用上下左右键来移动,如果玩家移动到目标位置上,则该目标位置会被清空。当所有目标位置都被清空后,玩家胜利。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值