python+pygame 实现贪吃蛇小游戏

上代码

重新修改了一下,加入了游戏结束后的菜单,可以重新开始或者退出,不用一结束就自动关闭游戏窗口

后续改进,每次启动时,随机生成食物位置和蛇的位置
import pygame
import random
import sys
from pygame.rect import Rect


def gameQuit():
    pygame.quit()
    sys.exit(0)  # 系统退出


class CSnake(object):
    def __init__(self):
        self.winWidth = 800
        self.winHeight = 600
        self.black = pygame.Color(0, 0, 0)  # 背景颜色,黑色
        self.green = pygame.Color(0, 255, 0)  # 食物颜色,绿色
        self.white = pygame.Color(255, 255, 255)  # 身体颜色,白色
        self.speedStep = 5  # 速度步长,累计吃到几次后速度+1

    def initialize(self):
        pygame.init()
        clock = pygame.time.Clock()  # 创建时钟对象 (可以控制游戏循环频率)
        playSurface = pygame.display.set_mode((self.winWidth, self.winHeight))  # 创建游戏窗口。大小800x600
        pygame.display.set_caption('贪吃蛇小游戏')  # 窗口名
        snakePosition = [80, 80]  # 贪吃蛇的头
        snakeBody = [[80, 80], [60, 80], [40, 80]]  # 初始化贪吃蛇的长度 (注:这里以20*20为一个标准小格子)
        foodPosition = [200, 400]  # 食物方块目标位置
        direction = 'right'  # 初始化移动方向
        changeDirection = direction  # 输入的 方向
        speed = 6  # 初始移动速度

        self.main(snakeBody, foodPosition, direction, changeDirection, snakePosition, playSurface, clock, speed)

    def main(self, snakeBody, foodPosition, direction, changeDirection, snakePosition, playSurface, clock, speed):
        eatCount = 0
        eventStop = pygame.USEREVENT
        isStop = False
        while True:
            for event in pygame.event.get():  # pygame.event.get() ,监听用户事件
                # print('event:', event)
                if event.type == pygame.QUIT:  # 接收到退出事件后,退出程序
                    gameQuit()
                elif event.type == eventStop:
                    isStop = True
                elif event.type == pygame.KEYDOWN:
                    # 接收键盘按键,设置需改变的方向
                    if event.key == pygame.K_LEFT:
                        changeDirection = 'left'
                    if event.key == pygame.K_RIGHT:
                        changeDirection = 'right'
                    if event.key == pygame.K_UP:
                        changeDirection = 'up'
                    if event.key == pygame.K_DOWN:
                        changeDirection = 'down'
                    if event.key == pygame.K_ESCAPE:
                        pygame.event.post(pygame.event.Event(pygame.QUIT))
                    if event.key == pygame.K_SPACE and changeDirection != 'pause':
                        changeDirection = 'pause'
                    elif event.key == pygame.K_SPACE and changeDirection == 'pause':
                        changeDirection = direction
                    if event.key == pygame.K_1 and isStop:
                        print('restart')
                        self.initialize()
            # 游戏暂停
            if isStop:
                pass
            elif changeDirection == 'pause':
                self.displayText(playSurface, " Game Pause. ", " Press space to continue. ")
            else:
                # 判断改变的方向是否可行,赋值给方向
                if changeDirection == 'left' and direction != 'right':
                    direction = changeDirection
                if changeDirection == 'right' and direction != 'left':
                    direction = changeDirection
                if changeDirection == 'up' and direction != 'down':
                    direction = changeDirection
                if changeDirection == 'down' and direction != 'up':
                    direction = changeDirection
                # 根据方向,改变蛇的运动方向
                if direction == 'left':
                    snakePosition[0] -= 20
                if direction == 'right':
                    snakePosition[0] += 20
                if direction == 'up':
                    snakePosition[1] -= 20
                if direction == 'down':
                    snakePosition[1] += 20
                # 将移动后的点位加入蛇身体列表首位
                snakeBody.insert(0, list(snakePosition))
                # 判断蛇头是否吃到食物
                if snakePosition[0] == foodPosition[0] and snakePosition[1] == foodPosition[1]:
                    x = random.randrange(1, int(self.winWidth / 20))
                    y = random.randrange(1, int(self.winHeight / 20))
                    foodPosition = [x * 20, y * 20]
                    eatCount += 1
                    if eatCount == self.speedStep:
                        speed += 1
                        eatCount = 0
                else:
                    snakeBody.pop()

                # 填充背景为黑色,覆盖改变后的蛇和食物的颜色
                playSurface.fill(self.black)
                # 开始在程序中画图,画蛇
                for position in snakeBody:
                    pygame.draw.rect(playSurface, self.white, Rect(position[0], position[1], 20, 20))
                # 画食物
                pygame.draw.rect(playSurface, self.green, Rect(foodPosition[0], foodPosition[1], 20, 20))
                # 设置运行速度
                clock.tick(speed)

            # 判断是否到达边缘,是否碰到身体
            if snakePosition[0] < 0 or snakePosition[0] > self.winWidth:
                self.gameOver(playSurface)
            elif snakePosition[1] < 0 or snakePosition[1] > self.winHeight:
                self.gameOver(playSurface)
            for i in snakeBody[1:]:
                if snakePosition[0] == i[0] and snakePosition[1] == i[1]:
                    self.gameOver(playSurface)

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

    def displayText(self, surface, *strText):
        font = pygame.font.Font(None, 50)
        count = 0
        for st in strText:
            text = font.render(st, True, (255, 0, 0), (255, 255, 255))
            textRect = text.get_rect()
            textRect.center = (int(self.winWidth / 2), int(self.winHeight / 3 + 40 * count))
            surface.blit(text, textRect)
            count += 1

    def gameOver(self, playSurface):
        pygame.event.post(pygame.event.Event(pygame.USEREVENT))
        self.displayText(playSurface,
                         " Game Over. ",
                         " ==== Menu ==== ",
                         " Press 1 to restart. ",
                         " Press ESC to quit game. "
                         )


if __name__ == '__main__':
    snake = CSnake()
    snake.initialize()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值