Python 2.7 TypeError:-=:';实例';和';int';用于pygame
我正在使用pygame,在处理我的健康栏时遇到了一个错误。当敌人通过某个点时,我希望生命值下降。游戏将类似于加拉加 帮助会很好。谢谢Python 2.7 TypeError:-=:';实例';和';int';用于pygame,python-2.7,Python 2.7,我正在使用pygame,在处理我的健康栏时遇到了一个错误。当敌人通过某个点时,我希望生命值下降。游戏将类似于加拉加 帮助会很好。谢谢 class Health: def __init__(self, x, y, health): self.x = x self.y = y self.width = 300 self.height = 96 self.velocity = 0 se
class Health:
def __init__(self, x, y, health):
self.x = x
self.y = y
self.width = 300
self.height = 96
self.velocity = 0
self.images = pygame.image.load('health.png')
self.numimages = 20
self.cImage = 0
self.health = 100
def update(self):
#if collide1 == True:
#self.health -= 25
if self.health > 96:
self.cImage = 0
if self.health >= 92 and self.health <= 96:
self.cImage = 1
if self.health >= 88 and self.health <= 92:
self.cImage = 2
if self.health >= 84 and self.health <= 88:
self.cImage = 3
if self.health >= 80 and self.health <= 84:
self.cImage = 4
if self.health >= 76 and self.health <= 80:
self.cImage = 5
if self.health >= 72 and self.health <= 76:
self.cImage = 6
if self.health >= 68 and self.health <= 72:
self.cImage = 7
if self.health >= 64 and self.health <= 68:
self.cImage = 8
if self.health >= 60 and self.health <= 64:
self.cImage = 9
if self.health >= 56 and self.health <= 60:
self.cImage = 10
if self.health >= 52 and self.health <= 56:
self.cImage = 11
if self.health >= 48 and self.health <= 52:
self.cImage = 12
if self.health >= 44 and self.health <= 48:
self.cImage = 13
if self.health >= 39 and self.health <= 44:
self.cImage = 14
if self.health >= 34 and self.health <= 39:
self.cImage = 15
if self.health >= 29 and self.health <= 34:
self.cImage = 16
if self.health >= 24 and self.health <= 29:
self.cImage = 17
if self.health >= 18 and self.health <= 24:
self.cImage = 18
if self.health >= 5 and self.health <= 18:
self.cImage = 19
if self.health < 5:
self.cImage = 19
if self.health == 0:
pygame.quit()
稍后在发生错误的while循环中
if enemy.rect.y >= 699 or enemy2.rect.y >= 700 or enemy3.rect.y >= 700 or enemy4.rect.y >= 700 or enemy5.rect.y >= 700 or enemy6.rect.y >= 700 or enemy7.rect.y >= 700 or enemy8.rect.y >= 700 or enemy9.rect.y >= 700:
health -= 10
那么,health是一个health()对象,然后从中减去10?Python不知道你的意思。试试health.health-=10。这将从自我健康中减去10,这是一个整数。它起作用了。非常感谢你。python新手,曾一度被它所吸引:你可以使用
elif
,然后你不需要所有和self.healt如果self.health
是92,那么如果self.health>=92
和和self.health,那么true就是了
if enemy.rect.y >= 699 or enemy2.rect.y >= 700 or enemy3.rect.y >= 700 or enemy4.rect.y >= 700 or enemy5.rect.y >= 700 or enemy6.rect.y >= 700 or enemy7.rect.y >= 700 or enemy8.rect.y >= 700 or enemy9.rect.y >= 700:
health -= 10