注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

小白的博客

嵌入式爱好者

 
 
 

日志

 
 

推箱子 3.0  

2012-10-09 00:17:52|  分类: JAVA学习笔记 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
五个类:GameFrame.java、GameFrame.java、GameKeyListener.java、TextExtends.java、Tool.java

GameFrame.java:
package www.liuhangtian;

import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class GameFrame extends JFrame{

private static final long serialVersionUID = 3817310756931365204L;
private JLabel worker;
private JLabel box;
private JLabel[] walls;
private JLabel goal;
private JPanel panel;
private static final int SIZE = 48;
/**
* 重写构造方法
*/
public GameFrame(String title) throws HeadlessException {
super(title);
this.initPanel();
this.initWall();
this.initWorker();
this.initBox();
this.initGoal();
this.addListener();
}

public GameFrame() throws HeadlessException {
super();
}
/**
* 重写setVisible方法
*/
@Override
public void setVisible(boolean b) {
super.setVisible(b);
}

/**
* 初始化桌布
*/
    private void initPanel(){
    panel = new JPanel();
    panel.setBackground(Color.red);
    panel.setLayout(null);
    this.add(panel);
    }
    
    /**
     * 在桌布上显示墙
     */
    private void initWall(){
   
    int j = 0;
   
    ImageIcon wallImg = new ImageIcon("wall.png");
    walls = new JLabel[88];
    for (int i = 0; i < walls.length; i++) {
walls[i] = new JLabel(wallImg);
panel.add(walls[i]);
}
   
for (int i = 0; i < walls.length; i++) {
//将图片加载到label上
walls[i] = new JLabel(wallImg);
panel.add(walls[i]);
}
for (int i = 0; i < 28; i++) {
walls[j++].setBounds(48*i, 0, 48, 48);
walls[j++].setBounds(48*i, 13*48 , 48, 48);
}
for (int i = 1; i < 13; i++) {
walls[j++].setBounds(0, i*48 , 48, 48);
walls[j++].setBounds(27*48, i*48 , 48, 48);
}

for (int i = 1; i <= 4; i++) {
walls[j++].setBounds(200, (4+i)*48 , 48, 48);
walls[j++].setBounds(400, (4+i)*48 , 48, 48);
}
    }
    
    /**
     * 将图片加载到标签,标签放到桌布上
     */
    private JLabel addcom(int x , int y , String img){
    ImageIcon image = new ImageIcon(img);
    JLabel label = new JLabel(image);
    panel.add(label);
    label.setBounds(x, y, 48, 48);
    return label;
    }
    
    /**
     * 在桌布上显示工人
     */
    private void initWorker(){
   
    worker = addcom(6*48, 6*48, "workerUp.png");
    }
    
    /**
     * 在桌布上显示箱子
     */
    private void initBox(){
   
    box = addcom(4*48, 4*48, "box.png");
    }
    
    /**
     * 在桌布上显示目标球
     */
    private void initGoal(){
   
    goal = addcom(16*48, 4*48, "goal.png");
    }
    
    private void close(){
    //this.setVisible(false);
    //this.close();
    this.dispose();
    }
    
    /**
     * 实现箱子的移动和转向
     */
private void addListener(){
    super.addKeyListener(new GameKeyListener() {
@Override
public void keyReleased(KeyEvent e) {
int x,y;
String img;
x = 0;
y = 0;
img = "workerDown.png";
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT : //向左
x -= SIZE;
img = "workerLeft.png";
break;
case KeyEvent.VK_UP : //向上
y -= SIZE;
img = "workerUp.png";
break;
case KeyEvent.VK_RIGHT : //向右
x += SIZE;
img = "workerRight.png";
break;
case KeyEvent.VK_DOWN : //向下
y += SIZE;
break;
default : //出错
break;
}
worker.setIcon(new ImageIcon(img));
worker.setLocation(worker.getBounds().x + x, worker.getBounds().y + y);
//工人撞墙处理
for (int i = 0; i < walls.length; i++) {
if(walls[i].getBounds().intersects(worker.getBounds())){
worker.setLocation(worker.getBounds().x - x, worker.getBounds().y - y);
}
}
//工人撞目标处理
if(goal.getBounds().intersects(worker.getBounds())){
worker.setLocation(worker.getBounds().x - x, worker.getBounds().y - y);
}
//工人推箱子处理
if(box.getBounds().intersects(worker.getBounds())){
box.setLocation(box.getBounds().x + x, box.getBounds().y + y);
}
//箱子撞墙处理
for (int i = 0; i < walls.length; i++) {
if(walls[i].getBounds().intersects(box.getBounds())){
box.setLocation(box.getBounds().x - x, box.getBounds().y - y);
if(box.getBounds().intersects(worker.getBounds())){
worker.setLocation(worker.getBounds().x - x, worker.getBounds().y - y);
}
}
}
//胜利判断
if(box.getBounds().intersects(goal.getBounds())){
JOptionPane.showMessageDialog(null, "您赢了!");
int result = JOptionPane.showConfirmDialog(null, "是否继续");
if(result==0){
JFrame gameFrame2 = new GameFrame2("推箱子小游戏第二关");
gameFrame2.setSize(28*48+16,14*48+38);
Tool.showCenterFrame(gameFrame2);
gameFrame2.setVisible(true);
close();
System.gc();
}
else{
JOptionPane.showMessageDialog(null, "谢谢使用!");
System.exit(0);
}
}

}
});
    }
}

GameFrame2.java:
package www.liuhangtian;

