贪吃蛇python代码有界面
时间: 2023-10-02 21:08:51 浏览: 34
以下是一个简单的贪吃蛇python代码,有界面。
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 设置游戏界面大小
screen = pygame.display.set_mode((600, 600))
# 设置游戏标题
pygame.display.set_caption("贪吃蛇")
# 定义蛇的初始位置
snake_head = [250, 250]
snake_body = [[250, 250], [240, 250], [230, 250]]
# 定义食物的初始位置
food_pos = [random.randint(0, 590), random.randint(0, 590)]
food_spawned = True
# 定义蛇的移动方向
direction = "RIGHT"
change_to = direction
# 定义游戏结束状态
game_over = False
# 定义计分板
score = 0
font_style = pygame.font.SysFont(None, 50)
# 定义游戏循环
clock = pygame.time.Clock()
# 定义绘制蛇的函数
def draw_snake(snake_body):
for pos in snake_body:
pygame.draw.rect(screen, (0, 255, 0), pygame.Rect(pos[0], pos[1], 10, 10))
# 定义绘制食物的函数
def draw_food(food_pos):
pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(food_pos[0], food_pos[1], 10, 10))
# 定义游戏结束的函数
def game_over_message():
message = font_style.render("游戏结束!", True, (255, 255, 255))
screen.blit(message, [150, 250])
# 游戏主循环
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:
change_to = "LEFT"
elif event.key == pygame.K_RIGHT:
change_to = "RIGHT"
elif event.key == pygame.K_UP:
change_to = "UP"
elif event.key == pygame.K_DOWN:
change_to = "DOWN"
# 更新移动方向
if change_to == "LEFT" and direction != "RIGHT":
direction = "LEFT"
elif change_to == "RIGHT" and direction != "LEFT":
direction = "RIGHT"
elif change_to == "UP" and direction != "DOWN":
direction = "UP"
elif change_to == "DOWN" and direction != "UP":
direction = "DOWN"
# 移动蛇的头部
if direction == "LEFT":
snake_head[0] -= 10
elif direction == "RIGHT":
snake_head[0] += 10
elif direction == "UP":
snake_head[1] -= 10
elif direction == "DOWN":
snake_head[1] += 10
# 判断是否吃到食物
if snake_head == food_pos:
food_spawned = False
score += 10
snake_body.append([0, 0])
# 生成新的食物
if not food_spawned:
food_pos = [random.randint(0, 590), random.randint(0, 590)]
food_spawned = True
# 移动蛇的身体
for i in range(len(snake_body) - 1, 0, -1):
snake_body[i] = snake_body[i - 1]
snake_body[0] = list(snake_head)
# 判断蛇是否撞墙或撞到自己的身体
if snake_head[0] < 0 or snake_head[0] > 590 or snake_head[1] < 0 or snake_head[1] > 590:
game_over = True
for block in snake_body[1:]:
if snake_head == block:
game_over = True
# 绘制游戏界面
screen.fill((0, 0, 0))
draw_snake(snake_body)
draw_food(food_pos)
# 绘制计分板
score_font = font_style.render("得分:" + str(score), True, (255, 255, 255))
screen.blit(score_font, [10, 10])
# 更新游戏显示
pygame.display.update()
# 设置游戏帧率
clock.tick(15)
# 显示游戏结束信息
game_over_message()
# 关闭pygame
pygame.quit()
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)