奥特曼打小怪兽

/*
* 回合制
* 奥特曼打怪兽
* */
public class Game {

    private Hero[] heroes = new Hero[3];
    private Monster[] monsters = new Monster[3];

    public void start(){
        init();//初始化

        int heroIndex = chooseHero();//选择英雄

        int monsterIndex = chooseMonster();//选择怪兽

        System.out.println("开始战斗!");
        Hero chooseHero = heroes[heroIndex];
        Monster chooseMonster = monsters[monsterIndex];

        while(true){
            chooseHero.hit(chooseMonster);
            if(chooseMonster.getHp()<=0){
                System.out.println("奥特曼胜利!");
                chooseHero.setExp(chooseHero.getExp()+chooseMonster.getExp());
                chooseHero.setMoney(chooseHero.getMoney()+chooseMonster.getMoney());
                System.out.println("获得"+chooseMonster.getExp()+"经验,"+chooseMonster.getMoney()+"金币。");
                break;
            }
            chooseMonster.hit(chooseHero);
            if(chooseHero.getHp()<=0){
                System.out.println("奥特曼牺牲,请大侠重新来过!");
                break;
            }
        }

    }

    private int chooseMonster() {
        for (int i = 0; i < monsters.length; i++) {
            System.out.println(i+"."+monsters[i]);
        }
        System.out.println("请选择怪兽:");
        Scanner scanner = new Scanner(System.in);
        int index = scanner.nextInt();
        //TODO 应该加判断
        return index;
    }

    private int chooseHero() {
        for (int i = 0; i < heroes.length; i++) {
            System.out.println(i+"."+heroes[i]);
        }
        System.out.println("请选择英雄:");
        Scanner scanner = new Scanner(System.in);
        int index = scanner.nextInt();
        //TODO 应该加判断
        return index;
    }

    public void init(){

        Hero h1 = new Hero("迪迦",100,100,100,100,1,0,100);
        Hero h2 = new Hero("赛罗",120,80,120,80,2,0,100);
        Hero h3 = new Hero("雷欧",90,90,90,80,2,0,100);

        heroes[0] = h1;
        heroes[1] = h2;
        heroes[2] = h3;

        System.out.println("奥特曼变身完毕!");

        Monster m1 = new Monster("哥斯拉",150,95,60,70,10,5000,5000);
        Monster m2 = new Monster("贝利亚",120,85,70,80,20,15000,15000);
        Monster m3 = new Monster("金刚",110,90,80,90,30,25000,25000);

        monsters[0] = m1;
        monsters[1] = m2;
        monsters[2] = m3;

        System.out.println("怪兽出场完毕!");

    }
}
//英雄奥特曼
public class Hero {

    private String name;//名字
    private int hp;//血量
    private int ac;//防御
    private int ap;//法术
    private int ad;//攻击
    private int grade;//等级
    private int exp;//经验
    private int money;//金币

    //叫
    public void cry(){
        System.out.println("Hero.cry");
    }

    //打
    public void hit(Monster monster){

        //伤害:拿 英雄的攻击随机值 - 怪兽的防御随机值
        //Math.random() 0~1之间的小数,能取到0,取不到1
        int shanghai = (int) (ad*Math.random() - monster.getAc()*Math.random());
        shanghai = shanghai < 0 ? 0 : shanghai;
        monster.setHp(monster.getHp()-shanghai);//拿怪兽原来的血量-伤害 等于剩余的新血量

        System.out.println(name+"打了"+monster.getName()+shanghai+"血,剩余"+monster.getHp()+"血");
    }

    @Override
    public String toString() {
        return "Hero{" +
                "name='" + name + '\'' +
                ", hp=" + hp +
                ", ac=" + ac +
                ", ap=" + ap +
                ", ad=" + ad +
                ", grade=" + grade +
                ", exp=" + exp +
                ", money=" + money +
                '}';
    }

    public Hero() {
    }

