I'm new to python and am making snake for a project/ I'm not sure how to randomize all the colors to different colors every time the snake eats and grows. I've looked up multiple things and nothing has worked.
1 Answer
Keep colors in variables like
snake_head_color = 'red'
snake_tail_color = 'green'
In main loop use these variables to draw objects like
# draw head
pygame.draw.rect(screen, snake_head_color, (100, 100, 48, 48))
# draw tail
for x in range(150, 700, 50):
pygame.draw.rect(screen, snake_tail_color, (x, 100, 48, 48))
And later you can use random.choice(list)
to assign new colors when it grows.
snake_head_color = random.choice(['red', 'yellow', 'gray50'])
snake_tail_color = random.choice(['green', 'blue', 'gray25'])
This way it will draw snake using new colors.
Minimal working code. It changes colors when you use mouse button (or wheel)
import pygame
import random
# === constants === # PEP8: UPPER_CASE_NAMES
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
FPS = 120
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# === classes === # PEP8: CamelCaseNames
# empty
# === functions === # PEP8: lower_case_names
# empty
# === main ===
pygame.init()
screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )
# set some color at start
snake_head_color = 'red'
snake_tail_color = 'green'
change_colors = False
# --- mainloop ---
clock = pygame.time.Clock()
running = True
while running:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
change_colors = True
# --- changes/moves/updates ---
if change_colors:
# select random color
snake_head_color = random.choice(['red', 'yellow', 'gray50']) # select new color
snake_tail_color = random.choice(['green', 'blue', 'gray25']) # select new color
change_colors = False
# --- draws ---
screen.fill(BLACK)
# draw head
pygame.draw.rect(screen, snake_head_color, (100, 100, 48, 48))
# draw tail
for x in range(150, 700, 50):
pygame.draw.rect(screen, snake_tail_color, (x, 100, 48, 48))
pygame.display.flip()
# --- FPS ---
dt = clock.tick(FPS)
#pygame.display.set_caption(f'{dt:.2f} ms')
# --- end ---
pygame.quit()
Because it always select new color from the same list so sometimes it may select exactly the same color as current color - and it may seen like it doesn't select color. But you can remove current color from list before select new color.
# --- changes/moves/updates ---
if change_colors:
color = ['red', 'yellow', 'gray50'] # create list with all colors
color.remove(snake_head_color) # remove current color
snake_head_color = random.choice(color) # select new color
color = ['green', 'blue', 'gray25'] # create list with all colors
color.remove(snake_tail_color) # remove current color
snake_tail_color = random.choice(color) # select new color
change_colors = False
BTW:
Previous version needs to create new local list with all colors in every loop.
If you want to keep lists in global variables then it may need to create local copy (using list.copy()
).
# --- constants --- (UPPER_CASE_NAMES)
SNAKE_HEAD_ALLOWED_COLORS = ['red', 'yellow', 'gray50']
SNAKE_TAIL_ALLOWED_COLORS = ['green', 'blue', 'gray25']
# ... later ...
# --- changes/moves/updates ---
if change_colors:
color = SNAKE_HEAD_ALLOWED_COLORS.copy() # create list with all colors (it needs `copy()`)
color.remove(snake_head_color) # remove current color
snake_head_color = random.choice(color) # select new color
color = SNAKE_TAIL_ALLOWED_COLORS.copy() # create list with all colors (it needs `copy()`)
color.remove(snake_tail_color) # remove current color
snake_tail_color = random.choice(color) # select new color
change_colors = False
Eventually you could use list comprehension
to select all colors except current color
# --- constants --- (UPPER_CASE_NAMES)
SNAKE_HEAD_ALLOWED_COLORS = ['red', 'yellow', 'gray50']
SNAKE_TAIL_ALLOWED_COLORS = ['green', 'blue', 'gray25']
# ... later ...
# --- changes/moves/updates ---
if change_colors:
color = [x for x in SNAKE_HEAD_ALLOWED_COLORS if x != snake_head_color]
snake_head_color = random.choice(color) # select new color
color = [x for x in SNAKE_TAIL_ALLOWED_COLORS if x != snake_tail_color]
snake_tail_color = random.choice(color) # select new color
change_colors = False
snake_color
) and use these variables to draw objects - and when snake eats something then assign new (random) values to these variables. You can use code likesnake_color = random.choice(['red', 'green', 'blue', 'yellow'])
\$\endgroup\$