从组

时间:2018-12-08 19:05:27

标签: pygame

我有一组rect,它们连续显示。我希望他们在单击它们后更改其颜色,直到再次单击它们

到目前为止,我已经有了以下代码来创建精灵:

class DrawableRect(pygame.sprite.Sprite):
    def __init__(self,color,width,height,value=0):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.value = value
        self.x = 0
        self.y = 0
    def change_value(self,color,value):
        self.image.fill(color)
        self.value=value

def DrawRects(start_x, start_y, rect_spacing, colour_list):
    current_x_pos = start_x
    for rect_num in range(0,8):
        rect = DrawableRect(colour_list[rect_num], boxW, boxH)
        rect.rect.x = current_x_pos
        rect.rect.y = start_y
        current_x_pos = current_x_pos + rect.rect.width + rect_spacing
        rects.add(rect)
    rects.draw(screen)

该应用的想法是让每个矩形代表一个位,当按下它时,它会在0和1之间交替显示,每个位的组成都会在某处显示等效的十进制数。

我读到组是无序的,因此无法建立索引,对吗?

1 个答案:

答案 0 :(得分:1)

这是我为适应您的目的而修改的示例。我在一个精灵群组中有一堆精灵(彩色矩形),并且我改变了*按下鼠标按钮时碰撞的任何精灵的颜色。

Here it is in action

这是代码,您可能对change_color()方法和MOUSEBUTTONUP事件处理代码最感兴趣。

import random
import pygame

screen_width, screen_height = 640, 480
def get_random_position():
    """return a random (x,y) position in the screen"""
    return (random.randint(0, screen_width - 1),  #randint includes both endpoints.
            random.randint(0, screen_height - 1)) 
color_list = ["red", "orange", "yellow", "green", "cyan", "blue", "blueviolet"]
colors = [pygame.color.Color(c) for c in color_list]

class PowerUp(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        width, height = 64, 32
        self.image = pygame.Surface([width, height])
        self.clicked = False  # track whether we've been clicked or not
        # initialise color
        self.color = random.choice(colors)
        self.image.fill(self.color)
        # Fetch the rectangle object that has the dimensions of the image
        self.rect = self.image.get_rect()
        # then move to a random position
        self.update()

    def update(self):
        #move to a random position
        self.rect.center = get_random_position()

    def random_color(self):
        # randomise color
        self.clicked = not self.clicked
        if self.clicked:
            color = random.choice(colors)
        else:
            color = self.color
        self.image.fill(color)

if __name__ == "__main__":
    pygame.init()
    screen = pygame.display.set_mode((screen_width, screen_height))
    pygame.display.set_caption('Sprite Color Switch Demo')
    clock = pygame.time.Clock() #for limiting FPS
    FPS = 60
    exit_demo = False

    pygame.key.set_repeat(300, 200)

    #create a sprite group to track the power ups.
    power_ups = pygame.sprite.Group()
    for _ in range(10):
        power_ups.add(PowerUp()) # create a new power up and add it to the group.

    # main loop
    while not exit_demo:
        for event in pygame.event.get():            
            if event.type == pygame.QUIT:
                exit_demo = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    exit_demo = True
                elif event.key == pygame.K_SPACE:
                    power_ups.update()
            elif event.type == pygame.MOUSEBUTTONUP:
                # check for collision
                for p in power_ups:
                    if p.rect.collidepoint(event.pos): # maybe use event?
                        p.random_color()

        screen.fill(pygame.Color("black")) # use black background
        power_ups.draw(screen)
        pygame.display.update()
        clock.tick(FPS)
    pygame.quit()
    quit()

如果您有任何疑问,请告诉我。显然,这不会对sprite进行行对齐,我认为您对此有一个了解。我建议您将所有屏幕绘制操作都放在一个位置,以便代码更清晰。

*新颜色是从短列表中随机分配的,因此它有14%的可能性不会与初始颜色保持不变。