python语言,总所周知是比较简单的,而且代码也不会像java那样多,下面就使用python的第三方库pygame进行开发一个贪吃蛇游戏。
1. pygame的安装
直接在cmd当中使用 pip install pygame 进行安装。
或者在pycharm当中自动导入安装也OK
2. 全局变量的定义与包的导入
在代码当中会使用到很多这种变量的值,直接在最开始进行定义,后面获取变量即可
W = 600
H = 400
fps = 12
size = (W, H)
ROW = 20
COL = 30
snake_color = (200, 200, 200)
MSGCOLOR = (3, 54, 73)
MSGBGCOLOR = (255, 255, 193)
blue = (64, 160, 171)
count = 0
pos = [160, 130]
pos1 = [190, 200]
import pygame, sys
import random
import time
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
3. 游戏开始界面初始化
使用while True循环对界面次序刷新,绘制字体以及添加背景图片进入当中 (图片可直接选择600X 400 的图片即可),并且对按压的键进行监听,123 分别表示选中不同的难度,即蛇移动的速度。
绘制字体的使用使用了字体文件,也可以使用本机的字体:
参考这篇文章,文字的绘制
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("贪吃蛇")
out = True
while out:
FPSClock = pygame.time.Clock()
img = pygame.image.load("bg.png")
screen.blit(img, (0, 0))
font = pygame.font.Font("STKAITI.TTF", 24)
fontRect = font.render("请选择游戏难度 (单击键盘)", True, MSGCOLOR, MSGBGCOLOR)
fontRect1 = font.render("1:简单 2:中等 3:复杂 ", True, MSGCOLOR, MSGBGCOLOR)
screen.blit(fontRect, pos)
screen.blit(fontRect1, pos1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
print(event.key)
if event.key == 257:
fps = 8
out = False
elif event.key == 258:
fps = 16
out = False
elif event.key == 259:
fps = 24
out = False
pygame.display.update()
FPSClock.tick(fps)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
效果图如下:绘制了文字,并且加入监听事件进入。
4. 游戏界面的设置
4.1 游戏界面,使用一个Point类进行划分
Point类可以对整个屏幕进行划分为多少个小格子,使用到python当中的类进行定义
class Point:
row = 0
col = 0
def __init__(self, row=0, col=0):
self.row = row
self.col = col
def copy(self):
return Point(row=self.row, col=self.col)
4.2 预处理
给定初始化蛇头坐标,蛇头的颜色, 蛇的身体,在这里都是Point类的对象。
获取顶点坐标,返回的也是Point对象
随机食物的产生,不能出现在蛇头或者蛇的身体的位置。
食物产生调用这个方法即可
蛇的移动方向给定一个向左
head = Point(row=int(ROW / 2), col=int(COL / 2))
head_color = (80, 80, 128)
snakenodes = [
Point(head.row, head.col),
Point(head.row, head.col + 1),
Point(head.row, head.col + 2)
]
def rect(Point, color):
left = Point.col * (W / COL)
top = Point.row * (H / ROW)
pygame.draw.rect(screen, color, (left, top, W / COL, H / ROW))
def gen_food():
while True:
pos = Point(row=random.randint(0
, ROW - 1), col=random.randint(0, COL - 1))
is_coll = False
if head.row == pos.row and head.col == pos.col:
is_coll = True
for node in snakenodes:
if node.row == pos.row and node.col == pos.col:
is_coll = True
break
if not is_coll:
break
return pos
food = gen_food()
food_color = (255, 255, 0)
dir = 'left'
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
4.3 窗口实现
在窗口当中对实体进行绘制,并且加入逻辑,比如说,相撞之后就表示游戏结束,
蛇头经过食物,计分的相加,以及再一次的产生新的食物
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("贪吃蛇")
out = True
while out:
FPSClock = pygame.time.Clock()
img = pygame.image.load("bg.png")
screen.blit(img, (0, 0))
font = pygame.font.Font("STKAITI.TTF", 24)
fontRect = font.render("请选择游戏难度 (单击键盘)", True, MSGCOLOR, MSGBGCOLOR)
fontRect1 = font.render("1:简单 2:中等 3:复杂 ", True, MSGCOLOR, MSGBGCOLOR)
screen.blit(fontRect, pos)
screen.blit(fontRect1, pos1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
print(event.key)
if event.key == 257:
fps
= 8
out = False
elif event.key == 258:
fps = 16
out = False
elif event.key == 259:
fps = 24
out = False
pygame.display.update()
FPSClock.tick(fps)
time.sleep(1)
quit = True
while quit:
screen = pygame.display.set_mode((600, 460))
FPSClock = pygame.time.Clock()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == 273 or event.key == pygame.K_UP:
if dir == 'left' or dir == 'right':
dir = 'up'
elif event.key == 274 or event.key == pygame.K_DOWN:
if dir == 'left' or dir == 'right':
dir = 'down'
if event.key == 276 or event.key == pygame.K_LEFT:
if dir == 'up' or dir == 'down':
dir = 'left'
if event.key == 275 or event.key == pygame.K_RIGHT:
if dir == 'up' or dir == 'down':
dir = 'right'
img = pygame.image.load("bg.png")
screen.blit(img, (0, 0))
pygame.draw.rect(screen, (255, 255, 255), (0, H, W, 10))
pygame.draw.rect(screen, blue, (0, H + 10, W, 60))
font = pygame.font.Font("STKAITI.TTF", 24)
fontRect = font.render(" 当前得分 : " + str
(count), True, MSGCOLOR, blue)
screen.blit(fontRect, (420, 420))
rect(head, head_color)
rect(food, food_color)
for node in snakenodes:
rect(node, snake_color)
eat = (head.row == food.row and head.col == food.col)
if eat:
food = gen_food()
if fps == 8:
count += 3
elif fps == 16:
count += 5
elif fps == 24:
count += 10
snakenodes.insert(0, head.copy())
if not eat:
snakenodes.pop()
if dir == 'left':
head.col -= 1
elif dir == 'right':
head.col += 1
elif dir == 'up':
head.row -= 1
elif dir == 'down':
head.row += 1
dead = False
if head.row < 0 or head.col < 0 or head.row >= ROW or head.col >= COL:
dead = True
print("撞墙")
for snake in snakenodes:
if head.col == snake.col and head.row == snake.row:
dead = True
print("撞到自己")
break
if dead:
print("游戏结束")
quit = False
pygame.display.update()
FPSClock.tick(fps)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
运行效果:得分会在吃掉食物之后进行刷新的,并且在前面选择的不同的难度加的分也是不同的。123难度对应的是一个食物分别是 3、5、10 分。
5. 得分的记录
在前面选择不同的难度之后,每一次吃掉食物之后。分数加的也不同,在这里对最终得分记录下来,并且获取count文件进行比较,判断是否打破记录。打破记录,把新的记录写进文件当中。
f = open("count", "r")
counts = f.read()
if count > int(counts):
file = open("count", "w")
file.write(str(count))
file.close()
f.close()
6. 游戏结束(是否打破记录)
得分已经获取到了,根据是否打破了记录来绘制不同的效果。同样的使用这个pygame进行初始化构造出一个新的窗口。
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("贪吃蛇")
while True:
FPSClock = pygame.time.Clock()
pygame.draw.rect(screen, MSGBGCOLOR, (0, 0, W, H))
font = pygame.font.Font("STKAITI.TTF", 24)
if count <= int(counts):
fontRect = font.render(" 你的得分是: " + str(count) + " 继续加油哦!", True, MSGCOLOR, MSGBGCOLOR)
elif count > int
(counts):
fontRect = font.render(" 恭喜你打破了记录!你的得分是 : " + str(count), True, MSGCOLOR, MSGBGCOLOR)
screen.blit(fontRect, (140, 140))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()
FPSClock.tick(fps)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
比如说这里,在得分没有超过count文件当中的得分记录的时候就会打印出继续加油,反之打印的是恭喜你打破记录。count得分进行输出的时候,count是一个int型的变量,要转换成string才能进行绘制,所以在上面使用到了
str()
函数,数据类型转换。
7. 项目运行
小萌新看过来,在获取到整个项目之后,运行Snake.py文件,之后会让你选择游戏难度,分别对应键盘上的123,可自己选择,难度选择之后,就会出现贪吃蛇的游戏界面了。虽然标题写的是250行开发一个贪吃蛇游戏,但是真的把多余的换行以及注释去掉之后,估计也就200行左右。而且很多代码也是大致相同的。
8. 项目获取
所有代码都在上面有所展示,如果遇到一些bug,可在下方百度网盘进行获取源码
源码获取:链接:
https://pan.baidu.com/s/1_66rlKQelO4CuSaH0__2ig
提取码:2nom