用Python做贪吃蛇小游戏

简介

贪吃蛇游戏是一款经典的益智游戏,既简单又耐玩。该游戏通过控制蛇头方向吃食物,从而使得蛇变得越来越长。

引言

贪吃蛇小游的设计过程,以及这个系列专题的整体架构

游戏预览

游戏开始界面
用Python做贪吃蛇小游戏

游戏进行界面
用Python做贪吃蛇小游戏

游戏结束界面

用Python做贪吃蛇小游戏

结构图

用Python做贪吃蛇小游戏

代码框架图

用Python做贪吃蛇小游戏

代码讲解

整个程序由于是调用了大量的pygame里面的库函数

main主函数-开始工作

主要承担一些游戏窗口的初始化工作,以及调用相关函数运行游戏。代码如下:

#主函数
def main():
	pygame.init() # 模块初始化
	snake_speed_clock = pygame.time.Clock() # 利用pygame的time模块创建Pygame时钟对象
	screen = pygame.display.set_mode((windows_width, windows_height)) #利用pygame显示模块设置模式窗口宽度窗口高度
	screen.fill(white)

	pygame.display.set_caption("Python 贪吃蛇小游戏") #设置标题
	show_start_info(screen)               #欢迎信息
	while True:
		running_game(screen, snake_speed_clock)
		show_gameover_info(screen)
        

基于以上代码,咱们来做几点讲解:

  • pygame.time.Clock()
    利用pygame的time模块创建Pygame时钟对象,控制帧速率。pygame.time.Clock()会控制每个循环多长时间运行一次。

  • pygame.display.set_mode((windows_width, windows_height))
    生成windows窗口,pygame.display.set_mode(resolution=(0,0),flags=0,depth=0)。返回的是一个surface对象(surface对象是用于表示图像的图像,只要指定尺寸,就可以利用),resolution可以控制生成windows窗口的大小,flags代表的是扩展选项,

  • pygame.surface.fill(color)。对surface对象填充某一种颜色,在这里表现为窗口背景颜色的填充。

然后就运行我们游戏三个函数了

  • show_start_info(screen)
    显示欢迎信息,最终效果表现为:
    用Python做贪吃蛇小游戏

接着死循环。因为我们的游戏设置是,当GameOver以后,我们可以按任意键重新开始游戏,或者退出。因此最后不断循环判断用户是否想重新开始游戏。

  • 游戏主体running_game(screen, snake_speed_clock)
    贪吃蛇运行的主体函数。整个程序的精髓所在。

  • show_gameover_info(screen)
    贪吃蛇死了,显示GameOver,表现为:
    用Python做贪吃蛇小游戏

show_start_info()欢迎进入游戏

代码如下:

#开始信息显示
def show_start_info(screen):
	font = pygame.font.Font('myfont.ttf', 40)
	tip = font.render('按任意键开始游戏~~~', True, (65, 105, 225))
	gamestart = pygame.image.load('gamestart.png')
	screen.blit(gamestart, (140, 30))
	screen.blit(tip, (240, 550))
	pygame.display.update()

	while True:  #键盘监听事件
		for event in pygame.event.get():  # event handling loop
			if event.type == QUIT:
				terminate()     #终止程序
			elif event.type == KEYDOWN:
				if (event.key == K_ESCAPE):  #终止程序
					terminate() #终止程序
				else:
					return #结束此函数, 开始游戏
        
  • 字体显示
    先创建一个Font对象,用自己的字体。有了Font对象以后, 就可以用render方法来写字了,然后通过blit方法blit到屏幕上。

  • 图像加载
    用 pygame.image.load()加载图像获得对象,在用blit方法刷到屏幕上。做完以上事件以后,要update一下刷新一下屏幕。

  • 监听键盘
    按任意键继续或者ESC退出……

running_game-让我们开始游戏吧

running_game(screen, snake_speed_clock)是游戏主要功能,也是最精髓的一部分
代码如下:

