帮我用Python重新写一个更好的贪吃蛇游戏代码
时间: 2023-04-09 07:02:39 浏览: 66
好的,我可以回答这个问题。以下是一个简单的贪吃蛇游戏的Python代码:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 设置游戏窗口大小
window_width = 500
window_height = 500
window = pygame.display.set_mode((window_width, window_height))
# 设置游戏标题
pygame.display.set_caption("贪吃蛇游戏")
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
# 定义蛇的初始位置和大小
snake_block_size = 10
snake_speed = 15
snake_list = []
snake_length = 1
# 定义食物的初始位置和大小
food_block_size = 10
food_x = round(random.randrange(0, window_width - food_block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, window_height - food_block_size) / 10.0) * 10.0
# 定义字体
font_style = pygame.font.SysFont(None, 30)
# 定义函数,用于显示分数
def show_score(score):
score_text = font_style.render("分数:" + str(score), True, black)
window.blit(score_text, [0, 0])
# 定义函数,用于画蛇
def draw_snake(snake_block_size, snake_list):
for x in snake_list:
pygame.draw.rect(window, green, [x[0], x[1], snake_block_size, snake_block_size])
# 定义函数,用于显示消息
def message(msg, color):
message_text = font_style.render(msg, True, color)
window.blit(message_text, [window_width / 6, window_height / 3])
# 游戏循环
game_over = False
while not game_over:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# 处理按键事件
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x_change = -snake_block_size
y_change = 0
elif keys[pygame.K_RIGHT]:
x_change = snake_block_size
y_change = 0
elif keys[pygame.K_UP]:
x_change = 0
y_change = -snake_block_size
elif keys[pygame.K_DOWN]:
x_change = 0
y_change = snake_block_size
else:
x_change = 0
y_change = 0
# 更新蛇的位置
snake_head = []
snake_head.append(food_x)
snake_head.append(food_y)
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
# 处理碰撞事件
for x in snake_list[:-1]:
if x == snake_head:
game_over = True
if food_x < 0 or food_x >= window_width or food_y < 0 or food_y >= window_height:
game_over = True
if game_over:
message("游戏结束,按Q退出,按C重新开始", red)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
quit()
elif event.key == pygame.K_c:
game_over = False
snake_list = []
snake_length = 1
food_x = round(random.randrange(0, window_width - food_block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, window_height - food_block_size) / 10.0) * 10.0
break
# 更新食物的位置
if food_x == snake_head[0] and food_y == snake_head[1]:
food_x = round(random.randrange(0, window_width - food_block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, window_height - food_block_size) / 10.0) * 10.0
snake_length += 1
# 清空窗口
window.fill(white)
# 画蛇和食物
draw_snake(snake_block_size, snake_list)
pygame.draw.rect(window, red, [food_x, food_y, food_block_size, food_block_size])
# 显示分数
show_score(snake_length - 1)
# 更新窗口
pygame.display.update()
# 控制游戏速度
clock = pygame.time.Clock()
clock.tick(snake_speed)
# 退出pygame
pygame.quit()
quit()
```
这个代码实现了一个简单的贪吃蛇游戏,包括蛇的移动、食物的生成、碰撞检测、分数统计等功能。你可以根据自己的需求对代码进行修改和优化。