魔塔小游戏Java版项目含源代码

         这是一款童年游戏4399网站上的魔塔仿制版,回忆童年,入手开发项目,全方面的代码解析


其他项目:

1.飞机大战Java版(Java+JavaSwing+多线程结构)

2.Java之马里奥游戏

3.大鱼吃小鱼小游戏(Java版含源代码,JavaSwing+多线程+接口)


项目的完整结构图:
魔塔小游戏Java版项目含源代码_第1张图片


游戏部分闯关图:

魔塔小游戏Java版项目含源代码_第2张图片


源代码如下:

怪物数据代码:

import com.mymt.bean.MonsterBean;

import java.util.HashMap;


public class MonsterData {

    public static HashMap<Integer, MonsterBean> monsterMap = new HashMap<>();

    static {
        monsterMap.clear();
        monsterMap.put(40, new MonsterBean(0, 50, 20, 1, 10, 1, "绿头怪"));
        monsterMap.put(41, new MonsterBean(1, 70, 15, 2, 20, 2, "红头怪"));
        monsterMap.put(42, new MonsterBean(2, 10, 20, 5, 30, 3, "小蝙蝠"));
        monsterMap.put(43, new MonsterBean(3, 200, 35, 10, 50, 5, "青头怪"));
        monsterMap.put(44, new MonsterBean(4, 110, 25, 5, 50, 4, "骷髅人"));
        monsterMap.put(45, new MonsterBean(5, 150, 40, 20, 80, 6, "骷髅士兵"));
        monsterMap.put(46, new MonsterBean(6, 200, 35, 45, 130, 10, "兽面人"));
        monsterMap.put(47, new MonsterBean(7, 250, 50, 90, 220, 19, "初级卫兵"));
        monsterMap.put(48, new MonsterBean(8, 150, 65, 30, 10, 8, "大蝙蝠"));
        monsterMap.put(49, new MonsterBean(9, 550, 160, 90, 25, 20, "红蝙蝠"));
        monsterMap.put(50, new MonsterBean(10, 1300, 300, 150, 40, 35, "白衣武士"));
        monsterMap.put(51, new MonsterBean(11, 700, 250, 125, 32, 30, "怪王"));
        monsterMap.put(52, new MonsterBean(12, 500, 400, 260, 47, 45, "红衣法师"));
        monsterMap.put(53, new MonsterBean(13, 15000, 1000, 1000, 100, 100, "红衣魔王"));
        monsterMap.put(54, new MonsterBean(14, 850, 350, 200, 45, 40, "金甲卫士"));
        monsterMap.put(55, new MonsterBean(15, 900, 750, 650, 77, 70, "金甲队长"));
        monsterMap.put(56, new MonsterBean(16, 400, 90, 50, 15, 12, "骷髅队长"));
        monsterMap.put(57, new MonsterBean(17, 1500, 830, 730, 80, 70, "灵法师"));
        monsterMap.put(58, new MonsterBean(18, 1200, 980, 900, 88, 75, "灵武士"));
        monsterMap.put(59, new MonsterBean(19, 30000, 1700, 1500, 250, 220, "冥灵魔王"));
        monsterMap.put(60, new MonsterBean(20, 250, 120, 70, 20, 17, "麻衣法师"));
        monsterMap.put(61, new MonsterBean(21, 2000, 680, 590, 70, 65, "冥战士"));
        monsterMap.put(62, new MonsterBean(22, 2500, 900, 850, 84, 75, "冥队长"));
        monsterMap.put(63, new MonsterBean(23, 125, 50, 25, 10, 7, "初级法师"));
        monsterMap.put(64, new MonsterBean(24, 100, 200, 110, 30, 25, "高级法师"));
        monsterMap.put(65, new MonsterBean(25, 500, 115, 65, 15, 15, "石头怪人"));
        monsterMap.put(66, new MonsterBean(26, 900, 450, 330, 50, 50, "兽面战士"));
        monsterMap.put(67, new MonsterBean(27, 1200, 620, 520, 65, 75, "双手剑士"));
        monsterMap.put(68, new MonsterBean(28, 1250, 500, 400, 55, 55, "冥卫兵"));
        monsterMap.put(69, new MonsterBean(29, 1500, 560, 460, 60, 60, "高级卫兵"));
        monsterMap.put(70, new MonsterBean(30, 3100, 1150, 1050, 92, 80, "影子战士"));
        monsterMap.put(188, new MonsterBean(31, 99999, 5000, 4000, 0, 0, "血影"));
        monsterMap.put(198, new MonsterBean(32, 99999, 9999, 5000, 0, 0, "魔龙"));
    }
}


