示例#1
0
    def __init__(self, screen, screen_light, alpha, sound_path, sound_voice, sound_pos):
        self.flag = True
        self.is_y = False
        self.screen = screen
        self.screen_light = screen_light
        self.is_setting = False
        self.alpha = alpha
        self.render = RenderTmx(r'resource/map2.tmx')
        self.temp_surface = self.render.surface.copy()
        self.fade = FadeScene(self.render.surface)
        self.thorn_group = pygame.sprite.Group()
        self.steps_group = pygame.sprite.Group()
        self.gully_group = pygame.sprite.Group()
        self.can_walk_group = pygame.sprite.Group()
        self.scene_exit = False
        self.mask = False
        self.t = 10
        self.init_object_group()
        self.dialog = GodDialog(3)
        self.is_regame = False
        self.restart_dialog = pygame.image.load(r'resource/image/regame.png')
        self.sound_path = sound_path
        self.sound_voice = sound_voice
        self.sound_pos = sound_pos
        self.res = [-1, -1, -1, -1, -1]

        # 详解:https://blog.csdn.net/qq_41556318/article/details/86305046
        pygame.mixer.music.set_pos(self.sound_pos)
        pygame.mixer.music.load(self.sound_path)
        pygame.mixer.music.set_volume(self.sound_voice)
        pygame.mixer.music.play(-1)  # -1即循环播放
    def __init__(self, screen, screen_light, alpha, sound_path, sound_voice,
                 sound_pos):
        pygame.init()
        pygame.mixer.init()

        self.screen = screen
        self.screen_light = screen_light
        self.render = RenderTmx(r'resource\begin.tmx')
        self.temp_surface = self.render.surface.copy()
        self.fade = FadeScene(self.render.surface)
        self.is_setting = False
        self.alpha = alpha
        self.sound_path = sound_path
        self.sound_voice = sound_voice
        self.sound_pos = sound_pos
        self.res = [-1, -1, -1, -1, -1]

        # 详解:https://blog.csdn.net/qq_41556318/article/details/86305046
        pygame.mixer.music.set_pos(self.sound_pos)
        pygame.mixer.music.load(self.sound_path)
        pygame.mixer.music.set_volume(self.sound_voice)
        pygame.mixer.music.play(-1)  # -1即循环播放

        surface = pygame.surface.Surface((800, 600))
        self.screen.blit(surface, (0, 0))
示例#3
0
    def __init__(self, screen, screen_light, alpha, sound_path, sound_voice,
                 sound_pos, *objects):
        pygame.init()
        pygame.mixer.init()

        # print(alpha)
        self.screen = screen
        self.render = RenderTmx(r'resource/map1.tmx')
        self.temp_surface = self.render.surface.copy()  # 临时surface用于人物自主移动
        self.objects = objects  # 后面要用就要用私有变量,不然后面的类方法将用不了该变量
        # 对象层
        self.guard_group = pygame.sprite.Group()
        self.monster_group = pygame.sprite.Group()
        self.box_group = pygame.sprite.Group()
        self.obstacle_group = pygame.sprite.Group()
        self.door_group = pygame.sprite.Group()
        self.invisible_group = pygame.sprite.Group()
        self.restart_dialog = pygame.image.load(r'resource/image/regame.png')
        self.is_regame = False
        self.init_object_group()
        self.fade = FadeScene(self.render.surface)
        self.dialog = GodDialog(1)
        self.door = Door(0, 0, False)
        self.door_dialog1 = DoorDialog(1)
        self.door_dialog2 = DoorDialog(2)
        self.door_dialog3 = DoorDialog(3)
        self.door_dialog4 = DoorDialog(4)
        self.screen_light = screen_light
        self.flag = True
        self.scene_exit = False
        self.is_setting = False
        self.alpha = alpha
        self.invisible = InvisibleObstacle(10, 10)
        self.sound_path = sound_path
        self.sound_voice = sound_voice
        self.sound_pos = sound_pos
        self.res = [-1, -1, -1, -1, -1]

        # 详解:https://blog.csdn.net/qq_41556318/article/details/86305046
        pygame.mixer.music.set_pos(self.sound_pos / 1000)
        pygame.mixer.music.load(self.sound_path)
        pygame.mixer.music.set_volume(self.sound_voice)
        pygame.mixer.music.play(-1)  # -1即循环播放
