王者荣耀代码

package com.sxt;
 
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
 
import javax.swing.JButton;
import javax.swing.JFrame;
 
import com.sxt.beast.Beast;
 
public class GameFrame extends JFrame {
 
    // 游戏模式 0 选择英雄,1 游戏开始,2 游戏胜利,3 游戏失败
    int state = 0;
    // 窗口尺寸
    private int windowWidth = 1400;
    private int windowHeight = 700;
    // 双缓冲图片
    private Image offScreenImage = null;
    // 攻击图片
    private Image attack = Toolkit.getDefaultToolkit().getImage("img/attack.jpg");
    //游戏胜利失败图片
    private Image gameWin = Toolkit.getDefaultToolkit().getImage("img/gameWin.png");
    private Image gameLose = Toolkit.getDefaultToolkit().getImage("img/gameLose.png");
    // 游戏背景
    Background background = new Background(this);
    // 游戏玩家
    Champion player;
    Champion champion = new ChampionHouyi(this,700,3800);
    // 双方小兵
    MinionBlue mb = new MinionBlue(this);
    MinionRed mr = new MinionRed(this);
    // 防御塔
    Turret turret = new Turret(this);
    //野怪
    public Beast beast = new Beast(this);
    // 攻击按钮
    JButton attackButton;
    // 游戏元素列表
    public ArrayList<GameObject> objList = new ArrayList<GameObject>();
    ArrayList<GameObject> redList = new ArrayList<GameObject>();// 红色方
    ArrayList<GameObject> blueList = new ArrayList<GameObject>();// 蓝色方
    public ArrayList<GameObject> removeList = new ArrayList<GameObject>();// 存放将要删除的元素
 
    public void launch() {
        // 设置尺寸
        setSize(windowWidth, windowHeight);
        // 窗口居中
        setLocationRelativeTo(null);
        // 关闭事件
        setDefaultCloseOperation(3);
        // 用户不能调整窗口大小
        setResizable(false);
        // 标题
        setTitle("王者荣耀");
        // 窗口可见
        setVisible(true);
        // 添加键盘监视器
        this.addKeyListener(new GameFrame.KeyMonitor());
        // 添加游戏元素
        objList.add(background);
        objList.addAll(turret.turretList);
        objList.addAll(beast.beastList);
        blueList.add(turret.turretList.get(0));
        redList.add(turret.turretList.get(4));
 
        /**
         * 攻击按钮
         */
        attackButton = new JButton();
        attackButton.setSize(130, 132);
        attackButton.setLocation(1150, 430);
        attackButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 按钮事件
                player.attack(redList);
            }
        });
        this.add(attackButton);
 
        while (true) {
            if (state == 1) {
                // 添加小兵
                //mb.createMinion(this, blueList);
                //mr.createMinion(this, redList);
                // 添加防御塔
                turret.addTurret();
            }
            repaint();
            try {
                Thread.sleep(17);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    public void paint(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(5984, 4452);
        }
        Graphics gImage = offScreenImage.getGraphics();
        if (state == 0) {
            for(int i = 0; i < champion.championList.size(); i++) {
                //添加图片
                Image classical = champion.championList.get(i).classical;
                gImage.drawImage(classical, i*160, 20, null);
                
                //添加按钮
                JButton championButton = new JButton();
                championButton.setSize(150, 150);
                championButton.setLocation(i*150, 0);
                int a = i;
                championButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        state = 1;
                        player = champion.championList.get(a);
                        objList.add(player);
                        player.addButton();
                    }
                });
                this.add(championButton);
            }
        } else if (state == 1) {
            for (int i = 0; i < objList.size(); i++) {
                objList.get(i).paintSelf(gImage);
            }
            // 绘制攻击图片
            gImage.drawImage(attack, player.getX() + 500, player.getY() + 100, null);
            objList.removeAll(removeList);
        } else if (state == 2) {//游戏胜利
            gImage.drawImage(gameWin, 0, 5, null);
        } else if (state == 3) {//游戏失败
            gImage.drawImage(gameLose, 0, 5, null);
        }
        if (state != 1) {
            g.drawImage(offScreenImage, 0, 0, null);
        } else {
            g.drawImage(offScreenImage, -player.getX() + 700, -player.getY() + 350, null);
        }
        /**
         * 添加按钮后不能调用键盘事件 因为程序的焦点变成了按钮 this.requestFocus() 把焦点重新改变到游戏界面上
         */
        this.requestFocus();
    }
 
    // main方法
    public static void main(String[] args) {
        GameFrame gameFrame = new GameFrame();
        gameFrame.launch();
    }
 
    // 键盘事件
    private class KeyMonitor extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            player.keyPressed(e);
        }
 
        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            player.keyReleased(e);
        }
    }
}

