推箱子4.0

推箱子4.0,第1张

推箱子4.0

Gameframe.java

package TXZ4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Gameframe extends Jframe {
    private JPanel panel = null;
    private JLabel[] goals = null;
    private JLabel[] boxs = null;
    private JLabel[] walls = null;
    private JLabel worker = null;
    private int index = 1;
    private String[] workerImgpaths = null;
    Gameframe(String title){
        super(title);
        setSize(22*48+15,12*48+35);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(3);
        initPanel();
        addEvent();
    }
    public void LoadLevelData(LevelData levelData) {
        panel.removeAll();
        initWalls(levelData.getWallsImgpaths(), levelData.getWallsLocations());
        workerImgpaths = levelData.getWorkerImgpaths();
        worker = initLabel(workerImgpaths[0], levelData.getWorkerLocations());
        int[][] boxLocations = levelData.getBoxLocations();
        boxs =new JLabel[boxLocations.length];
        for (int i = 0; i < boxLocations.length; i++) {
            boxs[i] = initLabel(levelData.getBoxImgpaths(),boxLocations[i]);
        }
        int[][] goalLocations = levelData.getGoalLocations();
        goals = new JLabel[goalLocations.length];
        for (int i = 0; i < goalLocations.length; i++) {
            goals[i] = initLabel(levelData.getGoalImgpaths(),goalLocations[i]);
        }
        panel.repaint();
    }
    private void initPanel(){
        panel = new JPanel();
        panel.setBackground(Color.GRAY);
        panel.setLayout(null);
        setContentPane(panel);
    }
    private JLabel initLabel(String impath,int[] location){
        ImageIcon img = new ImageIcon("imgs/"+impath);
        JLabel label = new JLabel(img);
        panel.add(label);
        label.setBounds(location[0]*48,location[1]*48,48,48);
        return label;
    }
    private void initWalls(String wallsImgPath, int[][] wallsLocations) {
        ImageIcon wallImg = new ImageIcon("imgs/" + wallsImgPath);
        walls = new JLabel[22 * 2 + (12 - 2) * 2 + wallsLocations.length];
        for (int i = 0; i < walls.length; i++) {
            walls[i] = new JLabel(wallImg);
        }
        int index = 0;
        for (int i = 0; i < 22; i++) {
            panel.add(walls[index]);
            walls[index++].setBounds(i * 48, 0, 48, 48);

            panel.add(walls[index]);
            walls[index++].setBounds(i * 48, 11 * 48, 48, 48);
        }

        for (int i = 1; i <= 10; i++) {
            panel.add(walls[index]);
            walls[index++].setBounds(0, i * 48, 48, 48);

            panel.add(walls[index]);
            walls[index++].setBounds(21 * 48, i * 48, 48, 48);
        }
        for (int i = 0; i < wallsLocations.length; i++) {
            int[] location = wallsLocations[i];//{6, 3}
            panel.add(walls[index]);
            walls[index++].setBounds(location[0] * 48, location[1] * 48, 48, 48);
        }
    }
    private void addEvent(){
        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                int keycold = e.getKeyCode();
                int x = 0,y = 0;
                int workerImgpathIndex = 0;
                if (keycold==KeyEvent.VK_W||keycold==KeyEvent.VK_UP){
                    y = -48;
                }else if (keycold==KeyEvent.VK_S||keycold==KeyEvent.VK_DOWN){
                    y = 48;
                    workerImgpathIndex = 1;
                }else if (keycold==KeyEvent.VK_A||keycold==KeyEvent.VK_LEFT){
                    x=-48;
                    workerImgpathIndex=2;
                }else if (keycold==KeyEvent.VK_D||keycold==KeyEvent.VK_RIGHT){
                    x = 48;
                    workerImgpathIndex=3;
                }
                ImageIcon workerImg = new ImageIcon("imgs/"+workerImgpaths[workerImgpathIndex]);
                worker.setBounds(worker.getBounds().x+x,worker.getBounds().y+y,48,48);
                worker.setIcon(workerImg);
                for (int i = 0; i < walls.length; i++) {
                    if (worker.getBounds().intersects(walls[i].getBounds())){
                        worker.setBounds(worker.getBounds().x-x,worker.getBounds().y-y,48,48);
                        break;
                    }
                }
                for (int i = 0; i < boxs.length; i++) {
                    if (worker.getBounds().intersects(boxs[i].getBounds())){
                        boxs[i].setBounds(boxs[i].getBounds().x+x,boxs[i].getBounds().y+y,48,48);
                        break;
                    }
                }
                for (int i = 0; i < boxs.length; i++) {
                    for (int j = 0; j < boxs.length; j++) {
                        if (i==j) continue;
                        if (boxs[i].getBounds().intersects(boxs[j].getBounds())){
                            boxs[i].setBounds(boxs[i].getBounds().x-x,boxs[i].getBounds().y-y,48,48);
                            worker.setBounds(worker.getBounds().x-x,worker.getBounds().y-y,48,48);
                            break;
                        }
                    }
                }
                for (int i = 0; i < walls.length ; i++) {
                    for (int j = 0; j < boxs.length; j++) {
                        if (boxs[j].getBounds().intersects(walls[i].getBounds())){
                            boxs[j].setBounds(boxs[j].getBounds().x-x,boxs[j].getBounds().y-y,48,48);
                            worker.setBounds(worker.getBounds().x-x,worker.getBounds().y-y,48,48);
                            break;
                        }
                    }
                }
                int count = 0;
                for (int i = 0; i < boxs.length; i++) {
                    for (int j = 0; j < goals.length; j++) {
                        if (boxs[i].getBounds().intersects(goals[j].getBounds())){
                            count++;
                        }
                    }
                }
                if (count== boxs.length){
                    JOptionPane.showMessageDialog(null,"恭喜通过第【"+index+"】关");
                    index++;
                    LevelData levelData = LevelDataManager.getNowLevelData(index);
                    if (levelData==null){
                        JOptionPane.showMessageDialog(null,"恭喜通关");
                        System.exit(0);
                    }else{
                        LoadLevelData(levelData);
                    }
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });
    }
}