人物坐标以及参数代码:

import com.mymt.data.ImageData;

import java.awt.image.BufferedImage;
import java.io.Serializable;



public class PlayerBean implements Serializable{

    private int level;      // 等级
    private int hp;         // 生命值
    private int attack;     // 攻击力
    private int defend;     // 防御力
    private int money;      // 金钱
    private int exp;        // 经验
    private int Ykey;       // 黄色钥匙数
    private int Bkey;       // 蓝色钥匙数
    private int Rkey;       // 红色钥匙数

    private int toward;     // 当前朝向 0-左 1-下 2-右 3-上
    private int posX;       // X 坐标值
    private int posY;       // Y 坐标值

    // 构造器 游戏开始时主角的初始属性
    public PlayerBean() {

        this.level = 1;     // 初始等级      1
        this.hp = 1000;     // 初始生命值  1000
        this.attack = 10;   // 初始攻击力    10
        this.defend = 10;   // 初始防御力    10
        this.money = 0;     // 初始金钱      0
        this.exp = 0;       // 初始经验值    0
        this.Ykey = 0;      // 初始黄钥匙数   0
        this.Bkey = 0;      // 初始蓝钥匙数   0
        this.Rkey = 0;      // 初始红钥匙数   0

        this.toward = 1;    // 初始朝向
        this.posX = 5;      // 初始 X坐标
        this.posY = 9;      // 初始 Y坐标


    }

    public void move(int cx, int cy) {
        posX = cx;
        posY = cy;
    }

