帮我生成一个贪吃蛇游戏的python代码
时间: 2024-02-05 16:02:35 浏览: 136
好的,以下是一个简单的贪吃蛇游戏的Python代码:
```python
import pygame
import random
# 初始化pygame模块
pygame.init()
# 设置游戏界面大小
screen_width = 480
screen_height = 480
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)
green = (0, 255, 0)
# 设置字体
font = pygame.font.SysFont(None, 25)
# 设置蛇的大小和速度
snake_size = 10
snake_speed = 15
# 定义函数,显示文字
def display_text(msg, color):
text = font.render(msg, True, color)
screen.blit(text, [screen_width/6, screen_height/3])
# 定义函数,绘制蛇
def draw_snake(snake_size, snake_pixels):
for pixel in snake_pixels:
pygame.draw.rect(screen, green, [pixel[0], pixel[1], snake_size, snake_size])
# 定义函数,运行游戏
def run_game():
# 初始化蛇的位置和长度
snake_x = screen_width / 2
snake_y = screen_height / 2
snake_pixels = [[snake_x, snake_y]]
snake_length = 1
# 初始化食物的位置
food_x = round(random.randrange(0, screen_width - snake_size) / 10.0) * 10.0
food_y = round(random.randrange(0, screen_height - snake_size) / 10.0) * 10.0
# 设置初始方向
direction = "up"
change_to = direction
# 设置游戏结束标志
game_over = False
# 开始游戏循环
while not game_over:
# 监听事件
for event in pygame.event.get():
# 如果是退出事件,则退出游戏
if event.type == pygame.QUIT:
game_over = True
# 如果是键盘事件,则改变方向
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
change_to = "up"
if event.key == pygame.K_DOWN:
change_to = "down"
if event.key == pygame.K_LEFT:
change_to = "left"
if event.key == pygame.K_RIGHT:
change_to = "right"
# 根据方向改变蛇的位置
if change_to == "up" and direction != "down":
direction = "up"
if change_to == "down" and direction != "up":
direction = "down"
if change_to == "left" and direction != "right":
direction = "left"
if change_to == "right" and direction != "left":
direction = "right"
# 根据方向计算蛇头的位置
if direction == "up":
snake_y -= snake_size
if direction == "down":
snake_y += snake_size
if direction == "left":
snake_x -= snake_size
if direction == "right":
snake_x += snake_size
# 判断是否吃到食物
if snake_x == food_x and snake_y == food_y:
food_x = round(random.randrange(0, screen_width - snake_size) / 10.0) * 10.0
food_y = round(random.randrange(0, screen_height - snake_size) / 10.0) * 10.0
snake_length += 1
# 更新蛇的位置
snake_pixels.append([snake_x, snake_y])
if len(snake_pixels) > snake_length:
del snake_pixels[0]
# 判断是否碰到墙壁或自己的身体
if snake_x < 0 or snake_x > screen_width - snake_size or snake_y < 0 or snake_y > screen_height - snake_size:
game_over = True
for pixel in snake_pixels[:-1]:
if pixel == [snake_x, snake_y]:
game_over = True
# 设置背景颜色
screen.fill(black)
# 绘制食物和蛇
pygame.draw.rect(screen, red, [food_x, food_y, snake_size, snake_size])
draw_snake(snake_size, snake_pixels)
# 更新屏幕
pygame.display.update()
# 判断游戏是否结束
if game_over:
display_text("游戏结束,按Q退出或者按R重新开始", white)
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()
if event.key == pygame.K_r:
run_game()
# 设置游戏速度
clock = pygame.time.Clock()
clock.tick(snake_speed)
# 运行游戏
run_game()
```
请注意,此代码是一个简单的示例,您可以根据需要进行修改和优化。
阅读全文