import pygame
import random
import copy
# 蛇
snake_list = [[10, 10]]
# 移动 一开始向下移动 定义移动方向
move_up = False
move_down = False
move_left = False
move_right = False
# 食物
food_point = [10 * random.randint(1, 49), 10 * random.randint(1, 49)]
# 障碍物
obstacle1_point = [10 * random.randint(4, 8) + food_point[0], 10 * random.randint(4, 8) + food_point[1]]
obstacle2_point = [-10 * random.randint(4, 8) + food_point[0], -10 * random.randint(4, 8) + food_point[1]]
"""初始化游戏"""
pygame.init()
# fps 刷新帧率
clock = pygame.time.Clock()
# 屏幕大小
screen = pygame.display.set_mode((500, 500))
# 绘制标题
pygame.display.set_caption("贪吃蛇小游戏")
"""进入游戏,玩游戏"""
# 设置游戏开关
running = True
while running:
# 设置 fps 为20帧
clock.tick(20)
# 绘制屏幕为白色
screen.fill([255, 255, 255])
# 用键盘控制移动方向
for event in pygame.event.get():
# 先判断按键是否按下
if event.type == pygame.KEYDOWN:
# 在判断按键是什么类型
if event.key == pygame.K_UP:
move_up = True
move_down = False
move_left = False
move_right = False
if event.key == pygame.K_DOWN:
move_up = False
move_down = True
move_left = False
move_right = False
if event.key == pygame.K_LEFT:
move_up = False
move_down = False
move_left = True
move_right = False
if event.key == pygame.K_RIGHT:
move_up = False
move_down = False
move_left = False
move_right = True
# 绘制食物
food_rect = pygame.draw.circle(surface=screen, color=[255, 0, 0], center=food_point, radius=10)
# 绘制障碍
obstacle1_rect = pygame.draw.rect(surface=screen, color=[255, 255, 0],
rect=(obstacle1_point[0], obstacle1_point[1], 20, 100), width=0)
obstacle2_rect = pygame.draw.rect(surface=screen, color=[255, 255, 0],
rect=(obstacle2_point[0], obstacle2_point[1], 100, 20), width=0)
# 绘制蛇
snake_rect = []
for snake_point in snake_list:
snake_rect.append(pygame.draw.circle(surface=screen, color=[0, 0, 255], center=snake_point, radius=5))
# 检测碰撞
if food_rect.collidepoint(snake_point):
# 吃掉食物增加一个点
snake_list.append(food_point)
# 重新绘制食物
food_point = [10 * random.randint(1, 49), 10 * random.randint(1, 49)]
food_rect = pygame.draw.circle(surface=screen, color=[255, 0, 0], center=food_point, radius=10)
# 重新绘制障碍
obstacle1_point = [10 * random.randint(4, 8) + food_point[0], 10 * random.randint(4, 8) + food_point[1]]
obstacle2_point = [-10 * random.randint(4, 8) + food_point[0], -10 * random.randint(4, 8) + food_point[1]]
obstacle1_rect = pygame.draw.rect(surface=screen, color=[255, 255, 0],
rect=(obstacle1_point[0], obstacle1_point[1], 20, 100), width=0)
obstacle2_rect = pygame.draw.rect(surface=screen, color=[255, 255, 0],
rect=(obstacle2_point[0], obstacle2_point[1], 100, 20), width=0)
break
"""让蛇动起来"""
pos = len(snake_list) - 1
# 移动身子
while pos > 0:
# 把后面的点往前移动
snake_list[pos] = copy.deepcopy(snake_list[pos-1])
pos -= 1
# 蛇头单独处理
if move_up:
snake_list[0][1] -= 10
if snake_list[0][1] < 0:
snake_list[0][1] = 500
if move_down:
snake_list[0][1] += 10
if snake_list[0][1] > 500:
snake_list[0][1] = 0
if move_left:
snake_list[0][0] -= 10
if snake_list[0][0] < 0:
snake_list[0][0] = 500
if move_right:
snake_list[0][0] += 10
if snake_list[0][0] > 500:
snake_list[0][0] = 0
# 如果蛇撞上了自己 结束游戏
head_rect = snake_rect[0]
count = len(snake_rect)
while count > 1:
# colliderect 矩形碰撞
if head_rect.colliderect(snake_rect[count-1]) or head_rect.colliderect(obstacle1_rect)\
or head_rect.colliderect(obstacle2_rect):
# 如果蛇撞到了自己/障碍物 游戏结束
running = False
print("你输了")
count -= 1
if len(snake_list) >= 20:
print("你赢了")
running = False
# 移动 修改蛇的点的位置
# 显示绘制内容
pygame.display.update()
print("游戏结束")
pygame.quit()