用python写一个贪吃蛇
时间: 2024-01-06 20:05:43 浏览: 16
好的,以下是一个简单的贪吃蛇游戏实现,你可以在你的Python编程环境中运行它:
```python
import pygame
import random
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
# 设置屏幕大小
screen_width = 600
screen_height = 400
# 初始化 Pygame 库
pygame.init()
# 创建屏幕
game_display = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Greedy Snake')
# 定义游戏时钟
clock = pygame.time.Clock()
# 定义贪吃蛇的大小和速度
snake_block_size = 10
snake_speed = 15
# 定义字体
font_style = pygame.font.SysFont(None, 30)
# 显示得分
def show_score(score):
score_font = font_style.render("Score: " + str(score), True, white)
game_display.blit(score_font, [0, 0])
# 画出贪吃蛇
def draw_snake(snake_block_size, snake_list):
for x in snake_list:
pygame.draw.rect(game_display, black, [x[0], x[1], snake_block_size, snake_block_size])
# 显示消息
def message(msg, color):
message_font = font_style.render(msg, True, color)
game_display.blit(message_font, [screen_width / 6, screen_height / 3])
# 主函数
def gameLoop():
game_over = False
game_close = False
# 设置贪吃蛇的初始化位置
x1 = screen_width / 2
y1 = screen_height / 2
# 设置贪吃蛇的移动速度和方向
x1_change = 0
y1_change = 0
# 创建初始贪吃蛇
snake_List = []
Length_of_snake = 1
# 设置食物的随机位置
foodx = round(random.randrange(0, screen_width - snake_block_size) / 10.0) * 10.0
foody = round(random.randrange(0, screen_height - snake_block_size) / 10.0) * 10.0
while not game_over:
while game_close:
game_display.fill(black)
message("You Lost! Press Q-Quit or C-Play Again", red)
show_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
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:
x1_change = -snake_block_size
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block_size
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block_size
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block_size
x1_change = 0
# 当贪吃蛇撞墙时游戏结束
if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_close = True
# 移动贪吃蛇的位置
x1 += x1_change
y1 += y1_change
# 绘制食物
game_display.fill(white)
pygame.draw.rect(game_display, red, [foodx, foody, snake_block_size, snake_block_size])
# 把贪吃蛇插入 snake_List 中
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
# 当贪吃蛇身体长度过长时游戏结束
if len(snake_List) > Length_of_snake:
del snake_List[0]
# 当贪吃蛇吃到食物时,生成一个新的食物
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
draw_snake(snake_block_size, snake_List)
show_score(Length_of_snake - 1)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, screen_width - snake_block_size) / 10.0) * 10.0
foody = round(random.randrange(0, screen_height - snake_block_size) / 10.0) * 10.0
Length_of_snake += 1
clock.tick(snake_speed)
# 退出 Pygame 库
pygame.quit()
# 退出 Python
quit()
# 运行主函数
gameLoop()
```
希望这个代码能对你有所帮助!