面向对象之英雄对战游戏

一、模块

1.  对决台 (对象有英雄和怪物)

2、商城

3、充值

4、查看信息

二、定义类和方法

1、定义角色类,英雄类和怪物类继承角色类的方法和属性

import random,time#引入包一个随机函数,一个时间

#定义一个角色类
class Role:
    def __init__(self, name, hp, atk, ):
        self.name = name#类的成员属性 姓名、血量、伤害
        self.hp = hp
        self.atk = atk

#定义一个攻击方法
    def attack(self, enemy):#enemy根据你传的对象接值,并不代表具体的敌人对象
        enemy.hp = enemy.hp - self.atk
#定义一个英雄类
class Hero(Role):
    def __init__(self, name, hp, atk):
        super().__init__(name, hp, atk)#继承父类Role的方法和属性,提高代码的利用率
        self.money = 0#英雄类有一个自己的成员属性money
        self.hp_ = self.hp#方便下面用户退出界面血量能回复
#定义一个怪物类
class Monster(Role):
    def __init__(self,name=None, hp=None, atk=None):
        super().__init__(name, hp, atk)
        self.m_num = random.randint(1,5)#随机1-5
        self.name = f"tk-{self.m_num}"#根据随机数给怪物起名字
        if self.m_num == 1:
            self.hp = 10
            self.atk = 15
        elif self.m_num == 2:
            self.hp = 20
            self.atk = 20
        elif self.m_num == 3:
            self.hp = 30
            self.atk = 25
        elif self.m_num == 4:
            self.hp = 40
            self.atk = 30
        elif self.m_num == 5:
            self.hp = 50
            
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Java 王者荣耀英雄代码示例: ```java // 定义英雄类 class Hero { private String name; private int hp; private int mp; private int damage; public Hero(String name, int hp, int mp, int damage) { this.name = name; this.hp = hp; this.mp = mp; this.damage = damage; } public void attack(Hero target) { int damage = this.damage; if (this.mp >= 10) { damage *= 2; this.mp -= 10; } target.receiveDamage(damage); System.out.println(this.name + "对" + target.getName() + "造成了" + damage + "点伤害!"); } public void receiveDamage(int damage) { this.hp -= damage; if (this.hp < 0) { this.hp = 0; } } public boolean isDead() { return this.hp == 0; } public String getName() { return name; } } // 游戏场景 public class Game { public static void main(String[] args) { // 初始化英雄 Hero hero1 = new Hero("盖伦", 1000, 100, 100); Hero hero2 = new Hero("寒冰", 800, 150, 80); // 开始斗 while (!hero1.isDead() && !hero2.isDead()) { // 英雄1攻击英雄2 hero1.attack(hero2); // 英雄2攻击英雄1 hero2.attack(hero1); } // 判断胜负 if (hero1.isDead()) { System.out.println(hero2.getName() + "获胜!"); } else { System.out.println(hero1.getName() + "获胜!"); } } } ``` 上述代码实现了英雄类和简单的英雄逻辑。在游戏场景中,我们创建了两个英雄对象,然后让两个英雄轮流攻击对方,直到有一方英雄死亡为止,最后判断胜负并输出结果。在英雄类中,我们定义了攻击和受伤方法,以及判断是否死亡的方法。在攻击方法中,我们判断英雄是否有足够的蓝量释放技能,如有则造成双倍伤害,否则造成普通伤害。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值