目前我正在使用 pygame 开发一款小型“太空侵略者”风格的游戏,我想根据分数/健康水平创造各种结果。我希望这样做,如果敌舰已经通过玩家并离开屏幕并且分数目标尚未达到,那么我希望它显示一条消息,说明目标分数尚未达到。
到目前为止,我有这段代码,我认为应该允许这种情况发生:
if block.rect.y >= 600 and health >=25 and score < 70:
font = pygame.font.Font("freesansbold.ttf", 30)
label2 = font.render("Score target not met", 1, (0,255,0))
label2Rect = label.get_rect()
labelRect.center = (400, 250)
在这里它被更深入地实现:
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
laser.play()
k_space = True
if k_space:
bullet = Bullet()
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
all_sprites_list.add(bullet)
bullet_list.add(bullet)
k_space = False
for player in player_list:
block_hit_list = pygame.sprite.spritecollide(player, block_list, True)
for block in block_hit_list:
collision.play()
block_list.remove(block)
all_sprites_list.remove(block)
health -= 25
if health == 0:
font = pygame.font.Font("freesansbold.ttf", 30)
label = font.render("GAME OVER", 1, (255,0,0))
labelRect = label.get_rect()
labelRect.center = (400, 250)
gameover.play()
collision.stop()
player_list.remove(player)
all_sprites_list.remove(player)
break
for bullet in bullet_list:
block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
for block in block_hit_list:
explosion.play()
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
score += 10
if health >= 25 and score == 70:
win.play()
font = pygame.font.Font("freesansbold.ttf", 30)
label = font.render("LEVEL COMPLETE!", 1, (0,255,0))
labelRect = label.get_rect()
labelRect.center = (400, 250)
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
break
if score >= 70:
collision.stop()
laser.stop()
explosion.stop()
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
block_list.remove(block)
all_sprites_list.remove(block)
player_list.remove(player)
all_sprites_list.remove(player)
if health == 0:
laser.stop()
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
if bullet.rect.y < -10:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
#NOT FUNCTIONING
if block.rect.y >= 600 and health >=25 and score < 70:
font = pygame.font.Font("freesansbold.ttf", 30)
label2 = font.render("Score target not met", 1, (0,255,0))
label2Rect = label.get_rect()
labelRect.center = (400, 250)
#NOT FUNCTIONING
font = pygame.font.Font(None, 30)
text = font.render("Score: " + str(score), True, WHITE)
textpos = text.get_rect(centerx=screen.get_width()/5)
font = pygame.font.Font(None, 30)
text2 = font.render("Health: " + str(health), True, GREEN)
textpos2 = text2.get_rect(centerx=screen.get_height())
all_sprites_list.update()
screen.blit(background, (0,0))
if label != None:
screen.blit(label, labelRect)
if health == 75:
text2 = font.render("Health: " + str(health), True, YELLOW)
elif health == 50:
text2 = font.render("Health: " + str(health), True, ORANGE)
elif health == 25:
text2 = font.render("Health: " + str(health), True, RED)
elif health == 0:
text = font.render(" ", True, BLACK)
text2 = font.render(" ", True, BLACK)
if score >= 70:
text = font.render(" ", True, BLACK)
text2 = font.render(" ", True, BLACK)
screen.blit(text, textpos)
screen.blit(text2, textpos2)
screen.blit(label, labelRect)
all_sprites_list.draw(screen)
pygame.display.update()
clock.tick(190)
pygame.quit()
如果有人知道为什么一旦遇到 if 语句就不会打印文本,我真的很感谢您的帮助。谢谢你。
更新
我刚刚意识到字体确实会打印到屏幕上,但是只有当我设法避免与所有敌舰发生碰撞并且我不发射子弹时才会发生这种情况。有谁知道为什么会发生这种情况?谢谢