软件价值2-贪吃蛇游戏

贪吃蛇游戏虽然很多,不过它可以作为软件创作的开端,用python来实现,然后dist成windows系统可执行文件。

import pygame
import sys
import random

# 初始化
pygame.init()

# 游戏设置
width, height = 640, 480
cell_size = 20
snake_speed = 15

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

# 创建窗口
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("贪吃蛇游戏")

# 初始化蛇
snake = [(100, 100), (90, 100), (80, 100)]
snake_direction = (cell_size, 0)

# 初始化食物
food = (random.randint(0, width - cell_size) // cell_size * cell_size,
        random.randint(0, height - cell_size) // cell_size * cell_size)

# 游戏循环
clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and snake_direction != (0, cell_size):
                snake_direction = (0, -cell_size)
            elif event.key == pygame.K_DOWN and snake_direction != (0, -cell_size):
                snake_direction = (0, cell_size)
            elif event.key == pygame.K_LEFT and snake_direction != (cell_size, 0):
                snake_direction = (-cell_size, 0)
            elif event.key == pygame.K_RIGHT and snake_direction != (-cell_size, 0):
                snake_direction = (cell_size, 0)

    # 移动蛇
    head = (snake[0][0] + snake_direction[0], snake[0][1] + snake_direction[1])

    # 边界碰撞检测
    if head[0] < 0:
        head = (width - cell_size, head[1])
    elif head[0] >= width:
        head = (0, head[1])
    elif head[1] < 0:
        head = (head[0], height - cell_size)
    elif head[1] >= height:
        head = (head[0], 0)

    snake = [head] + snake[:-1]

    # 判断是否吃到食物
    if head == food:
        snake.append(snake[-1])
        food = (random.randint(0, width - cell_size) // cell_size * cell_size,
                random.randint(0, height - cell_size) // cell_size * cell_size)

    # 判断是否游戏结束
    if head in snake[1:]:
        pygame.quit()
        sys.exit()

    # 绘制窗口
    window.fill(black)

    # 绘制蛇
    for segment in snake:
        pygame.draw.rect(window, white, pygame.Rect(segment[0], segment[1], cell_size, cell_size))

    # 绘制食物
    pygame.draw.rect(window, red, pygame.Rect(food[0], food[1], cell_size, cell_size))

    # 更新显示
    pygame.display.flip()

    # 控制帧率
    clock.tick(snake_speed)

发布: 

用PyInstaller可以将 Python 脚本打包成 Windows、Linux 和 macOS 上的可执行文件。你可以使用以下命令安装 PyInstaller:

pip install pyinstaller

然后,可以使用以下命令将你的游戏脚本打包成一个可执行文件:

pyinstaller --onefile greedySnake.py

这将在 dist 文件夹中生成一个单一的可执行文件。

运行:

贪吃蛇游戏

相关推荐

  1. 软件价值2-贪吃游戏

    2024-01-31 01:48:02       60 阅读
  2. 贪吃游戏

    2024-01-31 01:48:02       54 阅读
  3. 贪吃游戏

    2024-01-31 01:48:02       47 阅读
  4. 贪吃游戏

    2024-01-31 01:48:02       47 阅读

最近更新

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

    2024-01-31 01:48:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-31 01:48:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-31 01:48:02       82 阅读
  4. Python语言-面向对象

    2024-01-31 01:48:02       91 阅读

热门阅读

  1. day35_js

    2024-01-31 01:48:02       56 阅读
  2. 深入到 TLP:PCI Express 设备如何通信(第二部分)

    2024-01-31 01:48:02       58 阅读
  3. ETCD节点故障的容错方案

    2024-01-31 01:48:02       64 阅读
  4. C++ 手记

    2024-01-31 01:48:02       50 阅读
  5. 【vue】前后端不在同一网络下,前端解决跨域

    2024-01-31 01:48:02       48 阅读
  6. python3-cookbook-查找两字典的相同点

    2024-01-31 01:48:02       70 阅读
  7. 738. 单调递增的数字 - 力扣(LeetCode)

    2024-01-31 01:48:02       51 阅读
  8. 达梦 hibernate连接主备集群

    2024-01-31 01:48:02       56 阅读
  9. 蓝桥杯练习-dfs算法飞机降落问题

    2024-01-31 01:48:02       58 阅读