找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2167|回复: 1

[讨论] class中凹凸曼打小怪兽遇到的问题

4

主题

9

帖子

9

积分

贫民

积分
9
天皮丶 发表于 2019-7-16 00:52:53 | 显示全部楼层 |阅读模式
from abc import ABCMeta, abstractclassmethod
import random
import time

class Fighter(object, metaclass=ABCMeta):

    __slots__ = ('_name', '_hp')

    def __init__(self, name, hp):
        '''初始化方法

        :param name:名字
        :param hp: 生命值

        '''

        self._name = name
        self._hp = hp

    @property
    def name(self):
        return self._name

    @property
    def hp(self):
        return self._hp

    @hp.setter
    def hp(self, hp):
        self._hp = hp if hp >= 0 else 0

    @property
    def alive(self):
        return self._hp > 0

    @abstractclassmethod
    def attack(self, other):
        '''
        攻击
        :param other:被攻击的对象
        :return:
        '''
        pass

class Ultraman(Fighter):

    '''奥特曼'''

    __slots__ = ('_name', '_hp', '_mp')

    def __init__(self, name, hp, mp):
        super().__init__(name, hp)
        self._mp = mp

    def reduce(self):
        re_blood = random.randint(15, 25)
        return re_blood

    def magic_attack_reduce(self):
        re_blood = random.randint(10, 15)
        return re_blood

    def attack(self, other):
        other.hp -= self.reduce()

    def huge_attack(self, other):
        '''
        打掉对方至少50点或四分之三的血
        :param other: 被攻击的对象
        :return: 使用成功返回True,否则返回False
        '''
        if self._mp >= 50:
            self._mp -= 50
            injury = other.hp * 3 // 4
            injury = injury if injury >= 50 else 50
            other.hp -= injury
            return True
        else:
            self.attack(other)
            return False

    def huge_attack_reduce(self, other):
        huge_reduce = other.hp * 3 // 4
        huge_reduce = huge_reduce if huge_reduce >= 50 else 50
        return huge_reduce

    def magic_attack(self, others):
        '''
        魔法攻击
        :param others:被攻击的群体
        :return: 使用魔法攻击成功后返回True否则返回False
        '''
        if self._mp >= 20:
            self._mp -= 20
            for temp in others:
                if temp.alive:
                    temp.hp -= self.magic_attack_reduce()
            return True
        else:
            return False

    def resume(self):
        '''回复魔法值'''
        incr_point = random.randint(1, 10)
        self._mp += incr_point
        return incr_point

    def __str__(self):
        time.sleep(1)
        return '~~~%s奥特曼~~~\n' % self._name +  \
            '生命值:%d\n' % self._hp + \
            '魔法值:%d\n' % self._mp


class Monster(Fighter):

    __slots__ = ('_name', '_hp')

    def reduce_blood(self):
        re_blood = random.randint(10, 20)
        return re_blood

    def attack(self, other):
        other.hp -= self.reduce_blood()

    def __str__(self):
        return '~~~%s小怪兽~~~' % self._name + \
            '生命值:%d' % self._hp


def is_any_alive(monsters):

        for monster in monsters:
            if monster.alive > 0:
                return True
        return False


def select_alive_one(monsters):

        monsters_len = len(monsters)
        while True:
            index = random.randrange(monsters_len)
            monster = monsters[index]
            if monster.alive > 0:
                return monster


def display_info(ultraman, monsters):
        print(ultraman)
        for monster in monsters:
            print(monster, end=' \n' )
            time.sleep(0.3)


def main():
    u = Ultraman('泰罗', 1000, 120)
    m1 = Monster('狄仁杰', 250)
    m2 = Monster('李元芳', 500)
    m3 = Monster('傻逼', 750)
    ms = [m1, m2, m3]
    while u.alive and is_any_alive(ms):
        m = select_alive_one(ms)
        skill = 10
        
if:
            time.sleep(
1)
            
if u.huge_attack(m):
               
print('%s使用究极必杀技虐了%s,造成了%d伤害' % (u.name, m.name, u.huge_attack_reduce()))
            
else:
               
print('%s使用了普通攻击打了%s,造成了%d伤害' % (u.name, m.name, u.reduce()))
               
print('%s的魔法值回复了%d点.' % (u.name, u.resume()))
        
if m.alive > 0:
            time.sleep(
1)
            
print('%s回击了%s.造成%d点伤害' % (m.name, u.name, m.reduce_blood()))
            m.attack(u)
        display_info(u
, ms)
        fight_round +=
1
        time.sleep(1)
   
print('\n=======战斗结束!=======\n')
   
if u.alive > 0:
        
print('%s奥特曼胜利!' % u.name)
   
else:
        
print('小怪兽胜利!')


if __name__ == '__main__':
    main()



这是我学习来,并自己改进的一些地方,实现了对普通攻击和魔法伤害的数值的print,但是必杀技就不能print了,raise这个“TypeError: huge_attack_reduce() missing 1 required positional argument: 'other'”求解答
回复

使用道具 举报

0

主题

30

帖子

30

积分

贫民

积分
30
胡扶林 发表于 2019-7-16 14:15:12 | 显示全部楼层
报错的意思是: huge_attck_reduce()缺少一个位置参数other
print('%s使用究极必杀技虐了%s,造成了%d伤害' % (u.name, m.name, u.huge_attack_reduce()))   这一句话中的u.huge_attack_reduce()这个函数没有传参,少了一个未知参数 other。这个函数需要传一个参数other才能正常运行。
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表