Exemplo n.º 1
0
async def fly_garbage(canvas, column, garbage_frame, obstacles, speed=0.5):
    """
        Animate garbage, flying from top to bottom.
        Column position will stay same, as specified on start.
    """
    from main import obstacles_in_last_collisions

    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number - 1)

    row = 0

    row_size, col_size = get_frame_size(garbage_frame)
    obstacle = Obstacle(row, column, rows_size=row_size, columns_size=col_size)
    obstacles.append(obstacle)

    while row < rows_number:
        obstacle.row = row
        draw_frame(canvas, row, column, garbage_frame)
        await asyncio.sleep(0)
        draw_frame(canvas, row, column, garbage_frame, negative=True)
        row += speed

        if obstacle in obstacles_in_last_collisions:
            obstacles_in_last_collisions.remove(obstacle)
            obstacles.remove(obstacle)
            explode_row, explode_col = get_garbage_center(
                row, column, row_size, col_size
            )
            await explode(canvas, explode_row, explode_col)
            return

    obstacles.remove(obstacle)
Exemplo n.º 2
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5):
    """Animate garbage, flying from top to bottom. Сolumn position will stay same, as specified on start."""
    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number - 1)

    row = 1
    frame_rows, frame_columns = get_frame_size(garbage_frame)

    barrier = Obstacle(row, column, frame_rows, frame_columns)
    obstacles.append(barrier)

    while row < rows_number - frame_rows - 3:  # -3 to save the year info

        barrier.row = row

        draw_frame(canvas, row, column, garbage_frame)
        await asyncio.sleep(0)
        draw_frame(canvas, row, column, garbage_frame, negative=True)

        row += speed

        if barrier in obstacles_in_last_collisions:
            await explode(canvas, row, column)
            obstacles_in_last_collisions.remove(barrier)
            obstacles.remove(barrier)
            return False

    obstacles.remove(barrier)
Exemplo n.º 3
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.2):
    """Animate garbage, flying from top to bottom. Сolumn position will stay same, as specified on start."""

    height, width = canvas.getmaxyx()
    rows_number, columns_number = height, width
    row_size, column_size = get_frame_size(garbage_frame)

    column = max(column, 0)
    column = min(column, columns_number - 1)

    row = 0

    obstacle = Obstacle(row, column, row_size, column_size)
    global_obstacles.append(obstacle)
    while row < rows_number:
        draw_frame(canvas, row, column, garbage_frame)
        await asyncio.sleep(0)

        draw_frame(canvas, row, column, garbage_frame, negative=True)
        if obstacle in global_obstacles_in_last_collisions:
            global_obstacles_in_last_collisions.remove(obstacle)
            await explode(canvas, row + row_size // 2,
                          column + column_size // 2)
            global_obstacles.remove(obstacle)
            return
        row += speed
        obstacle.row = row

    global_obstacles.remove(obstacle)
Exemplo n.º 4
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5):
    """Animate garbage, flying from top to bottom. Сolumn position will stay
    same, as specified on start."""
    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number - 1)

    row = 0

    rows_size, columns_size = get_frame_size(garbage_frame)
    obstacle = Obstacle(row, column, rows_size, columns_size)
    obstacles.append(obstacle)

    try:
        while row < rows_number:
            draw_frame(canvas, row, column, garbage_frame)
            await asyncio.sleep(0)
            draw_frame(canvas, row, column, garbage_frame, negative=True)
            row += speed
            obstacle.row = row

            if obstacle in obstacles_in_last_collisions:
                obstacles_in_last_collisions.remove(obstacle)
                await explode(canvas, row, column)
                return
    finally:
        obstacles.remove(obstacle)
Exemplo n.º 5
0
async def fly_garbage(playground, column, garbage_frame, speed=0.5):
    """
    Animate garbage, flying from top to bottom.
    Column position will stay same, as specified on start.
    """
    min_row, min_col, max_row, max_col = get_playground_limits(playground)

    column = max(column, min_col)
    column = min(column, max_col)

    row = min_row

    rows, cols = get_frame_size(garbage_frame)
    obstacle = Obstacle(row, column, rows, cols)
    obstacles.append(obstacle)
    center_row = round(rows / 2)
    center_col = round(cols / 2)

    while row < max_row:
        if obstacle in obstacles_in_last_collision:
            obstacles.remove(obstacle)
            obstacles_in_last_collision.remove(obstacle)
            del obstacle
            await explode(playground, row + center_row, column + center_col)
            return
        obstacle.row = row
        draw_frame(playground, row, column, garbage_frame)
        await asyncio.sleep(0)
        draw_frame(playground, row, column, garbage_frame, negative=True)
        row += speed
    else:
        obstacles.remove(obstacle)
