我们要使用Python编写贪吃蛇游戏,需要使用到pygame模块,即在PyCharm终端输入
pip install pygame
安装完毕即可。 在pygame有三个对象比较重要,分别是 pygame.display—设置场景显示,包括页面大小,页面标题,页面更新(刷新)等; pygame.time–设置一切与时间相关的设置,游戏的帧频,游戏的持续时间等; pygame.event–设置与事件相关的处理,比如鼠标点击事件,键盘按下事件等; pygame.draw–绘制图形到界面上。
1.搭建初始框架
要做贪吃蛇游戏,首先第一步就是搭建一个初始界面,具体包括设置界面的size,设置关闭界面事件,设置帧频以及页面背景渲染和更新页面等,具体代码如下所示:
import pygame
W = 960
H = 800
size = (960, 600)
pygame.init()
window = pygame.display.set_mode(size)
pygame.display.set_caption("贪吃蛇大作战")
showWindow = True
clock = pygame.time.Clock()
while showWindow:
for event in pygame.event.get():
if event.type == pygame.QUIT:
showWindow = False
pygame.draw.rect(window, (255, 255, 255), (0, 0, W, H))
pygame.display.flip()
clock.tick(30)
此时运行程序可以显示一个正常的界面,点击右上角关闭可以关闭程序!不仅如此,而且对页面进行了简单的渲染,即改变了界面的背景颜色为白色;
2.绘制场景与蛇头
我们可以将整个界面按照行row与列col来进行划分,因此页面中的每一块都对应着一个坐标(row,col),这样我们只需要设置相应的行row与列col,即可绘制出蛇头与食物; 具体代码如下所示:
class Point:
def __init__(self, row=0, col=0):
self.row = row
self.col = col
def copy(self):
return Point(self.row, self.col)
ROW = 40
COL = 30
def rect(point, color):
cell_width = W/COL
cell_height = H/ROW
left = point.col*cell_width
top = point.row*cell_height
pygame.draw.rect(
window, color,
(left, top, cell_width, cell_height)
)
接下来在游戏循环中绘制蛇头与食物即可,如下所示:
head = Point(row=int(ROW/2), col=int(COL/2))
head_color = (0, 128, 128)
food = ge_food()
food_color = (255, 255, 0)
rect(head, head_color)
rect(food, food_color)
3.移动蛇头
我们需要知道蛇身是随着蛇头移动的,因此,首先我们就要使得蛇头移动起来,而蛇头在界面中使用row与col唯一定位,我们只需要明白其中的移动逻辑即可,具体如下:
left-->head.col-1,row不变
right-->head.col+1,row不变
top-->head.row-1,col不变
down-->head.row+1,col不变
同时需要知道,蛇头在向右移的时候不能再改变直接方向向左移,这样不合理;向上移动的时候不能直接改变方向向下移动;其他方向类似; 虽然我们每次改变的的行列数都是1,但是我们是将上述的改变写在游戏循环中,因此在界面上我们可以看见蛇头不断的移动。
direct = 'left'
While showWindow:
for event in pygame.event.get():
if event.type == pygame.QUIT:
showWindow = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if direct=='left' or direct=='right':
direct = "top"
elif event.key == pygame.K_DOWN:
if direct == 'left' or direct == 'right':
direct = "down"
elif event.key == pygame.K_LEFT:
if direct=='top' or direct=='down':
direct = 'left'
elif event.key == pygame.K_RIGHT:
if direct == 'top' or direct == 'down':
direct = 'right'
if direct == 'left':
head.col-=1
elif direct == 'right':
head.col+=1
elif direct == 'top':
head.row-=1
else:
head.row+=1
4.绘制蛇体
绘制蛇体也很简单,我们只需要将蛇体的坐标存放在一个snakes列表中,当蛇头移动时,我们只需要将蛇体按照蛇头的方向依次移动,并且舍弃列表中的最后一个元素即可。简单来说,我们编写程序时,就要在蛇头移动之前,将蛇头坐标插入到蛇体列表snakes中,并舍弃snakes中的最后一个坐标即可。
snake_color = (128, 128, 128)
snakes = []
snakes.insert(0, head.copy())
snakes.pop()
for snake in snakes:
rect(snake, snake_color)
5.解决相关不足
这里我们的贪吃蛇游戏仍然有很多不足,即还要解决蛇撞墙的问题,蛇吃蛇身的问题以及蛇吃到食物就会增长的问题,这里将一一得到解决; 具体代码如下所示:
eat = False
if head.row == food.row and head.col == food.col:
eat = True
if eat:
food = ge_food()
snakes.insert(0, head.copy())
if not eat:
snakes.pop()
dead = False
if head.col<0 or head.row<0 or head.row>=ROW or head.col>=COL:
dead = True
for snake in snakes:
if snake.row==head.row and snake.col==head.col:
dead = True
break
if dead:
showWindow = False
这里可以做的更加精细,这里我设置的是一旦蛇撞墙与蛇咬蛇身就直接退出界面。
6.运行结果展示
黄色的是随机产生的食物,而深色的是蛇头,蛇头吃到食物就吃长出蛇身。由于时间比较紧张,所以只是完成了基本的功能,后期会继续完善,包括多种游戏模式的设置,无尽模式,闯关模式等,还有背景图片背景音乐的加入,甚至可以用自定义的图片来设置蛇头的显示等等。
7.全部代码展示
import pygame
import random
class Point:
def __init__(self, row=0, col=0):
self.row = row
self.col = col
def copy(self):
return Point(self.row, self.col)
W = 800
H = 600
snakes = []
def ge_food():
while True:
pos = Point(random.randint(0, ROW - 1), random.randint(0, COL - 1))
is_collide = False
if pos.row == head.row and pos.col == head.col:
is_collide = True
for snake in snakes:
if (snake.row == pos.row and snake.col == pos.col):
is_collide = True
break
if not is_collide:
return pos
ROW = 40
COL = 30
size = (W, H)
pygame.init()
window = pygame.display.set_mode(size)
pygame.display.set_caption("贪吃蛇大作战")
bak_color = (255, 255, 255)
head = Point(row=int(ROW/2), col=int(COL/2))
head_color = (0, 128, 128)
food = ge_food()
food_color = (255, 255, 0)
snake_color = (128, 128, 128)
direct = 'left'
def rect(point, color):
cell_width = W/COL
cell_height = H/ROW
left = point.col*cell_width
top = point.row*cell_height
pygame.draw.rect(
window, color,
(left, top, cell_width, cell_height)
)
showWindow = True
clock = pygame.time.Clock()
while showWindow:
for event in pygame.event.get():
if event.type == pygame.QUIT:
showWindow = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if direct=='left' or direct=='right':
direct = "top"
elif event.key == pygame.K_DOWN:
if direct == 'left' or direct == 'right':
direct = "down"
elif event.key == pygame.K_LEFT:
if direct=='top' or direct=='down':
direct = 'left'
elif event.key == pygame.K_RIGHT:
if direct == 'top' or direct == 'down':
direct = 'right'
eat = False
if head.row == food.row and head.col == food.col:
eat = True
if eat:
food = ge_food()
snakes.insert(0, head.copy())
if not eat:
snakes.pop()
if direct == 'left':
head.col-=1
elif direct == 'right':
head.col+=1
elif direct == 'top':
head.row-=1
else:
head.row+=1
dead = False
if head.col<0 or head.row<0 or head.row>=ROW or head.col>=COL:
dead = True
for snake in snakes:
if snake.row==head.row and snake.col==head.col:
dead = True
break
if dead:
showWindow = False
pygame.draw.rect(window, bak_color, (0, 0, W, H))
rect(head, head_color)
for snake in snakes:
rect(snake, snake_color)
rect(food, food_color)
pygame.display.flip()
clock.tick(15)
|