#游戏运行主体
def running_game(screen,snake_speed_clock):
	startx = random.randint(3, map_width - 8) 	#蛇开始游戏的位置
	starty = random.randint(3, map_height - 8)
	snake_coords = [{'x': startx, 'y': starty},  #初始贪吃蛇
                  {'x': startx - 1, 'y': starty},
                  {'x': startx - 2, 'y': starty}]

	direction = RIGHT                     #  开始时向右移动

	food = get_random_location()          #食物随机位置

	while True:
		for event in pygame.event.get():
			if event.type == QUIT:
				terminate()
			elif event.type == KEYDOWN:
				if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT:
					direction = LEFT
				elif (event.key == K_RIGHT or event.key == K_d) and direction != LEFT:
					direction = RIGHT
				elif (event.key == K_UP or event.key == K_w) and direction != DOWN:
					direction = UP
				elif (event.key == K_DOWN or event.key == K_s) and direction != UP:
					direction = DOWN
				elif event.key == K_ESCAPE:
					terminate()

		move_snake(direction, snake_coords) 		#移动蛇

		ret = snake_is_alive(snake_coords)
		if not ret:
			break 								#蛇死了. 游戏结束
		snake_is_eat_food(snake_coords, food)    #判断蛇是否吃到食物

		screen.fill(BG_COLOR)
		#draw_grid(screen)
		draw_snake(screen, snake_coords)
		draw_food(screen, food)
		draw_score(screen, len(snake_coords) - 3)
		pygame.display.update()
		snake_speed_clock.tick(snake_speed) 		#控制fps,使蛇获得速度
        
  • 关于贪吃蛇
    这里我们采用一个元组存储贪吃蛇身体各个部分的坐标(一条贪吃蛇不是由很多节组成的嘛)。最后再写个方法根据元组坐标把贪吃蛇画出来就行。
    关于食物
    同样做法。存坐标,最后画出来。

关于移动
监听键盘,根据用户按键,用direction变量记录移动方向。然后更新贪吃蛇元组里面的坐标(其实每次移动只用更新头尾就行)。最后统一画出来。移动做法具体是,我们把每次头部移动的新坐标插入贪吃蛇元组,然后删掉尾部一节(注意,删除尾部我们放在了另外一个函数里做)。

#移动贪吃蛇
def move_snake(direction, snake_coords):
    if direction == UP:
        newHead = {'x': snake_coords[HEAD]['x'], 'y': snake_coords[HEAD]['y'] - 1}
    elif direction == DOWN:
        newHead = {'x': snake_coords[HEAD]['x'], 'y': snake_coords[HEAD]['y'] + 1}
    elif direction == LEFT:
        newHead = {'x': snake_coords[HEAD]['x'] - 1, 'y': snake_coords[HEAD]['y']}
    elif direction == RIGHT:
        newHead = {'x': snake_coords[HEAD]['x'] + 1, 'y': snake_coords[HEAD]['y']}

    snake_coords.insert(0, newHead)
		
  • 开始阶段
    先把贪吃蛇和食物的坐标随机生成,贪吃蛇一开始3节长,先设置向右移动。

  • 移动我们的贪吃蛇
    监听键盘,用户按下键盘只是改变direction的值,再用move_snake(direction, snake_coords)函数更新贪吃蛇坐标。如果不按,那direction值一直不变,贪吃蛇就一直向前走。

  • 相关判断
    要判断贪吃蛇是否挂了,表现为:

    头坐标超出地图范围
    头坐标等于身体某节坐标


#判断蛇死了没
def snake_is_alive(snake_coords):
	tag = True
	if snake_coords[HEAD]['x'] == -1 or snake_coords[HEAD]['x'] == map_width or snake_coords[HEAD]['y'] == -1 or \
			snake_coords[HEAD]['y'] == map_height:
		tag = False # 蛇碰壁啦
	for snake_body in snake_coords[1:]:
		if snake_body['x'] == snake_coords[HEAD]['x'] and snake_body['y'] == snake_coords[HEAD]['y']:
			tag = False # 蛇碰到自己身体啦
	return tag
	
  • 判断贪吃蛇是否吃到食物,表现为:

    头坐标等于食物坐标,那么吃到食物。这时候就不用删尾部一节了,因为吃到食物变长了。
    如果没有吃到食物,那么是正常移动,删掉尾部一节坐标。


#判断贪吃蛇是否吃到食物
def snake_is_eat_food(snake_coords, food):  #如果是列表或字典,那么函数内修改参数内容,就会影响到函数体外的对象。
	if snake_coords[HEAD]['x'] == food['x'] and snake_coords[HEAD]['y'] == food['y']:
		food['x'] = random.randint(0, map_width - 1)
		food['y'] = random.randint(0, map_height - 1) # 实物位置重新设置
	else:
		del snake_coords[-1]  # 如果没有吃到实物, 就向前移动, 那么尾部一格删掉
	

draw_snake-画出我们的贪吃蛇

  • 画出我们的游戏
    最后调用相关函数,讲我们的地图,贪吃蛇,食物等等统统画出来。

代码如下:


#将贪吃蛇画出来
def draw_snake(screen, snake_coords):
	for coord in snake_coords:
		x = coord['x'] * cell_size
		y = coord['y'] * cell_size
		wormSegmentRect = pygame.Rect(x, y, cell_size, cell_size)
		pygame.draw.rect(screen, dark_blue, wormSegmentRect)
		wormInnerSegmentRect = pygame.Rect(                #蛇身体里面的第二层亮蓝色
			x + 4, y + 4, cell_size - 8, cell_size - 8)
		pygame.draw.rect(screen, blue, wormInnerSegmentRect)
		

