一共有四个类推箱子小游戏
package com.lmdxy;
import java.awt.*;
public class Level1 {
private String boxImg;
private int[][] boxLocations;
private String goalImg;
private int[][] goalLocations;
private String wallImg;
private int[][] wallLocations;
private String[] workerImg;
private int[] workerLocations;
private String name;
private Color panelColor;
private int JFx;
private int JFy;
public String getBoxImg() {
return boxImg;
}
public void setBoxImg(String boxImg) {
this.boxImg = boxImg;
}
public int[][] getBoxLocations() {
return boxLocations;
}
public void setBoxLocations(int[][] boxLocations) {
this.boxLocations = boxLocations;
}
public String getGoalImg() {
return goalImg;
}
public void setGoalImg(String goalImg) {
this.goalImg = goalImg;
}
public int[][] getGoalLocations() {
return goalLocations;
}
public void setGoalLocations(int[][] goalLocations) {
this.goalLocations = goalLocations;
}
public String getWallImg() {
return wallImg;
}
public void setWallImg(String wallImg) {
this.wallImg = wallImg;
}
public int[][] getWallLocations() {
return wallLocations;
}
public void setWallLocations(int[][] wallLocations) {
this.wallLocations = wallLocations;
}
public String[] getWorkerImg() {
return workerImg;
}
public void setWorkerImg(String[] workerImg) {
this.workerImg = workerImg;
}
public int[] getWorkerLocations() {
return workerLocations;
}
public void setWorkerLocations(int[] workerLocations) {
this.workerLocations = workerLocations;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Color getPanelColor() {
return panelColor;
}
public void setPanelColor(Color panelColor) {
this.panelColor = panelColor;
}
public int getJFx() {
return JFx;
}
public void setJFx(int JFx) {
this.JFx = JFx;
}
public int getJFy() {
return JFy;
}
public void setJFy(int JFy) {
this.JFy = JFy;
}
}
package com.lmdxy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class GameFrame1 extends JFrame {
private JLabel[] box;
private JLabel[] goal;
private JLabel[] wall;
private JLabel worker;
private JPanel panel;
private int nowIndex = 1;
private String[] workerImgPath;
public GameFrame1(Level1 level1){
super.setSize(level1.getJFx() * 48 + 10, level1.getJFy() * 48 + 32);
super.setResizable(false);
super.setLocationRelativeTo(null);
iniPanel();
}
public void JFsize(Level1 level1){
super.setSize(level1.getJFx() * 48 + 10, level1.getJFy() * 48 + 32);
super.setLocationRelativeTo(null);
}
public void iniPanel(){
this.panel = new JPanel();
this.panel.setLayout(null);
super.setContentPane(this.panel);
}
public JLabel[] initLocations(String imgPath, int[][] locations){
ImageIcon icon = new ImageIcon(imgPath);
JLabel[] labels = new JLabel[locations.length];
for (int i = 0; i < labels.length; i++){
labels[i] = new JLabel(icon);
this.panel.add(labels[i]);
labels[i].setBounds(locations[i][0] * 48, locations[i][1] * 48, 48, 48);
}
return labels;
}
public JLabel initLocations(String imgPath, int[] locations){
ImageIcon icon = new ImageIcon(imgPath);
JLabel label = new JLabel(icon);
this.panel.add(label);
label.setBounds(locations[0] * 48, locations[1] * 48, 48, 48);
return label;
}
public JLabel[] wallLocations(Level1 level1, String imgPath, int[][] locations){
ImageIcon icon = new ImageIcon(imgPath);
JLabel[] labels = new JLabel[locations.length + level1.getJFx() * 2 + (level1.getJFy() - 1) * 2];
for (int i = 0; i < labels.length; i++) {
labels[i] = new JLabel(icon);
}
int index = 0;
for (int i = 0; i < level1.getJFx(); i++) {
this.panel.add(labels[index]);
labels[index++].setBounds(i * 48, 0, 48, 48);
this.panel.add(labels[index]);
labels[index++].setBounds(i * 48, (level1.getJFy() - 1) * 48, 48, 48);
}
for (int i = 1; i < level1.getJFy(); i++) {
this.panel.add(labels[index]);
labels[index++].setBounds(0, i * 48, 48, 48);
this.panel.add(labels[index]);
labels[index++].setBounds((level1.getJFx() - 1) * 48, i * 48, 48, 48);
}
for (int i = 0; i < locations.length; i++) {
this.panel.add(labels[index]);
labels[index++].setBounds(locations[i][0] * 48, locations[i][1] * 48, 48, 48);
}
return labels;
}
public void levelData(Level1 level1){
panel.removeAll();
super.setTitle("推箱子:" + "第" + nowIndex + "关" + level1.getName());
this.box = initLocations(level1.getBoxImg(), level1.getBoxLocations());
this.goal = initLocations(level1.getGoalImg(), level1.getGoalLocations());
this.wall = wallLocations(level1, level1.getWallImg(), level1.getWallLocations());
this.workerImgPath = level1.getWorkerImg();
this.worker = initLocations(level1.getWorkerImg()[0], level1.getWorkerLocations());
panel.setBackground(level1.getPanelColor());
panel.repaint();
}
public void addEvent(Level1 level1){
super.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
int wokPath = 0;
int x = 0;
int y = 0;
if(keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT){
x = -48;
}if(keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_UP){
y = -48;
wokPath = 1;
}if(keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT){
x = 48;
wokPath = 2;
}if(keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN){
y = 48;
wokPath = 3;
}if(keyCode == KeyEvent.VK_SPACE){
for (int i = 0; i < box.length; i++)
box[i].setBounds(goal[i].getBounds().x, goal[i].getBounds().y, 48, 48);
}
ImageIcon icon = new ImageIcon(workerImgPath[wokPath]);
worker.setIcon(icon);
worker.setBounds(worker.getBounds().x + x, worker.getBounds().y + y, 48, 48);
for (int i = 0; i < wall.length; i++) {
if(worker.getBounds().contains((wall[i].getBounds()))){
worker.setBounds(worker.getBounds().x - x, worker.getBounds().y - y, 48, 48);
}
}
for (int i = 0; i < box.length; i++) {
if (box[i].getBounds().contains(worker.getBounds())) {
box[i].setBounds(box[i].getBounds().x + x, box[i].getBounds().y + y, 48, 48);
}
for (int j = 0; j < box.length; j++) {
if (j == i) continue;
if (box[i].getBounds().contains(box[j].getBounds())) {
box[i].setBounds(box[i].getBounds().x - x, box[i].getBounds().y - y, 48, 48);
worker.setBounds(worker.getBounds().x - x, worker.getBounds().y - y, 48, 48);
}
}
}
for (int i = 0; i < wall.length; i++) {
for (int j = 0; j < box.length; j++) {
if(box[j].getBounds().contains(wall[i].getBounds())){
box[j].setBounds(box[j].getBounds().x - x, box[j].getBounds().y - y, 48, 48);
worker.setBounds(worker.getBounds().x - x, worker.getBounds().y - y, 48, 48);
}
}
}
int count = 0;
for (int i = 0; i < box.length; i++) {
for (int j = 0; j < goal.length; j++) {
if (box[i].getBounds().contains(goal[j].getBounds())){
count++;
break;
}
}
}
if (count == goal.length){
Level1 level11 = LevelManger1.level1Manger(nowIndex +1);
if (level11 == null){
JOptionPane.showMessageDialog(null, "恭喜过关");
System.exit(0);
} else {
int reault = JOptionPane.showConfirmDialog(null,
"恭喜第" + nowIndex + "关通过。是否进入下一关?",
"请选择:",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (reault == JOptionPane.YES_OPTION){
nowIndex++;
JFsize(level11);
levelData(level11);
}
if (reault == JOptionPane.NO_OPTION){
JOptionPane.showMessageDialog(null,"欢迎下次挑战");
System.exit(0);
}
}
}
}
});
}
}
package com.lmdxy;
import java.awt.*;
public class LevelManger1 {
public static Level1 level1Manger(int index) {
Level1 level1 = new Level1();
if (index == 1) {
level1.setJFx(14);
level1.setJFy(14);
level1.setBoxImg("Txz_01/src/imgs/box2.png");
level1.setBoxLocations(new int[][]{{3, 3}, {4, 4}});
level1.setGoalImg("Txz_01/src/imgs/goal2.png");
level1.setGoalLocations(new int[][]{{5, 5}, {6, 6}});
level1.setWallImg("Txz_01/src/imgs/wall2.png");
level1.setWallLocations(new int[][]{{7, 7}, {8, 8}});
level1.setWorkerImg(new String[]{
"Txz_01/src/imgs/workerLeft2.png",
"Txz_01/src/imgs/workerUp2.png",
"Txz_01/src/imgs/workerRight2.png",
"Txz_01/src/imgs/workerDown2.png"
});
level1.setWorkerLocations(new int[]{9, 9});
level1.setName("一级");
level1.setPanelColor(Color.GREEN);
return level1;
} else if (index == 2) {
level1.setJFx(10);
level1.setJFy(10);
level1.setBoxImg("Txz_01/src/imgs/box2.png");
level1.setBoxLocations(new int[][]{{3, 3}, {4, 4}});
level1.setGoalImg("Txz_01/src/imgs/goal2.png");
level1.setGoalLocations(new int[][]{{5, 5}, {6, 6}});
level1.setWallImg("Txz_01/src/imgs/wall2.png");
level1.setWallLocations(new int[][]{{7, 7}, {8, 8}});
level1.setWorkerImg(new String[]{
"Txz_01/src/imgs/workerLeft2.png",
"Txz_01/src/imgs/workerUp2.png",
"Txz_01/src/imgs/workerRight2.png",
"Txz_01/src/imgs/workerDown2.png"
});
level1.setWorkerLocations(new int[]{2, 2});
level1.setName("二级");
level1.setPanelColor(Color.GRAY);
return level1;
} else {
return null;
}
}
}
package com.lmdxy;
public class Run1 {
public static void main(String[] args) {
Level1 level1 = new Level1();
level1 = LevelManger1.level1Manger(1);
GameFrame1 gameFrame1 = new GameFrame1(level1);
gameFrame1.levelData(level1);
gameFrame1.setVisible(true);
gameFrame1.addEvent(level1);
}
}