I recently started to learn pygame
and herein lies my question. Is there anyway I can use different colors for each line drawn using pygame.draw.lines
? This is my code.
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480), pygame.SCALED)
screen.fill('white')
pygame.draw.lines(screen, 'red', True, [(10, 100), (20, 200), (30, 100)]) # all lines are red
while True:
pygame.display.update()
Output I get
Output I want
What I tried
pygame.draw.lines(screen, ['green', 'blue', 'orange'], True, [(10, 100), (20, 200), (30, 100)])
Error
ValueError: invalid color argument
In other words, I want to achieve what I want in the following code but using pygame.draw.lines
pygame.draw.line(screen, 'green', (10, 100), (20, 200))
pygame.draw.line(screen, 'blue', (20, 200), (30, 100))
pygame.draw.line(screen, 'orange', (30, 100), (10, 100))
I checked the pygame.draw.lines
docs but I am not sure if the color
argument there takes a list as I have given. It only mentions "Color or int or tuple(int, int, int, [int]))
"
This example is for a triangle, but in my actual use-case I want to draw a decagon and I am not sure if using pygame.draw.line
10 times is the way to go if I want a different color for each line.
PS : Do let me know if any clarification is needed. Many Thanks.
You have to draw each line segment separately. Write a function that uses a list of points and colors to draw the line:
def draw_colorful_line(surf, colors, closed, points, width=1):
for i in range(len(points)-1):
pygame.draw.line(surf, colors[i], points[i], points[i+1], width)
if closed:
pygame.draw.line(surf, colors[-1], points[-1], points[0], width)
Use the function to draw the line:
colors = ['green', 'blue', 'red']
points = [(10, 100), (20, 200), (30, 100)]
draw_colorful_line(screen, colors, True, points)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With