Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 用延时画线游戏_Python 2.7_Pygame - Fatal编程技术网

Python 2.7 用延时画线游戏

Python 2.7 用延时画线游戏,python-2.7,pygame,Python 2.7,Pygame,我想画多条延迟线。我正在使用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.trans

我想画多条延迟线。我正在使用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()

`

您可以使用
time.wait()
,但这只会将程序冻结一段时间,或者您可以使用一个变量来保存绘制线的时间。基本上,当你画第一行时,你必须在
nextLine=time.clock()+1
循环中有一个if,说
if time.clock>nextLine:
,然后画下一行。

你必须在
屏幕上画线,而不是在
背景图像上画线。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)
编辑:或者最好在while循环之前创建startpoint列表,并使用
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)

首先,您需要正确格式化代码,以便缩进与实际代码匹配。否则,我们将不得不手动编辑您的代码来测试它,这需要不必要的时间/精力和一些甚至可能不正确的猜测工作。第二,寻求调试的问题必须包括期望的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。因此,您需要添加您所提供的代码的错误,以及错误原因(它是否引起错误或做了不希望的事情等)?最后一个是最好的。但这让我很难弄清楚如何删除旧的行。你为什么要删除它们?如果你解释得更详细一点,我可以提供另一个例子,因为我的列表中有100多个坐标。我希望在绘制下一条线之前,第一条线在半秒钟内可见。我仍然不确定您到底想做什么。请描述一下你的最终目标。我想我找到了答案。我不能删除一条线,所以我能做的是每次画一条线时我都会执行screen.blit(background_image,[0,0])。结果将是第一条线消失,并绘制下一条线。
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)