-4

我昨天问了一个关于这个程序的问题,但我遇到了另一个问题。代码仍然运行并认为它以像素为单位着色,但是当到达页面底部时它应该停止,它没有。

这是代码:

import pygame,sys, random, time
y = 0
x = 0
keepgoing = True
pygame.init()
screen = pygame.display.set_mode((500,500))
pixel = raw_input('Please enter a NUMBER that goes into 500. ')
end = 500-(int(pixel))
int(pixel)
screen.fill((255,255,255))

while keepgoing:
    print x,y
    r = random.randint(0,255)
    g = random.randint(0,255)
    b = random.randint(0,255)
    pygame.draw.rect(screen, (r,g,b),(x,y,int(pixel),int(pixel)))
    if x >= end:
        x = 0
        y += int(pixel)
        r = random.randint(0,255)
        g = random.randint(0,255)
        b = random.randint(0,255)
    pygame.draw.rect(screen, (r,g,b),(x,y,int(pixel),int(pixel)))
    if x == end and y == end:
        print 'done'
        keepgoing == False
        break
    x += (int(pixel))

    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); sys.exit();



for event in pygame.event.get():

    if event.type == pygame.QUIT:
        pygame.quit(); sys.exit();

如果你在 python IDLE 中运行代码,你会明白我的意思。

4

3 回答 3

1

尝试改变:

if x >= end:

到:

if x >= end and y <= end:

另外,改变:

    keepgoing == False

到:

    keepgoing = False

另外,考虑改变:

if x == end and y == end:

到:

if x >= end and y >= end:

注意:您可能应该使用嵌套的 for 循环并在 x 像素和 y 像素之间循环。它可能比使用while-loop更好。

于 2013-09-09T17:07:02.403 回答
0

为什么不只使用for循环?

from itertools import product
for x, y in product(range(end), repeat=2):
    ...
于 2013-09-09T17:06:38.087 回答
0
keepgoing == False

==进行比较检查。你要

keepgoing = False
于 2013-09-09T19:45:08.590 回答