|
|
帮我把这个代码字体颜色改成蓝色:import pygame
import sys
import random
pygame.init()
# 设置屏幕大小和颜色
screen_width = 600
screen_height = 400
cell_size = 20
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('贪吃蛇')
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
# 定义贪吃蛇和食物
snake = [(100, 100), (80, 100), (60, 100)]
snake_head = snake[0]
food = (random.randint(0, screen_width // cell_size - 1) * cell_size,
random.randint(0, screen_height // cell_size - 1) * cell_size)
food_color = red
# 定义方向
direction = 'RIGHT'
clock = pygame.time.Clock()
def draw_objects():
screen.fill(white)
for pos in snake:
pygame.draw.rect(screen, black, (pos[0], pos[1], cell_size, cell_size))
pygame.draw.rect(screen, food_color, (food[0], food[1], cell_size, cell_size))
def move_snake():
global direction
x, y = snake_head
if direction == 'UP':
y -= cell_size
elif direction == 'DOWN':
y += cell_size
elif direction == 'LEFT':
x -= cell_size
elif direction == 'RIGHT':
x += cell_size
new_head = (x, y)
snake.insert(0, new_head)
if snake_head != food:
snake.pop()
else:
create_food()
def create_food():
global food
food = (random.randint(0, screen_width // cell_size - 1) * cell_size,
random.randint(0, screen_height // cell_size - 1) * cell_size)
def check_collision():
if snake_head[0] < 0 or snake_head[0] >= screen_width or snake_head[1] < 0 or snake_head[1] >= screen_height:
return True
if snake_head in snake[1:]:
return True
return False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and direction != 'DOWN':
direction = 'UP'
elif event.key == pygame.K_DOWN and direction != 'UP':
direction = 'DOWN'
elif event.key == pygame.K_LEFT and direction != 'RIGHT':
direction = 'LEFT'
elif event.key == pygame.K_RIGHT and direction != 'LEFT':
direction = 'RIGHT'
move_snake()
if check_collision():
pygame.quit()
sys.exit()
if snake_head == food:
create_food()
draw_objects()
pygame.display.update()
clock.tick(10) |
|