推箱子3.0小游戏

分为四个板块,关卡数据,关卡数据管理者,GameFrame和运行。

 LevelData.java

package com.lyc.txz32;

public class LevelData {
    public String goalImgpath;
    public int[] goalLocation;
    public String boxImgpath;
    public int[] boxLocation;
    public String wallImgpath;
    public int[][] wallsLocation;
    public String[] workerImgpaths;
    public int[] workerLocation;

    public String getGoalImgpath() {
        return goalImgpath;
    }

    public void setGoalImgpath(String goalImgpath) {
        this.goalImgpath = goalImgpath;
    }

    public int[] getGoalLocation() {
        return goalLocation;
    }

    public void setGoalLocation(int[] goalLocation) {
        this.goalLocation = goalLocation;
    }

    public String getBoxImgpath() {
        return boxImgpath;
    }

    public void setBoxImgpath(String boxImgpath) {
        this.boxImgpath = boxImgpath;
    }

    public int[] getBoxLocation() {
        return boxLocation;
    }

    public void setBoxLocation(int[] boxLocation) {
        this.boxLocation = boxLocation;
    }

    public String getWallImgpath() {
        return wallImgpath;
    }

    public void setWallImgpath(String wallImgpath) {
        this.wallImgpath = wallImgpath;
    }

    public int[][] getWallsLocation() {
        return wallsLocation;
    }

    public void setWallsLocation(int[][] wallsLocation) {
        this.wallsLocation = wallsLocation;
    }

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

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

    public int[] getWorkerLocation() {
        return workerLocation;
    }

    public void setWorkerLocation(int[] workerLocation) {
        this.workerLocation = workerLocation;
    }
}

LevelDataManager.java

package com.lyc.txz32;

public class LevelDataMananger {
    public static LevelData getNowLevelData(int i){
        LevelData levelData = new LevelData();
        if (i==1){
            levelData.setGoalImgpath("goal3.png");
            levelData.setGoalLocation(new int[]{8,9});
            levelData.setBoxImgpath("box3.png");
            levelData.setBoxLocation(new int[]{3,3});
            levelData.setWallImgpath("wall3.png");
            int[][] wallslocations ={
                    {3, 5},
                    {4, 6},
                    {6, 8},
                    {7, 9},
                    {6, 3},
                    {8, 6},
                    {10, 3},
                    {12, 3},
                    {12, 4},
                    {12, 5},
                    {14, 2}
            };
            levelData.setWallsLocation(wallslocations);
            String[] workerImgapths ={"workerLeft3.png","workerRight3.png","workerUp3.png","workerDown3.png"};
            levelData.setWorkerImgpaths(workerImgapths);
            levelData.setWorkerLocation(new int[]{17,6});

        }else if (i==2){

            levelData.setGoalImgpath("goal.png");
            levelData.setGoalLocation(new int[]{8,9});
            levelData.setBoxImgpath("box.png");
            levelData.setBoxLocation(new int[]{3,3});
            levelData.setWallImgpath("wall.png");
            int[][] wallslocations ={
                    {3, 5},
                    {4, 6},
                    {6, 8},
                    {7, 9},
                    {6, 3},
                    {8, 6},
                    {10, 3},
                    {12, 3},
                    {12, 4},
                    {12, 5},
                    {14, 2}
            };
            levelData.setWallsLocation(wallslocations);
            String[] workerImgapths ={"workerLeft.png","workerRight.png","workerUp.png","workerDown.png"};
            levelData.setWorkerImgpaths(workerImgapths);
            levelData.setWorkerLocation(new int[]{17,6});
        }else {
            return null;
        }
        return levelData;
    }
}

GameFrame.java

