用python编写一个贪吃蛇游戏
时间: 2023-08-27 17:05:52 浏览: 82
贪吃蛇.app.zip
以下是一个基于 Python 的简单贪吃蛇游戏示例:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 游戏界面宽高
width = 800
height = 600
# 定义颜色
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)
# 设置游戏窗口
game_window = pygame.display.set_mode((width, height))
pygame.display.set_caption('贪吃蛇游戏')
# 定义蛇的初始位置和移动速度
snake_position = [100, 50]
snake_body = [[100, 50], [90, 50], [80, 50]]
speed = pygame.time.Clock()
# 初始化食物位置
food_position = [random.randrange(1, (width // 10)) * 10,
random.randrange(1, (height // 10)) * 10]
food_spawn = True
# 初始化游戏结束标志
game_over = False
# 初始化方向,初始向右移动
direction = 'RIGHT'
change_to = direction
# 定义移动函数
def move_snake(change_to):
if change_to == 'RIGHT' and direction != 'LEFT':
return 'RIGHT'
if change_to == 'LEFT' and direction != 'RIGHT':
return 'LEFT'
if change_to == 'UP' and direction != 'DOWN':
return 'UP'
if change_to == 'DOWN' and direction != 'UP':
return 'DOWN'
return direction
# 定义游戏结束函数
def game_over_screen():
my_font = pygame.font.SysFont('times new roman', 50)
game_over_surface = my_font.render('Game Over', True, red)
game_over_rect = game_over_surface.get_rect()
game_over_rect.midtop = (width / 2, height / 4)
game_window.fill(black)
game_window.blit(game_over_surface, game_over_rect)
pygame.display.flip()
pygame.time.wait(2000)
pygame.quit()
quit()
# 游戏主循环
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
change_to = 'RIGHT'
if event.key == pygame.K_LEFT:
change_to = 'LEFT'
if event.key == pygame.K_UP:
change_to = 'UP'
if event.key == pygame.K_DOWN:
change_to = 'DOWN'
# 移动蛇头位置
direction = move_snake(change_to)
if direction == 'RIGHT':
snake_position[0] += 10
if direction == 'LEFT':
snake_position[0] -= 10
if direction == 'UP':
snake_position[1] -= 10
if direction == 'DOWN':
snake_position[1] += 10
# 长度增加
snake_body.insert(0, list(snake_position))
if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:
food_spawn = False
else:
snake_body.pop()
# 生成新的食物
if not food_spawn:
food_position = [random.randrange(1, (width // 10)) * 10,
random.randrange(1, (height // 10)) * 10]
food_spawn = True
game_window.fill(black)
# 绘制蛇身和食物
for pos in snake_body:
pygame.draw.rect(game_window, green,
pygame.Rect(pos[0], pos[1], 10, 10))
pygame.draw.rect(game_window, white,
pygame.Rect(food_position[0], food_position[1], 10, 10))
# 游戏结束条件
if snake_position[0] < 0 or snake_position[0] > width - 10:
game_over = True
if snake_position[1] < 0 or snake_position[1] > height - 10:
game_over = True
for block in snake_body[1:]:
if snake_position[0] == block[0] and snake_position[1] == block[1]:
game_over = True
pygame.display.update()
speed.tick(16)
# 显示游戏结束界面
game_over_screen()
```
您可以将以上代码保存为 Python 文件,然后运行该文件即可开始贪吃蛇游戏。通过方向键来控制蛇的移动,并尽量吃到食物,避免撞到边界或自己的身体。游戏结束后会显示“Game Over”字样,然后自动退出游戏。
希望这个示例能帮助您开始编写贪吃蛇游戏。如有任何疑问,请随时提问。
阅读全文