我正在尝试为pygame中的角色绘制一个健康栏。我收到一个错误,说出姓名' draw_shield_bar'没有定义。我尝试过player.draw_shield_bar但仍然无法正常工作。有人可以帮助符号。下面我已经包含了调用函数的代码,然后是定义该函数的播放器类。帮助将不胜感激。
def draw(self):
# Game Loop - draw
whitehouse = pg.image.load("whiteHouseBG.png")
self.screen.blit(whitehouse,(0,0))
self.all_sprites.draw(self.screen)
self.draw_text(str(self.score), 22, WHITE, WIDTH / 2, 15)
draw_shield_bar(self.screen, 50, 50, player.shield)
# *after* drawing everything, flip the display
pg.display.flip()
class Player(pg.sprite.Sprite):
def __init__(self, game):
pg.sprite.Sprite.__init__(self)
self.game = game
self.walking = False
self.jumping = False
self.current_frame = 0
self.last_update = 0
self.load_images()
#me:girl standing
self.image = self.girl_standing[0]
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT/2)
self.pos = vec(WIDTH/2, HEIGHT*3/4)
self.vel = vec(0, 0)
self.acc = vec(0, 0)
self.shield = 100
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
self.clock = pg.time.Clock()
def draw_shield_bar(surf, x, y, pct):
if pct < 0:
pct = 0
BAR_LENGTH = 200
BAR_HEIGHT = 20
fill = (pct/100 * BAR_LENGTH)
outline_rect = pg.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)
fill_rect = pg.Rect(x, y, fill, BAR_HEIGHT)
pg.draw.rect(surf, GREEN, fill_rect)
pg.draw.rect(surf, WHITE, outline_rect, 2)
答案 0 :(得分:1)
假设player
是Player
类的实例,使用player.draw_shield_bar
是正确的。它失败的原因可能是因为你忘了使用self
作为draw_shield_bar方法的第一个参数。它应该是:
def draw_shield_bar(self, surf, x, y, pct):
如果这不起作用。我想看看完整的Traceback。