简体   繁体   English

有没有办法可以用一个函数改变pygame中许多形状的颜色

[英]is there a way i can change the colour of many shapes in pygame with a function

I am trying to code a clown with hair that the user can change colour of based on user input.我正在尝试编写一个带有头发的小丑,用户可以根据用户输入更改其颜色。 How would I change BLUE to a variable to allow the user to input a colour?如何将 BLUE 更改为变量以允许用户输入颜色? I have only used functions with numbers.我只使用过带数字的函数。

def hair():
pygame.draw.circle(gameWindow, BLUE, (240, 200), 40, 0)
pygame.draw.circle(gameWindow, BLUE, (180, 230), 30, 0)
pygame.draw.circle(gameWindow, BLUE, (300, 230), 30, 0)
pygame.draw.circle(gameWindow, BLUE, (320, 270), 20, 0)
pygame.draw.circle(gameWindow, BLUE, (160, 270), 20, 0)
pygame.draw.circle(gameWindow, BLUE, (210, 230), 20, 0)
pygame.draw.circle(gameWindow, BLUE, (270, 230), 20, 0)
pygame.draw.circle(gameWindow, BLUE, (200, 200), 20, 0)
pygame.draw.circle(gameWindow, BLUE, (280, 200), 20, 0)
pygame.draw.circle(gameWindow, BLUE, (150, 250), 20, 0)
pygame.draw.circle(gameWindow, BLUE, (330, 250), 20, 0)

You can do it in many ways.您可以通过多种方式做到这一点。 One way is to display rectangles with different colors.一种方法是显示具有不同颜色的矩形。 When the user clicks the rectangles the color changes accordingly, something like this in your events loop:当用户单击矩形时,颜色会相应更改,事件循环中的内容如下所示:

for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
        pos = pygame.mouse.get_pos()

        # Different buttons here
        if yellow_button.collidepoint(pos):
            color = YELLOW
        if blue_button.collidepoint(pos):
            color = BLUE

Then you just replace the 'BLUE' in your code with 'color'.然后,您只需将代码中的“蓝色”替换为“颜色”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM