为什么这段代码不产生具有随机颜色的形状?

时间:2019-08-21 20:47:06

标签: python python-3.x pygame

我正在研究“ Program Arcade Games”一书 使用Python和Pygame”,并在第12章:类简介的末尾完成“实验”。

我为此编写的代码通过调用其构造函数而不是颜色来随机化在“ my_list”中创建的每个形状的坐标大小和移动方向,创建的所有形状都具有相同的颜色,这是为什么?



import pygame
import random

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

class Rectangle():
    x = 0
    y = 0
    width = 10
    height = 10
    change_x = 2
    change_y = 2
    color = [0, 0, 0]

    def __init__(self):
        self.x = random.randrange(0, 700)
        self.y = random.randrange(0, 500)
        self.change_x = random.randrange(-3., 3)
        self.change_y = random.randrange(-3., 3)
        self.width = random.randrange(20, 70)
        self.height = random.randrange(20, 70)
        for i in range(3):
            self.color[i] = random.randrange(0, 256)


    def draw(self, screen):
        pygame.draw.rect(screen, self.color, [self.x, self.y, self.width, self.height], 0)

    def move(self):
        if self.x < 0:
            self.change_x *= -1
        if self.x > 700-self.width:
            self.change_x *= -1
        if self.y < 0:
            self.change_y *= -1
        if self.y > 500-self.height:
            self.change_y *= -1
        self.x += self.change_x
        self.y += self.change_y

class Ellipse(Rectangle):
    def draw(self, screen):
        pygame.draw.ellipse(screen, self.color, [self.x, self.y, self.width, self.height], 0)



pygame.init()

# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("My Game")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

my_list = []

for i in range(10):
    my_list.append(Rectangle())

for i in range(10):
    my_list.append(Ellipse())


# -------- Main Program Loop -----------
while not done:
    # --- Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # --- Game logic should go here

    # --- Screen-clearing code goes here

    # Here, we clear the screen to white. Don't put other drawing commands
    # above this, or they will be erased with this command.

    # If you want a background image, replace this clear with blit'ing the
    # background image.
    screen.fill(WHITE)

    # --- Drawing code should go here
    for shape in my_list:
        shape.draw(screen)
        shape.move()
    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # --- Limit to 60 frames per second
    clock.tick(60)

# Close the window and quit.
pygame.quit()```

1 个答案:

答案 0 :(得分:5)

从未创建实例属性self.color。现有变量color是类属性。了解Class and Instance Variables的区别。类属性仅存在一次,并且在每个实例中(当然)在读取时具有相同的值。该类的每个实例都存在一个实例变量,并且在每个实例中可以具有不同的值。

通过以下方式创建颜色通道列表:

class Rectangle():

    def __init__(self):

        # [...]

        self.color = [0, 0, 0]
        for i in range(3):
            self.color[i] = random.randrange(0, 256)

分别

class Rectangle():

    def __init__(self):

        # [...]

        self.color = [random.randrange(0, 256) for _ in range(3)]