コード例 #1
0
def main():
    """start the game"""

    # initiate logging system
    logs.init()

    # set caption and title screen
    pg.display.set_caption("Stickman's New World")
    pg.display.set_icon(PICS['game_icon'])

    # place starting image on screen
    SURFACE.blit(PICS['title_screen'], (0, 0))
    continue_ = True
    logging.debug('starting game')

    while continue_:
        if MAIN_GAME_STATE.get('TERMINAL') is not None:
            MAIN_GAME_STATE['TERMINAL'].threaded_update()
        SURFACE.blit(PICS['title_screen'], (0, 0))
        SURFACE.blit(MAIN_GAME_STATE['CURSOR'], pg.mouse.get_pos())
        for event in pg.event.get():
            check_quit(event)

            if event.type == MOUSEBUTTONDOWN:
                # on to the next loop
                continue_ = not continue_
        pg.display.update()

    music.check(MAIN_GAME_STATE)
    choose_function()
コード例 #2
0
def secondly_check(game_state):
    """
    run this every second or so. it will choose to check some things.
    """
    game_state['PARTICLES'] = list(
        filter(lambda x: x.lifespan != 0, game_state['PARTICLES']))
    game_state['PROJECTILES'] = list(
        filter(lambda x: x.lifespan != 0, game_state['PROJECTILES']))
    music.check(game_state)
コード例 #3
0
def main():
    """run the game, after the title screen."""
    continue_ = True
    frames = 0
    menu = pg.Surface((800, 200))
    menu.fill((0, 255, 0))
    MAIN_GAME_STATE['MOUSEDOWN'] = False
    music.check(MAIN_GAME_STATE)

    while continue_:
        for entity in MAIN_GAME_STATE['ENTITIES']:
            # we don't need to worry about dead entities here, they
            # will be removed automatically by update.
            entity.update()
        # print(MAIN_GAME_STATE['SETTINGS'])

        frames += 1
        print('FPS: ', CLOCK.get_fps())

        if frames % (60 * 30) == 0:
            logging.debug('FPS: %f', CLOCK.get_fps())

        if frames % 60 == 0:
            # run secondly check
            secondly_check(MAIN_GAME_STATE)

        MAIN_GAME_STATE['MOUSE_POS'] = pg.mouse.get_pos()
        events = [event for event in pg.event.get()]

        for event in events:
            terminal.handle(events)

        if MAIN_GAME_STATE['AREA'] == Area.MAP:
            draw_map()
            handle_map()

        elif MAIN_GAME_STATE['AREA'] == Area.STAGE:
            MAIN_GAME_STATE['STAGE'].update(events)

        if MAIN_GAME_STATE.get('TERMINAL') is not None:
            for event in events:
                MAIN_GAME_STATE['TERMINAL'].add_event(event)

            MAIN_GAME_STATE['TERMINAL'].threaded_update()

        MAIN_GAME_STATE['MAIN_DISPLAY_SURF'].blit(MAIN_GAME_STATE['CURSOR'],
                                                  pg.mouse.get_pos())
        # MAIN_GAME_STATE['MAIN_DISPLAY_SURF'].blit(menu, (0, 400))
        pg.display.update()

        CLOCK.tick(FPS)
コード例 #4
0
def choose_function():
    def func():
        return None

    continue_ = True
    rects = draw_choices()
    while continue_:
        music.check(MAIN_GAME_STATE)
        SURFACE.blit(PICS['menu_background'], (0, 0))

        if MAIN_GAME_STATE.get('TERMINAL') is not None:
            MAIN_GAME_STATE['TERMINAL'].threaded_update()

        if MAIN_GAME_STATE['AREA'] == Area.MAP:
            gameplay.main()
            continue_ = False

        label("Stickman's New World", CENTRE_X, 100, size=60)

        for lbl in rects:
            lbl.draw(SURFACE)
        for event in pg.event.get():
            check_quit(event)
            terminal.handle(event)

            for lbl in rects:
                lbl.handle(event, SURFACE)

                if event.type == MOUSEBUTTONDOWN and lbl.rect.collidepoint(
                        *event.pos):
                    func = lbl.function
                    continue_ = False

        if MAIN_GAME_STATE.get('TERMINAL') is not None:
            MAIN_GAME_STATE['TERMINAL'].threaded_update()

        SURFACE.blit(MAIN_GAME_STATE['CURSOR'], pg.mouse.get_pos())
        pg.display.update()
        CLOCK.tick(FPS)

    func()
