Python项目——贪吃蛇

1、原理

  • 整个界面由一个二维数组组成。游戏开始时,会随机生成一个苹果(红点)和一条蛇(黄点)。蛇会在二维数组中移动,当蛇碰到苹果时,苹果被吃,蛇的长度加一(红点变黄点)。然后再次随机生成一个苹果。
  • 蛇一旦碰到自身,游戏结束。
  •   

2、工具

  • 语言:python3.11
  • 编译器:PyCharm
  • 包:pygame
    • pip install pygame

3、代码

  • # 贪吃蛇
    import pygame
    import random
    import copy
    
    # 蛇身,蛇头在最后
    snake_list = [[10, 10]]
    
    # 移动方向
    move_up = False
    move_down = True
    move_left = False
    move_right = False
    
    # 苹果 随机
    apple_point = [random.randrange(20, 780, 20), random.randrange(20, 580, 20)]  # 20~780随机生成一个数,不能生成到外面去
    
    """初始化游戏"""
    pygame.init()
    # fps 刷新帧率
    clock = pygame.time.Clock()
    # 设置屏幕大小
    screen = pygame.display.set_mode((800, 600))
    # 绘制标题
    pygame.display.set_caption('贪吃蛇')
    
    """进入游戏"""
    # 设置游戏开关
    running = True
    while running:
        # 设置fps为15
        clock.tick(15)
        # 绘制屏幕为黑色
        screen.fill([0, 0, 0])
        # 用键盘控制移动方向
        for event in pygame.event.get():
            # 判断按键是否按下
            if event.type == pygame.KEYDOWN:
                # 判断按键类型
                if event.key == pygame.K_UP and not move_down:
                    move_up = True
                    move_down = False
                    move_left = False
                    move_right = False
                if event.key == pygame.K_DOWN and not move_up:
                    move_up = False
                    move_down = True
                    move_left = False
                    move_right = False
                if event.key == pygame.K_LEFT and not move_right:
                    move_up = False
                    move_down = False
                    move_left = True
                    move_right = False
                if event.key == pygame.K_RIGHT and not move_left:
                    move_up = False
                    move_down = False
                    move_left = False
                    move_right = True
        # 绘制苹果
        apple_rect = pygame.draw.circle(screen, [255, 0, 0], apple_point, 20)  # 绘制圆点
        # 绘制蛇
        snake_rect = []  # 蛇身
        for snake_point in snake_list:
            snake_rect.append(pygame.draw.circle(screen, [255, 255, 255], snake_point, 10, 0))
            # 点碰撞检测,吃苹果
            if apple_rect.collidepoint(snake_point):
                # 蛇吃掉苹果,增加一个点
                snake_list.append(apple_point)
                # 重新绘制苹果
                apple_point = [random.randrange(20, 780, 20), random.randrange(20, 580, 20)]
                apple_rect = pygame.draw.circle(screen, [255, 0, 0], apple_point, 20, 0)  # 绘制圆点
                break
    
        """让蛇移动"""
        pos = len(snake_list) - 1
        # 蛇身移动
        while pos > 0:
            snake_list[pos] = copy.deepcopy(snake_list[pos - 1])  # 把前一个蛇身赋值给后一个蛇身
            pos -= 1
        # 更改蛇头位置,蛇头单独处理,蛇头控制方向
        if move_up:
            snake_list[pos][1] -= 20
            if snake_list[pos][1] < 0:
                snake_list[pos][1] = 600
        if move_down:
            snake_list[pos][1] += 20
            if snake_list[pos][1] > 600:
                snake_list[pos][1] = 0
        if move_left:
            snake_list[pos][0] -= 20
            if snake_list[pos][0] < 0:
                snake_list[pos][0] = 800
        if move_right:
            snake_list[pos][0] += 20
            if snake_list[pos][0] > 800:
                snake_list[pos][0] = 0
    
        # 蛇撞上自己,结束游戏
        head_rect = snake_rect[0]  # 蛇头
        count = len(snake_rect)
        while count > 1:
            # 矩形碰撞检测
            if head_rect.colliderect(snake_rect[count - 1]):
                running = False
            count -= 1
        # 将绘制的内容显示出来
        pygame.display.update()
    
    pygame.quit()

4、运行结果

猜你喜欢

转载自blog.csdn.net/weixin_45100742/article/details/135042245