欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现角色+武器攻击小游戏

程序员文章站 2024-01-07 22:24:40
java实现小游戏1.游戏有2个角色可供选择:男玩家 攻击力 10女玩家 攻击力 82.游戏有3种武器剑 攻击力 10 40%几率造成双倍伤害斧 攻击力 15 20%几率一击杀死怪物弓 攻击力 25 35%几率被闪避(无法造成伤害)3.游戏有1种怪物怪物 生命值 250-300 (随机)4.玩家可以装备武器装备后 玩家实际攻击力=玩家本身攻击力+武器攻击力(武器特效触发时,攻击力计算使用玩家实际攻击力)特殊要求 男玩家不能装备弓、女玩家不能装备斧...

java实现小游戏

1.游戏有2个角色可供选择:
		男玩家 攻击力 10
		女玩家 攻击力 8
2.游戏有3种武器
		剑 攻击力 10 40%几率造成双倍伤害
		斧 攻击力 15 20%几率一击杀死怪物
		弓 攻击力 25 35%几率被闪避(无法造成伤害)
3.游戏有1种怪物
		怪物 生命值 250-300 (随机)
4.玩家可以装备武器
		装备后 玩家实际攻击力=玩家本身攻击力+武器攻击力	(武器特效触发时,攻击力计算使用玩家实际攻击力)
		特殊要求 男玩家不能装备弓、女玩家不能装备斧

实现代码

    private static Random random = new Random();
    public static void main(String args[]){
        //随机生成一个新玩家(男/女)
        GamePlayer player = null;
        //补全新玩家生成代码
        //随机生成一把武器,装备给玩家
        Weapon weapon=null;
        //补全武器生成代码、装备给玩家代码(要求必须装备成功才能继续向下进行)
        int p = random.nextInt(2);
        int w = random.nextInt(2);
        if (p == 0){
            player = new BoyPlayer();
            System.out.println("生成玩家为男玩家");
            if (w == 1){
                weapon = new Axe();
                System.out.println("玩家获得武器:斧头");
            }
        }else {
            player = new GirlPlayer();
            System.out.println("生成玩家为女玩家");
            if (w == 1){
                weapon = new Bow();
                System.out.println("玩家获得武器:弓");
            }
        }
        if (w == 0){
            weapon = new Sword();
            System.out.println("玩家获得武器:剑");
        }
        //随机生成一个怪物
        Monster monster=null;
        //补全怪物生成代码
        monster = new Monster();
        //玩家攻击怪物直到杀死怪物,记录攻击次数
        int attackNumber=0; //定义攻击怪物的次数
        int HealthPoint = monster.getHealthPoint();
        int damage = 0;
        while (HealthPoint != 0){
            attackNumber++;
            damage = weapon.getAttact(player);
            System.out.println("本次攻击伤害:" + damage);
            if (damage > HealthPoint){
                System.out.println("怪物死亡");
                HealthPoint = 0;
            }else {
                HealthPoint = HealthPoint - damage;
                System.out.println("怪物剩余血量:" + HealthPoint);
            }

        }
        System.out.print("杀死怪物一共攻击了"+attackNumber+"次");//杀死怪物一共攻击了多少次
    }
}
/**
 * 玩家抽象类
 */
abstract class GamePlayer{
    public int attact = 8;
    public int getAttact() {
        return 0;
    }
}
/**
 * 男玩家类
 */
class BoyPlayer extends GamePlayer{
    @Override
    public int getAttact() {
        return super.attact + 2;
    }
}
/**
 * 女玩家类
 */
class GirlPlayer extends GamePlayer{
    @Override
    public int getAttact() {
        return super.attact;
    }
}
/**
 * 武器抽象类
 */
abstract class Weapon{
    public int attact = 10;
    public boolean getChance(int percentage){
        Random random = new Random();
        int i = random.nextInt(99);
        if(i>=0&&i<percentage)
            return true;
        else
            return false;
    }
    public int getAttact(GamePlayer gamePlayer) {
        return 0;
    }
}
/**
 * 剑类
 */
class Sword extends Weapon{
    @Override
    public int getAttact(GamePlayer gamePlayer) {
        boolean chance = super.getChance(40);
        if (chance){
            System.out.println("剑:触发双倍伤害");
            return 2* super.attact + 2*gamePlayer.getAttact();
        }else {
            return super.attact;
        }
    }
}
/**
 * 斧头类
 */
class Axe extends Weapon{
    @Override
    public int getAttact(GamePlayer gamePlayer) {
        boolean chance = super.getChance(20);
        if (chance){
            System.out.println("斧头:触发一击必杀");
            return 300;
        }else {
            return super.attact + 5;
        }
    }
}
/**
 * 弓类
 */
class Bow extends Weapon{
    @Override
    public int getAttact(GamePlayer gamePlayer) {
        boolean chance = super.getChance(20);
        if (chance){
            System.out.println("弓:攻击被闪避(无法造成伤害)");
            return 0;
        }else {
            return super.attact + 15;
        }
    }
}
/**
 * 怪物类
 */
class Monster {
    Random random = new Random();
    private int HealthPoint =random.nextInt(300)%(300-250+1) + 250;
    public int getHealthPoint() {
        System.out.println("怪物出现,血量:"+HealthPoint);
        return HealthPoint;
    }
}   

本文地址:https://blog.csdn.net/adds001/article/details/110792559