首先先获取相关坐标,最后调用pygame.draw.rect将身体各个部分画出来

draw_food-画出我们的食物


#将食物画出来
def draw_food(screen, food):
	x = food['x'] * cell_size
	y = food['y'] * cell_size
	appleRect = pygame.Rect(x, y, cell_size, cell_size)
	pygame.draw.rect(screen, Red, appleRect)
		

先获取食物的位置,然后就把矩形画出来。


#将食物画出来
def draw_food(screen, food):
	x = food['x'] * cell_size
	y = food['y'] * cell_size
	appleRect = pygame.Rect(x, y, cell_size, cell_size)
	pygame.draw.rect(screen, Red, appleRect)
		

获得Font对象以后,render写字,最后设置位置,在屏幕上blit出来。

所有代码在下面:


## 导入相关模块
import random	#返回随机0~1的数
import pygame	#Pygame是跨平台Python模块,专为电子游戏设计,包含图像、声音。建立在SDL基础上,允许实时电子游戏研发而无需被低级语言(如机器语言和汇编语言)束缚。
import sys	#当执行import sys后, python在 sys.path 变量中所列目录中寻找 sys 模块文件。然后运行这个模块的主块中的语句进行初始化,然后就可以使用模块了 。

from pygame.locals import *


snake_speed = 15 #贪吃蛇的速度
windows_width = 800
windows_height = 600 #游戏窗口的大小
cell_size = 20       #贪吃蛇身体方块大小,注意身体大小必须能被窗口长宽整除

''' #初始化区
由于我们的贪吃蛇是有大小尺寸的, 因此地图的实际尺寸是相对于贪吃蛇的大小尺寸而言的
'''
map_width = int(windows_width / cell_size)
map_height = int(windows_height / cell_size)

# RGB颜色定义
white = (255, 255, 255)
black = (0, 0, 0)
gray = (230, 230, 230)
dark_gray = (40, 40, 40)
DARKGreen = (0, 155, 0)
Green = (0, 255, 0)
Red = (255, 0, 0)
blue = (0, 0, 255)
dark_blue =(0,0, 139)


BG_COLOR = black #游戏背景颜色

# 定义方向
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4

HEAD = 0 #贪吃蛇头部下标

#主函数
def main():
	pygame.init() # 模块初始化
	snake_speed_clock = pygame.time.Clock() # 利用pygame的time模块创建Pygame时钟对象
	screen = pygame.display.set_mode((windows_width, windows_height)) #利用pygame显示模块设置模式窗口宽度窗口高度
	screen.fill(white)

	pygame.display.set_caption("Python 贪吃蛇") #设置标题
	show_start_info(screen)               #欢迎信息
	while True:
		running_game(screen, snake_speed_clock)
		show_gameover_info(screen)


#游戏运行主体
def running_game(screen,snake_speed_clock):
	startx = random.randint(3, map_width - 8) #蛇开始游戏的位置
	starty = random.randint(3, map_height - 8)
	snake_coords = [{'x': startx, 'y': starty},  #初始贪吃蛇
                  {'x': startx - 1, 'y': starty},
                  {'x': startx - 2, 'y': starty}]

	direction = RIGHT       #  开始时向右移动

	food = get_random_location()     #实物随机位置

	while True:
		for event in pygame.event.get():
			if event.type == QUIT:
				terminate()
			elif event.type == KEYDOWN:
				if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT:
					direction = LEFT
				elif (event.key == K_RIGHT or event.key == K_d) and direction != LEFT:
					direction = RIGHT
				elif (event.key == K_UP or event.key == K_w) and direction != DOWN:
					direction = UP
				elif (event.key == K_DOWN or event.key == K_s) and direction != UP:
					direction = DOWN
				elif event.key == K_ESCAPE:
					terminate()

		move_snake(direction, snake_coords) #移动蛇

		ret = snake_is_alive(snake_coords)
		if not ret:
			break #蛇死了. 游戏结束
		snake_is_eat_food(snake_coords, food) #判断蛇是否吃到食物

		screen.fill(BG_COLOR)
		#draw_grid(screen)
		draw_snake(screen, snake_coords)
		draw_food(screen, food)
		draw_score(screen, len(snake_coords) - 3)
		pygame.display.update()
		snake_speed_clock.tick(snake_speed) #控制fps,使蛇获得速度
#将食物画出来
def draw_food(screen, food):
	x = food['x'] * cell_size
	y = food['y'] * cell_size
	appleRect = pygame.Rect(x, y, cell_size, cell_size)
	pygame.draw.rect(screen, Red, appleRect)