コード例 #5
0
def main(game_state, bgimage):
    """run the main settings function."""

    # font file
    fontfile = os.path.join('data', 'Michael`s Font.ttf')
    # font objects
    font_header = pygame.font.Font(fontfile, 70)
    font_button = pygame.font.Font(fontfile, 30)

    # extract the two needed things for this function
    settings = game_state['SETTINGS']
    surf = game_state['MAIN_DISPLAY_SURF']

    # generate the list of switches needed for each setting
    switches = get_switches(game_state)

    # button to return to the main screen.
    return_btn: pygame.Surface = font_button.render('Return', True,
                                                    (255, 255, 255))

    # rectangle object bound to return button
    return_rect = return_btn.get_rect(center=(400, 560))

    # header to be drawn and show the user where they are.
    header = font_header.render('Settings', True, (255, 255, 255))

    # used to determine where the header should go.
    header_rect = header.get_rect(center=(400, 70))

    while True:
        # check the settings to handle the music.
        music.check(game_state)

        # background image to be drawn first
        surf.blit(bgimage, (0, 0))

        # draw the return button
        surf.blit(return_btn, return_rect)

        # draw the settings header
        surf.blit(header, header_rect)

        # draw each switch
        for switch in switches:
            switch.draw(surf)

        # event handler
        for event in pygame.event.get():
            # check for quits
            if event.type == pygame.QUIT:
                raise SystemExit

            # check to see if anything was clicked on
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # check to see if the return button was clicked.
                if return_rect.collidepoint(event.pos):
                    # return to previous screen
                    return

                # check all switches
                for switch in switches:
                    # someone clicked on a switch
                    if switch.rect.collidepoint(event.pos):
                        # switch the state (False -> True, True -> False)
                        switch.state = not switch.state
                        # update the setting
                        settings[switch.text] = switch.state
                        print(settings)
        # add the cursor (always in the foreground)
        surf.blit(database.PICS['cursor'], pygame.mouse.get_pos())
        # update screen
        pygame.display.update()
