【发布时间】: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