python 贪吃蛇

下面是一个简单的贪吃蛇游戏的Python代码示例,使用了Pygame库:

```python
import pygame
import time
import random

# 初始化Pygame
pygame.init()

# 设置屏幕大小和标题
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('贪吃蛇')

# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

# 设置蛇和食物的大小
block_size = 20

# 设置蛇的移动速度
snake_speed = 15

# 定义字体
font_style = pygame.font.SysFont(None, 50)

# 定义得分显示函数
def show_score(score):
    score_text = font_style.render("得分: " + str(score), True, black)
    screen.blit(score_text, [0, 0])

# 定义蛇的绘制函数
def draw_snake(block_size, snake_list):
    for x in snake_list:
        pygame.draw.rect(screen, green, [x[0], x[1], block_size, block_size])

# 主函数
def gameLoop():
    game_over = False
    game_close = False

    # 初始化蛇的起始位置和长度
    lead_x = screen_width / 2
    lead_y = screen_height / 2
    lead_x_change = 0
    lead_y_change = 0
    snake_list = []
    snake_length = 1

    # 初始化食物位置
    food_x = round(random.randrange(0, screen_width - block_size) / block_size) * block_size
    food_y = round(random.randrange(0, screen_height - block_size) / block_size) * block_size

    # 游戏循环
    while not game_over:

        while game_close == True:
            screen.fill(white)
            message = font_style.render("游戏结束!按Q退出,按C重新开始", True, black)
            screen.blit(message, [screen_width / 5, screen_height / 3])
            show_score(snake_length - 1)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    game_over = True
                    game_close = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    if event.key == pygame.K_c:
                        gameLoop()

        # 控制蛇的移动
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    lead_x_change = -block_size
                    lead_y_change = 0
                elif event.key == pygame.K_RIGHT:
                    lead_x_change = block_size
                    lead_y_change = 0
                elif event.key == pygame.K_UP:
                    lead_y_change = -block_size
                    lead_x_change = 0
                elif event.key == pygame.K_DOWN:
                    lead_y_change = block_size
                    lead_x_change = 0

        # 边界判断
        if lead_x >= screen_width or lead_x < 0 or lead_y >= screen_height or lead_y < 0:
            game_close = True

        lead_x += lead_x_change
        lead_y += lead_y_change
        screen.fill(black)

        # 绘制食物
        pygame.draw.rect(screen, red, [food_x, food_y, block_size, block_size])

        snake_head = []
        snake_head.append(lead_x)
        snake_head.append(lead_y)
        snake_list.append(snake_head)

        # 控制蛇的长度
        if len(snake_list) > snake_length:
            del snake_list[0]

        for x in snake_list[:-1]:
            if x == snake_head:
                game_close = True

        draw_snake(block_size, snake_list)
        show_score(snake_length - 1)

        pygame.display.update()

        # 如果蛇吃到了食物
        if lead_x == food_x and lead_y == food_y:
            food_x = round(random.randrange(0, screen_width - block_size) / block_size) * block_size
            food_y = round(random.randrange(0, screen_height - block_size) / block_size) * block_size
            snake_length += 1

        # 设置蛇的移动速度
        clock = pygame.time.Clock()
        clock.tick(snake_speed)

    pygame.quit()
    quit()

gameLoop()
```

这是一个简单的贪吃蛇游戏,使用了Pygame库来实现图形化界面和游戏逻辑。你可以将这段代码保存为一个`.py`文件,然后在你的计算机上运行它。希望你喜欢!

相关推荐

  1. python贪吃

    2024-04-29 07:42:01       57 阅读
  2. python 贪吃

    2024-04-29 07:42:01       28 阅读
  3. python制作贪吃游戏

    2024-04-29 07:42:01       56 阅读
  4. 贪吃代码python实现

    2024-04-29 07:42:01       27 阅读
  5. python项目练习——29.贪吃

    2024-04-29 07:42:01       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-29 07:42:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-29 07:42:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-29 07:42:01       87 阅读
  4. Python语言-面向对象

    2024-04-29 07:42:01       96 阅读

热门阅读

  1. c/c++传值和传引用的区别

    2024-04-29 07:42:01       32 阅读
  2. Pytorch中保存模型的两种方法

    2024-04-29 07:42:01       30 阅读
  3. 基于 PyTorch 框架写一个图片分类模型

    2024-04-29 07:42:01       34 阅读
  4. C++Webserver服务器常见面试问题总结

    2024-04-29 07:42:01       27 阅读
  5. python代码实现支持向量机对鸢尾花分类

    2024-04-29 07:42:01       37 阅读
  6. webpack配置文件

    2024-04-29 07:42:01       29 阅读
  7. 华为OD机试-螺旋数字矩阵

    2024-04-29 07:42:01       38 阅读