文章目录
- 原文:https://blog.c12th.cn/archives/39.html
 - Python 贪吃蛇(pygame)
 - 前言
 - 部分代码
 - 完整代码
 
原文:https://blog.c12th.cn/archives/39.html
Python 贪吃蛇(pygame)
前言
源代码参考B站: BV1cs411T7wW

效果展示
部分代码
- 框架
 
# 初始化
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()
 
- 小键盘
 
# 小键盘                                         # 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"
 
- 碰撞判定
 
# 碰撞判定
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
 
clock = pygame.time.Clock()
# FPS
clock.tick(20)
 
完整代码
# 工程: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)
 







![[云服务器14] 搭建属于你自己的Git服务器](https://i-blog.csdnimg.cn/direct/e243ff53c10f40638811375f6960166e.png#pic_center)










