写个贪吃蛇python代码
时间: 2023-05-27 15:06:07 浏览: 37
以下是一个简单的贪吃蛇Python代码:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 游戏窗口大小
window_width = 800
window_height = 600
# 创建游戏窗口
game_display = 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
# 创建字体对象
font_style = pygame.font.SysFont(None, 30)
# 定义显示分数的函数
def show_score(score):
score_text = font_style.render("Score: " + str(score), True, black)
game_display.blit(score_text, [0, 0])
# 定义绘制贪吃蛇的函数
def draw_snake(snake_block_size, snake_list):
for x in snake_list:
pygame.draw.rect(game_display, green, [x[0], x[1], snake_block_size, snake_block_size])
# 游戏主函数
def game_loop():
# 初始贪吃蛇的位置和长度
snake_position_x = window_width / 2
snake_position_y = window_height / 2
snake_length = 1
snake_list = []
# 初始食物的位置
food_position_x = round(random.randrange(0, window_width - snake_block_size) / 10.0) * 10.0
food_position_y = round(random.randrange(0, window_height - snake_block_size) / 10.0) * 10.0
# 定义初始方向
direction = "right"
# 游戏结束标志
game_over = False
# 游戏分数
score = 0
# 游戏循环
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"
# 根据方向移动贪吃蛇头部
if direction == "right":
snake_position_x += snake_block_size
elif direction == "left":
snake_position_x -= snake_block_size
elif direction == "up":
snake_position_y -= snake_block_size
elif direction == "down":
snake_position_y += snake_block_size
# 判断贪吃蛇是否撞墙
if snake_position_x >= window_width or snake_position_x < 0 or snake_position_y >= window_height or snake_position_y < 0:
game_over = True
# 判断贪吃蛇是否吃到食物
if snake_position_x == food_position_x and snake_position_y == food_position_y:
food_position_x = round(random.randrange(0, window_width - snake_block_size) / 10.0) * 10.0
food_position_y = round(random.randrange(0, window_height - snake_block_size) / 10.0) * 10.0
snake_length += 1
score += 10
# 绘制游戏背景
game_display.fill(white)
# 绘制食物
pygame.draw.rect(game_display, red, [food_position_x, food_position_y, snake_block_size, snake_block_size])
# 将贪吃蛇头部的位置加入贪吃蛇列表中
snake_head = []
snake_head.append(snake_position_x)
snake_head.append(snake_position_y)
snake_list.append(snake_head)
# 如果贪吃蛇长度大于限制,删除贪吃蛇尾部的位置
if len(snake_list) > snake_length:
del snake_list[0]
# 判断贪吃蛇是否撞到自己
for x in snake_list[:-1]:
if x == snake_head:
game_over = True
# 绘制贪吃蛇
draw_snake(snake_block_size, snake_list)
# 显示分数
show_score(score)
# 更新游戏窗口
pygame.display.update()
# 设置游戏速度
clock = pygame.time.Clock()
clock.tick(snake_speed)
# 退出pygame
pygame.quit()
# 运行游戏
game_loop()
```
运行上述代码后,将会显示一个简单的贪吃蛇游戏界面。通过上、下、左、右键来控制贪吃蛇的移动,吃到食物会得到10分,撞墙或撞到自己会结束游戏。