コード例 #6
0
def new_game():

    MAIN_GAME_STATE['AREA'] = Area.TITLE

    char_imgs = [
        CharacterImage(
            'swordsman',
            # fake weapon. only has colour attribute
            Namespace(colour='grey'),
            (START_X, START_Y),
            None,
        ),
        CharacterImage(
            'angel',
            Namespace(colour='gold'),
            (START_X + 150, START_Y),
            None,
        ),
        CharacterImage(
            'archer',
            Namespace(colour='brown'),
            (START_X + 300, START_Y),
            None,
        ),
        CharacterImage(
            'spearman',
            Namespace(colour='grey'),
            (START_X + 450, START_Y),
            None,
        ),
        CharacterImage(
            'wizard',
            Namespace(colour='blue'),
            (START_X + 600, START_Y),
            None,
        ),
    ]

    selected_char_imgs = [
        CharacterImage(
            'swordsman',
            # fake weapon. only has colour attribute
            Namespace(colour='grey'),
            (12, 16),
            None,
        ),
        CharacterImage(
            'angel',
            Namespace(colour='gold'),
            (12, 16),
            None,
        ),
        CharacterImage(
            'archer',
            Namespace(colour='brown'),
            (12, 16),
            None,
        ),
        CharacterImage(
            'spearman',
            Namespace(colour='grey'),
            (12, 16),
            None,
        ),
        CharacterImage(
            'wizard',
            Namespace(colour='blue'),
            (12, 16),
            None,
        ),
    ]

    def null():
        return None

    y = WIN_Y // 2 + 30

    char_lbls = [
        ClickableLabel('Swordsman', (START_X, y), null, WHITE, textsize=24),
        ClickableLabel(
            'Angel',
            (START_X + 150, y),
            null,
            WHITE,
            textsize=24,
        ),
        ClickableLabel(
            'Archer',
            (START_X + 300, y),
            null,
            WHITE,
            textsize=24,
        ),
        ClickableLabel(
            'Spearman',
            (START_X + 450, y),
            null,
            WHITE,
            textsize=24,
        ),
        ClickableLabel(
            'Wizard',
            (START_X + 600, y),
            null,
            WHITE,
            textsize=24,
        ),
    ]

    # this is a list of four, and contains a box aligned with its image.
    chosen = [(None, None)] * 4

    def set_(box):
        old = get_selected()
        # print(old, 'HoWdY YoU FeLlErS')
        old.selected = False
        box.selected = True

    chosen_boxes = [
        Box((250, 400), 30, 30, WHITE, onclick=lambda: set_(chosen_boxes[0])),
        Box((350, 400), 30, 30, WHITE, onclick=lambda: set_(chosen_boxes[1])),
        Box((450, 400), 30, 30, WHITE, onclick=lambda: set_(chosen_boxes[2])),
        Box((550, 400), 30, 30, WHITE, onclick=lambda: set_(chosen_boxes[3])),
    ]

    chosen_boxes[0].selected = True

    def get_selected():
        """return the selected box."""
        for i in chosen_boxes:
            if i.selected:
                # print(chosen_boxes.index(i))
                return i

    continue_ = True

    next_button = ClickableLabel("Next", (700, 420),
                                 lambda: None,
                                 WHITE,
                                 textsize=50)
    next_button.draw(SURFACE)
    if None not in chosen:
        # fills the next_button.rect spot. will not show up yet
        next_button.draw(SURFACE)
    filled = False

    while continue_:
        music.check(MAIN_GAME_STATE)
        SURFACE.blit(PICS['menu_background'], (0, 0))
        if None not in chosen:
            next_button.draw(SURFACE)
            filled = True

        label('Choose your players:', MID_X, 75, 60)
        for box in chosen_boxes:
            box.draw(SURFACE)

        for i in char_imgs:
            i.build_image(SURFACE, COLOURS['beige'], False)

        for i in char_lbls:
            i.draw(SURFACE)

        for event in pg.event.get():
            check_quit(event)
            terminal.handle(event)

            for lbl in char_lbls:
                lbl.handle(event, SURFACE)
                if event.type == MOUSEBUTTONDOWN and lbl.rect.collidepoint(
                        *event.pos):
                    # need to add the character's image to the selected box.
                    item = selected_char_imgs[char_lbls.index(lbl)]
                    box = chosen_boxes.index(get_selected())
                    chosen[box] = (item, get_selected())

                if event.type == MOUSEBUTTONDOWN and \
                        next_button.rect.collidepoint(*event.pos) and \
                        filled:
                    continue_ = False

            for box in chosen_boxes:
                box.handle(event)

        for pair in chosen:
            if pair == (None, None):
                break

            character, box = pair
            coords = box.topleft[0] + 10, box.topleft[1] + 17

            character.update_coords(coords)

            character.build_image(SURFACE, COLOURS['beige'], False)

            # pg.display.update()
        # print((str(num_selected) + "\n") * 10)

        if MAIN_GAME_STATE.get('TERMINAL') is not None:
            MAIN_GAME_STATE['TERMINAL'].threaded_update()

        if MAIN_GAME_STATE['AREA'] != Area.TITLE:
            continue_ = False
            gameplay.main()

        SURFACE.blit(MAIN_GAME_STATE['CURSOR'], pg.mouse.get_pos())
        pg.display.update()
        CLOCK.tick(24)

    continue_ = True
    MAIN_GAME_STATE["AREA"] = database.Area.MAP
    MAIN_GAME_STATE["PLAYERS"] = get_characters_from_images(
        [i[0] for i in chosen])

    gameplay.main()