python贪吃蛇代码
时间: 2023-07-02 09:14:28 浏览: 22
以下是一个简单的Python贪吃蛇代码示例:
```python
import pygame, random
# 初始化 Pygame
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 定义屏幕大小
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# 创建屏幕
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
# 设置窗口标题
pygame.display.set_caption('贪吃蛇')
# 设置游戏时钟
clock = pygame.time.Clock()
# 定义方块大小和间隔
BLOCK_SIZE = 20
BLOCK_GAP = 2
# 定义贪吃蛇和食物的初始位置
snake_x = 10
snake_y = 10
food_x = random.randint(0, SCREEN_WIDTH // BLOCK_SIZE - 1) * (BLOCK_SIZE + BLOCK_GAP)
food_y = random.randint(0, SCREEN_HEIGHT // BLOCK_SIZE - 1) * (BLOCK_SIZE + BLOCK_GAP)
# 定义贪吃蛇的初始长度和速度
snake_length = 1
snake_speed = 5
# 定义贪吃蛇的移动方向
snake_direction = 'right'
# 定义贪吃蛇的身体坐标列表
snake_body = [(snake_x, snake_y)]
# 定义字体
font = pygame.font.Font(None, 36)
# 游戏循环
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 and snake_direction != 'down':
snake_direction = 'up'
elif event.key == pygame.K_DOWN and snake_direction != 'up':
snake_direction = 'down'
elif event.key == pygame.K_LEFT and snake_direction != 'right':
snake_direction = 'left'
elif event.key == pygame.K_RIGHT and snake_direction != 'left':
snake_direction = 'right'
# 移动贪吃蛇
if snake_direction == 'up':
snake_y -= BLOCK_SIZE + BLOCK_GAP
elif snake_direction == 'down':
snake_y += BLOCK_SIZE + BLOCK_GAP
elif snake_direction == 'left':
snake_x -= BLOCK_SIZE + BLOCK_GAP
elif snake_direction == 'right':
snake_x += BLOCK_SIZE + BLOCK_GAP
# 判断是否吃到食物
if snake_x == food_x and snake_y == food_y:
food_x = random.randint(0, SCREEN_WIDTH // BLOCK_SIZE - 1) * (BLOCK_SIZE + BLOCK_GAP)
food_y = random.randint(0, SCREEN_HEIGHT // BLOCK_SIZE - 1) * (BLOCK_SIZE + BLOCK_GAP)
snake_length += 1
# 更新贪吃蛇的身体坐标列表
snake_body.insert(0, (snake_x, snake_y))
if len(snake_body) > snake_length:
snake_body.pop()
# 绘制背景
screen.fill(BLACK)
# 绘制贪吃蛇
for i, (x, y) in enumerate(snake_body):
pygame.draw.rect(screen, GREEN, [x, y, BLOCK_SIZE, BLOCK_SIZE])
if i == 0:
pygame.draw.rect(screen, BLUE, [x, y, BLOCK_SIZE, BLOCK_SIZE])
# 绘制食物
pygame.draw.rect(screen, RED, [food_x, food_y, BLOCK_SIZE, BLOCK_SIZE])
# 绘制分数
score_text = font.render('Score: ' + str(snake_length - 1), True, WHITE)
screen.blit(score_text, [10, 10])
# 更新屏幕
pygame.display.update()
# 控制游戏速度
clock.tick(snake_speed)
# 退出 Pygame
pygame.quit()
```
注意:以上代码只是一个简单的示例,可能还需要进行优化和改进。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)