实践|pygame贪吃蛇

创建界面

1
2
3
4
5
6
7
8
9
10
11
12
print("创建游戏窗口")
# 导入GUI
pygame.init()
# 设置窗口尺寸
screen = pygame.display.set_mode((800, 600))
# 设置窗口标题
pygame.display.set_caption("贪吃蛇")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.quit():
running = False

参数初始化

1
2
3
4
5
6
7
8
9
10
class Point:
row = 0
clo = 0

def __init__(self, row, clo):
self.row = row
self.clo = clo

def copy(self):
return Point(row=self.row, clo=self.clo)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
pygame.init()
# 初始化图形界面大小
GUI_width = 800
GUI_height = 400

ROW = 30
CLO = 40
# 初始化分数
game_score = 0

direct = 'left'
GUI_screen = pygame.display.set_mode((GUI_width, GUI_height))
pygame.display.set_caption('贪吃蛇游戏')

# 蛇头
head = Point(row=int(ROW / 2), clo=int(CLO / 2))
# 蛇身
snake = [
Point(row=head.row, clo=head.clo + 1),
Point(row=head.row, clo=head.clo + 2),
Point(row=head.row, clo=head.clo + 3)
]

运行

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
running = True
# 设置帧频率
clock = pygame.time.Clock()
display_score_text = '得分:' + str(game_score)
try:
score_font = pygame.font.SysFont('bahnschrift', 30) # 创建一个font对象,显示结果
except FileNotFoundError:
print("没有这个字体,下面是已安装的字体")
print(pygame.font.get_fonts())
score_font = pygame.font.Font(None, 30)
WhiteFont = (255, 255, 255)
while running:
# 处理帧频 锁帧
clock.tick(30)
# pygame.event.get()获取当前事件的队列 可以同时发生很多事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
GUI_screen.blit(score_surface, (10, 5)) # 将结果绘制出来
# 这里小细节蛇不可以直接左右上下 要判断当前是在什么状态下前行
if event.key == 273 or event.key == 119:
if direct == 'left' or direct == 'right':
direct = 'top'
if event.key == 274 or event.key == 115:
if direct == 'left' or direct == 'right':
direct = 'bottom'
if event.key == 276 or event.key == 97:
if direct == 'top' or direct == 'bottom':
direct = 'left'
if event.key == 275 or event.key == 100:
if direct == 'top' or direct == 'bottom':
direct = 'right'
# 吃东西
eat = (head.row == snake_food_location.row and head.clo == snake_food_location.clo)

if eat:
snake_food_location = Point(row=random.randint(0, ROW - 1), clo=random.randint(0, CLO - 1))
game_score = game_score + 1
snake.insert(0, head.copy())
if not eat:
snake.pop()

# 移动一下
if direct == 'left':
head.clo -= 1
if direct == 'right':
head.clo += 1
if direct == 'top':
head.row -= 1
if direct == 'bottom':
head.row += 1
dead = False
if head.clo < 0 or head.row < 0 or head.clo >= CLO or head.row >= ROW:
dead = True
for body in snake:
if head.clo == body.clo and head.row == body.row:
dead = True
break
game_over = False
if dead:
print('Game Over')
game_over = True
# 背景画图
pygame.draw.rect(GUI_screen, (245, 135, 155), (0, 0, GUI_width, GUI_height))
score_surface = score_font.render("Score : %s" % str(game_score), True, WhiteFont)
# 图像,绘制的位置,绘制的截面框
GUI_screen.blit(score_surface, (0, 0), score_surface.get_rect())
# 蛇头
rect(head, head_color)
# 绘制食物
rect(snake_food_location, snake_food_color)
# 绘制蛇的身子
for body in snake:
rect(body, snake_color)
if game_over:
running = False
# pygame.quit()
# break
# quit()

pygame.display.flip()