import pygame
import random
# 初始化 Pygame 库
pygame.init()
# 游戏窗口的大小
window_width = 800
window_height = 600
# 创建游戏窗口
game_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
# 定义字体
font_style = pygame.font.SysFont(None, 50)
# 显示分数
def show_score(score):
score_font = font_style.render("Score: " + str(score), True, white)
game_window.blit(score_font, [0, 0])
# 画蛇
def draw_snake(snake_block_size, snake_list):
for x in snake_list:
pygame.draw.rect(game_window, green, [x[0], x[1], snake_block_size, snake_block_size])
# 游戏主循环
def game_loop():
game_over = False
game_close = False
# 蛇的初始位置
x1 = window_width / 2
y1 = window_height / 2
# 蛇的移动方向
x1_change = 0
y1_change = 0
# 蛇的长度
snake_list = []
Length_of_snake = 1
# 随机生成食物的位置
foodx = round(random.randrange(0, window_width - snake_block_size) / 10.0) * 10.0
foody = round(random.randrange(0, window_height - snake_block_size) / 10.0) * 10.0
# 游戏循环
while not game_over:
# 游戏结束
while game_close:
game_window.fill(black)
message = font_style.render("Game Over!", True, red)
game_window.blit(message, [window_width / 3, window_height / 3])
show_score(Length_of_snake - 1)
pygame.display.update()
# 按下 Q 键退出游戏,按下 C 键重新开始游戏
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
game_loop()
# 判断按键事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block_size
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block_size
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block_size
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block_size
x1_change = 0
# 判断蛇是否撞墙
if x1 >= window_width or x1 < 0 or y1 >= window_height or y1 < 0:
game_close = True
# 更新蛇的位置
x1 += x1_change
y1 += y1_change
# 游戏窗口背景色
game_window.fill(black)
# 画出食物
pygame.draw.rect(game_window, red, [foodx, foody, snake_block_size, snake_block_size])
# 将蛇的位置加入蛇的列表中
snake_head = []
snake_head.append(x1)
snake_head.append(y1)
snake_list.append(snake_head)
# 如果蛇的长度大于 Length_of_snake,则删除列表中的第一个元素
if len(snake_list) > Length_of_snake:
del snake_list[0]
# 判断蛇是否吃到食物
for x in snake_list[:-1]:
if x == snake_head:
game_close = True
# 画出蛇
draw_snake(snake_block_size, snake_list)
# 显示分数
show_score(Length_of_snake - 1)
# 更新游戏窗口
pygame.display.update()
# 如果蛇吃到食物,则增加蛇的长度,并重新生成食物的位置
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, window_width - snake_block_size) / 10.0) * 10.0
foody = round(random.randrange(0, window_height - snake_block_size) / 10.0) * 10.0
Length_of_snake += 1
# 控制蛇的速度
clock = pygame.time.Clock()
clock.tick(snake_speed)
# 退出 Pygame 库
pygame.quit()
# 退出 Python 解释器
quit()
# 运行游戏主循环
game_loop()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下