写一个贪吃蛇程序
时间: 2023-09-05 13:15:07 浏览: 54
以下是一个简单的贪吃蛇程序,使用 Python 语言编写:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置游戏窗口大小和标题
screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Snake Game")
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
# 设置字体
font = pygame.font.SysFont(None, 25)
# 定义蛇的初始位置和大小
snake_block_size = 10
snake_speed = 20
x1 = screen_width / 2
y1 = screen_height / 2
# 定义蛇的移动方向
x1_change = 0
y1_change = 0
# 定义食物的初始位置
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
# 定义蛇的长度
snake_list = []
length_of_snake = 1
# 定义游戏结束的函数
def message(msg, color):
text = font.render(msg, True, color)
screen.blit(text, [screen_width / 6, screen_height / 3])
# 游戏循环
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_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_over = True
# 更新蛇的位置
x1 += x1_change
y1 += y1_change
# 绘制背景和食物
screen.fill(black)
pygame.draw.rect(screen, red, [foodx, foody, snake_block_size, snake_block_size])
# 绘制蛇
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
for x in snake_list:
pygame.draw.rect(screen, white, [x[0], x[1], snake_block_size, snake_block_size])
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
# 显示分数
score = length_of_snake - 1
score_text = font.render("Score: " + str(score), True, white)
screen.blit(score_text, [0, 0])
# 更新屏幕
pygame.display.update()
# 控制游戏速度
clock = pygame.time.Clock()
clock.tick(snake_speed)
# 显示游戏结束信息
message("Game Over", red)
pygame.display.update()
# 等待一段时间后退出游戏
pygame.time.wait(2000)
# 关闭 Pygame
pygame.quit()
quit()
```
这是一个比较基础的贪吃蛇程序,如果想要增加更多功能,可以在此基础上进行扩展。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)