又是同一个程序

2024-09-27 07:21:16 发布

您现在位置:Python中文网/ 问答频道 /正文

昨天我问了一个关于这个项目的问题,但我遇到了另一个问题。 代码仍在运行,并认为它以像素为单位着色,但当到达页面底部时,它应该停止,而不是停止。你知道吗

代码如下:

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运行代码,您就会明白我的意思。你知道吗


Tags: 代码eventifdisplaysysrandomscreenpygame
3条回答

为什么不直接使用for循环呢?你知道吗

from itertools import product
for x, y in product(range(end), repeat=2):
    ...
keepgoing == False

==进行比较检查。你想要什么

keepgoing = False

尝试更改:

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循环更好。你知道吗

相关问题 更多 >

    热门问题