用python写贪吃蛇
时间: 2023-07-19 15:43:14 浏览: 79
好的,下面是一个简单的Python贪吃蛇游戏代码,希望对你有所帮助:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 定义颜色
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
# 设置游戏窗口大小和标题
screen_width = 640
screen_height = 480
pygame.display.set_caption('贪吃蛇')
screen = pygame.display.set_mode((screen_width, screen_height))
# 定义游戏帧率和时钟对象
fps = pygame.time.Clock()
# 定义贪吃蛇的初始位置和大小
snake_position = [100, 50]
snake_body = [[100, 50], [90, 50], [80, 50]]
# 定义食物的初始位置
food_position = [random.randrange(1, screen_width // 10) * 10,
random.randrange(1, screen_height // 10) * 10]
# 定义食物的大小
food_size = [10, 10]
# 定义初始方向
direction = 'RIGHT'
change_direction = direction
# 定义字体对象
font = pygame.font.SysFont('simhei', 18)
def show_score(score):
"""显示得分"""
score_text = font.render('得分: ' + str(score), True, white)
screen.blit(score_text, [0, 0])
def game_over():
"""游戏结束"""
gameover_text = font.render('游戏结束!', True, white)
gameover_rect = gameover_text.get_rect()
gameover_rect.midtop = (screen_width / 2, screen_height / 4)
screen.blit(gameover_text, gameover_rect)
pygame.display.flip()
pygame.time.delay(2000)
pygame.quit()
quit()
# 开始游戏循环
score = 0
while True:
# 处理游戏事件
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:
change_direction = 'RIGHT'
elif event.key == pygame.K_LEFT:
change_direction = 'LEFT'
elif event.key == pygame.K_UP:
change_direction = 'UP'
elif event.key == pygame.K_DOWN:
change_direction = 'DOWN'
elif event.key == pygame.K_ESCAPE:
pygame.quit()
quit()
# 判断是否改变方向
if change_direction == 'RIGHT' and not direction == 'LEFT':
direction = 'RIGHT'
elif change_direction == 'LEFT' and not direction == 'RIGHT':
direction = 'LEFT'
elif change_direction == 'UP' and not direction == 'DOWN':
direction = 'UP'
elif change_direction == 'DOWN' and not direction == 'UP':
direction = 'DOWN'
# 移动贪吃蛇
if direction == 'RIGHT':
snake_position[0] += 10
elif direction == 'LEFT':
snake_position[0] -= 10
elif direction == 'UP':
snake_position[1] -= 10
elif direction == 'DOWN':
snake_position[1] += 10
# 更新贪吃蛇的身体
snake_body.insert(0, list(snake_position))
if snake_position == food_position:
food_position = [random.randrange(1, screen_width // 10) * 10,
random.randrange(1, screen_height // 10) * 10]
score += 10
else:
snake_body.pop()
# 绘制游戏界面
screen.fill(black)
for position in snake_body:
pygame.draw.rect(screen, green, pygame.Rect(
position[0], position[1], 10, 10))
pygame.draw.rect(screen, red, pygame.Rect(
food_position[0], food_position[1], food_size[0], food_size[1]))
show_score(score)
pygame.display.update()
# 判断游戏是否结束
if snake_position[0] < 0 or snake_position[0] > screen_width - 10:
game_over()
elif snake_position[1] < 0 or snake_position[1] > screen_height - 10:
game_over()
for block in snake_body[1:]:
if snake_position == block:
game_over()
# 控制游戏帧率
fps.tick(20)
```
该代码使用Pygame库实现了一个简单的贪吃蛇游戏,主要包括游戏界面的绘制、键盘事件的处理、贪吃蛇的移动、得分的显示、游戏结束等功能。你可以直接将代码复制到Python环境中运行,体验游戏的效果。
阅读全文