贪吃蛇小游戏代码
import pygame
import time
import random
初始化pygame
pygame.init()
定义颜色
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
定义屏幕大小
width = 600
height = 400
创建游戏窗口
game_window = pygame.display.set_mode((width, height))
pygame.display.set_caption('贪吃蛇游戏')
定义时钟
clock = pygame.time.Clock()
定义蛇的大小和速度
snake_block = 10
snake_speed = 15
定义字体样式
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
显示得分
def display_score(score):
value = score_font.render("得分: " + str(score), True, yellow)
game_window.blit(value, [0, 0])
绘制蛇
def draw_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(game_window, green, [x[0], x[1], snake_block, snake_block])
显示消息
def display_message(msg, color):
mesg = font_style.render(msg, True, color)
game_window.blit(mesg, [width / 6, height / 3])
游戏循环
def game_loop():
game_over = False
game_close = False
# 蛇的初始位置
x1 = width / 2
y1 = height / 2
# 蛇的移动变化量
x1_change = 0
y1_change = 0
# 蛇的身体
snake_list = []
snake_length = 1
# 食物的位置
food_x = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
food_y = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
while not game_over:
while game_close:
game_window.fill(blue)
display_message("你输了! 按Q退出游戏或C重新开始", red)
display_score(snake_length - 1)
pygame.display.update()
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
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0
# 如果蛇撞到边界,游戏结束
if x1 >= width or x1 < 0 or y1 >= height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
game_window.fill(black)
pygame.draw.rect(game_window, red, [food_x, food_y, snake_block, snake_block])
snake_head = []
snake_head.append(x1)
snake_head.append(y1)
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_close = True
draw_snake(snake_block, snake_list)
display_score(snake_length - 1)
pygame.display.update()
# 如果蛇吃到食物,增加长度
if x1 == food_x and y1 == food_y:
food_x = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
food_y = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
snake_length += 1
clock.tick(snake_speed)
pygame.quit()
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代理 了,记录一下