示例#4
0
class Scene1:
    def __init__(self, screen, screen_light, alpha, sound_path, sound_voice,
                 sound_pos, *objects):
        pygame.init()
        pygame.mixer.init()

        # print(alpha)
        self.screen = screen
        self.render = RenderTmx(r'resource/map1.tmx')
        self.temp_surface = self.render.surface.copy()  # 临时surface用于人物自主移动
        self.objects = objects  # 后面要用就要用私有变量,不然后面的类方法将用不了该变量
        # 对象层
        self.guard_group = pygame.sprite.Group()
        self.monster_group = pygame.sprite.Group()
        self.box_group = pygame.sprite.Group()
        self.obstacle_group = pygame.sprite.Group()
        self.door_group = pygame.sprite.Group()
        self.invisible_group = pygame.sprite.Group()
        self.restart_dialog = pygame.image.load(r'resource/image/regame.png')
        self.is_regame = False
        self.init_object_group()
        self.fade = FadeScene(self.render.surface)
        self.dialog = GodDialog(1)
        self.door = Door(0, 0, False)
        self.door_dialog1 = DoorDialog(1)
        self.door_dialog2 = DoorDialog(2)
        self.door_dialog3 = DoorDialog(3)
        self.door_dialog4 = DoorDialog(4)
        self.screen_light = screen_light
        self.flag = True
        self.scene_exit = False
        self.is_setting = False
        self.alpha = alpha
        self.invisible = InvisibleObstacle(10, 10)
        self.sound_path = sound_path
        self.sound_voice = sound_voice
        self.sound_pos = sound_pos
        self.res = [-1, -1, -1, -1, -1]

        # 详解:https://blog.csdn.net/qq_41556318/article/details/86305046
        pygame.mixer.music.set_pos(self.sound_pos / 1000)
        pygame.mixer.music.load(self.sound_path)
        pygame.mixer.music.set_volume(self.sound_voice)
        pygame.mixer.music.play(-1)  # -1即循环播放

    def init_object_group(self):
        for group in self.render.tiled.tmx_data.objectgroups:  # 得到图片信息
            if isinstance(
                    group, pytmx.TiledObjectGroup
            ):  # isinstance函数: runoob.com/python/python-func-isinstance.html
                if group.name == '守卫':
                    for obj in group:
                        guard = NPC(obj.x, obj.y, obj.width, obj.height, 2,
                                    'guard', 'guard')
                        self.guard_group.add(guard)

                if group.name == 'gamer':
                    for obj in group:
                        self.gamer = Gamer(obj.x, obj.y, 10, 10, True, 4, 2)

                if group.name == '怪兽':
                    for obj in group:
                        if int(obj.name) % 2:
                            monster = InteractiveMoveNPC(
                                obj.x, obj.y, 'monsters', '红野人', False, 50, 2)
                            self.monster_group.add(monster)
                        else:
                            monster = InteractiveMoveNPC(
                                obj.x, obj.y, 'monsters', '蓝野人', False, 40, 1)
                            self.monster_group.add(monster)

                if group.name == '宝箱':
                    cnt = 0
                    mask = random.randint(1, 5)
                    for obj in group:
                        cnt += 1
                        if cnt == mask:
                            box = Treasure(obj.x, obj.y, False)
                        else:
                            box = Treasure(obj.x, obj.y, True)
                        self.box_group.add(box)

                if group.name == '门':
                    cnt = 0
                    mask = random.randint(1, 4)
                    for obj in group:
                        cnt += 1
                        if cnt == mask:
                            door = Door(obj.x, obj.y, True)
                        else:
                            door = Door(obj.x, obj.y, False)
                        self.door_group.add(door)

                if group.name == '神秘人':
                    for obj in group:
                        self.god = InteractiveMoveNPC(obj.x, obj.y, 'magicman',
                                                      'god', True, 45, 1)

                if group.name == '障碍':
                    for obj in group:
                        if obj.name == 'invisible':
                            invisible = InvisibleObstacle(obj.x, obj.y)
                            self.invisible_group.add(invisible)
                        else:
                            obstacle = pygame.sprite.Sprite()
                            obstacle.rect = pygame.Rect(
                                obj.x, obj.y, obj.width, obj.height)
                            self.obstacle_group.add(obstacle)

    def get_current_surface(self):
        surface = self.fade.get_back_image()
        self.temp_surface.blit(surface, (0, 0))
        # 覆盖的思想
        self.temp_surface.blit(self.render.surface, (0, 0))
        for it in self.objects:
            it.draw(self.temp_surface)
        for guard in self.guard_group:  # 从tmx的定点为左上角开始绘制所以位置会有少许偏差
            guard.draw(self.temp_surface)
        for box in self.box_group:
            box.draw(self.temp_surface)
        for monster in self.monster_group:
            monster.draw(self.temp_surface)

        # 开宝箱
        collide_box = pygame.sprite.spritecollide(self.gamer, self.box_group,
                                                  False)
        if len(collide_box) > 0:
            for box in collide_box:
                box.status = False
            if not self.gamer.key and box.key:
                box.draw_key(self.temp_surface)
                self.gamer.key = True

        self.gamer.draw(self.temp_surface)

        # 碰撞隐形障碍
        collide_invisible = pygame.sprite.spritecollide(
            self.gamer, self.invisible_group, False)
        if len(collide_invisible) > 0:
            for invisible in collide_invisible:
                if not invisible.flag:
                    invisible.draw(self.temp_surface)
                    self.invisible = invisible
                if not invisible.status:
                    self.gamer.hp -= 20
                    invisible.status = True
        else:
            self.invisible.flag = True

        # 进门
        collide_door = pygame.sprite.spritecollide(self.gamer, self.door_group,
                                                   False)
        if len(collide_door) > 0:
            for door in collide_door:
                if self.gamer.key and door.jump:
                    self.scene_exit = True
                elif self.gamer.key and not door.jump:
                    if door.status1:
                        self.temp_surface.blit(self.door_dialog4.surface,
                                               (0, 0))
                        if not door.flag1:
                            self.gamer.hp -= 20
                            door.flag1 = True
                    else:
                        self.temp_surface.blit(self.door_dialog2.surface,
                                               (0, 0))
                        if not door.flag3:
                            self.gamer.hp -= 20
                            door.flag3 = True
                        self.door = door
                elif not self.gamer.key:
                    if door.status2:
                        self.temp_surface.blit(self.door_dialog3.surface,
                                               (0, 0))
                        if not door.flag2:
                            self.gamer.hp -= 20
                            door.flag2 = True
                    else:
                        self.temp_surface.blit(self.door_dialog1.surface,
                                               (0, 0))
                        if not door.flag4:
                            self.gamer.hp -= 20
                            door.flag4 = True
                        self.door = door
        else:
            if self.door.pos_x != 0:
                if self.gamer.key:
                    self.door.status1 = True
                else:
                    self.door.status2 = True

            # print(self.door.status2, "+++++++")

        self.god.draw(self.temp_surface)

        pygame.draw.rect(
            self.temp_surface,
            pygame.Color(240, 65, 85),
            # pygame.draw模块详解:https://www.cnblogs.com/leonyoung/archive/2012/07/01/2572205.html
            pygame.Rect(self.gamer.pos_x - 10, self.gamer.pos_y - 20,
                        self.gamer.hp / 2, 5))

        if self.is_regame:
            self.temp_surface.blit(self.restart_dialog, (100, 30))

        # 开始提示
        if self.flag:
            self.temp_surface.blit(self.dialog.surface, (0, 0))

        return self.temp_surface

    def run(self):
        clock = pygame.time.Clock()  # 计时器
        while not self.scene_exit:
            # print(self.gamer.hp)
            mouse_down = False
            pressed_key = 0
            if self.gamer.hp <= 0:
                self.is_regame = True

            is_pressed = False
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                elif event.type == pygame.KEYDOWN:  # 按下键盘上的键这个事件
                    is_pressed = True
                    pressed_key = event.key
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    mouse_down = True

            if self.is_regame:
                if mouse_down:
                    x, y = pygame.mouse.get_pos()
                    if 240 <= x <= 570 and 170 <= y <= 215:
                        sys.exit()
                    elif 240 <= x <= 570 and 290 <= y <= 330:
                        self.res[0] = -2
                        return self.res

            # 碰撞怪兽
            collide_monster = pygame.sprite.spritecollide(
                self.gamer, self.monster_group, False)
            if len(collide_monster) > 0:
                self.is_regame = True

            if is_pressed:
                self.gamer.key_move(pressed_key, self.obstacle_group)
                if pressed_key == pygame.K_y:
                    self.flag = False
                elif pressed_key == pygame.K_ESCAPE:
                    self.is_setting = False
                    surface = pygame.surface.Surface((800, 600))
                    self.screen.blit(surface, (0, 0))
                elif pressed_key == pygame.K_x:
                    self.is_setting = True

            self.god.collide(self.gamer)

            if not self.is_setting:
                current_surface = self.get_current_surface()
                self.screen.blit(current_surface, (0, 0))
                self.screen.blit(self.screen_light, (0, 0))

            # print(mouse_down)
            # print(self.alpha)

            if self.is_setting:
                self.screen.blit(
                    pygame.image.load(r'resource/image/setting.png'), (0, 0))
                self.screen.blit(self.screen_light, (0, 0))
                if mouse_down:
                    # print("-----")
                    x, y = pygame.mouse.get_pos()
                    if 400 <= x <= 465 and 270 <= y <= 310:
                        if 0 < self.alpha <= 150:
                            self.alpha -= 10
                            self.screen_light.set_alpha(self.alpha)
                    elif 510 <= x <= 575 and 270 <= y <= 310:
                        # print("+++++")
                        if 0 <= self.alpha < 150:
                            self.alpha += 10
                            self.screen_light.set_alpha(self.alpha)
                    elif 400 <= x <= 465 and 150 <= y <= 180:
                        if self.sound_voice >= 0.9:
                            self.sound_voice += 0.1
                            pygame.mixer.music.set_volume(self.sound_voice)
                    elif 510 <= x <= 575 and 150 <= y <= 180:
                        if self.sound_voice >= 0.1:
                            self.sound_voice -= 0.1
                            pygame.mixer.music.set_volume(self.sound_voice)
                    elif 390 <= x <= 485 and 405 <= y <= 450:
                        pygame.mixer.music.stop()
                        self.sound_path = r'resource/music/张国荣 - 愿你决定.mp3'
                        pygame.mixer.music.load(self.sound_path)
                        pygame.mixer.music.set_volume(
                            pygame.mixer.music.get_volume())
                        pygame.mixer.music.play(-1)
                    elif 495 <= x <= 600 and 405 <= y <= 450:
                        pygame.mixer.music.stop()
                        self.sound_path = r'resource/music/123.mp3'
                        pygame.mixer.music.load(self.sound_path)
                        pygame.mixer.music.set_volume(
                            pygame.mixer.music.get_volume())
                        pygame.mixer.music.play(-1)

            pygame.display.update()
            self.res[1] = self.alpha
            self.res[2] = self.sound_path
            self.res[3] = pygame.mixer.music.get_volume()
            self.res[4] = pygame.mixer.music.get_pos()
            clock.tick(5)  # 1s循环40次,即刷新页面40次

        return self.res