import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class GameFrame2 extends JFrame{

private static final long serialVersionUID = 3817310756931365204L;
private JLabel worker;
private JLabel box;
private JLabel[] walls;
private JLabel goal;
private JPanel panel;
private static final int SIZE = 48;
/**
* 重写构造方法
*/
public GameFrame2(String title) throws HeadlessException {
super(title);
this.initPanel();
this.initWall();
this.initWorker();
this.initBox();
this.initGoal();
this.addListener();
}

public GameFrame2() throws HeadlessException {
super();
}
/**
* 重写setVisible方法
*/
@Override
public void setVisible(boolean b) {
super.setVisible(b);
}

/**
* 初始化桌布
*/
    private void initPanel(){
    panel = new JPanel();
    panel.setBackground(Color.red);
    panel.setLayout(null);
    this.add(panel);
    }
    
    /**
     * 在桌布上显示墙
     */
    private void initWall(){
   
    int j = 0;
   
    ImageIcon wallImg = new ImageIcon("wall.png");
    walls = new JLabel[88];
    for (int i = 0; i < walls.length; i++) {
walls[i] = new JLabel(wallImg);
panel.add(walls[i]);
}
   
for (int i = 0; i < walls.length; i++) {
//将图片加载到label上
walls[i] = new JLabel(wallImg);
panel.add(walls[i]);
}
for (int i = 0; i < 28; i++) {
walls[j++].setBounds(48*i, 0, 48, 48);
walls[j++].setBounds(48*i, 13*48 , 48, 48);
}
for (int i = 1; i < 13; i++) {
walls[j++].setBounds(0, i*48 , 48, 48);
walls[j++].setBounds(27*48, i*48 , 48, 48);
}

for (int i = 1; i <= 4; i++) {
walls[j++].setBounds(200, (4+i)*48 , 48, 48);
walls[j++].setBounds(400, (4+i)*48 , 48, 48);
}
    }
    
    /**
     * 将图片加载到标签,标签放到桌布上
     */
    private JLabel addcom(int x , int y , String img){
    ImageIcon image = new ImageIcon(img);
    JLabel label = new JLabel(image);
    panel.add(label);
    label.setBounds(x, y, 48, 48);
    return label;
    }
    
    /**
     * 在桌布上显示工人
     */
    private void initWorker(){
   
    worker = addcom(6*48, 6*48, "workerUp.png");
    }
    
    /**
     * 在桌布上显示箱子
     */
    private void initBox(){
   
    box = addcom(4*48, 4*48, "box.png");
    }
    
    /**
     * 在桌布上显示目标球
     */
    private void initGoal(){
   
    goal = addcom(16*48, 4*48, "goal.png");
    }
    
    private void close(){
    this.close();
    this.setVisible(false);
    this.dispose();
    }
    
    /**
     * 实现箱子的移动和转向
     */
private void addListener(){
    super.addKeyListener(new GameKeyListener() {
@Override
public void keyReleased(KeyEvent e) {
int x,y;
String img;
x = 0;
y = 0;
img = "workerDown.png";
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT : //向左
x -= SIZE;
img = "workerLeft.png";
break;
case KeyEvent.VK_UP : //向上
y -= SIZE;
img = "workerUp.png";
break;
case KeyEvent.VK_RIGHT : //向右
x += SIZE;
img = "workerRight.png";
break;
case KeyEvent.VK_DOWN : //向下
y += SIZE;
break;
default : //出错
break;
}
worker.setIcon(new ImageIcon(img));
worker.setLocation(worker.getBounds().x + x, worker.getBounds().y + y);
//工人撞墙处理
for (int i = 0; i < walls.length; i++) {
if(walls[i].getBounds().intersects(worker.getBounds())){
worker.setLocation(worker.getBounds().x - x, worker.getBounds().y - y);
}
}
//工人撞目标处理
if(goal.getBounds().intersects(worker.getBounds())){
worker.setLocation(worker.getBounds().x - x, worker.getBounds().y - y);
}
//工人推箱子处理
if(box.getBounds().intersects(worker.getBounds())){
box.setLocation(box.getBounds().x + x, box.getBounds().y + y);
}
//箱子撞墙处理
for (int i = 0; i < walls.length; i++) {
if(walls[i].getBounds().intersects(box.getBounds())){
box.setLocation(box.getBounds().x - x, box.getBounds().y - y);
if(box.getBounds().intersects(worker.getBounds())){
worker.setLocation(worker.getBounds().x - x, worker.getBounds().y - y);
}
}
}
//胜利判断
if(box.getBounds().intersects(goal.getBounds())){
JOptionPane.showMessageDialog(null, "您赢了!");
int result = JOptionPane.showConfirmDialog(null, "是否继续");
if(result==0){
close();
System.gc();
}
else{
JOptionPane.showMessageDialog(null, "谢谢使用!");
System.exit(0);
}
}

}
});
    }
}

GameKeyListener.java:
package www.liuhangtian;

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

public class GameKeyListener implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
}

@Override
public void keyReleased(KeyEvent e) {

}

}

Tool.java:
package www.liuhangtian;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class Tool {
public static void showCenterFrame(JFrame frame){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if(frameSize.height > screenSize.height){
frameSize.height = screenSize.height;
}
if(frameSize.width > screenSize.width){
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width)/2, 
(screenSize.height - frameSize.height)/2);
}
}


TextExtends.java:
package www.liuhangtian;

public class TextExtends {
public static void main(String[] args){
GameFrame gameFrame = new GameFrame("推箱子小游戏第一关");
gameFrame.setSize(28*48+16,14*48+38);
Tool.showCenterFrame(gameFrame);
gameFrame.setVisible(true);
}
  评论这张
 
阅读(566)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017