简体   繁体   中英

What are the different colors for pygame.draw.rect() in Python?

I am learning to use PyGame to make a snake game. Now here is how the code looks so far(It's incomplete, please don't give actual code for making a snake game:):

# Setting up PyGame and a window
import pygame
pygame.init()
dis = pygame.display.set_mode((400, 300))
pygame.display.update()
pygame.display.set_caption('Snake game by snoopstick')
game_over = False

# Creating Colors
blue = (0, 0, 225)
red = (255, 0, 0)

while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
    pygame.draw.rect(dis, blue, [200, 150, 10, 10])
    pygame.display.update()

pygame.quit()
quit()

You can see I already can make red and blue, but I want to know how to make more colors or more different types of games. Just what different parameters can go in place of blue and red.

Those are RGB colors , there is a selector in the link provided!

Colors are a mixture of red, green, and blue. Just like paint. You control the amount of each in the mix by using values between 0 and 255 (inclusive).

You can make any color you want, with the Pygame Color class - https://www.pygame.org/docs/ref/color.html

Take a look at the Color class constructor for a variety of ways to create colors.

If you want to try out some color mixing, use the HTML color picker page .

pygame has a large number of named colors predefined, which is useful. Instead of redefining them yourself, you can just get the red and blue colors from pygames predefined list:

red = pygame.Color('red')
blue = pygame.Color('blue')

You can see all the colors directly by looking at the dict pygame.color.THECOLORS, but that is a lot of colors to go through. Here is a link to my answer to a previous answer to a similar question. Maybe I should just leave it to the link for the answer? Not sure, so I will include enough to be useful.

I find this code snippet handy to execute in a interactive session and then I can just use the one I want in my actual code.

import pygame
from pprint import  pprint

def print_colors(color_name):
    color_list = [(c, v) for c, v in pygame.color.THECOLORS.items() if color_name in c]
    pprint(color_list)

print_colors('slategrey')

which outputs:

[('darkslategrey', (47, 79, 79, 255)),
 ('slategrey', (112, 128, 144, 255))
 ('lightslategrey', (119, 136, 153, 255))]

I do that in an interactive session to get all the names that include 'slategrey' and then in my actual code I can use the one I want like this:

darkslategrey = pygame.Color('darkslategrey')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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