用时间延迟pygame绘制线条



我想在时间延迟下绘制多行。我正在使用循环进行X和Y坐标。这是我的代码:

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()
`

您必须在screen而不是background_image上绘制行。500毫秒为半秒(10毫秒是A bit 快速)。另外,不要为列表和坐标使用相同的变量名称。

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)

此版本仍然有问题,因为您停止执行其余主循环运行时。这意味着您不能在此期间退出或处理其他事件。

使用计时器变量,我为您提供了一个更复杂的示例。您从中减去三角洲时间(自上次滴答以来通过的时间),当它低于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)

您可以使用time.wait(),但这只会冻结您的程序,或者您可以拥有一个可以绘制该行的时间的变量。基本上,当您绘制第一行时具有nextLine = time.clock()+1,并且在循环中说if time.clock > nextLine:,然后绘制下一行。

最新更新