【问题标题】:Make image appear for 300ms when pressing space按下空格使图像显示 300 毫秒
【发布时间】:2019-05-26 20:03:01
【问题描述】:

我之前问过这个问题,但我认为我自己的表述有误,所以这次我将添加图片。

我有一个游戏,屏幕底部有一个人(实际上是唐纳德特朗普)向来袭的敌人发射子弹向上到屏幕顶部。

他有一把枪,在枪管的末端我想补充一点,当我按下空格键时会出现一个火焰精灵,300 毫秒后它会消失(直到我再次按下空格键并继续循环)。

这是游戏的图片和我的意思:

1 = 未按下任何键

2 = 按下空格

3 = 空间不再被按下,超过 300 毫秒,现在我希望火焰精灵消失,直到再次按下空间

我该怎么做? :)

【问题讨论】:

标签: python image pygame sprite


【解决方案1】:

只需创建一个变量来存储按下键时的超时值,然后从该值中减去每帧经过的时间。

如果该值 > 0,则显示带有火焰的图像。如果该值为 0,则显示没有火的图像。

这是我一起破解的一个简单示例:

import pygame

class Actor(pygame.sprite.Sprite):
    def __init__(self, *grps):
        super().__init__(*grps)
        # create two images:
        # 1 - the no-fire-image
        # 2 - the with-fire-image
        self.original_image = pygame.Surface((100, 200))
        self.original_image.set_colorkey((1,2,3))
        self.original_image.fill((1,2,3))
        self.original_image.subsurface((0, 100, 100, 100)).fill((255, 255, 255))
        self.fire_image = self.original_image.copy()
        pygame.draw.rect(self.fire_image, (255, 0, 0), (20, 0, 60, 100))

        # we'll start with the no-fire-image
        self.image = self.fire_image
        self.rect = self.image.get_rect(center=(300, 240))

        # a field to keep track of our timeout
        self.timeout = 0

    def update(self, events, dt):

        # every frame, substract the amount of time that has passed
        # from your timeout. Should not be less than 0.
        if self.timeout > 0:
            self.timeout = max(self.timeout - dt, 0)

        # if space is pressed, we make sure the timeout is set to 300ms
        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_SPACE]:
            self.timeout = 300

        # as long as 'timeout' is > 0, show the with-fire-image 
        # instead of the default image
        self.image = self.original_image if self.timeout == 0 else self.fire_image

def main():
    pygame.init()
    screen = pygame.display.set_mode((600, 480))
    screen_rect = screen.get_rect()
    clock = pygame.time.Clock()
    dt = 0
    sprites_grp = pygame.sprite.Group()

    # create out sprite
    Actor(sprites_grp)

    # standard pygame mainloop
    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return

        sprites_grp.update(events, dt)

        screen.fill((80, 80, 80))
        sprites_grp.draw(screen)
        pygame.display.flip()
        dt = clock.tick(60)

if __name__ == '__main__':
    main()

【讨论】:

  • 很抱歉没有尽快回复,但很忙。首先,谢谢!似乎可以正常工作。但有一件事:我也有它,如果我按左键,图像会水平翻转,如果我按右键,则不会水平翻转。我需要四个不同的if,因为我会在这里有火焰照片吗?这是没有flamepic的原版:
  • self.screen.blit(self.image, self.rect) if self.orientation == "Right": self.screen.blit(self.image, self.rect) elif self.orientation == "左": self.screen.blit(pygame.transform.flip(self.image, True, False), self.rect)
  • 所以我的意思是,我是否需要类似的东西:如果左和超时> 0:显示flamepic并水平翻转elif左和超时== 0:水平翻转elif右和超时> 0:显示flamepic unflipped elif right and timeout == 0: Show original unflipped 我想你知道我的意思嘿嘿? :) 编辑:抱歉发了 3 条回复,我一直按回车键
  • 其实没关系,我也得到了它与精灵的翻转 :) 再次感谢
猜你喜欢
  • 2019-11-25
  • 2023-03-22
  • 1970-01-01
  • 2012-09-02
  • 2013-03-17
  • 1970-01-01
  • 2011-03-06
  • 2013-08-08
  • 2014-12-07
相关资源
最近更新 更多