使用python写一个复杂版的贪吃蛇
时间: 2023-09-08 19:07:27 浏览: 36
好的,我可以为您提供一个基于Python的复杂版贪吃蛇的代码,以下是代码:
```
import pygame
import random
# 初始化pygame
pygame.init()
# 设置屏幕尺寸和字体
screen_width = 480
screen_height = 480
font = pygame.font.SysFont('Arial', 25)
# 创建屏幕对象
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置窗口标题
pygame.display.set_caption('贪吃蛇')
# 定义颜色值
white = (255, 255, 255)
black = (0, 0, 0)
green = (0, 255, 0)
red = (255, 0, 0)
# 设置每个方块的大小
block_size = 20
# 加载图片
head_img = pygame.image.load('head.png')
tail_img = pygame.image.load('tail.png')
apple_img = pygame.image.load('apple.png')
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.body = [(screen_width/2, screen_height/2)]
self.direction = 'right'
self.score = 0
self.add_block = False
def move(self):
# 获取头部位置
x, y = self.body[0]
# 根据方向移动头部
if self.direction == 'up':
y -= block_size
elif self.direction == 'down':
y += block_size
elif self.direction == 'left':
x -= block_size
elif self.direction == 'right':
x += block_size
# 添加头部
self.body.insert(0, (x, y))
# 如果需要添加身体块,则不删除尾部
if not self.add_block:
self.body.pop()
else:
self.score += 1
self.add_block = False
def draw(self):
# 绘制蛇头
x, y = self.body[0]
screen.blit(head_img, (x, y))
# 绘制蛇身体
for i, (x, y) in enumerate(self.body[1:-1]):
prev_x, prev_y = self.body[i]
next_x, next_y = self.body[i+2]
if prev_y == next_y:
screen.blit(tail_img, (x, y))
elif prev_x == next_x:
screen.blit(pygame.transform.rotate(tail_img, 90), (x, y))
# 绘制蛇尾
x, y = self.body[-1]
prev_x, prev_y = self.body[-2]
if prev_y == y:
screen.blit(tail_img, (x, y))
elif prev_x == x:
screen.blit(pygame.transform.rotate(tail_img, 90), (x, y))
def eat_apple(self, apple_pos):
if self.body[0] == apple_pos:
self.add_block = True
return True
return False
def collide_with_wall(self):
x, y = self.body[0]
if x < 0 or x > screen_width - block_size or y < 0 or y > screen_height - block_size:
return True
return False
def collide_with_self(self):
for i, (x, y) in enumerate(self.body):
if (x, y) in self.body[i+1:]:
return True
return False
# 定义苹果类
class Apple:
def __init__(self):
self.pos = self.random_pos()
def draw(self):
x, y = self.pos
screen.blit(apple_img, (x, y))
def random_pos(self):
x = random.randrange(0, screen_width-block_size, block_size)
y = random.randrange(0, screen_height-block_size, block_size)
return (x, y)
# 初始化贪吃蛇和苹果
snake = Snake()
apple = Apple()
# 游戏循环
running = True
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and snake.direction != 'down':
snake.direction = 'up'
elif event.key == pygame.K_DOWN and snake.direction != 'up':
snake.direction = 'down'
elif event.key == pygame.K_LEFT and snake.direction != 'right':
snake.direction = 'left'
elif event.key == pygame.K_RIGHT and snake.direction != 'left':
snake.direction = 'right'
# 移动贪吃蛇
snake.move()
# 碰撞检测
if snake.eat_apple(apple.pos):
apple.pos = apple.random_pos()
if snake.collide_with_wall() or snake.collide_with_self():
running = False
# 绘制屏幕
screen.fill(white)
snake.draw()
apple.draw()
# 显示分数
score = font.render('Score: {}'.format(snake.score), True, black)
screen.blit(score, (10, 10))
# 刷新屏幕
pygame.display.flip()
# 退出pygame
pygame.quit()
```
该游戏包含了贪吃蛇的基本功能,如移动、吃苹果、碰撞检测等,并且添加了图片和分数显示等更多细节。您可以自由修改和扩展它以获得更好的游戏体验。