python链表实现贪吃蛇
时间: 2023-12-11 09:03:42 浏览: 255
以下是Python链表实现贪吃蛇的示例代码:
```python
import pygame
import random
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# 初始化pygame
pygame.init()
# 设置游戏窗口大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置游戏标题
pygame.display.set_caption('Snake Game')
# 设置游戏时钟
clock = pygame.time.Clock()
# 定义字体
font = pygame.font.SysFont(None, 25)
# 定义蛇的初始位置和大小
block_size = 10
snake_speed = 15
# 定义函数,用于显示消息
def message_to_screen(msg, color):
screen_text = font.render(msg, True, color)
screen.blit(screen_text, [screen_width/2, screen_height/2])
# 定义函数,用于绘制蛇
def draw_snake(block_size, snake_list):
for x in snake_list:
pygame.draw.rect(screen, green, [x[0], x[1], block_size, block_size])
# 定义函数,用于显示得分
def show_score(score):
score_text = font.render("Score: " + str(score), True, black)
screen.blit(score_text, [0, 0])
# 定义函数,用于显示游戏结束消息
def game_over():
message_to_screen("Game over, press C to play again or Q to quit", red)
pygame.display.update()
# 定义函数,用于实现贪吃蛇游戏
def gameLoop():
game_exit = False
game_over_flag = False
# 初始化蛇的位置和长度
lead_x = screen_width / 2
lead_y = screen_height / 2
lead_x_change = 0
lead_y_change = 0
snake_list = []
snake_length = 1
# 初始化食物的位置
food_x = round(random.randrange(0, screen_width - block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, screen_height - block_size) / 10.0) * 10.0
while not game_exit:
while game_over_flag == True:
game_over()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_exit = True
game_over_flag = False
if event.key == pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
lead_x_change = block_size
lead_y_change = 0
elif event.key == pygame.K_UP:
lead_y_change = -block_size
lead_x_change = 0
elif event.key == pygame.K_DOWN:
lead_y_change = block_size
lead_x_change = 0
# 判断蛇是否超出边界
if lead_x >= screen_width or lead_x < 0 or lead_y >= screen_height or lead_y < 0:
game_over_flag = True
# 更新蛇的位置
lead_x += lead_x_change
lead_y += lead_y_change
# 绘制游戏背景
screen.fill(white)
# 绘制食物
pygame.draw.rect(screen, blue, [food_x, food_y, block_size, block_size])
# 将蛇的位置添加到列表中
snake_head = []
snake_head.append(lead_x)
snake_head.append(lead_y)
snake_list.append(snake_head)
# 如果蛇的长度超过了snake_length,就删除列表中的第一个元素
if len(snake_list) > snake_length:
del snake_list[0]
# 判断蛇是否碰到了自己
for segment in snake_list[:-1]:
if segment == snake_head:
game_over_flag = True
# 绘制蛇
draw_snake(block_size, snake_list)
# 显示得分
show_score(snake_length - 1)
# 更新食物的位置
if lead_x == food_x and lead_y == food_y:
food_x = round(random.randrange(0, screen_width - block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, screen_height - block_size) / 10.0) * 10.0
snake_length += 1
# 更新屏幕
pygame.display.update()
# 设置游戏时钟
clock.tick(snake_speed)
# 退出pygame
pygame.quit()
# 开始游戏
gameLoop()
```
阅读全文