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

小白的博客

嵌入式爱好者

 
 
 

日志

 
 

推箱子小游戏——新版  

2012-10-10 02:03:25|  分类: JAVA学习笔记 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
五个文件:StartGame.java、GameFrame.java、GameKeyListener.java、Guanka.java、Tool.java

package www.liuhangtian;

public class StartGame {
public static void main(String[] args){

GameFrame gameFrame = new GameFrame("推箱子小游戏第1关");
gameFrame.setSize(14*48, 14*48);
Tool.showCenterFrame(gameFrame);
gameFrame.setVisible(true);
}

}


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{

int dengji = 1;
private static final int SIZE = 48;
private JPanel panel = null;

private JLabel worker = null;
private JLabel[] walls = null;
private JLabel[] boxes = null;
private JLabel[] goals = null;

public GameFrame() throws HeadlessException {
super();
}

public GameFrame(String title) throws HeadlessException {
super(title);
initPanel();
initGuanka(1);
addListener();
}

private void initPanel(){
panel = new JPanel();
panel.setBackground(Color.BLACK);
panel.setLayout(null);
this.add(panel);
}

private void initGuanka(int i){
Guanka guanka = new Guanka();
walls = guanka.setWalls(i, panel);
boxes = guanka.setBoxes(i, panel);
goals = guanka.setGoals(i, panel);
worker = guanka.setWoker(i, panel);
}

/**
* 判断是否撞墙
*/
private boolean intersects(JLabel label , JLabel[] walls){
boolean sign = false;

for (int i = 0; i < walls.length; i++) {
if(walls[i].getBounds().intersects(label.getBounds())){
sign = true;
}
}
return sign;
}

/**
* 判断箱子和箱子是否相撞
* @param i
* @param boxes
* @return
*/
private boolean boxToBoxIntersects(int i , JLabel[] boxes){
boolean sign = false;
for (int j = 0; j < boxes.length; j++) {
if(j != i){
if(boxes[i].getBounds().intersects(boxes[j].getBounds())){
sign = true;
break;
}
}
}
return sign;
}

private void addListener(){
super.addKeyListener(new GameKeyListener(){
@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
int x = 0;
int y = 0;
String img;
img = "workerUp.png";
switch(e.getKeyCode()){

case KeyEvent.VK_LEFT:
x -= SIZE;
img = "workerLeft.png";
break;
case KeyEvent.VK_RIGHT:
x += SIZE;
img = "workerRight.png";
break;
case KeyEvent.VK_UP:
y -= SIZE;
img = "workerUp.png";
break;
case KeyEvent.VK_DOWN:
y += SIZE;
img = "workerDown.png";
break;
}

ImageIcon image = new ImageIcon(img);
worker.setIcon(image);
worker.setLocation(worker.getBounds().x + x, worker.getBounds().y + y);

//工人撞墙
if(intersects(worker, walls)){
worker.setLocation(worker.getBounds().x - x, worker.getBounds().y - y);
}

//工人推箱子
for (int i = 0; i < boxes.length; i++) {
if(boxes[i].getBounds().intersects(worker.getBounds())){
boxes[i].setLocation(boxes[i].getBounds().x + x, boxes[i].getBounds().y + y);
//箱子撞墙或箱子相撞
if(intersects(boxes[i], walls) | boxToBoxIntersects(i, boxes)){
worker.setLocation(worker.getBounds().x - x, worker.getBounds().y - y);
boxes[i].setLocation(boxes[i].getBounds().x - x, boxes[i].getBounds().y - y);
}

break;
}
}


//判断胜利
boolean boxsign = true;
for (int i = 0; i < boxes.length; i++) {
boxsign &= intersects(boxes[i], goals);
}
if(boxsign){
JOptionPane.showMessageDialog(null, "您赢了");
if(dengji == 3){
JOptionPane.showMessageDialog(null, "恭喜,您通关了!");
System.exit(1);
}
if(JOptionPane.showConfirmDialog(null, "您是否想要继续") == 0){
dengji++;
setTitle("推箱子小游戏第"+dengji+"关");
panel.removeAll();
initGuanka(dengji);
panel.repaint();
}
else{
JOptionPane.showMessageDialog(null, "Bybe!");
System.exit(1);
}
}

}

@Override
public void keyReleased(KeyEvent e) {

}
});
}

}


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) {

}
}



