请帮我完成一个贪吃蛇的小程序游戏代码
时间: 2023-08-20 10:09:56 浏览: 366
好的,以下是一个简单的贪吃蛇小游戏的代码,使用Python语言实现:
```python
import pygame
import random
# 初始化
pygame.init()
# 窗口大小
window_width = 800
window_height = 600
# 创建窗口
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("贪吃蛇小游戏")
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
# 定义字体
font = pygame.font.SysFont(None, 25)
# 定义游戏速度
clock = pygame.time.Clock()
fps = 10
# 定义蛇的初始位置
block_size = 10
snake_list = []
snake_length = 1
# 定义食物的位置
food_x = round(random.randrange(0, window_width - block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, window_height - block_size) / 10.0) * 10.0
# 定义蛇的方向
direction = "right"
# 游戏结束函数
def game_over():
message("游戏结束,按Q退出或按C重新开始", red)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
quit()
if event.key == pygame.K_c:
game_loop()
# 定义显示文字的函数
def message(msg, color):
screen_text = font.render(msg, True, color)
window.blit(screen_text, [window_width / 6, window_height / 3])
# 游戏主循环
def game_loop():
global direction
game_exit = False
game_over_flag = False
# 蛇的初始位置
lead_x = window_width / 2
lead_y = window_height / 2
lead_x_change = 10
lead_y_change = 0
while not game_exit:
while game_over_flag == True:
game_over()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and direction != "right":
direction = "left"
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT and direction != "left":
direction = "right"
lead_x_change = block_size
lead_y_change = 0
elif event.key == pygame.K_UP and direction != "down":
direction = "up"
lead_y_change = -block_size
lead_x_change = 0
elif event.key == pygame.K_DOWN and direction != "up":
direction = "down"
lead_y_change = block_size
lead_x_change = 0
# 判断蛇是否撞墙
if lead_x >= window_width or lead_x < 0 or lead_y >= window_height or lead_y < 0:
game_over_flag = True
# 更新蛇的位置
lead_x += lead_x_change
lead_y += lead_y_change
# 填充窗口背景色
window.fill(white)
# 绘制食物
pygame.draw.rect(window, green, [food_x, food_y, block_size, block_size])
# 绘制蛇的身体
snake_head = []
snake_head.append(lead_x)
snake_head.append(lead_y)
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
for segment in snake_list[:-1]:
if segment == snake_head:
game_over_flag = True
for segment in snake_list:
pygame.draw.rect(window, black, [segment[0], segment[1], block_size, block_size])
# 判断蛇是否吃到食物
if lead_x == food_x and lead_y == food_y:
food_x = round(random.randrange(0, window_width - block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, window_height - block_size) / 10.0) * 10.0
snake_length += 1
# 更新窗口
pygame.display.update()
# 控制游戏速度
clock.tick(fps)
# 退出游戏
pygame.quit()
quit()
# 运行游戏
game_loop()
```
这段代码实现了一个基本的贪吃蛇小游戏,具体的玩法可以参考游戏内的提示。
阅读全文