package com.sxt;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.ArrayList;
 
import com.sxt.beast.Beast;
 
public abstract class GameObject {
 
    // 坐标
    private int x;
    private int y;
    // 图片
    private Image img;
    // 游戏界面
    public GameFrame gameFrame;
    // 速度
    private int spd;
    // 初始生命值
    private int hp;
    // 当前生命值
    private int currentHp;
    // 攻击目标
    private GameObject target;
    // 是否有目标
    private boolean hasTarget = false;
    // 攻击距离
    private int dis;
    // 攻击时间间隔
    private int attackCoolDownTime;
    // 攻击是否冷却
    private boolean attackCoolDown = true;
    // 是否存活
    private boolean alive = true;
    // 是否被控制
    boolean beControlled = false;
 
    public GameObject(GameFrame gameFrame) {
        this.gameFrame = gameFrame;
    }
 
    public GameObject(int x, int y, GameFrame gameFrame) {
        this.x = x;
        this.y = y;
        this.gameFrame = gameFrame;
    }
 
    public GameObject() {
        // TODO Auto-generated constructor stub
    }
 
    public void addHp(Graphics g, int difX, int difY, int width, int height, Color color) {
        // 绘制外部轮廓
        g.setColor(Color.black);
        g.drawRect(getX() - difX, getY() - difY, width, height);
        // 填充矩形
        g.setColor(color);
        g.fillRect(getX() - difX, getY() - difY, (int) (width * getCurrentHp() / getHp()), height);
    }
 
    public double getDis(int x1, int y1, int x2, int y2) {
        return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
    }
 
    // 矩形矩形碰撞检测
    public boolean recIntersectsRec(Rectangle r1, Rectangle r2) {
        return r1.intersects(r2);
    }
 
    public boolean recIntersectsCir(Rectangle rec, int x, int y, int r) {
        /** 矩形于圆相交: 圆心到至少一个矩形定点的距离小于r */
        if ((getDis(x, y, rec.x, rec.y) < r) || (getDis(x, y, rec.x, rec.y + rec.height) < r)
                || (getDis(x, y, rec.x + rec.width, rec.y) < r)
                || (getDis(x, y, rec.x + rec.width, rec.y + rec.height) < r)) {
            return true;
        }
        return false;
    }
 
    // 攻击方法
    public void attack(ArrayList<GameObject> gameObjList) {
        if (hasTarget) {
            // 目标离开范围后寻找新的目标
            if (!recIntersectsCir(target.getRec(), getX(), getY(), getDis())) {
                setHasTarget(false);
            }
            // 目标死亡,寻找新目标
            else if (!target.isAlive()) {
                setHasTarget(false);
            } else if (isAttackCoolDown() && isAlive()) {
                Bullet bullet = null;
                // 防御塔攻击
                if (Turret.class.isAssignableFrom(getClass())) {
                    bullet = new Bullet(gameFrame, this, getTarget(), 500, 50);
                }
                // 小兵攻击
                else if (Minion.class.isAssignableFrom(getClass())) {
                    bullet = new Bullet(gameFrame, this, getTarget(), 50, 30);
                }
                // 玩家攻击
                else if (this instanceof Champion) {
                    bullet = new Bullet(gameFrame, this, getTarget(), 500, 50, "img/bullet.gif");
                }
                gameFrame.objList.add(bullet);
                // 线程开始
                new AttackCD().start();
            }
        } else {
            // 遍历列表
            for (GameObject obj : gameObjList) {
                // 判断攻击范围(圆形)与敌方(矩形)是否相交
                if (recIntersectsCir(obj.getRec(), getX(), getY(), getDis())) {
                    // 找到目标
                    setTarget(obj);
                    setHasTarget(true);
                    // 跳出循环
                    break;
                }
            }
            // 玩家是否在攻击范围内
            if (!hasTarget && gameObjList == gameFrame.blueList) {
                if (recIntersectsCir(gameFrame.player.getRec(), getX(), getY(), getDis())) {
                    // 找到目标
                    setTarget(gameFrame.player);
                    setHasTarget(true);
                }
            } else {
                for (GameObject obj : gameFrame.beast.beastList) {
                    // 判断攻击范围(圆形)与敌方(矩形)是否相交
                    if (recIntersectsCir(obj.getRec(), getX(), getY(), getDis())) {
                        // 找到目标
                        setTarget(obj);
                        setHasTarget(true);
                        // 跳出循环
                        break;
                    }
                }
            }
        }
    }
 
