Python 贪吃蛇(pygame)

前言

源代码参考B站: BV1cs411T7wW


效果展示


部分代码

  • 框架

    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
    # 初始化
    pygame.init()
    W = 800
    H = 600

    ROW = 30
    COL = 40

    size = (W, H)
    window = pygame.display.set_mode(size)
    pygame.display.set_caption("贪吃蛇")

    bg_color = (255, 255, 255)


    # 绘制
    def rect(point, color):
    left = point.col * W / COL
    top = point.row * H / ROW

    pygame.draw.rect(
    window, color,
    (left, top, W / COL, H / ROW)
    )
    pass


    # 游戏循环
    quit = True
    while quit:
    # 处理事件
    for event in pygame.event.get():
    # print(event) # 鼠标测试
    if event.type == pygame.QUIT:
    quit = False

    # 背景
    pygame.draw.rect(window, bg_color, (0, 0, W, H))

    pygame.display.flip()

  • 小键盘

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # 小键盘                                         # wasd
    if event.key == 1073741906 or event.key == 82: # if event.key==273 or event.key==119:
    if direct == "left" or direct == "right":
    direct = "up"
    elif event.key == 1073741905 or event.key == 81: # elif event.key==274 or event.key==115:
    if direct == "left" or direct == "right":
    direct = "down"
    elif event.key == 1073741904 or event.key == 80: # elif event.key==273 or event.key==119:
    if direct == "up" or direct == "down":
    direct = "left"
    elif event.key == 1073741903 or event.key == 79: # elif event.key==273 or event.key==119:
    if direct == "up" or direct == "down":
    direct = "right"
  • 碰撞判定

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    # 碰撞判定
    dead = False
    # 撞墙
    if head.col < 0 or head.row < 0 or head.col >= COL or head.row >= ROW:
    dead = True

    # 撞自己
    for snake in snakes:
    if head.col == snake.col and head.row == snake.row:
    dead = True
    break

    # 死亡
    if dead:
    print("game over")
    quit = False
  • FPS

    1
    2
    3
    4
    clock = pygame.time.Clock()

    # FPS
    clock.tick(20)

完整代码

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# 工程:test
# 创建时间:2024/8/2 19:05
# encoding:utf-8
# 源代码参考B站:BV1cs411T7wW

import pygame
import random


class Point:
row = 0
col = 0

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

def copy(self):
return Point(row=self.row, col=self.col)


# 初始化
pygame.init()
W = 800
H = 600

ROW = 30
COL = 40

size = (W, H)
window = pygame.display.set_mode(size)
pygame.display.set_caption("贪吃蛇")

bg_color = (255, 255, 255)
snake_color = (255, 200, 200)

head = Point(row=int(ROW / 2), col=int(COL / 2))
head_color = (0, 128, 128)

snakes = [
Point(row=head.row, col=head.col + 1),
Point(row=head.row, col=head.col + 2),
Point(row=head.row, col=head.col + 3),
]


# 生成食物
def gen_food():
while 1:
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 snake in snakes:
if snake.row == pos.row and snake.col == pos.col:
is_coll = True
break

if not is_coll:
break

return pos


# 食物
food = gen_food()
food_color = (0, 255, 0)

direct = "left"


# 绘制
def rect(point, color):
left = point.col * W / COL
top = point.row * H / ROW

pygame.draw.rect(
window, color,
(left, top, W / COL, H / ROW)
)
pass


# 游戏循环
quit = True
clock = pygame.time.Clock()
while quit:
# 处理事件
for event in pygame.event.get():
# print(event) # 鼠标测试
if event.type == pygame.QUIT:
quit = False
elif event.type == pygame.KEYDOWN:
# print(event) # 按键测试
# 小键盘 # wasd
if event.key == 1073741906 or event.key == 82: # if event.key==273 or event.key==119:
if direct == "left" or direct == "right":
direct = "up"
elif event.key == 1073741905 or event.key == 81: # elif event.key==274 or event.key==115:
if direct == "left" or direct == "right":
direct = "down"
elif event.key == 1073741904 or event.key == 80: # elif event.key==273 or event.key==119:
if direct == "up" or direct == "down":
direct = "left"
elif event.key == 1073741903 or event.key == 79: # elif event.key==273 or event.key==119:
if direct == "up" or direct == "down":
direct = "right"

# 吃东西
eat = (head.row == food.row and head.col == food.col)

# 食物刷新
if eat:
food = gen_food()
print("蛇的长度:", len(snakes) + 1) # 实时显示蛇的长度

# 处理身子
snakes.insert(0, head.copy())
if not eat:
snakes.pop()

# 移动
if direct == "left":
head.col -= 1
elif direct == "right":
head.col += 1
elif direct == "up":
head.row -= 1
elif direct == "down":
head.row += 1

# 碰撞判定
dead = False
# 撞墙
if head.col < 0 or head.row < 0 or head.col >= COL or head.row >= ROW:
dead = True

# 撞自己
for snake in snakes:
if head.col == snake.col and head.row == snake.row:
dead = True
break

# 死亡
if dead:
print("game over")
quit = False

# 背景
pygame.draw.rect(window, bg_color, (0, 0, W, H))

# 蛇头
for snake in snakes:
rect(snake, snake_color)
rect(head, head_color)
rect(food, food_color)

pygame.display.flip()

# FPS
clock.tick(20)