我想画一段延时的多行。我正在使用for循环来寻找x和y坐标。我正在寻找一种解决方案来绘制这些线之间的时间延迟(例如1秒)。这是我的代码:
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((1600, 900))
RED = (230, 30, 30)
background_image = pygame.image.load("image.jpg")
background_image = pygame.transform.scale(background_image, (1600,900))
clock = pygame.time.Clock()
# Main loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Fill the background
screen.blit(background_image, [0, 0])
x= [60,70,80]
y= [500,600,700]
for x,y in zip(x,y):
pygame.draw.line(background_image, RED,(x,y),(900,1280), 10)
pygame.display.update()
pygame.time.delay(10)
# Update the screen
pygame.display.flip()
`
答案 0 :(得分:1)
您必须在screen
而不是background_image
上绘制线条。 500毫秒是半秒(10毫秒是位快)。另外,不要对列表和坐标使用相同的变量名称。
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((1024, 768))
RED = (150, 30, 30)
# Replaced the image with a Surface to test the code.
background_image = pygame.Surface((1024, 768))
background_image.fill((50, 90, 140))
clock = pygame.time.Clock()
xs = [60, 70, 80, 90]
ys = [500, 600, 700, 800]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.blit(background_image, [0, 0])
for x, y in zip(xs, ys):
# Draw the lines on the screen not the background.
pygame.draw.line(screen, RED, (x, y), (400, 480), 10)
pygame.display.update()
pygame.time.delay(500)
pygame.display.flip()
clock.tick(30)
此版本仍有问题,因为您停止执行其余主要版本 for循环运行时循环。这意味着您不能在此期间退出或处理其他事件。
我有一个更复杂的例子,你有一个计时器变量。您从它中减去增量时间(自上次刻度后经过的时间),当它低于0时,您将另一个坐标添加到用于绘图的列表中。
import sys
import pygame
pygame.init()
screen = pygame.display.set_mode((1024, 768))
RED = (120, 30, 30)
background_image = pygame.Surface((1024, 768))
background_image.fill((50, 90, 140))
clock = pygame.time.Clock()
xs = [60, 70, 80, 90]
ys = [400, 500, 600, 700]
xys = zip(xs, ys)
startpoints = [] # This holds the current startpoints.
dt = 0
timer = 500 # A countdown timer.
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
timer -= dt # Decrease timer.
if timer <= 0: # If timer < 0 append the next xy coords.
try:
# `next` just gives you the next item in the xys zip iterator.
startpoints.append(next(xys))
# When the zip iterator is exhausted, clear the
# startpoints list and create a new zip iterator.
except StopIteration:
startpoints.clear()
xys = zip(xs, ys)
timer = 500 # Reset the timer.
screen.blit(background_image, [0, 0])
for x, y in startpoints: # Loop over the available startpoints.
pygame.draw.line(screen, RED, (x, y), (400, 480), 10)
pygame.display.flip()
dt = clock.tick(30)
或者您可以使用自定义事件和pygame.time.set_timer
:
add_coord_event = pygame.USEREVENT + 1
pygame.time.set_timer(add_coord_event, 500)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == add_coord_event:
try:
startpoints.append(next(xys))
# When the zip iterator is exhausted, clear the
# startpoints list and create a new zip iterator.
except StopIteration:
startpoints.clear()
xys = zip(xs, ys)
编辑:或者更好的方法是在while循环之前创建起始点列表,并使用i
对其进行切片。然后,您不必将项目从迭代器移动到列表。
xs = [60, 70, 80, 90]
ys = [400, 500, 600, 700]
startpoints = list(zip(xs, ys))
i = 0 # Current index, used to slice startpoints.
clock = pygame.time.Clock()
increase_index_event = pygame.USEREVENT + 1
pygame.time.set_timer(increase_index_event, 500)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == increase_index_event:
i += 1
i %= len(startpoints) + 1 # Keep i in the correct range.
screen.blit(background_image, [0, 0])
for x, y in startpoints[:i]:
pygame.draw.line(screen, RED, (x, y), (500, 580), 10)
pygame.display.flip()
clock.tick(30)
答案 1 :(得分:0)
您可以使用time.wait()
,但这只会冻结您的程序一段时间,或者您可以拥有一个变量来保存该行应该绘制的时间。基本上当你绘制第一行时nextLine = time.clock()+1
并在你的循环中有一个if if time.clock > nextLine:
然后绘制下一行。