简体   繁体   English

如何使用pygame Python使表面/图像每秒闪烁2次

[英]How to make surface/image blink 2 times a second with pygame Python

Experimenting with pygame lately and have come across alpha opacity. 最近尝试pygame并遇到alpha不透明。 How is it possible to tune the range of alpha so that my my image/surface will blink two times a second? 如何调整alpha的范围,以使我的图像/表面每秒闪烁两次?

Currently I'm drawing this at 30 frames per second. 目前,我正在以每秒30帧的速度进行绘制。

def blit_image(self):
        for alpha in range(0,255,50):
            for i in self.image_array:
                a = i.set_alpha(alpha)
                i.draw_on(self.screen)
        pygame.display.flip()

Something isn't right here, it's only making the borders of my image opaque, I'm unable to see if the blink is even actually happening. 这里不正确,只是使图像的边框变得不透明,我无法看到眨眼是否真的在发生。 Any thoughts of how to do this? 关于如何执行此操作的任何想法?

Briefly: (your code) You have 30 frames per second (30FPS) so every frame you decrease alpha +1/15 * 255 and after 15 frames you increase alpha -1/15 * 255 and after 15 frames you again decrease etc. 简短地说:(您的代码)您每秒有30帧(30FPS),因此每帧您将alpha降低+1/15 * 255 ,在15帧之后您将alpha -1/15 * 255 ,在15帧之后您再次降低等。

It is full example how to blink background color (it was answer for similar question on SO). 这是一个完整的示例,如何闪烁背景色(这是关于SO的类似问题的答案)。 It is not exacly what you do but maybe it helps you. 这并不完全是您的工作,但也许可以为您提供帮助。

import pygame

#----------------------------------------------------------------------

class Background():

    def __init__(self, screen):
        self.screen = screen

        self.timer = 0
        self.color = 0
        self.up = True # up or down

    #-------------------

    def change(self):

        if self.timer == 15: # 15 frames for UP and 15 frames for DOWN
            self.timer = 0
            self.up = not self.up

        self.timer += 1

        if self.up:
            self.color += 10
        else:
            self.color -= 10

        print self.up, self.color

    #-------------------

    def draw(self):
        self.screen.fill( (self.color, self.color, self.color) )

#----------------------------------------------------------------------

class Game():

    def __init__(self):
        pygame.init()

        self.screen = pygame.display.set_mode((800,600))

        self.background = Background(self.screen)

    #-------------------

    def run(self):

        clock = pygame.time.Clock()

        RUNNING = True

        while RUNNING:

            # ----- events -----

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    RUNNING = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        RUNNING = False

            # ----- changes -----

            self.background.change()

            # ----- draws ------

            self.background.draw()

            pygame.display.update()

            # ----- FPS -----

            clock.tick(30)

        #-------------------

        pygame.quit()

#----------------------------------------------------------------------

Game().run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM