Exemple #1
0
    def __init__(self, title: str, color: Tuple[int, int, int], instructions,
                 title_indent_bottom: int, keys_indent_right: int,
                 key_text_font: pygame.font.Font,
                 title_font: pygame.font.Font):
        title_text = title_font.render(title, True, color)

        instruction_surfaces = _get_instruction_surfaces(
            instructions, key_text_font, color, keys_indent_right)
        instruction_surface = get_surfaces_into_column(instruction_surfaces)

        surface = \
            get_surfaces_into_column([title_text, instruction_surface], Alignment.CENTER, title_indent_bottom)

        super().__init__(surface.get_size())
        super().blit(surface, (0, 0))
Exemple #2
0
def draw_bricks():
    global v_y, v_x, score, level, time, lives
    if check_empty_bricks() or level < 3:
        for el in bricks:
            pygame.draw.rect(window, el[2], el[0], border_radius=2)

        for elem in bricks:
            if ball.colliderect(elem[0]):
                el = elem[0]
                if abs(ball.right - el.left) < 30 / k:
                    v_x *= -1
                elif abs(ball.left - el.right) < 30 / k:
                    v_x *= -1
                elif abs(ball.bottom - el.top) < 30 / k and v_y > 0:
                    v_y *= -1
                elif abs(ball.top - el.bottom) < 30 / k and v_y < 0:
                    v_y *= -1

                if elem[1] == 0:
                    score += 5
                    bricks.remove(elem)
                elif elem[1] == 2:
                    if elem[-1] < 1:
                        elem[-1] += 1
                        elem[2] = (0, 255, 0)
                    else:
                        score += 10
                        bricks.remove(elem)

    else:
        run = True
        while run:
            window.fill((0, 0, 0))

            font = pygame.font.Font(f'{resources_path}fonts/font.ttf',
                                    int(80 / k))
            game_over_texts = [
                font.render(data[key], True, (0, 255, 0))
                for key in ("first_won", "second", "third")
            ]
            game_over_texts.insert(
                1, pygame.Surface((100, game_over_texts[0].get_height())))

            game_over_surface = get_surfaces_into_column(
                game_over_texts, Alignment.CENTER)
            game_over_rect = game_over_surface.get_rect(
                center=(screen_width // 2, screen_height // 2))
            window.blit(game_over_surface, game_over_rect)

            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RETURN:
                        main_menu()
                    elif event.key == pygame.K_ESCAPE:
                        run = False
                        quit()
        pygame.quit()
Exemple #3
0
def restart():
    global v_x, v_y, time, lives, brick_types
    if lives <= 0:
        run = True
        while run:
            window.fill((0, 0, 0))

            font = pygame.font.Font(f'{resources_path}fonts/font.ttf',
                                    int(80 / k))
            game_over_texts = [
                font.render(data[key], True, (0, 255, 0))
                for key in ("first_lose", "second", "third")
            ]
            game_over_texts.insert(
                1, pygame.Surface((100, game_over_texts[0].get_height())))

            game_over_surface = get_surfaces_into_column(
                game_over_texts, Alignment.CENTER)
            game_over_rect = game_over_surface.get_rect(
                center=(screen_width // 2, screen_height // 2))
            window.blit(game_over_surface, game_over_rect)

            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RETURN:
                        main_menu()
                    elif event.key == pygame.K_ESCAPE:
                        import main
                        main.main()
        pygame.quit()
    else:
        ball.x, ball.y = player.center[
            0] - ball_size / 2, player.top - ball_size

        current_time = pygame.time.get_ticks()
        if current_time - time < 2100:
            v_x, v_y = 0, 0
        else:
            v_x, v_y = random.choice((-10, 10)), 10
            time = None
Exemple #4
0
def main():
    global grid

    locked_positions = {}  # (x,y):(255,0,0)
    grid = create_grid(locked_positions)

    change_piece = False
    run = True
    current_piece = get_shape()
    next_piece = get_shape()
    clock = pygame.time.Clock()
    fall_time = 0
    speed_up_message_time = 0
    paused = False

    key_left_pressed_time = 0
    key_right_pressed_time = 0
    pressed_time_for_move = 24 // k

    fall_speed_coefficient = 1
    for c in configs:
        if resolution == c.resolution_name:
            fall_speed_coefficient = c.fall_speed_coefficient_in_tetris

    falls_number = 0
    falls_to_stage_up = 6
    fall_speed_stages = [
        i / 70 * fall_speed_coefficient for i in range(14, 3, -2)
    ]

    score = 0

    def get_fall_speed_current_stage():
        return min(falls_number // falls_to_stage_up,
                   len(fall_speed_stages) - 1)

    def get_fall_speed():
        return fall_speed_stages[get_fall_speed_current_stage()]

    is_blocked_key_down = False

    is_speed_up = False
    current_fall_speed = get_fall_speed()
    while run:
        if not paused:
            new_fall_speed = get_fall_speed()
            # If fall speed is changed
            if new_fall_speed != current_fall_speed:
                current_fall_speed = new_fall_speed
                is_speed_up = True
                pygame.mixer.Sound.play(speed_up_sound)
            else:
                if speed_up_message_time >= fps * 6:
                    is_speed_up = False
                    speed_up_message_time = 0

            grid = create_grid(locked_positions)
            fall_time += clock.get_rawtime()

            # PIECE FALLING CODE
            if fall_time / 1000 >= current_fall_speed:
                fall_time = 0
                current_piece.y += 1
                if not (valid_space(current_piece,
                                    grid)) and current_piece.y > 0:
                    current_piece.y -= 1
                    change_piece = True
        else:
            window.fill((0, 0, 0))
            main_menu_surface = get_instruction(data["paused"])
            main_menu_rect = main_menu_surface.get_rect(
                center=(screen_width // 2, screen_height // 2))
            window.blit(main_menu_surface, main_menu_rect)
            pygame.display.update()
            paused = check_pause(paused)
            continue

        def moveShapeLeft():
            current_piece.x -= 1
            if not valid_space(current_piece, grid):
                current_piece.x += 1

        def moveShapeRight():
            current_piece.x += 1
            if not valid_space(current_piece, grid):
                current_piece.x -= 1

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.display.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    moveShapeLeft()
                elif event.key == pygame.K_RIGHT:
                    moveShapeRight()

                elif event.key == pygame.K_UP:
                    # rotate shape
                    current_piece.rotation = current_piece.rotation + 1 % len(
                        current_piece.shape)
                    if not valid_space(current_piece, grid):
                        current_piece.rotation = current_piece.rotation - 1 % len(
                            current_piece.shape)

                if event.key == pygame.K_SPACE:
                    paused = not paused

                if event.key == pygame.K_ESCAPE:
                    import main
                    main.main()
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    key_left_pressed_time = 0
                elif event.key == pygame.K_RIGHT:
                    key_right_pressed_time = 0

        keys = pygame.key.get_pressed()
        if keys[pygame.K_DOWN] and not is_blocked_key_down:
            current_piece.y += 1
            if not valid_space(current_piece, grid):
                current_piece.y -= 1

        elif keys[pygame.K_LEFT]:
            key_left_pressed_time += 1
            if key_left_pressed_time >= pressed_time_for_move:
                moveShapeLeft()
        elif keys[pygame.K_RIGHT]:
            key_right_pressed_time += 1
            if key_right_pressed_time >= pressed_time_for_move:
                moveShapeRight()

        shape_pos = convert_shape_format(current_piece)

        # add piece to the grid for drawing
        for i in range(len(shape_pos)):
            x, y = shape_pos[i]
            if y > -1:
                grid[y][x] = current_piece.color

        # IF PIECE HIT GROUND
        if change_piece:
            for pos in shape_pos:
                p = (pos[0], pos[1])
                locked_positions[p] = current_piece.color
            current_piece = next_piece
            next_piece = get_shape()
            change_piece = False

            # call four times to check for multiple clear rows
            score += clear_rows(grid, locked_positions)

            # fall_speed -= 0.005
            falls_number += 1

            if keys[pygame.K_DOWN]:
                is_blocked_key_down = True

        if not keys[pygame.K_DOWN]:
            is_blocked_key_down = False

        draw_window(window, speed_up_message_time, is_speed_up, score)
        if is_speed_up:
            speed_up_message_time += 1
        draw_next_shape(next_piece, window)
        pygame.display.update()

        # Check if user lost
        if check_lost(locked_positions):
            run = False

        clock.tick(fps)

    window.fill((0, 0, 0))

    font = pygame.font.Font(f'{resources_path}fonts/font.ttf', int(80 / k))
    game_over_texts = [
        font.render(data[key], True, (0, 255, 0))
        for key in ("first", "second", "third")
    ]
    game_over_texts.insert(
        1, pygame.Surface((100, game_over_texts[0].get_height())))

    game_over_surface = get_surfaces_into_column(game_over_texts,
                                                 Alignment.CENTER)
    game_over_rect = game_over_surface.get_rect(center=(screen_width // 2,
                                                        screen_height // 2))
    window.blit(game_over_surface, game_over_rect)
    pygame.display.update()

    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.display.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    import main
                    main.main()
                elif event.key == pygame.K_RETURN:
                    run = False
                    main_menu()