    class AttackCD extends Thread {
        public void run() {
            // 将攻击功能设置为冷却状态
            setAttackCoolDown(false);
            // 线程休眠
            try {
                Thread.sleep(attackCoolDownTime);
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 将攻击功能设置为攻击状态
            setAttackCoolDown(true);
            // 线程终止
            this.stop();
        }
    }
 
    // 绘制元素
    public abstract void paintSelf(Graphics g);
 
    // 返回矩形
    public abstract Rectangle getRec();
 
    /**
     * @return the x
     */
    public int getX() {
        return x;
    }
 
    /**
     * @param x the x to set
     */
    public void setX(int x) {
        this.x = x;
    }
 
    /**
     * @return the y
     */
    public int getY() {
        return y;
    }
 
    /**
     * @param y the y to set
     */
    public void setY(int y) {
        this.y = y;
    }
 
    /**
     * @return the img
     */
    public Image getImg() {
        return img;
    }
 
    /**
     * @param img the img to set
     */
    public void setImg(String img) {
        this.img = Toolkit.getDefaultToolkit().getImage(img);
    }
 
    /**
     * @return the spd
     */
    public int getSpd() {
        return spd;
    }
 
    /**
     * @param spd the spd to set
     */
    public void setSpd(int spd) {
        this.spd = spd;
    }
 
    /**
     * @return the hp
     */
    public int getHp() {
        return hp;
    }
 
    /**
     * @param hp the hp to set
     */
    public void setHp(int hp) {
        this.hp = hp;
    }
 
    /**
     * @return the currentHp
     */
    public int getCurrentHp() {
        return currentHp;
    }
 
    /**
     * @param currentHp the currentHp to set
     */
    public void setCurrentHp(int currentHp) {
        if (currentHp < getHp()) {
            if (Beast.class.isAssignableFrom(getClass())) {
                System.out.println("yes");
                setTarget(gameFrame.player);
                setHasTarget(true);
            }
        }
        this.currentHp = currentHp;
    }
 
    /**
     * @return the target
     */
    public GameObject getTarget() {
        return target;
    }
 
    /**
     * @param target the target to set
     */
    public void setTarget(GameObject target) {
        this.target = target;
    }
 
    /**
     * @return the hasTarget
     */
    public boolean isHasTarget() {
        return hasTarget;
    }
 
    /**
     * @param hasTarget the hasTarget to set
     */
    public void setHasTarget(boolean hasTarget) {
        this.hasTarget = hasTarget;
    }
 
    /**
     * @return the dis
     */
    public int getDis() {
        return dis;
    }
 
    /**
     * @param dis the dis to set
     */
    public void setDis(int dis) {
        this.dis = dis;
    }
 
    /**
     * @return the attackCoolDownTime
     */
    public int getAttackCoolDownTime() {
        return attackCoolDownTime;
    }
 
    /**
     * @param attackCoolDownTime the attackCoolDownTime to set
     */
    public void setAttackCoolDownTime(int attackCoolDownTime) {
        this.attackCoolDownTime = attackCoolDownTime;
    }
 
    /**
     * @return the attackCoolDown
     */
    public boolean isAttackCoolDown() {
        return attackCoolDown;
    }
 
    /**
     * @param attackCoolDown the attackCoolDown to set
     */
    public void setAttackCoolDown(boolean attackCoolDown) {
        this.attackCoolDown = attackCoolDown;
    }
 
