【问题标题】:whats wrong with my pygame application?我的 pygame 应用程序有什么问题?
【发布时间】:2013-09-12 08:37:34
【问题描述】:

我想制作一个 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();

【问题讨论】:

    标签: python colors import pygame rgb


    【解决方案1】:

    你应该替换:

        # 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
    

    【讨论】:

      【解决方案2】:
      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)
      

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

      【讨论】:

        猜你喜欢
        • 2013-01-04
        • 1970-01-01
        • 2013-02-16
        • 1970-01-01
        • 2021-03-23
        相关资源
        最近更新 更多