用Python写一个贪吃蛇代码
时间: 2023-05-17 08:05:06 浏览: 82
以下是一个简单的贪吃蛇代码,使用 Python 语言编写:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置游戏窗口大小
window_width = 640
window_height = 480
window = pygame.display.set_mode((window_width, window_height))
# 设置游戏标题
pygame.display.set_caption('贪吃蛇')
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
# 定义蛇的初始位置和大小
snake_block_size = 10
snake_speed = 15
snake_list = []
snake_length = 1
snake_x = window_width / 2
snake_y = window_height / 2
# 定义食物的初始位置和大小
food_block_size = 10
food_x = round(random.randrange(0, window_width - food_block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, window_height - food_block_size) / 10.0) * 10.0
# 定义字体
font_style = pygame.font.SysFont(None, 30)
# 定义游戏结束函数
def game_over():
message = font_style.render('游戏结束!按 Q 退出,按 R 重新开始', True, red)
window.blit(message, [window_width / 6, window_height / 3])
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
quit()
elif event.key == pygame.K_r:
game_loop()
# 定义游戏循环函数
def game_loop():
global snake_x, snake_y, snake_list, snake_length, food_x, food_y
# 初始化蛇的位置和长度
snake_x = window_width / 2
snake_y = window_height / 2
snake_list = []
snake_length = 1
# 初始化食物的位置
food_x = round(random.randrange(0, window_width - food_block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, window_height - food_block_size) / 10.0) * 10.0
# 定义蛇的移动方向
direction = 'right'
# 游戏循环
game_exit = False
while not game_exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
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'
# 移动蛇的位置
if direction == 'right':
snake_x += snake_block_size
elif direction == 'left':
snake_x -= snake_block_size
elif direction == 'up':
snake_y -= snake_block_size
elif direction == 'down':
snake_y += snake_block_size
# 判断蛇是否吃到食物
if snake_x == food_x and snake_y == food_y:
food_x = round(random.randrange(0, window_width - food_block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, window_height - food_block_size) / 10.0) * 10.0
snake_length += 1
# 绘制游戏窗口
window.fill(white)
pygame.draw.rect(window, green, [food_x, food_y, food_block_size, food_block_size])
snake_head = []
snake_head.append(snake_x)
snake_head.append(snake_y)
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
for block in snake_list[:-1]:
if block == snake_head:
game_over()
for block in snake_list:
pygame.draw.rect(window, black, [block[0], block[1], snake_block_size, snake_block_size])
pygame.display.update()
# 判断蛇是否撞墙
if snake_x >= window_width or snake_x < 0 or snake_y >= window_height or snake_y < 0:
game_over()
# 控制游戏速度
clock = pygame.time.Clock()
clock.tick(snake_speed)
# 开始游戏
game_loop()
# 退出 Pygame
pygame.quit()
quit()
```
希望这个代码对你有所帮助!
阅读全文