示例#5
0
class Scene2:
    def __init__(self, screen, screen_light, alpha, sound_path, sound_voice, sound_pos):
        self.flag = True
        self.is_y = False
        self.screen = screen
        self.screen_light = screen_light
        self.is_setting = False
        self.alpha = alpha
        self.render = RenderTmx(r'resource/map2.tmx')
        self.temp_surface = self.render.surface.copy()
        self.fade = FadeScene(self.render.surface)
        self.thorn_group = pygame.sprite.Group()
        self.steps_group = pygame.sprite.Group()
        self.gully_group = pygame.sprite.Group()
        self.can_walk_group = pygame.sprite.Group()
        self.scene_exit = False
        self.mask = False
        self.t = 10
        self.init_object_group()
        self.dialog = GodDialog(3)
        self.is_regame = False
        self.restart_dialog = pygame.image.load(r'resource/image/regame.png')
        self.sound_path = sound_path
        self.sound_voice = sound_voice
        self.sound_pos = sound_pos
        self.res = [-1, -1, -1, -1, -1]

        # 详解:https://blog.csdn.net/qq_41556318/article/details/86305046
        pygame.mixer.music.set_pos(self.sound_pos)
        pygame.mixer.music.load(self.sound_path)
        pygame.mixer.music.set_volume(self.sound_voice)
        pygame.mixer.music.play(-1)  # -1即循环播放

    def init_object_group(self):
        for group in self.render.tiled.tmx_data.objectgroups:  # 得到图片信息
            if isinstance(group, pytmx.TiledObjectGroup):  # isinstance函数: runoob.com/python/python-func-isinstance.html
                if group.name == 'gamer':
                    for obj in group:
                        self.gamer = SceneGamer(obj.x, obj.y, 10, 10, True, 4, 2)

                if group.name == '地刺':
                    for obj in group:
                        thorn = NPC(obj.x, obj.y, obj.width, obj.height, 1, "", "berry")
                        self.thorn_group.add(thorn)

                if group.name == '传送门':
                    for obj in group:
                        self.door = Door(obj.x, obj.y, True)

                if group.name == '怪兽':
                    for obj in group:
                        if obj.name == '褐色':
                            self.monster1 = InteractiveMoveNPC(obj.x, obj.y, '2_monsters', 'player', False, 50, 0)
                        if obj.name == '绿色':
                            self.monster2 = InteractiveMoveNPC(obj.x, obj.y, '2_monsters', 'innman', False, 20, 1)

                if group.name == '台阶':
                    for obj in group:
                        step = pygame.sprite.Sprite()
                        step.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.steps_group.add(step)

                if group.name == '沟壑':
                    for obj in group:
                        # print(obj.x, obj.y)
                        gully = pygame.sprite.Sprite()
                        gully.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.gully_group.add(gully)

                if group.name == '可走层':
                    for obj in group:
                        can_walk = pygame.sprite.Sprite()
                        can_walk.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.can_walk_group.add(can_walk)


    def get_current_surface(self):
        # print(len(self.thorn_group))
        surface = self.fade.get_back_image()
        self.temp_surface.blit(surface, (0, 0))
        for thorn in self.thorn_group:
            thorn.draw(self.temp_surface)
        self.monster1.draw(self.temp_surface)
        self.monster2.draw(self.temp_surface)
        self.gamer.draw(self.temp_surface)
        # 开始提示
        if self.flag:
            self.temp_surface.blit(self.dialog.surface, (0, 0))
        if self.is_regame:
            self.temp_surface.blit(self.restart_dialog, (100, 30))
        return self.temp_surface

    def run(self):
        clock = pygame.time.Clock()  # 计时器
        while not self.scene_exit:
            mouse_down = False
            # print(self.gamer.pos_x, self.gamer.pos_y)
            if self.gamer.pos_y > 400:
                self.is_regame = True
            # print(self.gamer.pos_x)
            is_pressed = False
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                elif event.type == pygame.KEYDOWN:  # 按下键盘上的键这个事件
                    is_pressed = True
                    pressed_key = event.key
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    mouse_down = True

            # 检测可走路段 只能掉到190
            collide_can_walk = pygame.sprite.spritecollide(self.gamer, self.can_walk_group, False)
            if len(collide_can_walk) == 0 and not self.gamer.jump:
                self.gamer.pos_y += 30

            # 掉进沟壑里
            collide_gully = pygame.sprite.spritecollide(self.gamer, self.gully_group, False)
            # print(len(collide_gully), len(self.gully_group))
            # print(self.gamer.pos_x, self.gamer.pos_y)
            if len(collide_gully) > 0:
                self.gamer.pos_y += 40
                self.gamer.pos_x -= 10

            # 碰撞地刺
            collide_obstacle = pygame.sprite.spritecollide(self.gamer, self.thorn_group, False)
            if len(collide_obstacle) > 0:
                self.is_regame = True

            # 上台阶
            # print(len(self.steps_group))
            # for it in self.steps_group:
            #     print(it.rect, "+++", self.gamer.rect)
            collide_steps = pygame.sprite.spritecollide(self.gamer, self.steps_group, False)
            if len(collide_steps) > 0:
                self.gamer.jump = False
                self.t = 10
                self.gamer.a = 1

            # 碰npc
            collide_npc1 = pygame.sprite.collide_rect(self.gamer, self.monster1)
            collide_npc2 = pygame.sprite.collide_rect(self.gamer, self.monster2)
            if collide_npc1 or collide_npc2:
                self.is_regame = True


            # 通关
            collide_door = pygame.sprite.collide_rect(self.gamer, self.door)
            if collide_door:
                self.scene_exit = True

            if is_pressed:
                self.gamer.key_move(pressed_key)
                if pressed_key == pygame.K_y:
                    self.flag = False
                elif pressed_key == pygame.K_ESCAPE:
                    self.is_setting = False
                    surface = pygame.surface.Surface((800, 600))
                    self.screen.blit(surface, (0, 0))
                elif pressed_key == pygame.K_x:
                    self.is_setting = True

            if self.is_regame:
                self.gamer.pos_x -= 10
                if mouse_down:
                    x, y = pygame.mouse.get_pos()
                    if 240 <= x <= 570 and 170 <= y <= 215:
                        sys.exit()
                    elif 240 <= x <= 570 and 290 <= y <= 330:
                        self.res[0] = -2
                        return self.res

            if not self.flag:
                if self.gamer.pos_x >= 750:
                    self.gamer.pos_x = 750
                    self.gamer.rect = pygame.Rect(self.gamer.pos_x, self.gamer.pos_y, self.gamer.rect_width, self.gamer.rect_height)
                else:
                    self.gamer.pos_x += 10
                    self.gamer.rect = pygame.Rect(self.gamer.pos_x, self.gamer.pos_y, self.gamer.rect_width, self.gamer.rect_height)

            if self.gamer.jump:
                if self.t >= -10:
                    self.gamer.a = 1  # 前半段减速上跳
                    if self.t < 0:
                        self.gamer.a = -1  # 后半段加速下落
                    self.gamer.pos_y -= 0.5 * self.gamer.a * (self.t ** 2)  # 匀加速直线运动的位移公式

                    if self.gamer.pos_y < 0:
                        self.gamer.pos_y = 0  # 防止跳出边界
                    self.t -= 5
                else:
                    self.gamer.jump = False
                    self.t = 10

            if not self.is_setting:
                current_surface = self.get_current_surface()
                self.screen.blit(current_surface, (0, 0))
                self.screen.blit(self.screen_light, (0, 0))

            if self.is_setting:
                if self.gamer.pos_x != 0:
                    self.gamer.pos_x -= 10
                self.screen.blit(pygame.image.load(r'resource/image/setting.png'), (0, 0))
                self.screen.blit(self.screen_light, (0, 0))
                if mouse_down:
                    # print("-----")
                    x, y = pygame.mouse.get_pos()
                    if 400 <= x <= 465 and 270 <= y <= 310:
                        if 0 < self.alpha <= 150:
                            self.alpha -= 10
                            self.screen_light.set_alpha(self.alpha)
                    elif 510 <= x <= 575 and 270 <= y <= 310:
                        # print("+++++")
                        if 0 <= self.alpha < 150:
                            self.alpha += 10
                            self.screen_light.set_alpha(self.alpha)
                    elif 400 <= x <= 465 and 150 <= y <= 180:
                        if self.sound_voice >= 0.9:
                            self.sound_voice += 0.1
                            pygame.mixer.music.set_volume(self.sound_voice)
                    elif 510 <= x <= 575 and 150 <= y <= 180:
                        if self.sound_voice >= 0.1:
                            self.sound_voice -= 0.1
                            pygame.mixer.music.set_volume(self.sound_voice)
                    elif 390 <= x <= 485 and 405 <= y <= 450:
                        pygame.mixer.music.stop()
                        self.sound_path = r'resource/music/张国荣 - 愿你决定.mp3'
                        pygame.mixer.music.load(self.sound_path)
                        pygame.mixer.music.set_volume(pygame.mixer.music.get_volume())
                        pygame.mixer.music.play(-1)
                    elif 495 <= x <= 600 and 405 <= y <= 450:
                        pygame.mixer.music.stop()
                        self.sound_path = r'resource/music/123.mp3'
                        pygame.mixer.music.load(self.sound_path)
                        pygame.mixer.music.set_volume(pygame.mixer.music.get_volume())
                        pygame.mixer.music.play(-1)

            pygame.display.update()
            self.res[1] = self.alpha
            self.res[2] = self.sound_path
            self.res[3] = pygame.mixer.music.get_volume()
            self.res[4] = pygame.mixer.music.get_pos()
            clock.tick(5)  # 1s循环40次,即刷新页面40次

        return self.res
