python贪吃蛇编程代码
时间: 2023-05-13 20:01:16 浏览: 66
Python贪吃蛇编程代码可以用Pygame库来实现。Pygame是一个跨平台的Python模块,它提供了高效的游戏开发工具。以下是一个简单的贪吃蛇游戏的代码:
```python
import pygame
import time
import random
pygame.init()
# 设置窗口大小
screen_width = 600
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置标题
pygame.display.set_caption("贪吃蛇游戏")
# 设置颜色
red = (255, 0, 0)
green = (0, 255, 0)
black = (0, 0, 0)
white = (255, 255, 255)
# 设置字体
font_style = pygame.font.SysFont(None, 50)
# 定义函数,绘制蛇身
def snake(snake_block, snake_List):
for x in snake_List:
pygame.draw.rect(screen, green, [x[0], x[1], snake_block, snake_block])
# 定义函数,显示分数
def Your_score(score):
value = font_style.render("Your Score: " + str(score), True, white)
screen.blit(value, [0, 0])
# 定义函数,显示消息
def message(msg, color):
mesg = font_style.render(msg, True, color)
screen.blit(mesg, [screen_width / 6, screen_height / 3])
# 设置蛇和食物的初始位置
snake_block = 10
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) / 10.0) * 10.0
foody = round(random.randrange(0, screen_height - snake_block) / 10.0) * 10.0
# 设置游戏结束的标志
game_over = False
# 设置游戏循环
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:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0
# 判断是否撞到边界
if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_over = True
# 移动蛇
x1 += x1_change
y1 += y1_change
screen.fill(black)
# 绘制食物
pygame.draw.rect(screen, red, [foodx, foody, snake_block, snake_block])
# 更新蛇的长度
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_over = True
snake(snake_block, snake_List)
Your_score(Length_of_snake - 1)
# 判断是否产生新的食物
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, screen_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, screen_height - snake_block) / 10.0) * 10.0
Length_of_snake += 1
pygame.display.update()
# 设置帧率
clock = pygame.time.Clock()
clock.tick(20)
# 显示游戏结束的消息
message("You Lost! Press Q-Quit or C-Play Again", red)
pygame.display.update()
# 等待用户重新操作
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
if event.key == pygame.K_c:
game_over = 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) / 10.0) * 10.0
foody = round(random.randrange(0, screen_height - snake_block) / 10.0) * 10.0
if game_over:
break
# 关闭游戏
pygame.quit()
quit()
```
以上的代码实现了一个简单的贪吃蛇游戏。在游戏中,玩家需要通过控制蛇,不断地吃食物来增加长度,同时避免撞到墙壁或者自己的身体,直到游戏结束。