这是使Player成为类,enenmy_hit成为方法的一个主要示例。有关如何使用类和对象的详细信息,请参见Python Tutorial section on classes。在import random
class Player(object):
def __init__(self, health=100, xp=100, strength=0, dexterity=0,
wisdom=0, level_req=None, difficulty=1.0):
"""Initialize the player's attributes (health, xp, ...).
Create a player object with some default values, unless the have
been specified as keyword arguments.
"""
self.health = health
self.xp = xp
self.strength = strength
self.dexterity = dexterity
self.wisdom = wisdom
self.level_req = level_req
self.difficulty = difficulty
def hit(self):
"""This is the method that calculates a hit on the player.
It uses a difficulty factor `self.difficulty` set when the cl