    public BufferedImage draw() {
        if (toward == 0)
            return ImageData.playerMap.get(-1);
        if (toward == 1)
            return ImageData.playerMap.get(-2);
        if (toward == 2)
            return ImageData.playerMap.get(-3);
        if (toward == 3)
            return ImageData.playerMap.get(-4);
        return null;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getHp() {
        return hp;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public int getAttack() {
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

    public int getDefend() {
        return defend;
    }

    public void setDefend(int defend) {
        this.defend = defend;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public int getExp() {
        return exp;
    }

    public void setExp(int exp) {
        this.exp = exp;
    }

    public int getYkey() {
        return Ykey;
    }

    public void setYkey(int ykey) {
        Ykey = ykey;
    }

    public int getBkey() {
        return Bkey;
    }

    public void setBkey(int bkey) {
        Bkey = bkey;
    }

    public int getRkey() {
        return Rkey;
    }

    public void setRkey(int rkey) {
        Rkey = rkey;
    }

    public int getToward() {
        return toward;
    }

    public void setToward(int toward) {
        this.toward = toward;
    }

    public int getPosX() {
        return posX;
    }

    public void setPosX(int posX) {
        this.posX = posX;
    }

    public int getPosY() {
        return posY;
    }

    public void setPosY(int posY) {
        this.posY = posY;
    }
}


Main类:

import com.mymt.bean.ItemsBean;

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

import static com.mymt.MTGame.*;
import static com.mymt.util.BattleUtil.battleLPane;
import static com.mymt.util.ForecastUtil.displayForecast;
import static com.mymt.util.ForecastUtil.forecastLPane;
import static com.mymt.util.JumpUtil.displayJump;
import static com.mymt.util.JumpUtil.jumpLPane;
import static com.mymt.util.MsgUtil.msgLPane;
import static java.awt.event.KeyEvent.*;



public class Main {

    public static void main(String[] args) {
        gamePanel = new MTGame();
        gamePanel.setPreferredSize(new Dimension(GAME_PIX_72 * 18, GAME_PIX_72 * 13));

        gamePanel.add(forecastLPane);
        gamePanel.add(jumpLPane);
        gamePanel.add(battleLPane);
        gamePanel.add(msgLPane);
        gamePanel.add(timeLabel);
        gameFrame.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                if (!MTGame.inConversation)
                    switch (e.getKeyCode()) {

                        case VK_DOWN:   // 键盘 ↓
                            if (playerBean_1.getPosY() + 1 < 11 && playerBean_1.getPosY() + 1 >= 0) {
                                playerBean_1.setToward(1);
                                interaction(playerBean_1.getPosX(), playerBean_1.getPosY() + 1);
                                MTGame.gameFrame.repaint();
                            }
                            break;
                        case VK_RIGHT:  // 键盘 →
                            if (playerBean_1.getPosX() + 1 < 11 && playerBean_1.getPosX() + 1 >= 0) {
                                playerBean_1.setToward(2);
                                interaction(playerBean_1.getPosX() + 1, playerBean_1.getPosY());
                                MTGame.gameFrame.repaint();
                            }
                            break;
                        case VK_UP:     // 键盘 ↑
                            if (playerBean_1.getPosY() - 1 < 11 && playerBean_1.getPosY() - 1 >= 0) {
                                playerBean_1.setToward(3);
                                interaction(playerBean_1.getPosX(), playerBean_1.getPosY() - 1);
                                MTGame.gameFrame.repaint();
                            }
                            break;
                        case VK_LEFT:   // 键盘 ←
                            if (playerBean_1.getPosX() - 1 < 11 && playerBean_1.getPosX() - 1 >= 0) {
                                playerBean_1.setToward(0);
                                interaction(playerBean_1.getPosX() - 1, playerBean_1.getPosY());
                                gameFrame.repaint();
                            }
                            break;
                        case VK_J:      // 键盘 J
                            if (ItemsBean.isHasJump) {
                                displayJump();
                            }
                            break;
                        case VK_L:      // 键盘 L
                            if (ItemsBean.isHasForecast) {
                                displayForecast();
                            }
                            break;
                    }
                else if (e.getKeyCode() == e.VK_L)//bug
                {
                    inConversation = false;
                    forecastLPane.removeAll();
                    forecastLPane.setVisible(false);
                }
            }

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

        gameFrame.setContentPane(gamePanel);
        gameFrame.setResizable(false);
        gameFrame.pack();
        gameFrame.setLocationRelativeTo(null);
        gameFrame.setVisible(true);
        gameFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}


怪物实体类数据封装类

import java.io.Serializable;


public class MonsterBean implements Serializable {

    @Deprecated
    private int id;         // Deprecated
    private int hp;         // 生命值
    private int attack;     // 攻击力
    private int defend;     // 防御力
    private int money;      // 金钱
    private int exp;        // 经验
    private String name;    // 怪物名

    public MonsterBean(int id, int hp, int attack, int defend, int money, int exp, String name) {
        this.id = id;
        this.hp = hp;
        this.attack = attack;
        this.defend = defend;
        this.money = money;
        this.exp = exp;
        this.name = name;
    }

    public int getHp() {
        return hp;
    }

    public int getAttack() {
        return attack;
    }

    public int getDefend() {
        return defend;
    }

    public int getMoney() {
        return money;
    }

    public int getExp() {
        return exp;
    }

    public String getName() {
        return name;
    }


}

更多请关注博主资源以及Java项目专栏


作者:KJ.JK
本文仅用于交流学习,未经作者允许,禁止转载,更勿做其他用途,违者必究。
文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习

你可能感兴趣的:(Java项目,Java项目,java小游戏,关卡设计,魔塔4399小游戏)