Ejemplo n.º 1
0
def main(lfile):
    # Initialisation de la fenêtre d'affichage
    pygame.init()
    Config.screen = pygame.display.set_mode((Config.screenWidth, Config.screenHeight), HWSURFACE | DOUBLEBUF)
    pygame.display.set_caption(Config.titre+' ('+Config.name_app+')')
    # le logo d'hommage ^^
    Config.logo, Config.logo_r = Tools.load_png('logo.png')

    # les bords de la surface de jeu
    Config.bords = Border.Border()
    # le fond
    Config.bg, Config.bg_r = Tools.load_png('bg-editor.png')
    # on met à l'échelle le fond si besoin
    if (Config.bg_r.width, Config.bg_r.height) != (Config.bords.width, Config.bords.height):
        Config.bg = pygame.transform.scale(Config.bg, (Config.bords.width, Config.bords.height))

    # la zone d'info
    Config.zinfo = pygame.Surface([(Config.screenWidth - Config.bords.width), Config.screenHeight])
    Config.zinfo.fill(THECOLORS["black"])

    # zone de saisie/affichage du fichier (centrée dans la zone d'info)
    lvx = Config.bords.width + ((Config.screenWidth - Config.bords.width) // 2) - (Config.zinfo.get_rect().width // 2) + 3
    Ced.levelname = Gui.InputBox(lvx, Config.screenHeight - 55, Config.zinfo.get_rect().width - 10, 32, 'level-{0:03d}.txt'.format(Config.level))

    RazLevel()

    # Affichage
    Config.screen.blit(Config.bg, (0, 0))
    Config.bords.draw()
    if Ced.view_grid:
        DrawGrid()
    EditorMenu()
    DrawLevel()
    pygame.display.flip()

    if lfile is not None:
        # fichier à charger
        Ced.levelname.setText(lfile)
        LoadLevel()

    # Initialisation de l'horloge
    Config.clock = pygame.time.Clock()

    # Boucle d'évènements
    while True:
        event_handler()
        dt = Config.clock.tick(Config.FPS) / 1000

        # affichages
        Config.screen.blit(Config.bg, (0, 0))
        Config.bords.draw()
        if Ced.view_grid:
            DrawGrid()
        EditorMenu()
        DrawLevel()
        if Config.bossLevel:
            Ced.boss_sps.draw()
            if Ced.boss_brL is not None:
                Ced.boss_brL.draw()
            if Ced.boss_brR is not None:
                Ced.boss_brR.draw()
        Ced.levelname.draw()
        if Ced.z_select is not None:
            Ced.z_select.draw()
        pygame.display.flip()
Ejemplo n.º 2
0
    def setup(self):
        """ Initialize the GUI and place it properly; initialize the variables. """
        # Set up the collector.
        self.collector = Collector()
        self.collector.update_function = self.update_input_box

        # Create a key map.
        keycodes = {
            256: ['0', (2, 1), (18, 169, 202)],
            257: ['1', (1, 1), (196, 170, 77)],
            258: ['2', (1, 1), (242, 210, 72)],
            259: ['3', (1, 1), (45, 186, 97)],
            260: ['4', (1, 1), (121, 124, 125)],
            261: ['5', (1, 1), (133, 145, 115)],
            262: ['6', (1, 1), (196, 52, 92)],
            263: ['7', (1, 1), (245, 245, 252)],
            264: ['8', (1, 1), (206, 224, 182)],
            265: ['9', (1, 1), (244, 124, 124)],
            266: ['.', (1, 1), (255, 255, 255)],
            267: ['/', (1, 1), (255, 255, 255)],
            268: ['*', (1, 1), (255, 255, 255)],
            269: ['-', (1, 1), (255, 255, 255)],
            270: ['+', (1, 2), (255, 255, 255)],
            271: ['e', (1, 2), (255, 255, 255)],
            300: ['n', (1, 1), (255, 255, 255)]
        }

        # Create the key displays.
        self.keys = {}
        for code in keycodes:
            self.keys[code] = Gui.KeyButton(value=keycodes[code][0],
                                            font=BASIC_FONT,
                                            size=keycodes[code][1],
                                            color=Color(rgb=keycodes[code][2]))
            if code >= 256 and code <= 265:
                self.keys[code].keyup_function = self.collector.collect

        layout = ((300, 267, 268, 269), (263, 264, 265, 270), (260, 261, 262),
                  (257, 258, 259, 271), (256, 266))
        x = SPACER
        y = SPACER
        for row in layout:
            for col in row:
                self.keys[col].set_position(x, y)
                self.add_gui_updatable(self.keys[col])
                x += self.keys[col].width + SPACER
            y += Gui.GUIRect.HEIGHT + SPACER
            x = SPACER

        # Create the score display.
        self.hp_display = Gui.DisplayableText(value='♥♥♥',
                                              font=BASIC_FONT,
                                              color=Color(rgb=Color.WHITE),
                                              align='rc')
        self.hp_display.set_position(640 - SPACER, 400)
        self.add_gui(self.hp_display)

        # Create the HP display.
        self.score_display = Gui.DisplayableText(value='Score: 0',
                                                 font=BASIC_FONT,
                                                 color=Color(rgb=Color.WHITE),
                                                 align='lc')
        self.score_display.set_position(SPACER, 400)
        self.add_gui(self.score_display)

        # Create the HP lost display.
        self.hp_lost_display = Gui.FadingText(value='♥',
                                              font=BASIC_FONT,
                                              color=Color(rgb=Color.RED),
                                              align='lc')
        self.hp_lost_display.deactivate()
        self.add_gui(self.hp_lost_display)
        self.add_gui_updatable(self.hp_lost_display)

        # Create the input box.
        self.input_box = Gui.InputBox(width=self.owner.window_size[0] -
                                      4 * Gui.KeyButton.WIDTH - 6 * SPACER,
                                      font=BASIC_FONT)
        self.input_box.set_position(x=4 * (Gui.GUIRect.WIDTH + SPACER) +
                                    SPACER,
                                    y=SPACER,
                                    margin_x=5)
        self.add_gui(self.input_box)

        # Set up the TargetFactory.
        func_dict = {
            AC.REWARD: self.reward,
            AC.PENALTY: self.penalty,
            AC.DESPAWN_GOOD: self.despawn_good,
            AC.DESPAWN_BAD: self.despawn_bad,
            AC.DESPAWN_NONENTITY: self.despawn_nonentity,
            AC.SPAWN: self.spawn
        }
        self.target_factory = TargetFactory(
            functions=func_dict,
            after_adder=self.after_adder,
            board_position=self.input_box.get_position(),
            board_width=self.input_box.width,
            font=BASIC_FONT)

        # Set up a simple game.
        self.score = 0
        self.hp = 10
        for i in range(4):
            blueprint = TargetBlueprint()
            blueprint.attributes[AT.TARGET_TYPE] = TargetType.TIMED
            blueprint.attributes[AT.STRENGTH] = 3 - STRENGTH_INCREASE
            self.target_factory.create(blueprint)
        self.owner.score_keeper.begin()

        # Set up the key events map.
        for code in keycodes:
            self.key_events[KEYUP][code] = [self.keys[code].keyup]
            self.key_events[KEYDOWN][code] = [self.keys[code].keydown]
        self.key_events[KEYUP][K_ESCAPE] = [self.clear_or_surrender]
        self.key_events[KEYUP][8] = [self.collector.backspace]  # backspace
        self.key_events[KEYUP][271].append(self.shoot_target)  # enter
        # Below function disabled in the current one-mode-only game.
        # self.key_events[KEYUP][270].append(self.add_target)        # +
        self.key_events[KEYUP][46] = self.key_events[KEYUP][266]  # .=,
        self.key_events[KEYDOWN][46] = self.key_events[KEYDOWN][266]  # .=,

        # Perform the first update to show GUI properly.
        self.update_score()