package com.lyc.txz32;

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 box =null;
    private JLabel worker =null;
    private JLabel goal =null;
    private JLabel[] walls =null;
    private String[] workerImgpaths;
    private int index = 1;//记录现在关卡
    public GameFrame(String title) {
        super(title);
        //设置位置与大小
        setSize(22 * 48 + 10, 12 * 48 + 35);
        //设置窗体大小不可改变
        setResizable(false);
        //设置窗体位置居中
        setLocationRelativeTo(null);
        //关闭窗体时结束进程
        setDefaultCloseOperation(3);
        initPanel();
        addEvent();
    }
    public void LoadLevelData(LevelData levelData){
        //删除内存
        panel.removeAll();
        initWalls(levelData.getWallImgpath(), levelData.getWallsLocation());
        box = initLable(levelData.getBoxImgpath(),levelData.getBoxLocation());
        goal = initLable(levelData.getGoalImgpath(),levelData.getGoalLocation());
        workerImgpaths = levelData.getWorkerImgpaths();
        worker = initLable(workerImgpaths[0], levelData.getWorkerLocation());
        //把界面重画
        panel.repaint();
    }
    private JLabel initLable(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 wallImag = new ImageIcon("imgs/"+wallsImgpath);
        this.walls = new JLabel[22*2+10*2+wallsLocations.length];
        for (int i = 0; i < walls.length; i++) {
            walls[i] = new JLabel(wallImag);
        }
        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++) {
            panel.add(walls[index]);
            int[] location = wallsLocations[i];
            walls[index++].setBounds(location[0]*48,location[1]*48,48,48 );
        }
    }
    private void initPanel(){
        panel = new JPanel();
        panel.setLayout(null);
        panel.setBackground(Color.GRAY);
        super.setContentPane(panel);
    }
    private void addEvent(){
        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
            }
            @Override
            public void keyPressed(KeyEvent e) {
                int keycold = e.getKeyCode();
                String impath = workerImgpaths[0];
                int x =0,y=0;
                if (keycold==KeyEvent.VK_W||keycold==KeyEvent.VK_UP){
                    y = -48;
                    impath = workerImgpaths[2];
                }else if (keycold==KeyEvent.VK_S||keycold==KeyEvent.VK_DOWN){
                    y =48;
                    impath = workerImgpaths[3];
                }else if (keycold==KeyEvent.VK_A||keycold==KeyEvent.VK_LEFT){
                    x = -48;
                }else if (keycold==KeyEvent.VK_D||keycold==KeyEvent.VK_RIGHT){
                    x =48;
                    impath =workerImgpaths[1];
                }
                worker.setBounds(worker.getBounds().x+x,worker.getBounds().y+y,48,48);
                ImageIcon workerImag = new ImageIcon("imgs/"+impath);
                worker.setIcon(workerImag);
                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;
                    }
                }
                if (worker.getBounds().intersects(box.getBounds())){
                    box.setBounds(box.getBounds().x+x,box.getBounds().y+y,48,48);
                }
                for (int i = 0; i < walls.length; i++) {
                    if (box.getBounds().intersects(walls[i].getBounds())){
                        box.setBounds(box.getBounds().x-x,box.getBounds().y-y,48,48);
                        worker.setBounds(worker.getBounds().x-x,worker.getBounds().y-y,48,48);
                        break;
                    }
                }
                if (box.getBounds().intersects(goal.getBounds())){
                    JOptionPane.showMessageDialog(null,"恭喜你,第[" +index+ "]关通过!");
                    index++;
                    LevelData levelData = LevelDataMananger.getNowLevelData(index);
                    if (levelData == null){
                        JOptionPane.showMessageDialog(null,"恭喜过关");
                        System.exit(0);
                    }else {
                        LoadLevelData(levelData);
                    }

                }
            }
            @Override
            public void keyReleased(KeyEvent e) {
            }
        });
    }
}

Run.java

package com.lyc.txz32;

public class Run {
    public static void main(String[] args) {
        GameFrame gameFrame = new GameFrame("推箱子3.0");

        LevelData levelData = LevelDataMananger.getNowLevelData(1);

        gameFrame.LoadLevelData(levelData);

        gameFrame.setVisible(true);

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

diary_chao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值