0

我想制作一个 pygame 应用程序,它可以使 500,500 屏幕上的每个像素成为随机颜色,但它只会给顶行着色。

here is the code:

import pygame,sys, random, time
y = 0
x = 0 
true = True
pygame.init()
screen = pygame.display.set_mode((500,500))
screen.fill((255,255,255))
while true:
    r = random.randint(0,255)
    g = random.randint(0,255)
    b = random.randint(0,255)
    print x
    pygame.draw.line(screen, (r,g,b),(x,y),(x,y))
    if x >= 500:
        x == 0
        y += 1
    x += 1
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); sys.exit();
4

2 回答 2

3

您可能应该替换:

    # This checks if x == 0. It obviously isn't but it has no real effect
    x == 0

    # This will assign 0 to x variable if x >= 500
    x = 0
于 2013-09-08T20:34:36.280 回答
1
from pygame.locals import *

def random_colors(surf):
    for x in range(500):
        for y in range(500):
            c = Color(random.randint(0,255), random.randint(0,255), random.randint(0,255))
            surf.set_at((x,y), c)

请注意,如果您希望使用频繁的像素访问,请使用PixelArraysurfarray

于 2013-09-08T23:07:24.297 回答