    public Hero(String name, int hp, int ac, int ap, int ad, int grade, int exp, int money) {
        this.name = name;
        this.hp = hp;
        this.ac = ac;
        this.ap = ap;
        this.ad = ad;
        this.grade = grade;
        this.exp = exp;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHp() {
        return hp;
    }

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

    public int getAc() {
        return ac;
    }

    public void setAc(int ac) {
        this.ac = ac;
    }

    public int getAp() {
        return ap;
    }

    public void setAp(int ap) {
        this.ap = ap;
    }

    public int getAd() {
        return ad;
    }

    public void setAd(int ad) {
        this.ad = ad;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public int getExp() {
        return exp;
    }

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

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}
//怪兽
public class Monster {

    private String name;//名字
    private int hp;//血量
    private int ac;//防御
    private int ap;//法术
    private int ad;//攻击
    private int grade;//等级
    private int exp;//经验
    private int money;//金币

    //叫
    public void cry(){
        System.out.println("Hero.cry");
    }

    //打
    public void hit(Hero hero){
        //伤害:拿 怪兽的攻击随机值 - 英雄的防御随机值
        int shanghai = (int) (ad*Math.random() - hero.getAc()*Math.random());
        shanghai = shanghai < 0 ? 0 : shanghai;
        hero.setHp(hero.getHp()-shanghai);//拿怪兽原来的血量-伤害 等于剩余的新血量

        System.out.println(name+"打了"+hero.getName()+shanghai+"血,剩余"+hero.getHp()+"血");
    }

    @Override
    public String toString() {
        return "Hero{" +
                "name='" + name + '\'' +
                ", hp=" + hp +
                ", ac=" + ac +
                ", ap=" + ap +
                ", ad=" + ad +
                ", grade=" + grade +
                ", exp=" + exp +
                ", money=" + money +
                '}';
    }

    public Monster() {
    }

    public Monster(String name, int hp, int ac, int ap, int ad, int grade, int exp, int money) {
        this.name = name;
        this.hp = hp;
        this.ac = ac;
        this.ap = ap;
        this.ad = ad;
        this.grade = grade;
        this.exp = exp;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHp() {
        return hp;
    }

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

    public int getAc() {
        return ac;
    }

    public void setAc(int ac) {
        this.ac = ac;
    }

    public int getAp() {
        return ap;
    }

    public void setAp(int ap) {
        this.ap = ap;
    }

    public int getAd() {
        return ad;
    }

    public void setAd(int ad) {
        this.ad = ad;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public int getExp() {
        return exp;
    }

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

    public int getMoney() {
        return money;
    }

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


}
public class Test {
    public static void main(String[] args) {

        new Game().start();
    }

}

### 使用Python绘制奥特曼打小怪兽的图像或创建动画 为了实现这一目标,可以采用多种库来完成不同的部分。对于静态图像的生成,`Pillow` 是一个非常强大的工具;而对于动态效果,则 `matplotlib.animation` 或者 `pygame` 更加适合。 #### 静态图像绘制方法 通过加载现有的奥特曼和小怪兽图片文件,并利用 `PIL.ImageDraw` 对象来进行简单的图形操作,比如调整位置、旋转等动作姿态模拟战斗场景[^1]。 ```python from PIL import Image, ImageDraw def draw_static_battle(ultra_man_path="ultra.png", monster_path="monster.png"): ultra_img = Image.open(ultra_man_path).convert("RGBA") monster_img = Image.open(monster_path).convert("RGBA") background = Image.new('RGBA', (max(ultra_img.width, monster_img.width)*2, max(ultra_img.height, monster_img.height)), "white") # 将两个角色放置于背景上并保存最终结果 background.paste(ultra_img, (0, 0), mask=ultra_img) background.paste(monster_img, (background.width//2-monster_img.width//2, background.height-ultra_img.height), mask=monster_img) output_image = 'battle_result.png' background.save(output_image) return output_image ``` #### 动画制作方案 如果想要更生动的效果——即让奥特曼攻击小怪兽的过程形成一段连续的画面序列(帧),那么就需要引入时间维度的概念了。这里推荐使用 `pygame` 库因为它提供了简单易用的游戏开发框架支持基本的声音播放以及事件处理机制[^2]。 下面是一个基于 Pygame 的简易示例: ```python import pygame import sys class BattleAnimation(object): def __init__(self): self.screen_width = 800 self.screen_height = 600 pygame.init() screen = pygame.display.set_mode([self.screen_width,self.screen_height]) clock = pygame.time.Clock() ultra_surface = pygame.image.load("ultra.png").convert_alpha() monster_surface = pygame.image.load("monster.png").convert_alpha() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill((255, 255, 255)) # 更新精灵的位置或其他属性 screen.blit(ultra_surface,(int(self.screen_width*0.2), int(self.screen_height*0.4))) screen.blit(monster_surface,(int(self.screen_width*0.7), int(self.screen_height*0.4))) pygame.display.flip() clock.tick(30) if __name__ == '__main__': battle_animation = BattleAnimation() ``` 上述代码展示了如何设置窗口尺寸、载入图片资源并将它们显示出来。当然,在实际应用中还需要加入更多逻辑控制人物的动作变化,例如跳跃、挥拳等等,这可以通过改变每一帧中的坐标参数或者调用特定的方法来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值