撞墙会死,撞自己不会,每吃一个蛋会加速移速
import random
import pygame
# 初始化 Pygame
pygame.init()
# 定义游戏窗口大小
window_width = 640
window_height = 480
# 定义贪吃蛇的初始位置和大小
snake_x = 0
snake_y = 0
snake_size = 20
# 定义贪吃蛇的移动速度和方向
snake_speed = 0.1
add_snake_speed = 0.01
snake_directions = ['right', 'left', 'down', 'up']
snake_direction = random.choice(snake_directions)
# 定义食物的初始位置和大小
food_x = 0
food_y = 0
food_size = 20
# 定义贪吃蛇的身体
snake_body = []
# 定义游戏循环标志
running = True
# 创建游戏窗口
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('Snake Game')
# 创建时钟对象
clock = pygame.time.Clock()
# 初始化游戏
def init_game():
global snake_x, snake_y, snake_body, food_x, food_y
# 初始化贪吃蛇的位置和身体
snake_x = random.randint(0, window_width // snake_size - 1) * snake_size
snake_y = random.randint(0, window_height // snake_size - 1) * snake_size
snake_body = [{'x': snake_x, 'y': snake_y}]
# 初始化食物的位置
spawn_food()
# 更新游戏状态
def update_game():
global snake_x, snake_y, snake_body, food_x, food_y, running, snake_speed
# 移动贪吃蛇
if snake_direction == 'right':
snake_x += snake_speed * snake_size
elif snake_direction == 'left':
snake_x -= snake_speed * snake_size
elif snake_direction == 'down':
snake_y += snake_speed * snake_size
elif snake_direction == 'up':
snake_y -= snake_speed * snake_size
# 检测贪吃蛇是否撞墙
if snake_x < 0 or snake_x >= window_width or snake_y < 0 or snake_y >= window_height:
running = False
# 检测贪吃蛇是否吃到了食物
if abs(snake_x - food_x) < snake_size and abs(snake_y - food_y) < snake_size:
spawn_food()
# 将贪吃蛇的头部添加到身体列表中
snake_body.append({'x': snake_x, 'y': snake_y})
snake_speed = snake_speed + add_snake_speed
else:
# 移除贪吃蛇的尾部
snake_body.pop()
# 将贪吃蛇的头部添加到身体列表中
snake_body.insert(0, {'x': snake_x, 'y': snake_y})
# 绘制游戏元素
def draw_game():
# 绘制背景
window.fill((128, 128, 128))
# 绘制四周墙体
pygame.draw.rect(window, (255, 165, 0), (0, 0, window_width, snake_size))
pygame.draw.rect(window, (255, 165, 0), (0, 0, snake_size, window_height))
pygame.draw.rect(window, (255, 165, 0), (0, window_height - snake_size, window_width, snake_size))
pygame.draw.rect(window, (255, 165, 0), (window_width - snake_size, 0, snake_size, window_height))
# 绘制贪吃蛇的身体
for i, body_part in enumerate(snake_body):
# 计算蛇身颜色
color = (255 - 255 * i / len(snake_body), 255 * i / len(snake_body), 0)
pygame.draw.rect(window, color, (body_part['x'], body_part['y'], snake_size, snake_size))
# 绘制贪吃蛇的头部
pygame.draw.rect(window, (255, 0, 0), (snake_body[0]['x'], snake_body[0]['y'], snake_size, snake_size))
# 绘制食物
pygame.draw.rect(window, (0, 0, 255), (food_x, food_y, food_size, food_size))
# 更新屏幕
pygame.display.update()
# 生成食物的随机位置
def spawn_food():
global food_x, food_y
food_x = random.randint(0, window_width // snake_size - 1) * snake_size
food_y = random.randint(0, window_height // snake_size - 1) * snake_size
# 处理键盘输入
def handle_input():
global snake_direction
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT and snake_direction != 'left':
snake_direction = 'right'
elif event.key == pygame.K_LEFT and snake_direction != 'right':
snake_direction = 'left'
elif event.key == pygame.K_DOWN and snake_direction != 'up':
snake_direction = 'down'
elif event.key == pygame.K_UP and snake_direction != 'down':
snake_direction = 'up'
# 初始化游戏
init_game()
# 游戏循环
while running:
# 处理键盘输入
handle_input()
# 更新游戏状态
update_game()
# 绘制游戏元素
draw_game()
# 控制游戏帧率
clock.tick(60)
# 退出 Pygame
pygame.quit()