python贪吃蛇代码及注释
时间: 2023-12-25 17:59:08 浏览: 65
以下是Python实现贪吃蛇的代码及注释:
```python
import pygame
import random
# 定义颜色
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
# 初始化pygame
pygame.init()
# 设置屏幕大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode([screen_width, screen_height])
# 设置字体
font = pygame.font.SysFont(None, 25)
# 设置标题
pygame.display.set_caption('Snake Game')
# 设置时钟
clock = pygame.time.Clock()
# 设置方块大小
block_size = 10
# 定义函数,用于显示消息
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 gameLoop():
gameExit = False
gameOver = False
# 初始化贪吃蛇位置和长度
lead_x = screen_width / 2
lead_y = screen_height / 2
lead_x_change = 0
lead_y_change = 0
snake_list = []
snake_length = 1
# 初始化食物位置
randAppleX = round(random.randrange(0, screen_width - block_size) / 10.0) * 10.0
randAppleY = round(random.randrange(0, screen_height - block_size) / 10.0) * 10.0
# 游戏循环
while not gameExit:
# 游戏结束循环
while gameOver == True:
screen.fill(white)
message_to_screen("Game over, press C to continue or Q to quit", red)
pygame.display.update()
# 监听事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
gameOver = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c:
gameLoop()
# 监听事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = 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:
gameOver = True
# 移动贪吃蛇
lead_x += lead_x_change
lead_y += lead_y_change
# 画出背景
screen.fill(white)
# 画出食物
pygame.draw.rect(screen, red, [randAppleX, randAppleY, block_size, block_size])
# 将贪吃蛇头部的位置添加到贪吃蛇列表中
snake_head = []
snake_head.append(lead_x)
snake_head.append(lead_y)
snake_list.append(snake_head)
# 判断贪吃蛇长度是否达到指定值
if len(snake_list) > snake_length:
del snake_list[0]
# 判断是否吃到食物
for eachSegment in snake_list[:-1]:
if eachSegment == snake_head:
gameOver = True
# 画出贪吃蛇
draw_snake(block_size, snake_list)
# 更新分数
score = snake_length - 1
score_text = font.render("Score: " + str(score), True, black)
screen.blit(score_text, [0, 0])
# 更新屏幕
pygame.display.update()
# 判断是否吃到食物
if lead_x == randAppleX and lead_y == randAppleY:
randAppleX = round(random.randrange(0, screen_width - block_size) / 10.0) * 10.0
randAppleY = round(random.randrange(0, screen_height - block_size) / 10.0) * 10.0
snake_length += 1
# 设置游戏帧率
clock.tick(20)
# 退出pygame
pygame.quit()
# 退出程序
quit()
# 运行游戏
gameLoop()
```
以上是Python实现贪吃蛇的代码及注释,希望对你有所帮助!
阅读全文