Пример #1
0
def test_log_bound_scroll_up_once():
    pygame.font.init()
    log_deck = LogDeck()
    for i in range(log_deck.max_records * 10):
        log_deck.add_record(f"Text{i}")
    log_deck.scroll_up()
    assert log_deck.bound == 1
Пример #2
0
def test_log_max_records_scroll_up():
    pygame.font.init()
    log_deck = LogDeck()
    for i in range(log_deck.max_records):
        log_deck.add_record("Text")
    log_deck.scroll_up()
    assert log_deck.bound == 0
Пример #3
0
def test_log_bound_scroll_up_full():
    pygame.font.init()
    log_deck = LogDeck()
    for i in range(log_deck.max_records * 10):
        log_deck.add_record(f"Text{i}")
    for _ in range(log_deck.max_records * 20):
        log_deck.scroll_up()
    assert log_deck.bound == len(log_deck.log_list) - log_deck.max_records
Пример #4
0
def test_log_bound_scroll_mixed_return():
    pygame.font.init()
    log_deck = LogDeck()
    for i in range(log_deck.max_records * 10):
        log_deck.add_record(f"Text{i}")
    num_downs = log_deck.max_records
    num_ups = log_deck.max_records
    for _ in range(num_ups):
        log_deck.scroll_up()
    for _ in range(num_downs):
        log_deck.scroll_down()
    assert log_deck.bound == 0
Пример #5
0
def test_log_init_scroll_up():
    pygame.font.init()
    log_deck = LogDeck()
    log_deck.scroll_up()
    assert log_deck.bound == 0
Пример #6
0
def main():
    """docstring."""
    pygame.init()
    pygame.mixer.init()
    screen = pygame.display.set_mode((constants.WIDTH, constants.HEIGHT))
    pygame.display.set_caption("My Game")
    clock = pygame.time.Clock()

    all_sprites = pygame.sprite.Group()

    # player = Player()
    game_deck = GameDeck()
    all_sprites.add(game_deck)

    player1_deck = PlayerDeck(120, 60)
    all_sprites.add(player1_deck)

    player2_deck = PlayerDeck(180 + 11 * 30, 60)
    all_sprites.add(player2_deck)

    comp = ForPlayer(random_mode=True)
    pl = ForPlayer(random_mode=False)

    log_deck = LogDeck()
    all_sprites.add(log_deck)

    menu_deck = MenuDeck()
    all_sprites.add(menu_deck)
    all_sprites.add(menu_deck.button_start.border)
    all_sprites.add(menu_deck.button_start)
    all_sprites.add(menu_deck.button_exit.border)
    all_sprites.add(menu_deck.button_exit)

    pygame.font.init()
    running = True
    end_game = True
    while running:
        # Держим цикл на правильной скорости
        clock.tick(constants.FPS)
        # Ввод процесса (события)
        for event in pygame.event.get():
            # check for closing window
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    log_deck.scroll_up()
                elif event.key == pygame.K_DOWN:
                    log_deck.scroll_down()
                elif event.key == pygame.K_a:
                    log_deck.add_record(f"Player1 A{debug_var} +")
                elif event.key == pygame.K_q:
                    running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if menu_deck.button_exit.is_mouse_on_button():
                    running = False
                elif menu_deck.button_start.is_mouse_on_button():
                    end_game = False
                    player1_deck.renew()
                    player2_deck.renew()
                    comp = ForPlayer(random_mode=True)
                    comp.add_ships()
                    pl = ForPlayer(random_mode=False)
                    pl.add_ships()
                    comp.add_opponent_list(pl.ships.ships_list)
                    pl.add_opponent_list(comp.ships.ships_list)
                    player1_deck.draw_ships(pl.ships.ships_list)
                    player2_deck.draw_ships(comp.ships.ships_list, show=False)
                else:
                    if not comp.turn and not end_game:
                        x, y = event.pos
                        block_to_fire = pl.find_fired_block(x, y)
                        if block_to_fire is not None:
                            is_hit, killed, ind = pl.perform_fire(block_to_fire)
                            if is_hit:
                                comp.ships.ships.discard(block_to_fire)
                                if killed:
                                    comp.killed.append(comp.ships.ships_list[ind])
                            else:
                                comp.turn = True
                            letter = "ABCDEFGHIJ"[block_to_fire[0] - 1]
                            digit = block_to_fire[1]
                            res = '-'
                            if is_hit:
                                res = "+"
                                if killed:
                                    res = "++"
                            log_deck.add_record(f"Player1 {letter}{digit}{res}")

        # Обновление
        all_sprites.update()

        # Отрисовка
        screen.fill(constants.WHITE)
        all_sprites.draw(screen)
        log_deck.draw(screen)
        menu_deck.draw(screen)

        player2_deck.draw_dots(pl.empty_blocks)
        player1_deck.draw_dots(comp.empty_blocks)
        player2_deck.draw_killed(pl.hit_blocks)
        player1_deck.draw_killed(comp.hit_blocks)
        player1_deck.draw_ships(pl.ships.ships_list, pl.killed)
        player2_deck.draw_ships(comp.ships.ships_list, comp.killed, show=False)

        # После отрисовки всего, переворачиваем экран
        pygame.display.flip()

        if comp.turn and not end_game:
            block_to_fire = comp.random_fire()
            is_hit, killed, ind = comp.perform_fire(block_to_fire)
            if is_hit:
                pl.ships.ships.discard(block_to_fire)
                if killed:
                    pl.killed.append(pl.ships.ships_list[ind])
            else:
                comp.turn = False
            letter = "ABCDEFGHIJ"[block_to_fire[0] - 1]
            digit = block_to_fire[1]
            res = '-'
            if is_hit:
                res = "+"
                if killed:
                    res = "++"
            log_deck.add_record(f"Player2 {letter}{digit}{res}")
        if not comp.ships.ships and not end_game:
            end_game = True
        if not pl.ships.ships and not end_game:
            end_game = True

    pygame.quit()