Beispiel #1
0
def drawopt(text, x, y, func=0):
    """draw text tto the screen at (x, y).
    return class_.MyRect of rectangle."""
    fontobj = pg.font.Font(os.path.join('data', 'Michael`s Font.ttf'), 32)
    textsurf = fontobj.render(text, True, WHITE)
    textrect = textsurf.get_rect()
    textrect.center = (x, y)
    SURFACE.blit(textsurf, textrect)
    return class_.MyRect(textrect), textsurf, func
def draw_text(
        text,
        size=32,
        cen_of_txt=(WIN_X // 2, WIN_Y // 2),
        colour=(COLOURS['black'], COLOURS['white']),
) -> tuple:
    """
    function for drawing text on SURFACE,
    returns a tuple containing the rect
    of the surface, and the surface 
    itself.
    """
    FontObj = pg.font.Font('data\\Michael`s Font.ttf', size)
    FontSurf = FontObj.render(text, True, *colour)
    Rect = FontSurf.get_rect()
    Rect.center = cen_of_txt
    SURFACE.blit(FontSurf, Rect)
    return class_.MyRect(Rect, colour=COLOURS['white']), FontSurf
def draw_box(size, colour, pos, alpha=None, image=None) -> tuple:
    """
    return a square rectangle, surface pair
    uses MyRect
    """
    print(pos)
    new_surf = pg.surface.Surface(size)
    new_surf.fill(colour)

    if alpha is not None:
        new_surf.set_alpha(alpha)

    SURFACE.blit(new_surf, pos)

    if image is not None:
        SURFACE.blit(image, pos)

    return class_.MyRect(
        new_surf.get_rect(topleft=pos), colour=colour), new_surf
def choose_game_mode() -> int:
    """
    choose which option the player wants
    return 1 if player chooses new game
    return 2 if player chooses load game
    return 3 if player chooses load a backup.
    """
    print('choose_game_mode called')
    SURFACE.fill(COLOURS['white'])

    # I realize now that Ishould have used a function for the three sections below, but whatever.
    LabelPlay = pg.font.Font('data\\Michael`s Font.ttf', 32)
    PlaySurf=LabelPlay.render('New Game', True, COLOURS['black'], \
                                               COLOURS['white'])
    PlayRect = class_.MyRect(PlaySurf.get_rect())
    PlayRect.center = ((WIN_X // 2), (WIN_Y // 2) - 50)
    SURFACE.blit(PlaySurf, PlayRect)

    #################################################################

    LabelLoad = pg.font.Font('data\\Michael`s Font.ttf', 32)
    LoadSurf=LabelLoad.render('Load Game', True, COLOURS['black'], \
                                               COLOURS['white'])
    LoadRect = class_.MyRect(LoadSurf.get_rect())
    LoadRect.center = (WIN_X // 2, WIN_Y // 2)
    SURFACE.blit(LoadSurf, LoadRect)

    #################################################################

    LabelLoadEarlier = pg.font.Font('data\\Michael`s Font.ttf', 32)
    LESurf=LabelLoadEarlier.render('Load Earlier Save', True, COLOURS['black'], \
                                                              COLOURS['white'])
    LERect = class_.MyRect(LESurf.get_rect())
    LERect.center = (WIN_X // 2, WIN_Y // 2 + 50)
    SURFACE.blit(LESurf, LERect)

    while True:
        for event in pg.event.get():
            if event.type == QUIT:
                terminate()

            elif event.type == MOUSEMOTION:
                x, y = event.pos
                for (rect, surf) in ((PlayRect, PlaySurf),
                                     (LoadRect, LoadSurf), (LERect, LESurf)):
                    rect.handle(event, SURFACE, surf)

            elif event.type == MOUSEBUTTONDOWN:
                x, y = event.pos
                if PlayRect.collidepoint(x, y):
                    print("PlayRect Clicked")
                    return 0  #these return value, and end loop

                elif LoadRect.collidepoint(x, y):
                    print("LoadRect Called")
                    return 1

                elif LERect.collidepoint(x, y):
                    print("LERect called")
                    return 2

            pg.display.update()
def get_characters() -> list:
    """
    starts a new game,
    and lets the player choose
    their characters.
    returns a list of the characters
    the player has chosen.
    """
    SURFACE.fill(COLOURS['white'])

    draw_text(
        'Choose your players:', cen_of_txt=(WIN_X // 2, WIN_Y // 2 - 200))

    texts = {}
    pairs = []
    num = -250  # this is the starting point for the images to appear

    # puts all the characters in a line with their caption beneath
    for string in database.ALL_CLASSES:
        string = string.lower()
        texts[string] = draw_text(
            string, size=20, cen_of_txt=(WIN_X // 2 + num, WIN_Y // 2 + 200))

        pic = PICS['characters'][string]
        SURFACE.blit(pic, (texts[string][0].x + 20, texts[string][0].y + 30))

        pairs.append((string, class_.MyRect(pic.get_rect())))
        num += 100

    del num, string

    box_list = []

    # this loop puts 4 boxes to show which characters the user has chosen
    for i in range(WIN_X // 4, WIN_X // 4 * 3, 100):
        box_list.append((draw_box(
            (25, 25), COLOURS['gray'], (i, WIN_Y // 2), alpha=200),
                         (i, WIN_Y // 2))[0][0])

    del i

    print('pairs: ', *pairs, sep='\n')

    char_list = []
    clicked = 0
    boxes_with_pictures = []
    box_num_pairs = {
        1: box_list[0],
        2: box_list[1],
        3: box_list[2],
        4: box_list[3],
    }
    for key in box_num_pairs:
        print('key: ', key, ' box: ', box_num_pairs[key], sep='')

    while True:
        for event in pg.event.get():
            if event.type == QUIT:
                terminate()

            elif event.type == MOUSEMOTION:
                for key in texts:
                    M = texts[key]
                    M[0].handle(event, SURFACE, M[1])

            # this branch controls when a selection box is
            # selected, which one to underline.
            elif event.type == MOUSEBUTTONDOWN:  # if mouse is clicked
                x, y = event.pos

                for key, rect in zip(box_num_pairs.keys(),
                                     box_num_pairs.values()):
                    print(rect is box_num_pairs[key])

                    if rect.collidepoint(x, y):
                        # if click is in 'rect'
                        if not rect.underlined:
                            # only do this is 'rect' is underlined
                            rect.underline(SURFACE)

                        if clicked == key:
                            box_num_pairs[clicked].remove_underline(SURFACE)
                            clicked = 0

                        elif clicked == 0:
                            clicked = key

                        else:
                            box_num_pairs[clicked].remove_underline(SURFACE)
                            clicked = key

                for rect_key, rect in zip(box_num_pairs.keys(),
                                          box_num_pairs.values()):

                    for character_name, rect_surf_pair in zip(
                            texts.keys(), texts.values()):

                        if rect_surf_pair[0].collidepoint(x, y):
                            print('garpenchank')
                            try:
                                box_num_pairs[clicked].draw_inside(
                                    PICS['characters'][character_name],
                                    SURFACE)
                                boxes_with_pictures.append(clicked)

                            except (class_.SMRError, KeyError) as error:
                                print(error)

                            break

                        elif clicked in boxes_with_pictures:
                            print('gud')
                            box_num_pairs[clicked].remove_pic(SURFACE)

                        char_list = [
                            box.PicInside for box in box_num_pairs.values()
                        ]

        pg.display.update()

        if not None in char_list[:2]:
            print('howdy')
            char_list = [box.PicInside for box in box_num_pairs.values()]
        if not None in char_list:
            return char_list