Java Vs小游戏

英雄类:

import java.util.Random;
/**
 * 名字,技能,技能伤害,英雄初始化hp
 */
public class Hero {
    private String name;
    private String[] skill;
    private int[] hurt;
    private int Hp;
    public Hero(){ //空的构造器
        super();
    }
    public Hero(String name, String[] skill, int[] hurt, int hp) {
        super();
        this.name = name;
        this.skill = skill;
        this.hurt = hurt;
        Hp = hp;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String[] getSkill() {
        return skill;
    }
    public void setSkill(String[] skill) {
        this.skill = skill;
    }
    public int[] gethurt() {
        return hurt;
    }
    public void sethurt(int[] hurt) {
        this.hurt = hurt;
    }
    public int getHp() {
        return Hp;
    }
    public void setHp(int hp) {
        Hp = hp;
    }
    public void Skillhurt(Hero otherhero){ //攻击行为 otherhero谁调用谁就是otherhero
        // 创建Random获取随机数
        Random random = new Random();
        int i = random.nextInt(skill.length);
        String skill = this.skill[i]; // 通过随机数获取随机技能
        int n = otherhero.hurt[i]; //伤害
        otherhero.Hp = otherhero.Hp - n;
        System.out.println(this.name()+"使用"+skill+"技能,"+"对"+otherhero.getName()+"造成了"+n+"伤害,"+otherhero.getName()+"还剩"+otherhero.getHp()+"血量"); // this 猪八戒 otherhero
    }

    private String name() {
        return name;
    }
}

英雄游戏类:

/**
 * 1.创建两个hero
 * 2.使用if语句判断第一个攻击者
 * 3.调用攻击方法
 * 4.判断血量,分出胜负
 */
import java.util.Random;
public class VsHero {
    public static void main(String[] args) {
            hero();
    }
    public static void hero(){
        String[] skill = {"肉弹蹦床", "倒打一耙", "圈养时刻", "九齿钉耙"};
        int[] hart = {20, 30, 40, 60};
        Hero zbj = new Hero("猪八戒", skill, hart, 500);
        String[] skill1 = {"护身咒法", "斗战冲锋", "大圣神威", "如意金箍"};
        int[] hart1 = {20, 30, 40, 60};
        Hero swk = new Hero("孙悟空", skill1, hart1, 500);
        System.out.println("------Welcome to this Game!------");
        Random rd = new Random();
        int a = rd.nextInt(2);
        System.out.println("VS开始");
        // [0 2)包括0,但不包括2  0 1
        if (a == 0) { // 猪八戒先攻击
            System.out.println("猪八戒开始攻击");
            while (true) {
                if (swk.getHp() <= 0) {
                    System.out.println(swk.getName() + "血量小于0," + zbj.getName() + "获胜");
                    break;
                }
                zbj.Skillhurt(swk);
                if (zbj.getHp() <= 0) {
                    System.out.println(zbj.getName() + "血量小于0," + swk.getName() + "获胜");
                    break;
                }
                swk.Skillhurt(zbj);
            }
        } else if (a == 1) {
            System.out.println("孙悟空开始攻击");
            while (true) {
                if (zbj.getHp() <= 0) {
                    System.out.println(zbj.getName() + "血量小于0," + swk.getName() + "获胜");
                    break;
                }
                swk.Skillhurt(zbj);
                if (swk.getHp() <= 0) {
                    System.out.println(swk.getName() + "血量小于0," + zbj.getName() + "获胜");
                    break;
                }
                zbj.Skillhurt(swk);
            }
        }
    }
}

效果图:

给TA打赏
共{{data.count}}人
人已打赏
JavaScript博客

JavaScript Array

2020-10-30 15:45:58

Git博客

通过Git搭建本地仓库

2020-11-11 10:47:05

5 条回复 A文章作者 M管理员
  1. I appreciate you sharing this blog. Much thanks again.

  2. Wonderful article! We will be linking to this great article on our site. Keep up the good writing.

  3. You made some nice points there. I looked on the internet for the subject matter and found most individuals will agree with your site.

  4. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  5. good article

  6. Awesome post.

个人中心
今日签到
有新私信 私信列表
搜索