贪吃蛇
初始版本
初始版本,只存在基本数据结构——双向队列。
游戏思路
贪吃蛇通过不断得吃食物来增长自身,如果贪吃蛇碰到边界或者自身则游戏失败。
食物是绿色矩形来模拟,坐标为随机数生成,定义一个蛇长变量,判断蛇头坐标和食物坐标是否接近,如果蛇头接近食物,蛇长增加一个单位。
蛇结构体通过双向队列
实现蛇的移动和增长。用pygame相应库函数读取键盘事件,每次事件发生都对应蛇头的相应方向,例如按键盘下键贪吃蛇向下走。蛇的移动是通过在队头添加一个新的坐标,然后删掉队尾元素实现的。队列的长度始终为蛇长变量的值。
得分变量为蛇长变量减去一个单位,初始化的时候蛇长就为1个单位长度,故而需要减1。
完整代码
import pygame
import random
pygame.init()
# 以RGB的形式定义颜色
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 = 800
dis_height = 600
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('贪吃蛇 KevenDuan1.0')
# 创建clock对象
clock = pygame.time.Clock()
# 蛇的宽度
snake_block = 15
# 蛇的速度
snake_speed = 15
# 从系统库里获取字体
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
def Your_score(score):
value = score_font.render("Score: " + str(score), True, yellow)
# 在主surface里添加字体surface
dis.blit(value, [0, 0])
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width // 2 - 180, dis_height // 2])
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
# 食物坐标随机生成
foodx = round(random.randrange(snake_block, dis_width - snake_block, snake_block))
foody = round(random.randrange(snake_block, dis_height - snake_block, snake_block))
print(foodx, foody)
while not game_over:
while game_close == True:
dis.fill(blue)
message("Game over, press p again or q quit!", red)
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_q:
game_over = True
game_close = False
if event.key == pygame.K_p:
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 or event.key == pygame.K_a:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_w:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
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(blue)
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 foodx - 10 <= x1 <= foodx + 10 and foody - 10 <= y1 <= foody + 10:
foodx = round(random.randrange(snake_block, dis_width - snake_block, snake_block))
foody = round(random.randrange(snake_block, dis_height - snake_block, snake_block))
# print(foodx, foody)
Length_of_snake += 1
# 调节帧率来控制蛇的速度
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()
代码讲解
使用的pygame
和random
两个库。Pygame是一种流行的Python游戏开发库,它提供了许多功能,使开发人员可以轻松创建2D游戏。它具有良好的跨平台支持,可以在多个操作系统上运行。random库是用来获取随机数的,后面使用其获取食物的随机坐标。
import pygame
import random
初始化一些后期用到的参数,以及初始化pygame,创建clock时钟对象。
# 初始化pygame
pygame.init()
# 以RGB的形式定义颜色
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 = 800
dis_height = 600
dis = pygame.display.set_mode((dis_width, dis_height))
# 设置窗体的名称
pygame.display.set_caption('贪吃蛇 KevenDuan1.0')
# 创建clock对象
clock = pygame.time.Clock()
# 蛇的宽度
snake_block = 15
# 蛇的速度
snake_speed = 15
# 从系统库里获取字体
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
定义得分函数,实际上就是在画面中添加字体。
def Your_score(score):
value = score_font.render("Score: " + str(score), True, yellow)
# 在主surface里添加字体surface
dis.blit(value, [0, 0])
定义蛇体函数,实际上就是遍历蛇身队列,依次在画面中画出矩形。
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
定义文本函数,实际上就是在画面中显示文字。
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width // 2 - 180, dis_height // 2])
两个循环结构一个判断游戏是否结束一个判断游戏是否需要退出。
while not game_over:
while game_close == True:
dis.fill(blue)
message("Game over, press p again or q quit!", red)
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_q:
game_over = True
game_close = False
if event.key == pygame.K_p:
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 or event.key == pygame.K_a:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_w:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
y1_change = snake_block
x1_change = 0
蛇的移动是通过在队头添加一个新的坐标,然后删掉队尾元素实现的。队列的长度始终为蛇长变量的值。
x1 += x1_change
y1 += y1_change
dis.fill(blue)
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()
判断蛇头是否吃到食物,设置一定的范围,防止出现永远吃不到的bug
# 判断蛇是否吃到食物
if foodx - 10 <= x1 <= foodx + 10 and foody - 10 <= y1 <= foody + 10:
foodx = round(random.randrange(snake_block, dis_width - snake_block, snake_block))
foody = round(random.randrange(snake_block, dis_height - snake_block, snake_block))
# print(foodx, foody)
Length_of_snake += 1
增加菜单功能
通过查阅资料,发现pygame_menu提供了比较完善的游戏菜单功能。我通过pip下载后,查阅了官方样例代码,这个菜单确实是比较精美的,正是我所需要给游戏添加的东西。
import pygame
import pygame_menu
pygame.init()
surface = pygame.display.set_mode((600, 400))
def set_difficulty(value, difficulty):
# Do the job here !
print(1)
pass
def start_the_game():
# Do the job here !
pass
menu = pygame_menu.Menu('Welcome', 600, 400,
theme=pygame_menu.themes.THEME_BLUE)
menu.add.text_input('Name :', default='kevenduan')
menu.add.selector('Difficulty :', [('Hard', 1), ('Easy', 2)], onchange=set_difficulty)
menu.add.button('Play', start_the_game)
menu.add.button('Quit', pygame_menu.events.EXIT)
menu.mainloop(surface)
最终版本
新功能
最终版本添加了游戏界面菜单,有查询得分记录的功能,以及可以选择两个难度。
查询分数通过列表记录后,通过排序来显示得分前5的分数。
难度的控制是由蛇的速度来控制,由于咬到自身就游戏失败有点坑,在这里删除了。
完整代码
import pygame
import random
import pygame_menu
score = [0, 0, 0, 0, 0]
pygame.init()
# 以RGB的形式定义颜色
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 = 800
dis_height = 600
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('Snakes KevenDuan 1.0')
# 创建clock对象
clock = pygame.time.Clock()
# 蛇的宽度
snake_block = 15
# 蛇的速度
snake_speed = 15
# 从系统库里获取字体
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
def Your_score(score):
value = score_font.render("Score: " + str(score), True, yellow)
# 在主surface里添加字体surface
dis.blit(value, [0, 0])
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width // 2 - 150, dis_height // 2])
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
# 食物坐标随机生成
foodx = round(random.randrange(snake_block, dis_width - snake_block, snake_block))
foody = round(random.randrange(snake_block, dis_height - snake_block, snake_block))
print(foodx, foody)
while not game_over:
while game_close == True:
dis.fill(blue)
score.append(Length_of_snake - 1)
message(f"Game Over! your score is {Length_of_snake - 1}", red)
Your_score(Length_of_snake - 1)
# 修改得分
pygame.display.update()
pygame.time.wait(2000)
# 显示出菜单
menu.mainloop(dis)
# 获取事件
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 or event.key == pygame.K_a:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_w:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
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(blue)
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 foodx - 10 <= x1 <= foodx + 10 and foody - 10 <= y1 <= foody + 10:
foodx = round(random.randrange(snake_block, dis_width - snake_block, snake_block))
foody = round(random.randrange(snake_block, dis_height - snake_block, snake_block))
# print(foodx, foody)
Length_of_snake += 1
# 调节帧率来控制蛇的速度
clock.tick(snake_speed)
pygame.quit()
quit()
def set_difficulty(value, difficulty):
# Do the job here !
global snake_speed
if snake_speed == 15: snake_speed = 30
else: snake_speed = 15
def start_the_game():
# Do the job here !
gameLoop()
def rank():
# flag = True
while True:
dis.fill(blue)
score.sort()
print(score)
mesg1 = font_style.render(f"Your highest score is {score[-1]}", True, yellow)
dis.blit(mesg1, [dis_width // 2 - 150, dis_height // 2 - 30])
mesg2 = font_style.render(f"Ranking of scores:{score[-1:-6:-1]}", True, yellow)
dis.blit(mesg2, [dis_width // 2 - 190, dis_height // 2 + 10])
pygame.display.update()
pygame.time.wait(3000)
menu.mainloop(dis)
# menu.add.button('Quit', pygame_menu.events.EXIT)
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# flag = True
menu = pygame_menu.Menu('Welcome to my game', 800, 600,
theme=pygame_menu.themes.THEME_BLUE)
menu.add.text_input('Author :', default='KevenDuan')
menu.add.selector('Difficulty :', [('Easy', 1), ('Hard', 2)], onchange=set_difficulty)
menu.add.button('Yours socres', rank)
menu.add.button('Play', start_the_game)
menu.add.button('Quit', pygame_menu.events.EXIT)
menu.mainloop(dis)
标签:Python,pygame,作战,change,贪吃蛇,snake,event,block,dis
From: https://www.cnblogs.com/itduan/p/17908878.html