import java.awt.Color;
import java.awt.Panel;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class FrameGame extends JFrame {
JPanel panel = new JPanel();
JLabel worker = new JLabel(new ImageIcon("workerRight.png"));
JLabel box = new JLabel(new ImageIcon("box.png"));
JLabel goal = new JLabel(new ImageIcon("goal.png"));
int SPEED = 12;
int[] pos = new int[] { 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
0, 9, 0, 10, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0,
9, 0, 10, 0, 11, 0, 12, 1, 12, 2, 12, 3, 12, 4, 12, 5, 12, 6, 12,
7, 12, 8, 12, 9, 12, 10, 12, 10, 1, 10, 2, 10, 3, 10, 4, 10, 5, 10,
6, 10, 7, 10, 8, 10, 9, 10, 10, 10, 11, 10, 12, 1, 3, 2, 3, 3, 3,
4, 3, 5, 3, 6, 3, 5, 8, 6, 8, 7, 8, 8, 8, 9, 8, 10, 8, 11, 8 };
JLabel[] walls = new JLabel[pos.length / 2];
public FrameGame() {
this.setTitle("欢迎大家推箱子");
this.setBounds(200, 60, 535, 650);
this.setContentPane(panel);
for (int i = 0; i < walls.length; i++) {
walls[i] = new JLabel(new ImageIcon("wall.png"));
panel.add(walls[i]);
walls[i].setBounds(pos[i * 2] * 48, pos[i * 2 + 1] * 48, 48, 48);
}
panel.setLayout(null);
panel.setBackground(Color.black);
panel.add(worker);
panel.add(box);
panel.add(goal);
box.setBounds(240, 96, 48, 48);
goal.setBounds(336, 432, 48, 48);
worker.setBounds(240, 240, 48, 48);
this.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
int x = 0;
int y = 0;
switch (e.getKeyCode()) {
case 38:
System.out.println("上");
y = -SPEED;
worker.setIcon(new ImageIcon("workerUp.png"));
break;
case 39:
System.out.println("右");
worker.setIcon(new ImageIcon("workerRight.png"));
x = SPEED;
break;
case 40:
System.out.println("下");
worker.setIcon(new ImageIcon("workerDown.png"));
y = SPEED;
break;
case 37:
System.out.println("左");
worker.setIcon(new ImageIcon("workerLeft.png"));
x = -SPEED;
break;
default:
break;
}// switch
worker.setLocation(worker.getX() + x, worker.getY() + y);
for (int i = 0; i < walls.length; i++) {
if (worker.getBounds().intersects(walls[i].getBounds())) {
worker
.setLocation(worker.getX() - x, worker.getY()
- y);
}
}// for
if (worker.getBounds().intersects(box.getBounds())) {
box.setLocation(box.getX() + x, box.getY() + y);
}// if
for (int j = 0; j < walls.length; j++) {
if (box.getBounds().intersects(walls[j].getBounds())) {
box.setLocation(box.getX() - x, box.getY() - y);
worker.setLocation(worker.getX() - x, worker.getY()- y);
}// if
}// for
if (box.getX()==goal.getX() && box.getY()==goal.getY()) {
JOptionPane.showMessageDialog(null, "耶 ");
}//if
}// keyPressed
});// addKeyListener
this.setVisible(true);
}
public static void main(String[] args) {
new FrameGame();
}// main
}// class
没有合适的资源?快使用搜索试试~ 我知道了~
java推箱小程序dfsdsdfgg
共13个文件
png:7个
class:2个
prefs:1个
需积分: 9 7 下载量 27 浏览量
2011-03-11
11:30:42
上传
评论
收藏 22KB RAR 举报
温馨提示
java实现推箱子小程序 this.setTitle("欢迎大家推箱子"); this.setBounds(200, 60, 535, 650); this.setContentPane(panel); for (int i = 0; i < walls.length; i++) { walls[i] = new JLabel(new ImageIcon("wall.png")); panel.add(walls[i]); walls[i].setBounds(pos[i * 2] * 48, pos[i * 2 + 1] * 48, 48, 48);
资源推荐
资源详情
资源评论
收起资源包目录
箱子.rar (13个子文件)
箱子
box.png 1KB
.project 382B
bin
FrameGame$1.class 2KB
FrameGame.class 2KB
workerUp.png 2KB
goal.png 1KB
.settings
org.eclipse.jdt.core.prefs 629B
workerLeft.png 2KB
src
FrameGame.java 3KB
workerRight.png 2KB
wall.png 5KB
workerDown.png 2KB
.classpath 301B
共 13 条
- 1
资源评论
chenhj_1
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 系统源码A057-基于SpringBoot的失物招领平台的设计与实现
- 爬虫专栏第二篇:Requests 库实战:从基础 GET 到 POST 登录全攻略
- gvim 配置 使用 代码 详细讲解
- C#ASP.NET三层OA管理系统源码数据库 SQL2008源码类型 WebForm
- springboot排课系统(附源码+数据库)23791
- 机器学习的概要介绍与分析
- 基于web的海贼王动漫介绍 html+css网页设计6页+设计文档.zip
- 微信 支付宝简单制作页面
- 农业设备租赁系统基于 Vue 和 SpringBoot:助力农业现代化进程
- springboot老年人出行应用程序(附源码+数据库)23786
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功