LevelData.java

package TXZ4;

public class LevelData {
    public String goalImgpaths;
    public int[][] goalLocations;
    public String boxImgpaths;
    public int[][] boxLocations;
    public String[] workerImgpaths;
    public int[] workerLocations;
    public String wallsImgpaths;
    public int[][] wallsLocations;

    public String getGoalImgpaths() {
        return goalImgpaths;
    }

    public void setGoalImgpaths(String goalImgpaths) {
        this.goalImgpaths = goalImgpaths;
    }

    public int[][] getGoalLocations() {
        return goalLocations;
    }

    public void setGoalLocations(int[][] goalLocations) {
        this.goalLocations = goalLocations;
    }

    public String getBoxImgpaths() {
        return boxImgpaths;
    }

    public void setBoxImgpaths(String boxImgpaths) {
        this.boxImgpaths = boxImgpaths;
    }

    public int[][] getBoxLocations() {
        return boxLocations;
    }

    public void setBoxLocations(int[][] boxLocations) {
        this.boxLocations = boxLocations;
    }

    public String[] getWorkerImgpaths() {
        return workerImgpaths;
    }

    public void setWorkerImgpaths(String[] workerImgpaths) {
        this.workerImgpaths = workerImgpaths;
    }

    public int[] getWorkerLocations() {
        return workerLocations;
    }

    public void setWorkerLocations(int[] workerLocations) {
        this.workerLocations = workerLocations;
    }

    public String getWallsImgpaths() {
        return wallsImgpaths;
    }

    public void setWallsImgpaths(String wallsImgpaths) {
        this.wallsImgpaths = wallsImgpaths;
    }

    public int[][] getWallsLocations() {
        return wallsLocations;
    }

    public void setWallsLocations(int[][] wallsLocations) {
        this.wallsLocations = wallsLocations;
    }
}

LevelDataManager.java

package TXZ4;

public class LevelDataManager {
    public static LevelData getNowLevelData(int i){
        LevelData levelData =new LevelData();
        if (i==1){
            levelData.setGoalImgpaths("goal3.png");
            int[][] goalLocations = {{8,9}, {17,6}};
            levelData.setGoalLocations(goalLocations);
            levelData.setBoxImgpaths("box3.png");
            int[][] boxLocations = {{3,3}, {9,5}};
            levelData.setBoxLocations(boxLocations);
            String[] workerImgpaths = {"workerUp3.png","workerDown3.png","workerLeft3.png","workerRight3.png"};
            levelData.setWorkerImgpaths(workerImgpaths);
            levelData.setWorkerLocations(new int[]{17,6});
            levelData.setWallsImgpaths("wall3.png");
            int[][] wallsLocations = {
                    {4, 6},
                    {6, 8},
                    {7, 9},
                    {6, 3},
                    {8, 6},
                    {10, 3},
                    {12, 3},
                    {12, 4},
                    {12, 5},
                    {14, 2}
            };
            levelData.setWallsLocations(wallsLocations);
        }else if (i==2){
            levelData.setGoalImgpaths("goal.png");
            int[][] goalLocations = {{8,9}, {17,6}};
            levelData.setGoalLocations(goalLocations);
            levelData.setBoxImgpaths("box.png");
            int[][] boxLocations = {{3,3}, {9,5}};
            levelData.setBoxLocations(boxLocations);
            String[] workerImgpaths = {"workerUp.png","workerDown.png","workerLeft.png","workerRight.png"};
            levelData.setWorkerImgpaths(workerImgpaths);
            levelData.setWorkerLocations(new int[]{17,6});
            levelData.setWallsImgpaths("wall.png");
            int[][] wallsLocations = {
                    {4, 6},
                    {6, 8},
                    {7, 9},
                    {6, 3},
                    {8, 6},
                    {10, 3},
                    {12, 3},
                    {12, 4},
                    {12, 5},
                    {14, 2}
            };
            levelData.setWallsLocations(wallsLocations);
        }else {
            return null;
        }
        return levelData;
    }
}

Run.java

package TXZ4;

public class Run {
    public static void main(String[] args) {
        Gameframe gameframe = new Gameframe("推箱子4.0");
        LevelData levelData =LevelDataManager.getNowLevelData(1);
        gameframe.LoadLevelData(levelData);
        gameframe.setVisible(true);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5672557.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存