如何在经过时间后更新显示的元素?
How to update elements on display after passed time?
我在 Pygame 中得到了一个程序,它允许我显示列表 list
中的元素。每 3 秒,它更新并显示 list
中的下一个元素。
我的问题是元素在屏幕上重叠,但我想在每次 3 秒后更新它。我已经用过:
pygame.display.update()
但它不起作用。
list = ["x", "y", "z"]
if time > 3 and i < len(list):
font = pygame.font.SysFont("comicsansms", 72)
text2 = font.render(str(list[i]), True, (0, 128, 0))
screen.blit(text2,
(430 - text2.get_width() // 1, 220 - text2.get_height() // 2))
pygame.display.update()
pygame.display.flip()
clock.tick(30)
i = i + 1
这是每三秒更新一次显示的内容:
import sys
import time
import pygame
from pygame.locals import *
pygame.init()
FPS = 30
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
clock = pygame.time.Clock()
font = pygame.font.SysFont("comicsansms", 72)
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Test')
my_list = ["x", "y", "z"]
bkgr = BLACK
i = len(my_list) - 1 # Index of last elememnt (so first is next displayed).
start_time = 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if (time.time() - start_time) > 3: # 3 seconds since last update?
i = (i + 1) % len(my_list)
start_time = time.time()
screen.fill(bkgr)
text2 = font.render(str(my_list[i]), True, (0, 128, 0))
screen.blit(text2, (430 - text2.get_width() // 1,
220 - text2.get_height() // 2))
pygame.display.update()
clock.tick(FPS)
我在 Pygame 中得到了一个程序,它允许我显示列表 list
中的元素。每 3 秒,它更新并显示 list
中的下一个元素。
我的问题是元素在屏幕上重叠,但我想在每次 3 秒后更新它。我已经用过:
pygame.display.update()
但它不起作用。
list = ["x", "y", "z"]
if time > 3 and i < len(list):
font = pygame.font.SysFont("comicsansms", 72)
text2 = font.render(str(list[i]), True, (0, 128, 0))
screen.blit(text2,
(430 - text2.get_width() // 1, 220 - text2.get_height() // 2))
pygame.display.update()
pygame.display.flip()
clock.tick(30)
i = i + 1
这是每三秒更新一次显示的内容:
import sys
import time
import pygame
from pygame.locals import *
pygame.init()
FPS = 30
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
clock = pygame.time.Clock()
font = pygame.font.SysFont("comicsansms", 72)
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Test')
my_list = ["x", "y", "z"]
bkgr = BLACK
i = len(my_list) - 1 # Index of last elememnt (so first is next displayed).
start_time = 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if (time.time() - start_time) > 3: # 3 seconds since last update?
i = (i + 1) % len(my_list)
start_time = time.time()
screen.fill(bkgr)
text2 = font.render(str(my_list[i]), True, (0, 128, 0))
screen.blit(text2, (430 - text2.get_width() // 1,
220 - text2.get_height() // 2))
pygame.display.update()
clock.tick(FPS)