精灵遇到位置后打印文本

2024-10-01 19:21:32 发布

您现在位置:Python中文网/ 问答频道 /正文

目前我正在使用pygame开发一个小型的“太空入侵者”风格的游戏,我想根据分数/健康水平创建各种结果。我希望这样做,如果敌舰已经通过了玩家,而玩家还没有达到70分的分数限制,我想显示一条消息,说明目标分数还没有达到

到目前为止,我有这个代码,我认为应该允许这种情况发生:

        if block.rect.y == random.randrange(600) and health >=25 and score < 70:
            font = pygame.font.Font("freesansbold.ttf", 30)
            label = font.render("Score target not met", 1, (0,255,0))
            labelRect = label.get_rect()
            labelRect.center = (400, 250)
            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 block.rect.y == random.randrange(600) and health >=25 and score < 70 and score > 0:
            font = pygame.font.Font("freesansbold.ttf", 30)
            label = font.render("Score target not met", 1, (0,255,0))
            labelRect = label.get_rect()
            labelRect.center = (400, 250)
            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)

然而,一旦我执行了这个代码,一旦敌舰经过,就不会有文本被粘贴到窗口上

以下是我的完整代码,以防有人知道这是为什么:

http://pastebin.com/C0V5MnSH


Tags: andrectifallblockpygamelabelremove
1条回答
网友
1楼 · 发布于 2024-10-01 19:21:32

我看到了一些问题。首先,你不应该检查的敌人离开屏幕的内部

for bullet in bullet_list:
    for block in block_hit_list:

应该在主循环上创建新行

第二,每次你打电话

random.randrange(600)

你会得到一个介于0和600之间的新随机数。所以如果块仍在屏幕上,下面的代码有百分之六的几率被运行。如果块离开屏幕,则有0%的几率。相反,写

if block.rect.y >= 600:

所以整个事情应该是这样的:

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)


if block.rect.y >= 600 and health >=25 and score < 70 and score > 0:
    font = pygame.font.Font("freesansbold.ttf", 30)
    label = font.render("Score target not met", 1, (0,255,0))
    labelRect = label.get_rect()
    labelRect.center = (400, 250)

希望有帮助

相关问题 更多 >

    热门问题