package www.liuhangtian;

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

public class Guanka {
public JLabel[] setWalls(int dengji , JPanel panel){

int j = 0;
int wallsCount = 0;
switch (dengji) {
case 1:
wallsCount = 28;
break;
case 2:
wallsCount = 39;
break;
case 3:
wallsCount = 35;
break;

default:
break;
}

ImageIcon wallImg = new ImageIcon("wall.png");
JLabel[] walls = new JLabel[wallsCount];
for (int i = 0; i < walls.length; i++) {
walls[i] = new JLabel(wallImg);
panel.add(walls[i]);
}

switch(dengji){
case 1:
for (int i = 0; i < 3; i++) {
walls[j++].setBounds((5+i)*48, 3*48 , 48, 48);
walls[j++].setBounds((8+i)*48, 5*48 , 48, 48);
walls[j++].setBounds((3+i)*48, 6*48 , 48, 48);
walls[j++].setBounds((8+i)*48, 7*48 , 48, 48);
walls[j++].setBounds((3+i)*48, 8*48 , 48, 48);
walls[j++].setBounds((6+i)*48, 10*48 , 48, 48);
}
for (int i = 0; i < 2; i++) {
walls[j++].setBounds(5*48 , (4+i)*48 , 48, 48);
walls[j++].setBounds(7*48 , (4+i)*48 , 48, 48);
walls[j++].setBounds(6*48 , (8+i)*48 , 48, 48);
walls[j++].setBounds(8*48 , (8+i)*48 , 48, 48);
}
walls[j++].setBounds(3*48 , 7*48 , 48, 48);
walls[j++].setBounds(10*48 , 6*48 , 48, 48);
break;
case 2:
for (int i = 0; i < 3; i++) {
walls[j++].setBounds((4+i)*48, 3*48 , 48, 48);
walls[j++].setBounds((3+i)*48, 7*48 , 48, 48);
walls[j++].setBounds((9+i)*48, 5*48 , 48, 48);
walls[j++].setBounds((7+i)*48, 7*48 , 48, 48);
walls[j++].setBounds((5+i)*48, 11*48 , 48, 48);
walls[j++].setBounds((9+i)*48, 10*48 , 48, 48);
}

for (int i = 0; i < 4; i++) {
walls[j++].setBounds(3*48 , (3+i)*48 , 48, 48);
walls[j++].setBounds(7*48 , (3+i)*48 , 48, 48);
walls[j++].setBounds(11*48, (6+i)*48 , 48, 48);
walls[j++].setBounds(4*48 , (8+i)*48 , 48, 48);
}
for (int i = 0; i < 3; i++) {
walls[j++].setBounds(8*48 , (9+i)*48 , 48, 48);
}

walls[j++].setBounds(5*48 , 8*48 , 48, 48);
walls[j++].setBounds(9*48 , 6*48 , 48, 48);

break;

case 3:
for (int i = 0; i < 7; i++) {
walls[j++].setBounds((4+i)*48 , 3*48 , 48, 48);
walls[j++].setBounds((5+i)*48 , 9*48 , 48, 48);
}
for (int i = 0; i < 4; i++) {
walls[j++].setBounds(3*48 , (5+i)*48 , 48, 48);
walls[j++].setBounds(12*48 , (4+i)*48 , 48, 48);
}
for (int i = 0; i < 2; i++) {
walls[j++].setBounds(4*48 , (4+i)*48 , 48, 48);
walls[j++].setBounds(4*48 , (8+i)*48 , 48, 48);
walls[j++].setBounds(7*48 , (7+i)*48 , 48, 48);
walls[j++].setBounds(11*48 , (7+i)*48 , 48, 48);
}
for (int i = 0; i < 2; i++) {
walls[j++].setBounds((10+i)*48 , 4*48 , 48, 48);
}
for (int i = 0; i < 3; i++) {
walls[j++].setBounds((6+i)*48 , 5*48 , 48, 48);
}
break;
default:
break;
}
return walls;
}

public JLabel[] setGoals(int i , JPanel panel){
int length = 4;
switch (i) {
case 1:
length = 4;
break;
case 2:
length = 3;
break;
case 3:
length = 4;
break;

default:
break;
}
ImageIcon goalsImg = new ImageIcon("goal.png");
JLabel[] goals = new JLabel[length];
for (int j = 0; j < goals.length; j++) {
goals[j] = new JLabel(goalsImg);
panel.add(goals[j]);
}
switch (i) {
case 1:
goals[0].setBounds(4*48, 7*48, 48, 48);
goals[1].setBounds(6*48, 4*48, 48, 48);
goals[2].setBounds(9*48, 6*48, 48, 48);
goals[3].setBounds(7*48, 9*48, 48, 48);

break;
case 2:
goals[0].setBounds(10*48, 6*48 , 48, 48);
goals[1].setBounds(10*48, 7*48 , 48, 48);
goals[2].setBounds(10*48, 8*48 , 48, 48);

break;
case 3:
goals[0].setBounds(5*48, 7*48 , 48, 48);
goals[1].setBounds(6*48, 7*48 , 48, 48);
goals[2].setBounds(5*48, 8*48 , 48, 48);
goals[3].setBounds(6*48, 8*48 , 48, 48);

break;

default:
break;
}
return goals;
}
public JLabel[] setBoxes(int i , JPanel panel){
int length = 4;
switch (i) {
case 1:
length = 4;
break;
case 2:
length = 3;
break;
case 3:
length = 4;
break;

default:
break;
}
ImageIcon boxImg = new ImageIcon("box.png");
JLabel[] boxes = new JLabel[length];
for (int j = 0; j < boxes.length; j++) {
boxes[j] = new JLabel(boxImg);
panel.add(boxes[j]);
}
switch (i) {
case 1:
boxes[0].setBounds(6*48, 6*48, 48, 48);
boxes[1].setBounds(6*48, 7*48, 48, 48);
boxes[2].setBounds(7*48, 8*48, 48, 48);
boxes[3].setBounds(8*48, 6*48, 48, 48);
break;
case 2:
boxes[0].setBounds(5*48, 5*48 , 48, 48);
boxes[1].setBounds(5*48, 6*48 , 48, 48);
boxes[2].setBounds(6*48, 7*48 , 48, 48);
break;
case 3:
boxes[0].setBounds(5*48 , 5*48 , 48, 48);
boxes[1].setBounds(7*48 , 6*48 , 48, 48);
boxes[2].setBounds(9*48 , 7*48 , 48, 48);
boxes[3].setBounds(10*48, 6*48 , 48, 48);
break;

default:
break;
}
return boxes;
}

public JLabel setWoker(int i , JPanel panel){
ImageIcon workerImg = new ImageIcon("workerUp.png");
JLabel worker = new JLabel(workerImg);
panel.add(worker);
switch (i) {
case 1:
worker.setBounds(7*48, 7*48, 48, 48);
break;
case 2:
worker.setBounds(6*48, 6*48, 48, 48);
break;
case 3:
worker.setBounds(5*48, 6*48, 48, 48);
break;

default:
break;
}

return worker;
}
}


package www.liuhangtian;

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

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



  评论这张
 
阅读(986)| 评论(0)
推荐 转载

历史上的今天

评论

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

页脚

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