Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3657576
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2021-08-20 17:19:13

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))

        # 监听玩家键盘输入的运动方向值,并根据输入转为updownrightleft,方便程序中调用

        # 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相等,则判定相撞,设置flagture

            if head.x == body.x and head.y == body.y:

                die_flag = True

        # 若蛇头的xy在显示窗体外,或flagtrue,则显示结束界面,并退出游戏

        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)

阅读(1213) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~