Exemplo n.º 6
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5):
    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number - 1)

    row = 0
    row_size, column_size = get_frame_size(garbage_frame)
    obstacle = Obstacle(row, column, row_size, column_size)
    obstacles.append(obstacle)

    while row < rows_number:
        draw_frame(canvas, row, column, garbage_frame)
        obstacle.row, obstacle.column = row, column
        await asyncio.sleep(0)
        draw_frame(canvas, row, column, garbage_frame, negative=True)

        if obstacle in obstacles_in_last_collisions:
            obstacles_in_last_collisions.remove(obstacle)
            await explode(canvas, row + row_size // 2, column + column_size // 2)
            break

        row += speed

    obstacles.remove(obstacle)
Exemplo n.º 7
0
async def fly_garbage(canvas, column, garbage_frame, status, speed=0.5):
    """Animate garbage, flying from top to bottom. Сolumn position will stay same, as specified on start."""

    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number - 1)

    row = 0
    rows_size, columns_size = get_frame_size(garbage_frame)
    obstacle_object = Obstacle(row, column, rows_size, columns_size)
    status.obstacles.append(obstacle_object)
    try:
        while row < rows_number:
            if obstacle_object in status.obstacles_in_last_collisions:
                await explode(canvas, row + rows_size // 2,
                              column + columns_size // 2)
                status.obstacles_in_last_collisions.remove(obstacle_object)
                return
            draw_frame(canvas, row, column, garbage_frame)
            await sleep()
            draw_frame(canvas, row, column, garbage_frame, negative=True)
            row += speed
            obstacle_object.row = row
    finally:
        status.obstacles.remove(obstacle_object)
Exemplo n.º 8
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5):
    """Animate garbage, flying from top to bottom.
    Сolumn position will stay same, as specified on start."""
    global obstacles
    global obstacles_in_last_collision
    global coroutines

    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number - 1)
    row = 0
    row_size, column_size = get_frame_size(garbage_frame)
    obstacle = Obstacle(row, column, row_size, column_size)
    obstacles.append(obstacle)

    while row < rows_number:
        if obstacle in obstacles_in_last_collision:
            coroutines.append(
                explode(canvas, row + row_size // 2,
                        column + column_size // 2))
            obstacles_in_last_collision.remove(obstacle)
            break
        draw_frame(canvas, row, column, garbage_frame)
        obstacle.row = row
        obstacle.column = column
        await asyncio.sleep(0)
        draw_frame(canvas, row, column, garbage_frame, negative=True)
        row += speed
    obstacles.remove(obstacle)
Exemplo n.º 9
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5, uid=None):
    """Animate garbage, flying from top to bottom.
     Сolumn position will stay same, as specified on start."""
    rows_number, columns_number = canvas.getmaxyx()
    column = max(column, 0)
    column = min(column, columns_number - 1)
    row = 0
    garbage_rows, garbabe_columns = get_frame_size(garbage_frame)
    global obstacles
    global obstacles_in_last_collisions
    obstacle = Obstacle(row, column, garbage_rows, garbabe_columns)
    obstacles.append(obstacle)
    try:
        while row < rows_number:
            draw_frame(canvas, row, column, garbage_frame)
            await sleep(1)
            draw_frame(canvas, row, column, garbage_frame, negative=True)
            if obstacle in obstacles_in_last_collisions:
                obstacles_in_last_collisions.remove(obstacle)
                await explode(canvas, row + garbage_rows / 2,
                              column + garbabe_columns / 2)
                return
            row += speed
            obstacle.row = row
    finally:
        obstacles.remove(obstacle)
Exemplo n.º 10
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5):
    rows_number, columns_number = canvas.getmaxyx()
    column = max(column, 0)
    column = min(column, columns_number - 1)
    row = 0
    global obstacles, coros
    row_size, col_size = get_frame_size(garbage_frame)
    obstacle = Obstacle(row, column, row_size, col_size)
    obstacles.append(obstacle)

    try:
        while row < rows_number:
            if obstacle in obstacles_in_last_collisions:
                obstacles.remove(obstacle)
                await explode(canvas, row, column)
                return None
            else:
                draw_frame(canvas, row, column, garbage_frame)
                obstacle.row = row
                await asyncio.sleep(0)
                draw_frame(canvas, row, column, garbage_frame, negative=True)
                row += speed
    finally:
        if obstacle in obstacles:
            obstacles.remove(obstacle)
