当前位置: 首页 > 面试题库 >

如何在时间限制后生成精灵,以及如何显示计时器pygame

翟渝
2023-03-14
问题内容

我想在经过一定时间或生成了x数量的小怪后生成一个“ boss” Sprite,我如何在屏幕上显示计时器。

图片

当前代码

老板班

class Boss(pygame.sprite.Sprite):
def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.transform.scale(boss_img, (150, 200))
    self.image.set_colorkey(Black)
    self.rect = self.image.get_rect()
    self.rect.center = ((Width / 2, -70))
    self.speedy = 1
    self.shoot_delay = 250
    self.last_shot = pygame.time.get_ticks()
    self.hp = 150
    self.dead = False

def update(self):
    if self.hp <= 25:
        self.speedy = 3
    if self.rect.top > Height + 10:
        player.lives = 0

问题答案:

有几种方法可以在pygame中实现计时器。您可以使用pygame.Clock.tick返回的时间来增加或减少计时器变量,使用来计算时间差,pygame.time.get_ticks或者与一起使用自定义事件pygame.time.set_timer

示例1- 增量时间:

import sys
import random
import pygame as pg


class Block(pg.sprite.Sprite):

    def __init__(self, pos):
        super().__init__()
        self.image = pg.Surface((40, 40))
        self.image.fill(pg.Color('sienna1'))
        self.rect = self.image.get_rect(topleft=pos)


def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    font = pg.font.Font(None, 30)
    all_sprites = pg.sprite.Group()
    # Delta time is the time that has passed since clock.tick
    # was called the last time.
    dt = 0
    # We'll subtract dt (delta time) from this timer variable.
    timer = 1  # 1 means one second.

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True

        # Decrease timer to get a countdown.
        timer -= dt
        # When the timer is below or equal to 0, we spawn
        # a new block.
        if timer <= 0:
            all_sprites.add(Block((random.randrange(600),
                                  random.randrange(440))))
            # Reset the countdown timer to one second.
            timer = 1
        all_sprites.update()
        screen.fill(pg.Color('gray15'))
        all_sprites.draw(screen)
        timer_surface = font.render(
            str(round(timer, 3)), True, pg.Color('yellow'))
        screen.blit(timer_surface, (20, 20))

        pg.display.flip()
        # dt = time in seconds that passed since last tick.
        # Divide by 1000 to convert milliseconds to seconds.
        dt = clock.tick(30) / 1000


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()
    sys.exit()

如果您想精确地生成1个Sprite,则可以添加另一个变量,例如,boss_spawned = False并仅在Boss没有生成时更改计时器:

if not boss_spawned:
    timer -= dt
    if timer <= 0:
        all_sprites.add(Block((random.randrange(600),
                               random.randrange(440))))
        boss_spawned = True

或在生成后将计时器设置为正好为0,如果是,则仅减少计时器!= 0

if timer != 0:
    timer -= dt
    if timer <= 0:
        all_sprites.add(Block((random.randrange(600),
                               random.randrange(440))))
        timer = 0

示例2 -pygame.time.get_ticks(替换上面的主要函数):

def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    font = pg.font.Font(None, 30)
    all_sprites = pg.sprite.Group()
    # Start time.
    now = pg.time.get_ticks()
    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True

        # If the time difference is greater than 1000
        # milliseconds, spawn a block.
        time_difference = pg.time.get_ticks() - now
        if time_difference >= 1000:
            all_sprites.add(Block((random.randrange(600),
                                  random.randrange(440))))
            # Reset the start time.
            now = pg.time.get_ticks()
        all_sprites.update()
        screen.fill(pg.Color('gray15'))
        all_sprites.draw(screen)
        timer_surface = font.render(
            str(time_difference/1000), True, pg.Color('yellow'))
        screen.blit(timer_surface, (20, 20))

        pg.display.flip()
        clock.tick(30)

如果只想计算杀戮或生成的生物 ,则可以增加一个计数器变量,然后在超过某个限制时生成敌方首领。以下示例仅计算鼠标的点击次数,并在3次点击后生成一个块。

def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    font = pg.font.Font(None, 30)
    all_sprites = pg.sprite.Group()
    # We'll just count mouse clicks in this example.
    # You can replace it with the kill count in your game.
    clicks = 0
    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            if event.type == pg.MOUSEBUTTONDOWN:
                clicks += 1

        if clicks >= 3:
            all_sprites.add(Block((random.randrange(600),
                                   random.randrange(440))))
            clicks = 0
        all_sprites.update()
        screen.fill(pg.Color('gray15'))
        all_sprites.draw(screen)
        clicks_surface = font.render(str(clicks), True, pg.Color('yellow'))
        screen.blit(clicks_surface, (20, 20))

        pg.display.flip()
        clock.tick(30)


 类似资料:
  • 我想在Kotlin中为我的魔方解算器应用程序生成10毫秒,我该怎么做?我有一个自定义视图,它是一个计时器,我想每10毫秒更新一次计时器

  • 何时会生成绑定类,这里是ActivityMainBinding。我有编译时错误。“无法解析ActivityMainBinding”。 感谢任何帮助。谢谢

  • 问题内容: 目前,每次按键时,精灵仅移动1个像素。按住左右键时,如何使水暖工精灵不断移动? 问题答案: 您可以使用pygame.key.get_pressed来做到这一点。 例:

  • 我已经开始学习Swift和iOS了。为了学习的目的,我用openweathermap API编写了一个天气应用程序。在api响应中,它给出了以Unix UTC时间为单位的时间。我已经在扩展中使用这个函数将时间(Int)转换为字符串- 所以应用程序中的输出看起来像- 现在我想让这个uilabel滴答作响,显示当前的日期和时间,每秒更新一次。我必须为此使用timer()吗?还是有其他解决方案?

  • 问题内容: 我有具有日期字段的表。当我运行查询时,我看到以下内容: 2009年1月10日22:10:39 如何只检索时间(IE:22:10:39) 问题答案: 您可以尝试以下方法: 编辑: 正如@steven指出的那样,要使用24小时样式

  • 我正在开发一个应用程序,我想在屏幕上显示计时器,并用它做动作。我需要当计时器停止时,玩家会输掉比赛。这部分我可以单独做,但我不知道如何做的部分是如何在屏幕上显示计时器,并使剩余的时间可见。 我希望你能帮助我,谢谢你的帮助!