在python pygame中使用类更改对象颜色时,如何'cycle'列表项?



我用pygame.draw画了一只毛毛虫,它的颜色设置为self.color_scheme[0]

self.color_scheme = [red,yellow,purple]
...
...
pygame.draw.ellipse(screen, self.color_scheme[0], [x, y, 40, 45])

我想启用的是,当用户按下一个键,让我们说s然后颜色会改变为self.color_scheme[1]self.color_scheme[2]

类似

的内容
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_s:
            #scroll through self.color_scheme or randomly select a color

循环:

self.color_scheme_idx = -1
if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_s:
        self.color_scheme_idx += 1
        pygame.draw.ellipse(screen, self.color_scheme[self.color_scheme_idx], [x, y, 40, 45])
随机

:

import random
if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_s:
        pygame.draw.ellipse(screen, random.choice(self.color_scheme), [x, y, 40, 45])