Text 打印文本-Python/Pygame

Text 打印文本-Python/Pygame,text,python-3.x,printing,pygame,blit,Text,Python 3.x,Printing,Pygame,Blit,目前,我正在使用pygame开发一款小型“太空入侵者”风格的游戏,我希望根据分数/健康水平创建各种结果。我希望这样做,如果敌舰经过玩家并离开屏幕,并且分数目标尚未达到,那么我希望它显示一条消息,说明目标分数尚未达到 到目前为止,我有这个代码,我认为应该允许这种情况发生: if block.rect.y >= 600 and health >=25 and score < 70: font = pygame.font.Font("freesansbold.ttf", 30

目前,我正在使用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)
如果block.rect.y>=600且运行状况>=25且分数<70:
font=pygame.font.font(“freesansbold.ttf”,30)
label2=font.render(“未达到分数目标”,1,(0255,0))
label2Rect=label.get_rect()
labelRect.center=(400250)
在这里,它被更深入地实现:

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()
为True时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
如果event.type==pygame.KEYDOWN和event.key==pygame.K_转义:
pygame.quit()
sys.exit()
elif event.type==pygame.KEYDOWN和event.key==pygame.K_空间:
激光播放
k_空间=真
如果k_空间:
bullet=bullet()
bullet.rect.x=player.rect.x
bullet.rect.y=player.rect.y
所有精灵列表。添加(项目符号)
项目符号列表。添加(项目符号)
k_空间=False
对于玩家列表中的玩家:
block\u hit\u list=pygame.sprite.spritecollide(玩家,block\u list,True)
对于block_命中列表中的block_:
碰撞。播放()
块列表。删除(块)
所有精灵列表。删除(块)
健康-=25
如果运行状况==0:
font=pygame.font.font(“freesansbold.ttf”,30)
label=font.render(“游戏结束”,1,(255,0,0))
labelRect=label.get_rect()
labelRect.center=(400250)
gameover.play()
碰撞。停止()
玩家列表。移除(玩家)
所有精灵列表。移除(玩家)
打破
对于项目符号列表中的项目符号:
block\u hit\u list=pygame.sprite.spritecollide(bullet,block\u list,True)
对于block_命中列表中的block_:
爆炸。播放()
项目符号列表。删除(项目符号)
所有精灵列表。删除(项目符号)
分数+=10
如果健康状况>=25且得分==70:
赢,玩
font=pygame.font.font(“freesansbold.ttf”,30)
label=font.render(“级别完成!”,1,(0255,0))
labelRect=label.get_rect()
labelRect.center=(400250)
项目符号列表。删除(项目符号)
所有精灵列表。删除(项目符号)
打破
如果得分>=70:
碰撞。停止()
激光。停止()
爆炸,停止
项目符号列表。删除(项目符号)
所有精灵列表。删除(项目符号)
块列表。删除(块)
所有精灵列表。删除(块)
玩家列表。移除(玩家)
所有精灵列表。移除(玩家)
如果运行状况==0:
激光。停止()
项目符号列表。删除(项目符号)
所有精灵列表。删除(项目符号)
如果项目符号矩形y<-10:
项目符号列表。删除(项目符号)
所有精灵列表。删除(项目符号)
#不起作用
如果block.rect.y>=600且运行状况>=25且分数<70:
font=pygame.font.font(“freesansbold.ttf”,30)
label2=font.render(“未达到分数目标”,1,(0255,0))
label2Rect=label.get_rect()
labelRect.center=(400250)
#不起作用
font=pygame.font.font(无,30)
text=font.render(“分数:+str(分数),真,白色)
textpos=text.get\u rect(centerx=screen.get\u width()/5)
font=pygame.font.font(无,30)
text2=font.render(“健康状况:+str(健康状况),真,绿色)
textpos2=text2.get_rect(centerx=screen.get_height())
所有精灵列表。更新()
屏幕光点(背景,(0,0))
如果标签!=无:
屏幕。blit(标签,labelRect)
如果运行状况==75:
text2=font.render(“健康状况:+str(健康状况),真,黄色)
elif健康==50:
text2=font.render(“健康状况:+str(健康状况),真,橙色)
elif健康==25:
text2=font.render(“健康状况:+str(健康状况),真,红色)
elif运行状况==0:
text=font.render(“,True,黑色)
text2=font.render(“,True,黑色)
如果得分>=70:
text=font.render(“,True,黑色)
text2=font.render(“,True,黑色)
screen.blit(文本,textpos)
screen.blit(text2,textpos2)
屏幕。blit(标签,labelRect)
所有精灵列表。绘制(屏幕)
pygame.display.update()
时钟滴答(190)
pygame.quit()
如果有人知道为什么一旦满足If语句,文本就不会打印,我真的很感谢你的帮助。多谢各位


更新
我刚刚意识到,字体确实会被打印到屏幕上,但是只有在我设法避免与所有敌舰相撞并且我不发射子弹的情况下,才会发生这种情况。有人知道为什么会发生这种情况吗?谢谢

嗯。。。在我看来,你做的一切都是对的。以下是我的一些代码,我知道它可以在屏幕上快速显示文本:

font = pygame.font.Font("Fonts/Typewriter.ttf", 20)
deathCount = font.render("Deaths: "+str(deaths), 1,(255,255,255))
winTally = font.render("Wins: "+str(winCount), 1,(255, 255, 255))
screen.blit(deathCount, (5, 5))

除此之外,除非您的if声明无效,否则我不确定抱歉://

谢谢您的回复,我尝试了您的建议,但仍然没有成功:(我认为if语句的block.rect.y部分可能有问题,但我对python非常陌生,所以我不确定!您打算如何处理block.rect.y以及何时定义它?