【问题标题】:pygame rectangles drawn in loop scroll across screen循环绘制的pygame矩形在屏幕上滚动
【发布时间】:2016-03-19 12:40:27
【问题描述】:

这是我的完整代码:

 import pygame
pygame.init()

i = 0
x = 0

# Define the colors we will use in RGB format
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
BLUE  = (  0,   0, 255)
GREEN = (  0, 255,   0)
RED   = (255,   0,   0)

# Set the height and width of the screen
size = [600, 300]
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Test")

#Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()

while not done:
    clock.tick(10)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True

    screen.fill(WHITE)

    for x in range(x, x+100, 10):
       pygame.draw.rect(screen, BLACK, [x, 0, 10, 10], 1)

    pygame.display.flip()

pygame.quit()

绘制了 10 个方块,但它们向右滚动窗口,我不知道为什么。有什么办法可以阻止它吗?

谢谢。

我现在意识到这与矩形循环无关,但无论如何我已将其更改为建议的内容。

【问题讨论】:

  • 隔着屏幕是什么意思?因为从我在您提供的代码 sn-p 中可以看到,您正在沿 x 轴绘制 10 个矩形。这也是在屏幕上。 :) 除非您的目标是 x = x+2,否则 x = x+1 也是不正确的,这基本上是因为 for x in range(10) 确实会在每次迭代中增加 x 并且添加额外的增加语句将 x 增加 2。你不'不需要i(在一个范围内你可以设置步长并将限制增加到100)。
  • 跨屏幕是指沿 x 轴的 10 个矩形,然后所有 10 个矩形都在屏幕上移动。你对 x + 1 是正确的,谢谢,但我不明白你不需要 i 的意思(我可能只是愚蠢,抱歉)@rbaleksandar
  • 在那之前你是在做某种翻译吗?我没有使用 PyGame 的经验,但是根据我使用 OpenGL 的经验,例如,如果在 99.9% 的情况下你不希望它在屏幕上移动,那是因为你应用了一些在每次迭代期间都不会重置的转换的主循环,它只是添加到它之前的状态,导致“无限”转换(在你的情况下 - 翻译)。
  • 尝试发布一个最小的工作示例,因为到目前为止,您提供的代码没有任何问题。我刚刚安装了pygame 并使用了它,屏幕上没有任何移动。让你头疼的是循环之外的东西。
  • 我的意思是for x in range(0, 100, 10): pygame.draw.rect(screen, BLACK, [x, 0, 10, 10], 1) 并将x 指定为矩形起点的x 坐标。

标签: python python-2.7 pygame


【解决方案1】:

现在您已经添加了更多代码,我知道问题出在哪里,正如我所怀疑的那样 - 您正在以不正确的方式进行静态翻译,即您使用 x(您已在主循环之外定义) ) 在你的for x in range(x, x+100, 10):

如果您在 for 循环中添加 print(x) 语句,您将能够看到 x 变得越来越大,越来越大......这非常适合为您的场景添加动态,但我的猜测是(根据您的问题)您想在场景中添加 10 个静态矩形。

为此,您需要在每次 while 循环的新迭代开始时重置您的 x

while not done:
    clock.tick(10)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True

    screen.fill(WHITE)

    # Every single iteration of the while loop will first reset the x to its initial value
    # You can make x be any value you want your set of rectangles to start from
    x = 0

    # Now start adding up to x's initial value
    for x in range(x, x+100, 10):
      pygame.draw.rect(screen, BLACK, [x, 0, 10, 10], 1)

    pygame.display.flip()

如果您不使用 for 循环之外的变量来更改第一个矩形开始的 x 坐标并将 range(x, x+100, 10) 替换为 @,您也可以省略 x 的定义987654332@ 其中CONST 是给定值,例如 0、100、1000 等。

【讨论】:

  • 这看起来不错,但我需要在接受之前检查一下。如果您每次都将 x 重置为 0,它不会只是在同一个地方重绘矩形吗? @rbaleksander
  • 是的,绘图将始终从同一位置开始,其余 9 个矩形将根据您定义的 range 跟随。如果您希望矩形移动(因为从其当前状态只能得出结论您希望矩形保持原位),请更改您的问题。
  • 哦,我现在明白了,对不起。谢谢。
  • 所以最后这就是你想要的 - 静态而不是动态矩形?
  • 是的,我的问题可能措辞不当
【解决方案2】:

每次 for 循环运行时,范围都会增加,因为 x 是持久的。如果你删除x=0,在while 中重置它,或者在for 循环中使用不同的变量,我认为它会起作用。

x=0
while not done:

    for x in range(x, x+100, 10):
       pygame.draw.rect(screen, BLACK, [x, 0, 10, 10], 1)

【讨论】:

  • @rbaleksandar 显然没有阅读文本。我展示的代码不是为了工作,而是为了展示问题而没有所有无关的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多