Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Still new to using python and have a question. I'm working on changing colors on a screen using an array.

For example:

RED = (255,0,0)

BLACK= (0,0,0)

ColorArray = [RED,BLACK]

pygame.draw.rect(Page1,ColorArray[1],(80,60,100,10))

pygame.draw.rect(Page1,ColorArray[0],(80,60,100,10))

When I use my array, I get an error for an invalid color argument. Is there a workaround for this? When I just put RED or BLACK in the color location, everything works great.

Thanks,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
185 views
Welcome To Ask or Share your Answers For Others

1 Answer

Could you share your full code, because it works for me:

import pygame as pg
from pygame.locals import *

Page1 = pg.display.set_mode((500, 500), 0, 32)

RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

ColorArray = [RED, BLACK]

Page1.fill(WHITE)

pg.draw.rect(Page1, ColorArray[0], (80, 60, 100, 10))
pg.draw.rect(Page1, ColorArray[1], (100, 80, 100, 10))

pg.display.update()

while True:
    for event in pg.event.get():
        if event.type == QUIT:
            pg.quit()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...