drawing an image for a set number of frames pygame

家住魔仙堡 提交于 2020-12-13 10:02:28

问题


How would I make an image stay on the screen for a set number of frames in pygame? I am trying to make Simon. I need a square to light up for a certain number of time and then return to its original color.

import pygame
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((500,500))

Colors = ['Blue', 'Yellow']
while True:
    clock.tick(60)
    for i in range(len(Colors)):
         if Colors[i] == 'Yellow':
              pygame.draw.rect(screen,(153,153,0),(#bottom left coords))
              # after 20 frames pass
              pygame.draw.rect(scren,(255,255,0),(#bottom left coords))
         
         elif Colors[i] == 'Blue':
              pygame.draw.rect(screen,(0,104,0),(#bottom right coords))
              # after 20 frames pass
              pygame.draw.rect(scren,(0,255,0),(#bottom right coords))


    pygame.display.update()

回答1:


Probably the easiest way is to use the pygame.sprite.Sprite and store the logic of switching colors in the classes, but you need to understand the Sprites first, which I recommend.

Following your example, I recommend the following:

  1. Initialize before the game loop the blue_time_stop variable to e.g. 0
  2. Listen for an event to trigger the color change, e.g. when key is pressed:
for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        blue_time_stop = time.time() * 1000 + 2000
  1. Then you're doing something strange with colors, which I think is generally wrong, nevertheless when you draw the blue color, you might check whether the time has passed (instead of time you can count the number of frames that passed - it doesn't matter that much)
...
elif Colors[i] == 'Blue':
    if time.time() * 1000 > blue_time_stop:
        blue_color = (0,104,0)
    else:
        blue_color = (0,255,0)
    pygame.draw.rect(scren,blue_color ,(#bottom right coords))



回答2:


Here's an example using a timer to randomly cycle an active cell:

import random
import pygame

pygame.init()
clock = pygame.time.Clock()
width, height = 500, 500
screen = pygame.display.set_mode((width, height))
# custom events
PAUSE_TIMER_EVENT = pygame.USEREVENT + 1
COLOR_TIMER_EVENT = pygame.USEREVENT + 2

# using pygame color names
active_colors = ["red", "yellow", "green", "blue"]
background_colors = ["darkred", "yellow4", "darkgreen", "darkblue"]

color_duration = 900  # milliseconds

running = True
paused = False
active_cell = -1  # start without an active cell, so there's a transition
# initialise colour rotation timer
pygame.time.set_timer(COLOR_TIMER_EVENT, color_duration)
# number and location of squares (left, top, width, height)
rects = [(0, 0, 250, 250), (250, 0, 250, 250), (0, 250, 250, 250), (250, 250, 250, 250)]

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == PAUSE_TIMER_EVENT:
            paused = False
            pygame.time.set_timer(PAUSE_TIMER_EVENT, 0)  # cancel pause timer
            pygame.time.set_timer(COLOR_TIMER_EVENT, color_duration)  # reset timer
        elif event.type == COLOR_TIMER_EVENT:
            # change the colour and reset the timer
            active_cell = random.randint(0, 3)
            pygame.time.set_timer(COLOR_TIMER_EVENT, color_duration)  # reset timer
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and not paused:
                pygame.time.set_timer(PAUSE_TIMER_EVENT, 3000)
                pygame.time.set_timer(COLOR_TIMER_EVENT, 0)
                # clear the color timer to prevent updates
                paused = True
            elif event.key in [pygame.K_EQUALS, pygame.K_KP_PLUS]:
                # increase speed, so decrease timer duration, to a limit
                color_duration = max(100, color_duration - 100)
            elif event.key in [pygame.K_MINUS, pygame.K_KP_MINUS]:
                color_duration = min(5000, color_duration + 100)

    # Update the contents of the display
    # rects determines the number of coloured squares
    for i in range(len(rects)):
        if active_cell == i:
            color = pygame.color.Color(active_colors[i])
            pygame.display.set_caption(active_colors[i])
        else:
            color = pygame.color.Color(background_colors[i])
        screen.fill(color, rect=rects[i])

    pygame.display.update()
    clock.tick(60)
pygame.quit()

You can also pause the color rotation for three seconds by pressing Space and change the rotation rate using + and - keys.

The benefit of using timers is that it is independent of the frame rate.

There's no delay between transitions, so no flicker or indication other than the duration if the color remains the same.



来源:https://stackoverflow.com/questions/63141364/drawing-an-image-for-a-set-number-of-frames-pygame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!