    /**
     * @return the alive
     */
    public boolean isAlive() {
        return alive;
    }
 
    /**
     * @param alive the alive to set
     */
    public void setAlive(boolean alive) {
        this.alive = alive;
    }
}

<think>嗯,用户问的是关于Dev-C++和王者荣耀代码。首先,我需要确认他们是想了解如何在Dev-C++中编写类似王者荣耀的游戏代码,还是想获取现成的代码王者荣耀是一个复杂的3D手游,使用C++加上游戏引擎如Unity或Unreal Engine开发的。Dev-C++是一个比较旧的IDE,适合学习C++,但做游戏的话功能有限。 用户可能是初学者,可能对游戏开发不太了解,以为用Dev-C++就能写出王者荣耀这样的游戏。这时候我需要解释清楚游戏开发的基本流程和所需工具,避免他们走弯路。应该先介绍游戏开发的基础知识,比如图形库、游戏引擎,然后给出一个简单的示例,比如文字版战斗游戏,这样他们可以在Dev-C++里练习基础编程。 同时,要提醒他们大型游戏的复杂性,建议学习更专业的引擎和工具。可能还需要分步骤说明,先学习C++基础,再接触图形库,最后转向游戏引擎。这样用户能有一个清晰的学习路径,不会因为一开始就面对太复杂的项目而放弃。</think>关于使用 Dev-C++ 编写类似《王者荣耀》的代码,需要明确几个重要前提: 1. 代码局限性说明 - 《王者荣耀》是使用C++结合游戏引擎(如Unity/Unreal)开发的大型3D网游 - Dev-C++ 是轻量级IDE,仅适合小型程序开发 - 核心差异: ✅ 图形渲染:需要OpenGL/DirectX等图形库 ✅ 网络通信:需socket编程 ✅ 物理引擎:复杂碰撞检测 ✅ 资源管理:模型/贴图/音效等 2. 入门学习建议(Dev-C++环境) 🔥 第一步:掌握C++基础语法 🔥 第二步:学习Windows API图形编程 🔥 第三步:尝试简单小游戏开发(如文字RPG) 3. 简单文字版对战示例(可在Dev-C++运行): ```cpp #include <iostream> #include <cstdlib> #include <ctime> using namespace std; class Hero { private: string name; int hp; int attack; public: Hero(string n, int h, int a) : name(n), hp(h), attack(a) {} void fight(Hero &enemy) { int damage = rand() % attack + 10; enemy.hp -= damage; cout << name << " 对 " << enemy.name << " 造成 " << damage << " 伤害!" << endl; } bool isAlive() { return hp > 0; } void showStatus() { cout << name << " 生命值:" << hp << endl; } }; int main() { srand(time(0)); Hero 亚瑟("亚瑟", 1000, 150); Hero 李白("李白", 800, 180); while(亚瑟.isAlive() && 李白.isAlive()) { // 随机攻击顺序 if(rand() % 2 == 0){ 亚瑟.fight(李白); 李白.fight(亚瑟); } else { 李白.fight(亚瑟); 亚瑟.fight(李白); } 亚瑟.showStatus(); 李白.showStatus(); cout << "------------------------" << endl; system("pause"); // 按任意键继续 } cout << (亚瑟.isAlive() ? "亚瑟" : "李白") << " 获胜!" << endl; return 0; } ``` 4. 学习路线建议: 📌 阶段一:C++核心语法(3-6个月) 📌 阶段二:Windows API编程(2个月) 📌 阶段三:OpenGL图形编程(3个月起) 📌 阶段四:游戏引擎学习(Unity/UE 1年起) 5. 重要提示: 💡 真实游戏开发需要团队协作 💡 现代游戏开发主要使用现成引擎 💡 建议从简单2D游戏开始练习(如贪吃蛇) 💡 图形编程推荐改用Visual Studio 建议先通过此代码理解基本逻辑,后续可逐步添加以下功能: 1. 添加技能系统 2. 实现装备系统 3. 加入简单AI 4. 增加保存/读取进度功能 如果需要进一步学习具体方向的开发技术,可以告诉我您想重点突破的领域。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值