用Python编写简单的贪吃蛇小游戏
时间: 2023-12-07 16:05:00 浏览: 208
好的,以下是一个简单的贪吃蛇小游戏的代码,你可以在Python环境下运行它:
```
import pygame
import random
# 初始化Pygame
pygame.init()
# 定义游戏窗口的大小
window_width = 500
window_height = 500
# 定义游戏窗口
game_display = pygame.display.set_mode((window_width, window_height))
# 定义游戏标题
pygame.display.set_caption('贪吃蛇小游戏')
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
# 定义贪吃蛇的大小
block_size = 10
# 定义时钟
clock = pygame.time.Clock()
# 定义字体
font = pygame.font.SysFont(None, 25)
# 定义函数,用于显示消息
def message_to_screen(msg, color):
screen_text = font.render(msg, True, color)
game_display.blit(screen_text, [window_width/6, window_height/3])
# 定义函数,用于显示贪吃蛇
def snake(block_size, snake_list):
for block in snake_list:
pygame.draw.rect(game_display, black, [block[0], block[1], block_size, block_size])
# 定义函数,用于显示得分
def score(score):
text = font.render('得分:' + str(score), True, black)
game_display.blit(text, [0, 0])
# 定义游戏循环
def game_loop():
game_exit = False
game_over = False
# 定义贪吃蛇的初始位置和长度
lead_x = window_width / 2
lead_y = window_height / 2
lead_x_change = 0
lead_y_change = 0
snake_list = []
snake_length = 1
# 定义食物的初始位置
rand_food_x = round(random.randrange(0, window_width - block_size) / 10.0) * 10.0
rand_food_y = round(random.randrange(0, window_height - block_size) / 10.0) * 10.0
while not game_exit:
while game_over:
game_display.fill(white)
message_to_screen('游戏结束,按 Q 退出,按 C 重新开始', red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_exit = True
game_over = False
elif event.key == pygame.K_c:
game_loop()
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:
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
lead_x_change = block_size
lead_y_change = 0
elif event.key == pygame.K_UP:
lead_y_change = -block_size
lead_x_change = 0
elif event.key == pygame.K_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 = True
# 移动贪吃蛇
lead_x += lead_x_change
lead_y += lead_y_change
# 判断贪吃蛇是否吃到食物
if lead_x == rand_food_x and lead_y == rand_food_y:
rand_food_x = round(random.randrange(0, window_width - block_size) / 10.0) * 10.0
rand_food_y = round(random.randrange(0, window_height - block_size) / 10.0) * 10.0
snake_length += 1
game_display.fill(white)
pygame.draw.rect(game_display, red, [rand_food_x, rand_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 block in snake_list[:-1]:
if block == snake_head:
game_over = True
# 显示贪吃蛇和得分
snake(block_size, snake_list)
score(snake_length - 1)
pygame.display.update()
# 设置游戏每秒的帧数
clock.tick(10)
pygame.quit()
quit()
game_loop()
```
提示:在运行代码前,需要确保已经安装了 Pygame 模块,可以使用 `pip install pygame` 命令进行安装。
阅读全文