• 用python写一个贪吃蛇的程序能运行能用键盘控制


    用python写一个贪吃蛇的程序能运行能用键盘控制

    1.源码

    开发库使用:pygame random
    直接在终端运行:pip install pygame
    pycharm安装库:文件-设置-项目-Python 解释器
    在这里插入图片描述
    在这里插入图片描述

    import pygame
    import random
    
    # 游戏窗口尺寸
    window_width = 640
    window_height = 480
    
    # 蛇身和食物大小
    block_size = 20
    
    # 初始化pygame
    pygame.init()
    
    # 创建游戏窗口
    window = pygame.display.set_mode((window_width, window_height))
    pygame.display.set_caption("贪吃蛇")
    
    clock = pygame.time.Clock()
    
    # 定义颜色
    white = (255, 255, 255)
    black = (0, 0, 0)
    red = (255, 0, 0)
    
    # 定义蛇类
    class Snake:
        def __init__(self):
            self.body = [(window_width // 2, window_height // 2)]
            self.direction = "right"
    
        def move(self):
            x, y = self.body[0]
    
            if self.direction == "up":
                y -= block_size
            elif self.direction == "down":
                y += block_size
            elif self.direction == "left":
                x -= block_size
            elif self.direction == "right":
                x += block_size
    
            self.body.insert(0, (x, y))
    
        def change_direction(self, new_direction):
            if new_direction == "up" and self.direction != "down":
                self.direction = new_direction
            elif new_direction == "down" and self.direction != "up":
                self.direction = new_direction
            elif new_direction == "left" and self.direction != "right":
                self.direction = new_direction
            elif new_direction == "right" and self.direction != "left":
                self.direction = new_direction
    
        def draw(self):
            for x, y in self.body:
                pygame.draw.rect(window, white, (x, y, block_size, block_size))
    
    # 定义食物类
    class Food:
        def __init__(self):
            self.position = self.generate_food_position()
    
        def generate_food_position(self):
            x = random.randint(0, (window_width - block_size) // block_size) * block_size
            y = random.randint(0, (window_height - block_size) // block_size) * block_size
            return x, y
    
        def draw(self):
            x, y = self.position
            pygame.draw.rect(window, red, (x, y, block_size, block_size))
    
        def check_collision(self, snake):
            return snake.body[0] == self.position
    
    # 创建蛇和食物对象
    snake = Snake()
    food = Food()
    
    game_over = False
    
    # 游戏主循环
    while not game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    snake.change_direction("up")
                elif event.key == pygame.K_DOWN:
                    snake.change_direction("down")
                elif event.key == pygame.K_LEFT:
                    snake.change_direction("left")
                elif event.key == pygame.K_RIGHT:
                    snake.change_direction("right")
    
        snake.move()
    
        if food.check_collision(snake):
            food.position = food.generate_food_position()
        else:
            snake.body.pop()
    
        window.fill(black)
    
        snake.draw()
        food.draw()
    
        pygame.display.update()
        clock.tick(10)
    
    # 退出游戏
    pygame.quit()
    
    • 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

    2.运行效果

    在这里插入图片描述

  • 相关阅读:
    iPhone垃圾清理器:AnyMP4 iOS Cleaner for mac
    Qwt开发笔记(二):Qwt基础框架介绍、折线图介绍、折线图Demo以及代码详解
    将矩阵按对角线排序(c++题解)
    应用程序通过 Envoy 代理和 Jaeger 进行分布式追踪 —— Ingress Controller + Http服务 + Grpc服务(三)
    JVM类加载和垃圾回收
    Google发布Genie硬杠Sora:通过大量无监督视频训练最终生成可交互虚拟世界
    uniapp打包安卓apk的隐私政策配置
    java毕业设计健身房课程预约平台(附源码、数据库)
    软考高级系统架构设计师系列之:案例分析典型试题六
    Java 基于SpringBoot的某家乡美食系统
  • 原文地址:https://blog.csdn.net/weixin_46023406/article/details/133957929