你能帮我写一个贪吃蛇小游戏吗
时间: 2023-08-28 13:11:21 浏览: 33
当然可以!下面是一个简单的贪吃蛇小游戏代码,你可以在 Python 中运行它:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 设置游戏窗口大小
window_width = 600
window_height = 400
# 设置游戏窗口
game_window = pygame.display.set_mode((window_width, window_height))
# 设置游戏标题
pygame.display.set_caption('贪吃蛇小游戏')
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
green = (0, 255, 0)
red = (255, 0, 0)
# 定义字体
font = pygame.font.SysFont(None, 25)
# 定义函数:绘制文字
def draw_text(text, color, x, y):
textobj = font.render(text, 1, color)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
game_window.blit(textobj, textrect)
# 定义函数:绘制背景
def draw_background():
game_window.fill(black)
pygame.draw.rect(game_window, green, (0, 0, window_width, window_height), 10)
# 定义函数:绘制贪吃蛇
def draw_snake(snake_block_size, snake_list):
for x in snake_list:
pygame.draw.rect(game_window, white, [x[0], x[1], snake_block_size, snake_block_size])
# 定义主函数
def main():
# 设置初始变量
game_over = False
game_close = False
snake_block_size = 10
snake_speed = 15
# 设置初始位置
x1 = window_width / 2
y1 = window_height / 2
x1_change = 0
y1_change = 0
# 初始化贪吃蛇列表
snake_List = []
Length_of_snake = 1
# 设置食物位置
foodx = round(random.randrange(0, window_width - snake_block_size) / 10.0) * 10.0
foody = round(random.randrange(0, window_height - snake_block_size) / 10.0) * 10.0
# 游戏循环
while not game_over:
# 游戏关闭时的事件循环
while game_close == True:
draw_background()
draw_text("You Lost! Press Q-Quit or C-Play Again", red, window_width / 6, window_height / 3)
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:
main()
# 事件循环
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 >= window_width or x1 < 0 or y1 >= window_height or y1 < 0:
game_close = True
# 更新位置
x1 += x1_change
y1 += y1_change
# 绘制背景
draw_background()
# 绘制食物
pygame.draw.rect(game_window, 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_close = True
# 绘制贪吃蛇
draw_snake(snake_block_size, snake_List)
# 更新屏幕
pygame.display.update()
# 判断贪吃蛇是否吃到了食物
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, window_width - snake_block_size) / 10.0) * 10.0
foody = round(random.randrange(0, window_height - snake_block_size) / 10.0) * 10.0
Length_of_snake += 1
# 设置游戏帧率
clock = pygame.time.Clock()
clock.tick(snake_speed)
# 退出 Pygame
pygame.quit()
# 运行游戏
main()
```
这个小游戏比较简单,由于时间和篇幅的限制,只能提供这么多了,如果你有什么问题,欢迎随时问我。