【发布时间】:2020-10-11 17:45:22
【问题描述】:
当玩家输入字母“w”时,玩家的 z 值增加 1,敌人的 x 值增加 0.5。我希望在玩家输入字母“e”时删除敌人对象,并在正常按下 w 时增加 z 值。如果删除对象,如何让 python 忽略某些代码部分?
class Enemy:
x = 1
play = True
z = 1
while play:
command = input('')
if command == 'e':
del Enemy
if command == 'w':
z += 1
print(z)
if z >= Enemy.x:
# stop this from being executed after e is pressed
Enemy.x += 0.5
print(Enemy.x)
【问题讨论】:
-
您不能在 Python 中手动删除对象(
del thing只是取消绑定一个变量),并且您不应该尝试将删除对象用作任何事情的触发器或条件。