示例#6
0
class Scene3:
    def __init__(self, screen, screen_light, alpha, sound_path, sound_voice,
                 sound_pos):
        self.flag = True
        self.screen = screen
        self.screen_light = screen_light
        self.scene_exit = False
        self.is_setting = False
        self.alpha = alpha
        self.render = RenderTmx(r'resource/map3.tmx')
        self.temp_surface = self.render.surface.copy()
        self.fade = FadeScene(self.render.surface)
        self.birds_group = pygame.sprite.Group()
        self.steps_group = pygame.sprite.Group()
        self.pagoda_group = pygame.sprite.Group()
        self.gear_group = pygame.sprite.Group()
        self.can_walk_group = pygame.sprite.Group()
        self.thorn_group = pygame.sprite.Group()
        self.obstacle_group = pygame.sprite.Group()
        self.scene_exit = False
        self.mask = False
        self.t = 10
        self.init_object_group()
        self.dialog = GodDialog(4)
        self.is_regame = False
        self.restart_dialog = pygame.image.load(r'resource/image/regame.png')

        self.sound_path = sound_path
        self.sound_voice = sound_voice
        self.sound_pos = sound_pos
        self.res = [-1, -1, -1, -1, -1]

        # 详解:https://blog.csdn.net/qq_41556318/article/details/86305046
        pygame.mixer.music.set_pos(self.sound_pos / 1000)
        pygame.mixer.music.load(self.sound_path)
        pygame.mixer.music.set_volume(self.sound_voice)
        pygame.mixer.music.play(-1)  # -1即循环播放

    def init_object_group(self):
        for group in self.render.tiled.tmx_data.objectgroups:  # 得到图片信息
            if isinstance(
                    group, pytmx.TiledObjectGroup
            ):  # isinstance函数: runoob.com/python/python-func-isinstance.html
                if group.name == 'gamer':
                    for obj in group:
                        self.gamer = SceneGamer(obj.x, obj.y, 10, 10, True, 4,
                                                2)

                if group.name == '瞭望塔':
                    for obj in group:
                        pagoda = pygame.sprite.Sprite()
                        pagoda.rect = pygame.Rect(obj.x, obj.y, obj.width,
                                                  obj.height)
                        self.pagoda_group.add(pagoda)

                if group.name == '传送门':
                    for obj in group:
                        self.door = Door(obj.x, obj.y, True)

                if group.name == '飞鸟':
                    for obj in group:
                        bird = InteractiveMoveNPC(obj.x, obj.y, 'birds',
                                                  'devil', False, 50, 0)
                        self.birds_group.add(bird)

                if group.name == '齿轮':
                    for obj in group:
                        gear = NPC(obj.x, obj.y, obj.width, obj.height, 1, "",
                                   "齿轮")
                        self.gear_group.add(gear)

                if group.name == '台阶':
                    for obj in group:
                        step = pygame.sprite.Sprite()
                        step.rect = pygame.Rect(obj.x, obj.y, obj.width,
                                                obj.height)
                        self.steps_group.add(step)

                if group.name == '可走层':
                    for obj in group:
                        can_walk = pygame.sprite.Sprite()
                        can_walk.rect = pygame.Rect(obj.x, obj.y, obj.width,
                                                    obj.height)
                        self.can_walk_group.add(can_walk)

                if group.name == '地刺':
                    for obj in group:
                        thorn = NPC(obj.x, obj.y, obj.width, obj.height, 1, "",
                                    "berry")
                        self.thorn_group.add(thorn)

                if group.name == '骷髅大军':
                    for obj in group:
                        obstacle = pygame.sprite.Sprite()
                        obstacle.rect = pygame.Rect(obj.x, obj.y, obj.width,
                                                    obj.height)
                        self.obstacle_group.add(obstacle)

    def get_current_surface(self):
        # print(len(self.thorn_group))
        # print(len(self.thorn_group))
        surface = self.fade.get_back_image()
        self.temp_surface.blit(surface, (0, 0))
        for gear in self.gear_group:
            gear.draw(self.temp_surface)
        for bird in self.birds_group:
            bird.draw(self.temp_surface)
        for thorn in self.thorn_group:
            thorn.draw(self.temp_surface)
        self.gamer.draw(self.temp_surface)
        # 开始提示
        if self.flag:
            self.temp_surface.blit(self.dialog.surface, (0, 0))
        if self.is_regame:
            self.temp_surface.blit(self.restart_dialog, (100, 30))
        return self.temp_surface

    def run(self):
        clock = pygame.time.Clock()  # 计时器
        while not self.scene_exit:
            mouse_down = False
            if self.flag:
                self.temp_surface.blit(self.dialog.surface, (0, 0))

            # print(self.gamer.pos_x, self.gamer.pos_y)
            if self.gamer.pos_y > 385:
                self.is_regame = True
            # print(self.gamer.pos_x)
            is_pressed = False
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                elif event.type == pygame.KEYDOWN:  # 按下键盘上的键这个事件
                    is_pressed = True
                    pressed_key = event.key
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    mouse_down = True

            self.gamer.collide(self.obstacle_group)

            if self.is_regame:
                self.gamer.pos_x -= 10
                self.gamer.pos_y -= 10
                if mouse_down:
                    x, y = pygame.mouse.get_pos()
                    if 240 <= x <= 570 and 170 <= y <= 215:
                        sys.exit()
                    elif 240 <= x <= 570 and 290 <= y <= 330:
                        self.res[0] = -2
                        return self.res

            # 碰撞齿轮
            collide_gear = pygame.sprite.spritecollide(self.gamer,
                                                       self.gear_group, False)
            collide_obstacle = pygame.sprite.spritecollide(
                self.gamer, self.obstacle_group, False)
            collide_birds = pygame.sprite.spritecollide(
                self.gamer, self.birds_group, False)
            collide_thorn = pygame.sprite.spritecollide(
                self.gamer, self.thorn_group, False)
            collide_pagoda = pygame.sprite.spritecollide(
                self.gamer, self.pagoda_group, False)
            if len(collide_gear) > 0 or len(collide_obstacle) > 0 or len(
                    collide_birds) or len(collide_thorn) or len(
                        collide_pagoda):
                self.is_regame = True

            # 上台阶
            # print(len(self.steps_group))
            # for it in self.steps_group:
            #     print(it.rect, "+++", self.gamer.rect)
            collide_steps = pygame.sprite.spritecollide(
                self.gamer, self.steps_group, False)
            if len(collide_steps) > 0:
                self.gamer.jump = False
                self.t = 10
                self.gamer.a = 1

            # 通关
            collide_door = pygame.sprite.collide_rect(self.gamer, self.door)
            if collide_door:
                self.scene_exit = True

            if is_pressed:
                if not self.flag:
                    if pressed_key == pygame.K_UP:
                        self.gamer.pos_y -= 20
                elif pressed_key == pygame.K_y:
                    self.flag = False

            if is_pressed:
                if pressed_key == pygame.K_ESCAPE:
                    self.is_setting = False
                    surface = pygame.surface.Surface((800, 600))
                    self.screen.blit(surface, (0, 0))
                elif pressed_key == pygame.K_x:
                    # print("++++++++")
                    self.is_setting = True

            if not self.flag:
                if self.gamer.pos_x >= 750:
                    self.gamer.pos_x = 750
                    self.gamer.rect = pygame.Rect(self.gamer.pos_x,
                                                  self.gamer.pos_y,
                                                  self.gamer.rect_width,
                                                  self.gamer.rect_height)
                else:
                    self.gamer.pos_x += 10
                    self.gamer.rect = pygame.Rect(self.gamer.pos_x,
                                                  self.gamer.pos_y,
                                                  self.gamer.rect_width,
                                                  self.gamer.rect_height)
                self.gamer.pos_y += 10

            if not self.is_setting:
                current_surface = self.get_current_surface()
                self.screen.blit(current_surface, (0, 0))
                self.screen.blit(self.screen_light, (0, 0))

            if self.is_setting:
                if self.gamer.pos_x != 0:
                    self.gamer.pos_x -= 10
                if not self.flag:
                    self.gamer.pos_y -= 10
                self.screen.blit(
                    pygame.image.load(r'resource/image/setting.png'), (0, 0))
                self.screen.blit(self.screen_light, (0, 0))
                if mouse_down:
                    # print("-----")
                    x, y = pygame.mouse.get_pos()
                    if 400 <= x <= 465 and 270 <= y <= 310:
                        if 0 < self.alpha <= 150:
                            self.alpha -= 10
                            self.screen_light.set_alpha(self.alpha)
                    elif 510 <= x <= 575 and 270 <= y <= 310:
                        # print("+++++")
                        if 0 <= self.alpha < 150:
                            self.alpha += 10
                            self.screen_light.set_alpha(self.alpha)
                    elif 400 <= x <= 465 and 150 <= y <= 180:
                        if self.sound_voice >= 0.9:
                            self.sound_voice += 0.1
                            pygame.mixer.music.set_volume(self.sound_voice)
                    elif 510 <= x <= 575 and 150 <= y <= 180:
                        if self.sound_voice >= 0.1:
                            self.sound_voice -= 0.1
                            pygame.mixer.music.set_volume(self.sound_voice)
                    elif 390 <= x <= 485 and 405 <= y <= 450:
                        pygame.mixer.music.stop()
                        self.sound_path = r'resource/music/张国荣 - 愿你决定.mp3'
                        pygame.mixer.music.load(self.sound_path)
                        pygame.mixer.music.set_volume(
                            pygame.mixer.music.get_volume())
                        pygame.mixer.music.play(-1)
                    elif 495 <= x <= 600 and 405 <= y <= 450:
                        pygame.mixer.music.stop()
                        self.sound_path = r'resource/music/123.mp3'
                        pygame.mixer.music.load(self.sound_path)
                        pygame.mixer.music.set_volume(
                            pygame.mixer.music.get_volume())
                        pygame.mixer.music.play(-1)

            pygame.display.update()
            self.res[1] = self.alpha
            self.res[2] = self.sound_path
            self.res[3] = pygame.mixer.music.get_volume()
            self.res[4] = pygame.mixer.music.get_pos()
            clock.tick(5)  # 1s循环40次,即刷新页面40次

        return self.res