#将贪吃蛇画出来
def draw_snake(screen, snake_coords):
	for coord in snake_coords:
		x = coord['x'] * cell_size
		y = coord['y'] * cell_size
		wormSegmentRect = pygame.Rect(x, y, cell_size, cell_size)
		pygame.draw.rect(screen, dark_blue, wormSegmentRect)
		wormInnerSegmentRect = pygame.Rect(                #蛇身体里面的第二层亮蓝色
			x + 4, y + 4, cell_size - 8, cell_size - 8)
		pygame.draw.rect(screen, blue, wormInnerSegmentRect)
#画网格
def draw_grid(screen):
	for x in range(0, windows_width, cell_size):  # draw 水平 lines
		pygame.draw.line(screen, dark_gray, (x, 0), (x, windows_height))
	for y in range(0, windows_height, cell_size):  # draw 垂直 lines
		pygame.draw.line(screen, dark_gray, (0, y), (windows_width, y))
#移动贪吃蛇
def move_snake(direction, snake_coords):
    if direction == UP:
        newHead = {'x': snake_coords[HEAD]['x'], 'y': snake_coords[HEAD]['y'] - 1}
    elif direction == DOWN:
        newHead = {'x': snake_coords[HEAD]['x'], 'y': snake_coords[HEAD]['y'] + 1}
    elif direction == LEFT:
        newHead = {'x': snake_coords[HEAD]['x'] - 1, 'y': snake_coords[HEAD]['y']}
    elif direction == RIGHT:
        newHead = {'x': snake_coords[HEAD]['x'] + 1, 'y': snake_coords[HEAD]['y']}

    snake_coords.insert(0, newHead)
#判断蛇死了没
def snake_is_alive(snake_coords):
	tag = True
	if snake_coords[HEAD]['x'] == -1 or snake_coords[HEAD]['x'] == map_width or snake_coords[HEAD]['y'] == -1 or \
			snake_coords[HEAD]['y'] == map_height:
		tag = False # 蛇碰壁啦
	for snake_body in snake_coords[1:]:
		if snake_body['x'] == snake_coords[HEAD]['x'] and snake_body['y'] == snake_coords[HEAD]['y']:
			tag = False # 蛇碰到自己身体啦
	return tag
#判断贪吃蛇是否吃到食物
def snake_is_eat_food(snake_coords, food):  #如果是列表或字典,那么函数内修改参数内容,就会影响到函数体外的对象。
	if snake_coords[HEAD]['x'] == food['x'] and snake_coords[HEAD]['y'] == food['y']:
		food['x'] = random.randint(0, map_width - 1)
		food['y'] = random.randint(0, map_height - 1) # 实物位置重新设置
	else:
		del snake_coords[-1]  # 如果没有吃到实物, 就向前移动, 那么尾部一格删掉
#食物随机生成
def get_random_location():
	return {'x': random.randint(0, map_width - 1), 'y': random.randint(0, map_height - 1)}
#开始信息显示
def show_start_info(screen):
	font = pygame.font.Font('myfont.ttf', 40)
	tip = font.render('按任意键开始游戏~~~', True, (65, 105, 225))
	gamestart = pygame.image.load('gamestart.png')
	screen.blit(gamestart, (140, 30))
	screen.blit(tip, (240, 550))
	pygame.display.update()

	while True:  #键盘监听事件
		for event in pygame.event.get():  # event handling loop
			if event.type == QUIT:
				terminate()     #终止程序
			elif event.type == KEYDOWN:
				if (event.key == K_ESCAPE):  #终止程序
					terminate() #终止程序
				else:
					return #结束此函数, 开始游戏
#游戏结束信息显示
def show_gameover_info(screen):
	font = pygame.font.Font('myfont.ttf', 40)
	tip = font.render('按Q或者ESC退出游戏, 按任意键重新开始游戏~', True, (65, 105, 225))
	gamestart = pygame.image.load('gameover.png')
	screen.blit(gamestart, (60, 0))
	screen.blit(tip, (80, 300))
	pygame.display.update()

	while True:  #键盘监听事件
		for event in pygame.event.get():  # event handling loop
			if event.type == QUIT:
				terminate()     #终止程序
			elif event.type == KEYDOWN:
				if event.key == K_ESCAPE or event.key == K_q:  #终止程序
					terminate() #终止程序
				else:
					return #结束此函数, 重新开始游戏
#画成绩
def draw_score(screen,score):
	font = pygame.font.Font('myfont.ttf', 30)
	scoreSurf = font.render('得分: %s' % score, True, Green)
	scoreRect = scoreSurf.get_rect()
	scoreRect.topleft = (windows_width - 120, 10)
	screen.blit(scoreSurf, scoreRect)
#程序终止
def terminate():
	pygame.quit()
	sys.exit()


main()