Exemplo n.º 11
0
async def fly_garbage(canvas, column: int, garbage_frame: TrashFrame,
                      obstacle_id: int, obstacles: dict,
                      destoyed_obstacle_ids: set):
    """
    Animate garbage, flying from top to bottom.
    Сolumn position will stay same, as specified on start.
    """
    rows_number, columns_number = canvas.getmaxyx()

    corrected_column = column - garbage_frame.width - 1
    column = min(max(1, corrected_column), column)

    obstacles[obstacle_id] = Obstacle(0, column, garbage_frame.height,
                                      garbage_frame.width)

    speed = 0.01 + random()

    row = 0
    while row < rows_number:
        draw_frame(canvas, row, column, garbage_frame.frame)
        await asyncio.sleep(0)
        draw_frame(canvas, row, column, garbage_frame.frame, negative=True)

        if obstacle_id in destoyed_obstacle_ids:
            destoyed_obstacle_ids.remove(obstacle_id)
            center_row = row + int(garbage_frame.height / 2)
            center_col = column + int(garbage_frame.width / 2)
            await explode(canvas, center_row, center_col)
            break

        row += speed
        obstacles[obstacle_id].row = row

    del obstacles[obstacle_id]
Exemplo n.º 12
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5):
    """Animate garbage, flying from top to bottom. Сolumn position will stay same, as specified on start."""
    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number - 1)
    obstacle_height, obstacle_width = get_frame_size(garbage_frame)
    obstacle = Obstacle(
        0,
        column,
        rows_size=obstacle_height,
        columns_size=obstacle_width
    )
    obstacles.append(obstacle)

    row = 0

    while row < rows_number and obstacle not in obstacles_in_last_collisions:
        draw_frame(canvas, row, column, garbage_frame)
        await asyncio.sleep(0)
        draw_frame(canvas, row, column, garbage_frame, negative=True)
        row += speed
        obstacle.row = row

    if obstacle in obstacles_in_last_collisions:
        center_obstacle_row = row + obstacle_height / 2
        center_obstacle_column = column + obstacle_width / 2
        await explode(canvas, center_obstacle_row, center_obstacle_column)
        obstacles_in_last_collisions.remove(obstacle)

    obstacles.remove(obstacle)
Exemplo n.º 13
0
async def fly_garbage(canvas,
                      obstacles,
                      obstacles_collisions,
                      column,
                      garbage_frame,
                      speed=0.5):
    """Animate garbage, flying from top to bottom. Column position will stay same, as specified on start.

    E.g.
        with open('garbage.txt', "r") as garbage_file:
            frame = garbage_file.read()

        coroutine = fly_garbage(canvas, 10, frame):
    """

    assert speed > 0, AssertionError("Speed has to be positive")
    assert len(garbage_frame) and all(garbage_frame), AssertionError(
        "Frame can not be empty or has 0 height or width")

    frame, frame_height, frame_width = garbage_frame

    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number - 1)
    row = 0

    obstacle = Obstacle(row, column, frame_height, frame_width)
    obstacles.add(obstacle)

    while row < rows_number:

        if obstacle in obstacles_collisions:
            draw_frame(canvas, row, column, frame, negative=True)

            await explode(canvas, row, column)

            break

        draw_frame(canvas, row, column, frame)

        await asyncio.sleep(0)

        draw_frame(canvas, row, column, frame, negative=True)

        row += speed

        obstacle.row = row

    # delete obstacle
    obstacles.discard(obstacle)

    # restore borders
    border = ord('|')
    canvas.border(border, border)
Exemplo n.º 14
0
async def fly_garbage(canvas,
                      garbage_frame,
                      obstacles,
                      collisions,
                      is_running,
                      window_size,
                      border_depth,
                      speed=0.5):
    """
    Animate garbage, flying from top to bottom. Сolumn position will stay same,
    as specified on start.
    """
    _, window_size_x = window_size

    rows_number, columns_number = canvas.getmaxyx()

    frame_size = get_frame_size(garbage_frame)
    frame_size_y, frame_size_x = frame_size

    row = 1

    column = random.randint(border_depth + frame_size_x,
                            window_size_x - border_depth - frame_size_x)

    obstacle = Obstacle(row, column, frame_size_y, frame_size_x)
    obstacles.append(obstacle)

    try:
        while row < rows_number - frame_size_y - border_depth:
            draw_frame(canvas, row, column, garbage_frame)
            obstacle.row = row
            await asyncio.sleep(0)
            draw_frame(canvas, row, column, garbage_frame, negative=True)
            row += speed

            if obstacle in collisions or not is_running.state:
                await explode(canvas, row + median([0, frame_size_y]),
                              column + median([0, frame_size_x]))
                return
    finally:
        obstacles.remove(obstacle)
