用户:
爱坤创作者查看:0 回复:0 评论:0 创建时间:2024-05-12T14:58:34
【作品展示】

【作品介绍】
这是一款使用pygame库制作的贪吃蛇游戏,当游戏失败后按空格键重新开始,shift键退出游戏。
【作品源代码】
import pygame
import time
import random
# 初始化 Pygame 应用程序
pygame.init()
# 设置游戏窗口标题
pygame.display.set_caption('贪吃蛇')
# 创建字体对象,用于在游戏界面中显示文字
font_style = pygame.font.Font("C:/Windows/Fonts/STFANGSO.TTF", 20)
score_font = pygame.font.Font("C:/Windows/Fonts/STCAIYUN.TTF", 30)
# 定义一些颜色常量
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
# 定义游戏窗口的宽度和高度
dis_width = 600
dis_height = 400
# 创建游戏窗口
dis = pygame.display.set_mode((dis_width, dis_height))
# 创建时钟对象,用于控制游戏帧率
clock = pygame.time.Clock()
# 定义贪吃蛇方块大小和移动速度
snake_block = 10
snake_speed = 12
# 定义函数:显示分数
def Your_score(score):
value = score_font.render("分数: " + str(score), True, yellow)
dis.blit(value, [0, 0])
# 定义函数:绘制贪吃蛇
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis, white, [x[0], x[1], snake_block, snake_block])
# 定义函数:显示消息
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width / 6, dis_height / 3])
# 定义游戏循环函数
def gameLoop():
game_over = False
game_close = False
# 初始化贪吃蛇的位置和速度
x1 = dis_width / 2
y1 = dis_height / 2
x1_change = 0
y1_change = 0
snake_List = [] # 贪吃蛇的身体坐标列表
Length_of_snake = 1 # 初始贪吃蛇长度为1
# 随机生成食物的位置
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
# 游戏循环
while not game_over:
# 处理游戏结束的情况
while game_close == True:
dis.fill(blue)
message("你失败了,请重新开始游戏!", white)
Your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LSHIFT: # 按下左Shift键,退出游戏
game_over = True
game_close = False
if event.key == pygame.K_SPACE: # 按下空格键,重新开始游戏
gameLoop()
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 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0: # 如果贪吃蛇碰到边界,游戏结束
game_close = True
x1 += x1_change
y1 += y1_change
dis.fill(black) # 填充窗口背景色为黑色
pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])
snake_Head = [] # 创建贪吃蛇头部坐标列表
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head) # 将头部坐标添加到贪吃蛇身体坐标列表
if len(snake_List) > Length_of_snake: # 如果贪吃蛇长度超过要求长度,删除尾部坐标
del snake_List[0]
for x in snake_List[:-1]:# 检测贪吃蛇是否撞到自己身体,如果是则游戏结束
if x == snake_Head:
game_close = True
our_snake(snake_block, snake_List) # 绘制贪吃蛇
Your_score(Length_of_snake - 1) # 显示分数
pygame.display.update()# 刷新游戏界面
if x1 == foodx and y1 == foody: # 如果贪吃蛇头部与食物位置重合
foodx = round(random.randrange( # 随机生成新的食物位置
0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(
0, dis_height - snake_block) / 10.0) * 10.0
Length_of_snake += 1 # 贪吃蛇长度增加
clock.tick(snake_speed) # 控制游戏帧率
pygame.quit() # 退出 Pygame 应用
quit() # 退出 Python 解释器
gameLoop() # 重新调用游戏循环函数,实现游戏的循环运行
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn