我写了一个简单的游戏(只显示麻烦部分)除了health
得分外,一切都很好。我放了一个print语句来检查一个对象是否被另一个对象击中。因此,if statement
是True
因为我看到了print语句,但我的health
得分没有更新。我在这里错过了什么吗?谢谢你的帮助!
sqrls = pygame.image.load("crazySquirrel1.gif")
sqrlsrect = sqrls.get_rect()
sqrls_startx = 200
sqrls_starty = 530
sqrlsrect.center = (sqrls_startx, sqrls_starty)
girl = pygame.image.load('girl1.gif')
girlrect = girl.get_rect()
x = 850
y = 278
girlrect.center = (x, y)
screen.blit(girl,(x, y))
## adding health score
health = 3
font = pygame.font.SysFont("Comic Sans MS", 20)
text = font.render("Health: " + str(health),1, RED)
## main game loop **********************************
x_change = 0
endGame = False
while not endGame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_change = -2
elif event.key == pygame.K_RIGHT:
x_change = 2
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
sqrlsrect = sqrlsrect.move(sqrls_speed) # moving squirrel 1
if sqrlsrect.left < 0 or sqrlsrect.right > w:
sqrls_speed[0] = -sqrls_speed[0]
if sqrlsrect.top < 0 or sqrlsrect.bottom > h:
sqrls_speed[1] = -sqrls_speed[1]
if sqrlsrect.center == girlrect.center:
print ('Ouch s!')
health -= 1
screen.blit(bg, (0, 0))
screen.blit(sqrls, sqrlsrect)
screen.blit(girl, (x, y))
screen.blit(text, (w*0.88, h*0.03))
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
答案 0 :(得分:2)
虽然您正在修改health
,但您并未修改正在显示的实际文本。相反,请尝试使用以下内容:
if sqrlsrect.center == girlrect.center:
print ('Ouch s!')
health -= 1
text = font.render("Health: " + str(health),1, RED)