第一范文网 - 专业文章范例文档资料分享平台

java推箱子游戏源代码 

来源:用户分享 时间:2025/8/8 20:51:02 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

第一个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, \gameFrame.addComponent(1, \gameFrame.addComponent(2, \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(\gameFrame.setVisible(true);

搜索更多关于: java推箱子游戏源代码  的文档
java推箱子游戏源代码 .doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c33kjm9c4ji06i7k4fy1s_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top