Python 贪吃蛇游戏:简单易懂的代码实现
以下是一个简单的 Python 贪吃蛇游戏的代码:
import pygame
import random
# 初始化
pygame.init()
# 屏幕大小
screen_width = 480
screen_height = 480
# 颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
# 创建屏幕
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('贪吃蛇')
# 游戏时钟
clock = pygame.time.Clock()
# 蛇的方块大小
block_size = 20
# 字体
font = pygame.font.SysFont(None, 25)
# 绘制文字
def draw_text(text, color, x, y):
text_surface = font.render(text, True, color)
screen.blit(text_surface, (x, y))
# 绘制蛇
def draw_snake(snake_list):
for block in snake_list:
pygame.draw.rect(screen, black, [block[0], block[1], block_size, block_size])
# 游戏循环
def game_loop():
# 蛇的位置和长度
snake_x = screen_width / 2
snake_y = screen_height / 2
snake_length = 1
snake_list = [[snake_x, snake_y]]
# 食物的位置
food_x = round(random.randrange(0, screen_width - block_size) / block_size) * block_size
food_y = round(random.randrange(0, screen_height - block_size) / block_size) * block_size
# 蛇的移动方向
direction = 'right'
change_direction = direction
# 游戏结束标志
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:
change_direction = 'left'
elif event.key == pygame.K_RIGHT:
change_direction = 'right'
elif event.key == pygame.K_UP:
change_direction = 'up'
elif event.key == pygame.K_DOWN:
change_direction = 'down'
# 判断蛇的移动方向
if direction == 'right' and change_direction != 'left':
direction = change_direction
elif direction == 'left' and change_direction != 'right':
direction = change_direction
elif direction == 'up' and change_direction != 'down':
direction = change_direction
elif direction == 'down' and change_direction != 'up':
direction = change_direction
# 移动蛇
if direction == 'right':
snake_x += block_size
elif direction == 'left':
snake_x -= block_size
elif direction == 'up':
snake_y -= block_size
elif direction == 'down':
snake_y += block_size
# 判断是否吃到食物
if snake_x == food_x and snake_y == food_y:
food_x = round(random.randrange(0, screen_width - block_size) / block_size) * block_size
food_y = round(random.randrange(0, screen_height - block_size) / block_size) * block_size
snake_length += 1
# 更新蛇的位置
snake_head = [snake_x, snake_y]
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
# 判断游戏是否结束
if snake_x < 0 or snake_x >= screen_width or snake_y < 0 or snake_y >= screen_height:
game_over = True
for block in snake_list[:-1]:
if block == snake_head:
game_over = True
# 绘制屏幕
screen.fill(white)
pygame.draw.rect(screen, red, [food_x, food_y, block_size, block_size])
draw_snake(snake_list)
draw_text('长度:' + str(snake_length), black, 10, 10)
pygame.display.update()
# 控制游戏速度
clock.tick(10)
# 退出游戏
pygame.quit()
quit()
# 开始游戏
game_loop()
该游戏使用 Pygame 库实现,包含以下功能:
- 控制贪吃蛇的移动方向。
- 判断是否吃到食物,吃到食物则蛇的长度加一。
- 判断游戏是否结束,当蛇碰到边界或者碰到自己时游戏结束。
- 绘制屏幕,并在屏幕上显示贪吃蛇的长度。
- 控制游戏速度。
运行该代码,可以看到一个简单的贪吃蛇游戏。
原文地址: https://www.cveoy.top/t/topic/nBi3 著作权归作者所有。请勿转载和采集!