昨天我问了一个关于这个节目的问题,但我遇到了另一个问题。 代码仍然运行并认为它是以像素着色但它应该在到达页面底部时停止,但它不会。
以下是代码:
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中运行代码,你会明白我的意思。
答案 0 :(得分: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 更好。
答案 1 :(得分:0)
为什么不使用for
循环?
from itertools import product
for x, y in product(range(end), repeat=2):
...
答案 2 :(得分:0)
keepgoing == False
==
进行比较检查。你想要
keepgoing = False