Stepa1411
Stepa1411

Reputation: 27

Drawing polygons in pygame using list

I want to draw a lot of simple polygons. So I decided to make a tuple consisting of all polygons so I can draw them using for poly in POLYGONS.

POLYGONS = (sc,(255,255,255),(0,50),ect.)

for poly in POLYGONS:
    pygame.draw.polygon(poly)

But when I actually do that, function takes this tuples as tge first argument. Is there any way to to this efficien(without i[0],i[1] and so on)? I'm a beginner programmer, so that question may sound as obvious as hell.

Upvotes: 1

Views: 1349

Answers (1)

Rabbid76
Rabbid76

Reputation: 210968

You have to make a list of tuples:

POLYGONS = [
    ((255, 0, 0), [(100, 50), (50, 150), [150, 150]], True),
    ((0, 0, 255), [(100, 150), (50, 50), [150, 50]], True)
]

and you have to unzip the tuple with the asterisk (*) operator:

for poly in POLYGONS:
    pygame.draw.polygon(window, *poly)

Minimal example:

import pygame

pygame.init()
window = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()

POLYGONS = [
    ((255, 0, 0), [(100, 50), (50, 150), [150, 150]], True),
    ((0, 0, 255), [(100, 150), (50, 50), [150, 50]], True)
]

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    window_center = window.get_rect().center

    window.fill(0)
    for poly in POLYGONS:
        pygame.draw.polygon(window, *poly)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()

Upvotes: 1

Related Questions