Exemplo n.º 15
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5):
    """Animate garbage, flying from top to bottom. Сolumn position
    will stay same, as specified on start."""

    global obstacles, obstacles_in_last_collisions, score

    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number - 1)

    # Draw obstacle for collisions checks.
    obstacle_width, obstacle_height = get_frame_size(garbage_frame)
    obstacle = Obstacle(0, column, obstacle_width, obstacle_height)
    obstacles.append(obstacle)

    row = 0

    try:
        while row < rows_number:
            # Animate a garbage piece.
            draw_frame(canvas, row, column, garbage_frame)
            await asyncio.sleep(0)
            draw_frame(canvas, row, column, garbage_frame, negative=True)
            obstacle.row = row
            row += speed

            # Check for collision.
            if obstacle in obstacles_in_last_collisions:
                obstacles.remove(obstacle)
                await explode(canvas, row + obstacle_height / 2, column + obstacle_width / 2)
                score += 1
                obstacles_in_last_collisions.remove(obstacle)
                break
    finally:
        if obstacle in obstacles:
            obstacles.remove(obstacle)
Exemplo n.º 16
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5):
    """Animate garbage, flying from top to bottom. Сolumn position will stay same, as specified on start."""
    rows_number, columns_number = canvas.getmaxyx()
    height, width = get_frame_size(garbage_frame)

    column = max(column, 0)
    column = min(column, columns_number - 1)

    row = 0
    obstacle = Obstacle(0, column, height, width)
    obstacles.append(obstacle)
    try:
        while row < rows_number:
            draw_frame(canvas, row, column, garbage_frame)
            await sleep()
            draw_frame(canvas, row, column, garbage_frame, negative=True)
            row += speed
            obstacle.row = row
            if obstacle in obstacles_in_last_collisions:
                await explode(canvas, row + height // 2, column + width // 2)
                obstacles_in_last_collisions.remove(obstacle)
                break
    finally:
        obstacles.remove(obstacle)
Exemplo n.º 17
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5):
    """Animate garbage, flying from top to bottom. Сolumn position will stay same, as specified on start."""

    global obstacles
    global obstacles_in_last_collisions

    rows_number, columns_number = canvas.getmaxyx()

    column = max(column, 0)
    column = min(column, columns_number - 1)

    row = 0

    frame_rows, frame_columns = get_frame_size(garbage_frame)
    obs = Obstacle(row, column, frame_rows, frame_columns)
    obstacles.append(obs)

    while row < rows_number:

        draw_frame(canvas, row, column, garbage_frame)

        await asyncio.sleep(0)
        draw_frame(canvas, row, column, garbage_frame, negative=True)
        #if obstacle in obstacles_in_last_collisions - obstacle removed, coro stops
        if obs in obstacles_in_last_collisions:
            obstacles_in_last_collisions.remove(obs)
            obstacles.remove(obs)
            await explode(canvas, row, column)
            return

        row += speed
        obs.row = row
    try:
        obstacles.remove(obs)
    except ValueError:
        pass
Exemplo n.º 18
0
async def draw_rocket(
    state,
    canvas,
    start_row,
    start_column,
    border,
    negative=True,
    speed_boost=1,
):
    row, column = (start_row, start_column)
    row_speed = column_speed = 0
    frames = get_rocket_frames_iter()
    rows, columns = get_frame_size(next(frames))
    rocket = Obstacle(row, column, rows, columns)

    for frame in frames:
        rocket.row = row
        rocket.column = column
        for obstacle in state["obstacles"]:
            if rocket.has_collision(
                obstacle.row,
                obstacle.column,
                obstacle.rows_size,
                obstacle.columns_size,
            ):
                state["routines"].append(explode(canvas, row, column))
                state["routines"].append(end(canvas))
                return
        draw_frame(canvas, row, column, frame)
        await sleep()
        draw_frame(canvas, row, column, frame, negative=True)

        row_delta, column_delta, space = read_controls(canvas)
        if space and state["year"] >= 2020:
            state["routines"].append(fire(state, canvas, row, column + 2))

        frame_rows, frame_columns = get_frame_size(frame)

        row_speed, column_speed = update_speed(
            row_speed,
            column_speed,
            row_delta,
            column_delta,
        )

        if row_delta == -1:
            row = max(border["top"], row + row_speed)
        elif row_delta == 1:
            row = min(border["bottom"] - frame_rows, row + row_speed)
        elif row_delta == 0:
            row = min(border["bottom"] - frame_rows, row + row_speed)
            row = max(border["top"], row)

        if column_delta == 1:
            column = min(
                border["right"] - frame_columns,
                column + column_speed,
            )
        elif column_delta == -1:
            column = max(border["left"], column + column_speed)
        elif column_delta == 0:
            column = max(border["left"], column + column_speed)
            column = min(
                border["right"] - frame_columns,
                column,
            )