python贪吃蛇代码
时间: 2023-07-02 11:04:32 浏览: 42
以下是一个简单的Python贪吃蛇代码:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 设置屏幕尺寸
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
# 创建屏幕
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
# 设置游戏标题
pygame.display.set_caption('Snake Game')
# 设置游戏中的字体
font = pygame.font.SysFont(None, 25)
# 定义初始蛇的位置
snake_block_size = 10
snake_list = []
snake_length = 1
head_x = SCREEN_WIDTH / 2
head_y = SCREEN_HEIGHT / 2
# 设置初始速度
speed = 10
# 定义食物的位置
food_x = round(random.randrange(0, SCREEN_WIDTH - snake_block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, SCREEN_HEIGHT - snake_block_size) / 10.0) * 10.0
# 定义游戏结束的变量
game_over = False
# 定义计分板
score = 0
# 定义蛇的移动方向
direction = 'right'
# 定义蛇的移动函数
def move_snake(direction, head_x, head_y, snake_list, snake_length):
if direction == 'right':
head_x += snake_block_size
elif direction == 'left':
head_x -= snake_block_size
elif direction == 'up':
head_y -= snake_block_size
elif direction == 'down':
head_y += snake_block_size
# 将蛇头加入到蛇的列表中
snake_head = []
snake_head.append(head_x)
snake_head.append(head_y)
snake_list.append(snake_head)
# 如果蛇的长度超过了最大长度,删除蛇尾
if len(snake_list) > snake_length:
del snake_list[0]
return snake_list
# 定义撞到墙壁或自己的函数
def collision(snake_list, head_x, head_y):
# 撞到墙壁
if head_x >= SCREEN_WIDTH or head_x < 0 or head_y >= SCREEN_HEIGHT or head_y < 0:
return True
# 撞到自己
for block in snake_list[:-1]:
if block == [head_x, head_y]:
return True
return False
# 游戏循环
while not game_over:
# 处理游戏事件
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:
direction = 'left'
elif event.key == pygame.K_RIGHT:
direction = 'right'
elif event.key == pygame.K_UP:
direction = 'up'
elif event.key == pygame.K_DOWN:
direction = 'down'
# 移动蛇
snake_list = move_snake(direction, head_x, head_y, snake_list, snake_length)
head_x = snake_list[-1][0]
head_y = snake_list[-1][1]
# 判断是否撞到墙壁或自己
if collision(snake_list, head_x, head_y):
game_over = True
# 判断是否吃到食物
if head_x == food_x and head_y == food_y:
food_x = round(random.randrange(0, SCREEN_WIDTH - snake_block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, SCREEN_HEIGHT - snake_block_size) / 10.0) * 10.0
snake_length += 1
speed += 1
score += 10
# 绘制游戏界面
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, [food_x, food_y, snake_block_size, snake_block_size])
for block in snake_list:
pygame.draw.rect(screen, BLACK, [block[0], block[1], snake_block_size, snake_block_size])
# 显示分数
text = font.render("Score: " + str(score), True, RED)
screen.blit(text, [0, 0])
# 更新屏幕
pygame.display.update()
# 控制游戏速度
pygame.time.delay(100 - speed)
# 退出 Pygame
pygame.quit()
```
这个代码使用 Pygame 库创建了一个简单的贪吃蛇游戏。在游戏循环中,通过处理键盘事件来控制蛇的移动方向,同时判断蛇是否撞到墙壁或自己,并根据吃到食物的情况来增加蛇的长度、速度和分数。最后,通过 Pygame 库来绘制游戏界面,并控制游戏速度。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)