You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python文本RPG开发报错:enemy.damage_taken无法使用-=运算符

文本RPG攻击功能报错解决

我接触Python约5天,正在开发一款文本类RPG游戏作为首个实战项目。实现攻击功能时,代码提示enemy.damage_taken方法无法使用-=运算符。


相关代码

整合逻辑代码

#Imports
from player import Player
from items import Item
from spells import Spells
from monsters import Monster
from gameplay import Fight
#Creating spells
# Type, Name, Damage, Cost
#Below are buff spells
buff_A = Spells("buff", "Buff", 1.1, 5)
buff_B = Spells("buff", "Stronger Buff", 1.3, 15)
buff_C = Spells("buff", "Strongest Buff", 1.5, 50)
#Below Are heal spells
heal_A = Spells("heal", "Heal", 5, 10)
heal_B = Spells("heal", "Stronger Heal", 20, 30)
heal_C = Spells("heal", "Strongest Heal", 50, 100)
#Creating Items
#Type, Name, Description, Amount
potion = Item("potion", "Health Potion", "Heals you for 10 HP", 10)
elixir = Item("elixir", "Mana Potion", "Regains 15 MP", 15)
#Assiging Variables For Player
player_spells = [buff_A, heal_A]
player_items = [potion, elixir]
#Creating player
#NAME, HP, MP, ATK, DEF, SPELLS, ITEMS
player = Player("Nathan", 10, 20, 5, 5, player_spells, player_items)
#Creating monster
cricket = Monster("Cricket", 3, 3, 7)
enemies = [cricket]
#Fight Setup
fighting = True
#gameplay
fight1 = Fight(player, cricket)
running = True
#action = input("Please select an action, Attack, Magic, Items ")
while running:
  print("1 for attack etc.")
  choice = input(player.actions)
  list_chooser = int(choice) - 1

  if list_chooser == 0:
    dmg = player.generate_dmg
    enemies[0].damage_taken(dmg)      # Problem is on this line
    print("You did " + dmg + "damage")

Player类代码

# IMPORTS
import random
#CLASS PLAYER
class Player():
  def __init__(self, name, hp, mp, atk, df, magic, items):
    self.maxhp = hp
    self.hp = hp
    self.maxmp = mp
    self.mp = mp
    self.atklow = atk - 2
    self.atkhigh = atk + 2
    self.df = df - 100
    self.dfc = df / 100
    self.magic = magic
    self.items = items
    self.actions = ["Attack", "Magic", "Items", "Stats"]
    self.name = name
#Dealing Damage
  def generate_dmg(self):  
    return random.randrange(self.atklow, self.atkhigh)
#Taking Damage
  def take_dmg(self, dmg):
    self.hp -= dmg * self.dfc
    if self.hp < 0:
      self.hp = 0
    return self.hp
  #Healing
  def heal(self, dmg):
    self.hp += dmg
    if self.hp > self.maxhp:
      self.hp = self.maxhp
  #Returning HP Values
  def get_hp(self):
    return self.hp
  def get_maxhp(self):
    return self.maxhp
  #Mana Cost Logic
  def reduce_mp(self, cost):
    self.mp -= cost
  #Actions
  def get_stats(self):
    print("HP")
    print(self.hp)
    print("--")
    print(self.maxhp)
    print("")
    print("MP")
    print(self.mp)
    print("--")
    print(self.maxmp)
    print("")
    print("ATK")
    print(self.atklow + self.atkhigh)
    print("DEF")
    print(self.dfc)

Monster类代码

import random
class Monster:
  def __init__(self, name, atk, df, hp):
    self.name = name
    self.atk = atk
    self.hp = hp
    self.maxhp = hp
    self.atklow = self.atk - 2
    self.atkhigh = self.atk + 2
  def damage_dealt(self):
    return random.randrange(self.atklow, self.atkhigh)
  def damage_taken(self, dmg):
    self.hp -= dmg  
    if self.hp < 0:
      self.hp = 0
    return self.hp
  def get_hp(self):
    return self.hp
  def get_maxhp(self):
    return self.maxhp

报错核心原因

  • dmg = player.generate_dmg未调用方法,传递给damage_taken的是函数对象而非整数数值
  • print("You did " + dmg + "damage")尝试拼接字符串与非字符串对象,触发类型错误
  • self.hp -= dmg中dmg为非数值类型,无法执行减法运算

修复方案

针对上述问题,修改整合逻辑中攻击分支的代码:

if list_chooser == 0:
    dmg = player.generate_dmg()  # 调用方法,获取整数伤害值
    enemies[0].damage_taken(dmg)
    print(f"You did {dmg} damage")  # 使用f-string完成字符串与数值的拼接

修改后,dmg会正确获取到整数类型的伤害值,既能正常传递给damage_taken方法执行减法运算,也能正确输出伤害信息。


内容的提问来源于stack exchange,提问作者Sadturtle0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 18:15:47