共计 2801 个字符,预计需要花费 8 分钟才能阅读完成。
def start_game():
# 播放音乐
pygame.mixer.music.play(-1)
# 定义存分数的全局变量
global score
score = 0
# 定义寄存玩家键盘输入静止方向的变量,初始为向右
run_direction = "right"
# 定义贪吃蛇静止方向的变量,初始为玩家键入方向
run = run_direction
# 实例化贪吃蛇和食物对象
head = Snake(randint(0, 30) * 20, randint(0, 20) * 20)
# 实例化蛇身长度为 2 个单位
snake_body = [Snake(head.x, head.y + 20), Snake(head.x, head.y + 40)]
# 实例化食物列表,列表随着其中食物被吃掉应该一直缩短
food_list = [Food(randint(0, 45) * 20, randint(0, 28) * 20, (randint(10, 255), randint(10, 255), randint(10, 255)))]
for i in range(1,24):
food_list.append(Food(randint(0, 45) * 20, randint(0, 28) * 20, (randint(10, 255), randint(10, 255), randint(10, 255))))
# 实例化单个食物,不便循环内生成单个新食物
food = Food(randint(0, 45) * 20, randint(0, 28) * 20, (randint(10, 255), randint(10, 255), randint(10, 255)))
while True:
window.blit(background, (0,0))
# 监听玩家键盘输入的静止方向值,[能源期货](https://www.gendan5.com/cf/ef.html)并依据输出转为 up、down、right 或 left,不便程序中调用
# pygame.event.get()返回一个列表,寄存本次 game 执行中程序遇到的一连串事件(按工夫程序顺次寄存)for event in pygame.event.get():
# pygame.QUIT 事件是指用户点击窗口右上角的 "×"
if event.type == pygame.QUIT:
# 显示后果界面
show_end()
# 若事件类型是按下键盘,分↑ ↓ ← →四种状况探讨
elif event.type == pygame.KEYDOWN:
# 若事件类型是按下键盘↑
# key 是键值,示意按上来的键值是什么
if event.key == ord('w'):
run_direction = "up"
# 若事件类型是按下键盘↓
if event.key == ord('s'):
run_direction = "down"
# 若事件类型是按下键盘←
if event.key == ord('a'):
run_direction = "left"
# 若事件类型是按下键盘→
if event.key == ord('d'):
run_direction = "right"
# 绘制初始化的 25 个食物图像(24+1=25)
# 随着该列表中的食物被吃掉,列表应该一直 pop 以革除曾经被吃的事物
for item in food_list:
draw_food(item)
# 绘制被贪吃蛇吃掉后新增的食物图像
draw_food(food)
# 绘制蛇头图像
draw_snake(black, head)
# 绘制蛇身图像
for item in snake_body:
draw_snake(blue, item)
# 判断贪吃蛇原静止方向 (run) 与玩家键盘输入的静止方向 (run_direction) 是否违反失常静止状况
if run == "up" and not run_direction == "down":
run = run_direction
elif run == "down" and not run_direction == "up":
run = run_direction
elif run == "left" and not run_direction == "right":
run = run_direction
elif run == "right" and not run_direction == "left":
run = run_direction
# 插入蛇头地位到蛇身列表中
snake_body.insert(0, Snake(head.x, head.y))
# 依据玩家键入方向进行蛇头 xy 的更新
if run == "up":
head.y -= 20
elif run == "down":
head.y += 20
elif run == "left":
head.x -= 20
elif run == "right":
head.x += 20
# 判断是否死亡
die_flag = False
# 遍历寄存贪吃蛇位姿的列表,从第 1 个开始,(第 0 个位蛇头)
for body in snake_body[1:]:
# 如果蛇头的 xy 和蛇身 xy 相等,则断定相撞,设置 flag 为 ture
if head.x == body.x and head.y == body.y:
die_flag = True
# 若蛇头的 xy 在显示窗体外,或 flag 为 true,则显示完结界面,并退出游戏
if die_flag == True or head.x < 0 or head.x > 960 or head.y < 0 or head.y > 600:
# 进行播放音乐
pygame.mixer.music.stop()
show_end()
# die_snake(head, snake_body)
# 判断蛇头和食物坐标,若相等,则加分,并生成新的食物
# 定义标记,表明是否找到和蛇头相等的事物
global flag
flag = 0
# 如果蛇头和食物重合
for item in food_list:
if head.x == item.x and head.y == item.y or head.x == food.x and head.y == food.y:
flag = 1
score += 1
# 弹出被吃掉的这个食物
food_list.pop(food_list.index(item))
# 再产生一个食物
food = new_food(head)
# 把新食物插入 food_list,下一次循环中会更新绘制食物整体
food_list.append(food)
break
if flag == 0:
snake_body.pop()
font = pygame.font.SysFont("simHei", 25)
mode_title = font.render('失常模式', False, grey)
socre_title = font.render('得分: %s' % score, False, grey)
window.blit(mode_title, (50, 30))
window.blit(socre_title, (50, 65))
# 更新蛇头蛇身和食物的数据
pygame.display.update()
# 通过帧率设置贪吃蛇速度
clock.tick(8)
正文完