教你如何用python和pygame制作一个简单的贪食蛇游戏,可自定义

1.效果图

2.完整的代码

#第1步:导出模块
import pygame, sys, random
from pygame.locals import *

# 第2步:定义颜色变量,在pygame中采用的color是三色原RGB的数值法
# 目标方块儿红颜色
redColor = pygame.Color(255, 0, 0)
# 背景颜色为黑色
pinkColor = pygame.Color(255, 192, 203)
#黑色:0,0,0
#粉红色=pink=(255, 192, 203)
# 蛇颜色为白色
whiteColor = pygame.Color(255, 255, 255)

# 第3步:定义游戏结束
def gameOver():
    pygame.quit()
    sys.exit()

# 第4步:定义main函数 定义入口函数
def main():
    # 初始化pygame,很关键
    pygame.init()
    # 定义一个变量 控制速度
    fpsClock = pygame.time.Clock()
    # 创建一个窗口界面及大小
    playSurface = pygame.display.set_mode((640, 480))
    pygame.display.set_caption('<-自定义贪吃蛇->DIY') #游戏标题
    # 初始化目标方块的位置
    targetPosition = [300, 300]
    # 目标方块标记 判断贪吃蛇是否吃掉目标方块:1为没吃掉 0为吃掉
    targetFlag = 1
    # 初始化贪吃蛇的位置 (100,100)为基准
    # 初始化贪吃蛇长度 列表中有几个元素 就有几个身体
    snake_head = [100, 100]
    snake_body = [[80, 100], [60, 100]] #即初始化蛇2节,一节大小20
    # 初始化方向 默认为右
    direction = 'right'
    # 定义一个认为控制的方向的变量
    changedirection = direction
    # pygame 所有事件全部放到一个实时循环中

    while True:
        # 从队列中获取事件
        for event in pygame.event.get():
            if event.type == QUIT: #定义退出
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN: #定义循环中其他时间上下左右
                if event.key == K_RIGHT:
                    changedirection = 'right'
                if event.key == K_LEFT:
                    changedirection = 'left'
                if event.key == K_UP:
                    changedirection = 'up'
                if event.key == K_DOWN:
                    changedirection = 'down'
                if event.key == K_ESCAPE: #定义键盘ESC按键和quit=窗口的‘x’功能一样
                    pygame.event.post(pygame.event.Event(QUIT))
        # 确定方向
        if changedirection == 'left' and not direction == 'right':
            direction = changedirection
        if changedirection == 'right' and not direction == 'left':
            direction = changedirection
        if changedirection == 'up' and not direction == 'down':
            direction = changedirection
        if changedirection == 'down' and not direction == 'up':
            direction = changedirection

        # 根据方向移动蛇头,每前进一步是20,正好是一节蛇身
        if direction == 'right':
            snake_head[0] += 20
        if direction == 'left':
            snake_head[0] -= 20
        if direction == 'up':
            snake_head[1] -= 20
        if direction == 'down':
            snake_head[1] += 20

        # 增加蛇的长度
        snake_body.insert(0, list(snake_head))
        # 如果贪吃蛇位置和目标方块位置重合
        if snake_head[0] == targetPosition[0] and snake_head[1] == targetPosition[1]:
            targetFlag = 0
        else:
            snake_body.pop()
        if targetFlag == 0:
            x = random.randrange(1, 32)
            y = random.randrange(1, 24)
            targetPosition = [int(x * 20), int(y * 20)]
            targetFlag = 1
        # 填充背景颜色
        playSurface.fill(pinkColor)

        for position in snake_body:
            # rect函数内
            # 第一个参数surface  指定一个surface编辑区
            # 第二个参数color    指定颜色
            # 第三个参数rect     返回一个矩形包含位置信息(x,y),(width,height)
            # 第四个参数width    表示线条的粗细 width=0 实心  width=1 空心
            # 画蛇
            pygame.draw.rect(playSurface, whiteColor, Rect(position[0], position[1], 20, 20))
            # 画目标方块儿
            pygame.draw.rect(playSurface, redColor, Rect(targetPosition[0], targetPosition[1], 20, 20))

        # 更新显示到屏幕
        pygame.display.flip()

        # 判断游戏结束
        # 注意这个判断数值依据窗口大小适当可以调整,但是一个方块大小是20×20,所以窗口长和宽-20的数值
        # 比如640的长,则620;宽480,则460
        if snake_head[0] > 620 or snake_head[0] < 0:
            gameOver()
        if snake_head[1] > 460 or snake_head[1] < 0:
            gameOver()
        # 控制游戏速度
        fpsClock.tick(3)
        
'''
#附加---背景音乐添加
# (1)初始化,记住pygame模块使用,初始化必须在导出模块之后
pygame.mixer.init()
# (2)路径
file=r'/home/xgj/xgjpython/python3/pygame/贪食蛇系列/wmbyy.mp3' #自定义mp3的路径
# (3)加载音乐文件
track = pygame.mixer.music.load(file)
# (4)开始播放音乐流
pygame.mixer.music.play()

'''

# 启动入口 main函数
if __name__ == '__main__':
    main()
View Code

 

posted @ 2020-02-02 09:07  米大头jason  阅读(825)  评论(0编辑  收藏  举报