class begin_scene:
    def __init__(self, screen, screen_light, alpha, sound_path, sound_voice,
                 sound_pos):
        pygame.init()
        pygame.mixer.init()

        self.screen = screen
        self.screen_light = screen_light
        self.render = RenderTmx(r'resource\begin.tmx')
        self.temp_surface = self.render.surface.copy()
        self.fade = FadeScene(self.render.surface)
        self.is_setting = False
        self.alpha = alpha
        self.sound_path = sound_path
        self.sound_voice = sound_voice
        self.sound_pos = sound_pos
        self.res = [-1, -1, -1, -1, -1]

        # 详解:https://blog.csdn.net/qq_41556318/article/details/86305046
        pygame.mixer.music.set_pos(self.sound_pos)
        pygame.mixer.music.load(self.sound_path)
        pygame.mixer.music.set_volume(self.sound_voice)
        pygame.mixer.music.play(-1)  # -1即循环播放

        surface = pygame.surface.Surface((800, 600))
        self.screen.blit(surface, (0, 0))

    def get_surface(self):
        surface = self.fade.get_back_image()
        self.temp_surface.blit(surface, (0, 0))
        return self.temp_surface

    def run(self):
        scene_exit = False
        clock = pygame.time.Clock()  # 计时器
        while not scene_exit:
            mouse_down = False
            is_pressed = False
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                elif event.type == pygame.KEYDOWN:  # 按下键盘上的键这个事件
                    is_pressed = True
                    pressed_key = event.key
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    mouse_down = True

            if is_pressed:
                if pressed_key == pygame.K_n:
                    sys.exit()
                elif pressed_key == pygame.K_y:
                    scene_exit = True
                elif pressed_key == pygame.K_ESCAPE:
                    self.is_setting = False
                    surface = pygame.surface.Surface((800, 600))
                    self.screen.blit(surface, (0, 0))
                elif pressed_key == pygame.K_x:
                    self.is_setting = True

            if not self.is_setting:
                current_surface = self.get_surface()
                self.screen.blit(current_surface, (0, 0))
                self.screen.blit(self.screen_light, (0, 0))

            if self.is_setting:
                self.screen.blit(
                    pygame.image.load(r'resource/image/setting.png'), (0, 0))
                self.screen.blit(self.screen_light, (0, 0))
                if mouse_down:
                    # print("-----")
                    x, y = pygame.mouse.get_pos()
                    if 400 <= x <= 465 and 270 <= y <= 310:
                        if 0 < self.alpha <= 150:
                            self.alpha -= 10
                            self.screen_light.set_alpha(self.alpha)
                    elif 510 <= x <= 575 and 270 <= y <= 310:
                        # print("+++++")
                        if 0 <= self.alpha < 150:
                            self.alpha += 10
                            self.screen_light.set_alpha(self.alpha)
                    elif 400 <= x <= 465 and 150 <= y <= 180:
                        if self.sound_voice >= 0.9:
                            self.sound_voice += 0.1
                            pygame.mixer.music.set_volume(self.sound_voice)
                    elif 510 <= x <= 575 and 150 <= y <= 180:
                        if self.sound_voice >= 0.1:
                            self.sound_voice -= 0.1
                            pygame.mixer.music.set_volume(self.sound_voice)
                    elif 390 <= x <= 485 and 405 <= y <= 450:
                        pygame.mixer.music.stop()
                        self.sound_path = r'resource/music/张国荣 - 愿你决定.mp3'
                        pygame.mixer.music.load(self.sound_path)
                        pygame.mixer.music.set_volume(
                            pygame.mixer.music.get_volume())
                        pygame.mixer.music.play(-1)
                    elif 495 <= x <= 600 and 405 <= y <= 450:
                        pygame.mixer.music.stop()
                        self.sound_path = r'resource/music/123.mp3'
                        pygame.mixer.music.load(self.sound_path)
                        pygame.mixer.music.set_volume(
                            pygame.mixer.music.get_volume())
                        pygame.mixer.music.play(-1)

            pygame.display.update()
            self.res[1] = self.alpha
            self.res[2] = self.sound_path
            self.res[3] = pygame.mixer.music.get_volume()
            self.res[4] = pygame.mixer.music.get_pos()
            clock.tick(5)  # 1s循环40次,即刷新页面40次

        return self.res