Example #1
0
async def run_spaceship(canvas, row, column):
    """ controls location of spaceship and draws it on the screen"""
    row_speed = column_speed = 0
    global spaceship_frame
    global coroutines
    global year
    spaceship_collision = False
    while not spaceship_collision:
        maxx, maxy = canvas.getmaxyx()
        rows_direction, columns_direction, space_pressed = read_controls(
            canvas)
        row_speed, column_speed = update_speed(row_speed, column_speed,
                                               rows_direction,
                                               columns_direction)
        frame_rows, frame_columns = get_frame_size(spaceship_frame)
        row, column = speed_reduction_near_border(row, column, frame_rows,
                                                  frame_columns, maxx, maxy,
                                                  row_speed, column_speed)

        draw_frame(canvas, row, column, spaceship_frame)
        last_frame = spaceship_frame

        frame_rows, frame_columns = get_frame_size(spaceship_frame)
        await sleep(1)
        if space_pressed and (year >= 2020):
            coroutines.append(fire(canvas, row, column + 2))

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

        if if_collision_append_collision_list(row, column, frame_rows,
                                              frame_columns):
            spaceship_collision = True
    coroutines.append(
        explode(canvas, row + frame_rows // 2, column + frame_columns // 2))
    coroutines.append(show_gameover(canvas))
async def show_game_over(canvas, window_size):
    with open('frames/game_over.txt', 'r') as file:
        frame = file.read()

    window_size_y, window_size_x = window_size
    frame_size = get_frame_size(frame)
    frame_size_y, frame_size_x = frame_size

    window_center_x = median([
        0,
        window_size_x,
    ])
    frame_width_half = median([
        0,
        frame_size_x,
    ])
    text_start_position_x = window_center_x - frame_width_half
    column = text_start_position_x

    window_center_y = median([
        0,
        window_size_y,
    ])
    frame_height_half = median([
        0,
        frame_size_y,
    ])
    text_start_position_y = window_center_y - frame_height_half
    row = text_start_position_y
    while True:
        draw_frame(canvas, row, column, frame)
        await asyncio.sleep(0)
Example #3
0
async def starship_animation(canvas, start_row, start_column, images):
    for image in cycle(images):
        image_row, image_col = get_frame_size(image)
        rows_number, columns_number = canvas.getmaxyx()

        row_bottom = rows_number - image_row
        col_right = columns_number - image_col

        rows_direct, columns_direct, space_pressed = read_controls(canvas)
        start_row = start_row + rows_direct
        start_column = start_column + columns_direct

        if max(start_row, image_row) == image_row:
            start_row = 0
        if min(start_row, row_bottom) == row_bottom:
            start_row = row_bottom
        if max(start_column, image_col) == image_col:
            start_column = 0
        if min(start_column, col_right) == col_right:
            start_column = col_right

        draw_frame(canvas, start_row, start_column, image, negative=False)
        canvas.refresh()
        await asyncio.sleep(0)
        draw_frame(canvas, start_row, start_column, image, negative=True)
Example #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 = 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)
Example #5
0
async def show_gameover(canvas, window_height, window_width, frame):
    message_size_y, message_size_x = get_frame_size(frame)
    message_pos_y = round(window_height / 2) - round(message_size_y / 2)
    message_pos_x = round(window_width / 2) - round(message_size_x / 2)
    while True:
        draw_frame(canvas, message_pos_y, message_pos_x, frame)
        await asyncio.sleep(0)
Example #6
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)
Example #7
0
def get_garbage_animation_size(*garbage_animations):
    animation_sizes = [
        get_frame_size(garbage_animation)
        for garbage_animation in garbage_animations
    ]
    rows_sizes, columns_sizes = zip(*animation_sizes)
    return max(rows_sizes), max(columns_sizes)
Example #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."""
    rows_number, columns_number = canvas.getmaxyx()

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

    row = 0
    obstacle_row, obstacle_column = get_frame_size(garbage_frame)
    obstacle = Obstacle(row, column, obstacle_row, obstacle_column)
    obstacles.append(obstacle)
    # coroutines.append(show_obstacles(canvas, obstacles))

    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 += speed
        if obstacle in obstacles_in_last_collision:
            obstacles.remove(obstacle)
            obstacles_in_last_collision.remove(obstacle)
            await explode(canvas, row + obstacle_row // 2,
                          column + obstacle_column // 2)
            return
    else:
        obstacles.remove(obstacle)
Example #9
0
async def fly_garbage(canvas, column, garbage_frame, speed=0.5):
    """Animate garbage, flying from top to bottom. Column position will stay the same, as specified on start."""

    rows_number, columns_number = canvas.getmaxyx()

    garbage_current_row = 0

    garbage_rows_takes, garbage_columns_takes = get_frame_size(garbage_frame)

    column = max(column, 0)
    garbage_current_column = min(column, columns_number - garbage_columns_takes)

    current_obstacle = Obstacle(garbage_current_row, garbage_current_column, garbage_rows_takes, garbage_columns_takes)

    obstacles.append(current_obstacle)

    try:
        while garbage_current_row < rows_number:
            if current_obstacle in obstacles_in_last_collisions:
                obstacles_in_last_collisions.remove(current_obstacle)
                return

            draw_frame(canvas, garbage_current_row, garbage_current_column, garbage_frame)
            await asyncio.sleep(0)
            draw_frame(canvas, garbage_current_row, garbage_current_column, garbage_frame, negative=True)
            garbage_current_row += speed
            current_obstacle.row += speed
    finally:
        obstacles.remove(current_obstacle)
Example #10
0
async def run_spaceship(canvas, canvas_vertical_center, canvas_horizontal_center, frames):
    row, column = canvas_vertical_center, canvas_horizontal_center
    frame_rows, frame_columns = get_frame_size(frames[0])
    canvas_rows, canvas_column = canvas.getmaxyx()
    row_speed, column_speed = 0, 0
    while True:
        rows_direction, columns_direction, space_pressed = read_controls(canvas)

        row_speed, column_speed = update_speed(row_speed, column_speed, rows_direction, columns_direction)

        row = max(1, min(row + row_speed, canvas_rows - frame_rows - 1))
        column = max(1, min(column + column_speed, canvas_column - frame_columns - 1))

        previous_spaceship_frame = spaceship_frame

        draw_frame(canvas, row, column, spaceship_frame, negative=False)

        plasma_gun_activation_year = 2020

        if year > plasma_gun_activation_year and space_pressed:
            coroutines.append(fire(canvas, row, column + 2))

        await asyncio.sleep(0)

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

        for index, obstacle in enumerate(obstacles.copy()):
            # add show_gameover coroutine if spaceship meet garbage
            if obstacle.has_collision(row, column):
                coroutines.append(
                    show_gameover(canvas, canvas_vertical_center, canvas_horizontal_center)
                )

                return
Example #11
0
async def fly_garbage(canvas,
                      column,
                      garbage_frame,
                      garbage_id,
                      obstacles,
                      obstacles_in_last_collisions,
                      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()
    frame_rows, frame_columns = get_frame_size(garbage_frame)

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

    row = 0
    while row < rows_number and garbage_id 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
        update_obstacle(obstacles, row, column, frame_rows, frame_columns,
                        garbage_id)
    else:
        remove_obstacle(obstacles, garbage_id)
        if garbage_id in obstacles_in_last_collisions:
            obstacles_in_last_collisions.remove(garbage_id)
            await explode(canvas, row, column)
Example #12
0
async def run_spaceship(canvas, start_row, start_col):
    row, column = start_row, start_col
    row_speed, column_speed = 0, 0
    min_row, min_column = FRAME_BORDER_SIZE, FRAME_BORDER_SIZE
    max_row, max_column = get_correct_window_dimensions(canvas)

    while True:
        temp_spaceship_frame = spaceship_frame
        # frame_size = get_frame_size(temp_spaceship_frame)
        rows, columns = get_frame_size(temp_spaceship_frame)
        move_row, move_column, space_pressed = read_controls(canvas)
        row_speed, column_speed = update_speed(
            row_speed, column_speed, move_row, move_column)
        row, column = row + row_speed, column + column_speed

        column = min(max(min_column, column), max_column -
                     columns - FRAME_BORDER_SIZE)
        row = min(max(min_row, row), max_row -
                  rows - FRAME_BORDER_SIZE)

        collisions = [obstacle
                      for obstacle in obstacles if obstacle.has_collision(row, column, rows, columns)]
        if collisions:
            await show_gameover(canvas, start_row, start_col)

        if space_pressed and year >= PLASMA_GUN_ERA:
            coroutines.append(
                fire(canvas, row, column + columns // 2))

        draw_frame(canvas, row, column, temp_spaceship_frame)

        await sleep()
        draw_frame(canvas, row, column, temp_spaceship_frame, negative=True)
Example #13
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)
Example #14
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

    rows_number, columns_number = canvas.getmaxyx()

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

    row = 0

    frame_height, frame_width = get_frame_size(garbage_frame)
    obstacle = Obstacle(row, column, frame_height, frame_width)
    obstacles.append(obstacle)

    await sleep(1)
    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 += speed

        # Remove garbage that has encountered fire
        if obstacle in obstacles_in_last_collisions:
            obstacles_in_last_collisions.remove(obstacle)
            obstacles.remove(obstacle)
            await explode(canvas, row, column)
            return

    # Remove an obstacle flying over the edge
    obstacles.remove(obstacle)
Example #15
0
async def animate_spaceship(canvas, row, column):
    with open("frames/rocket_frame_1.txt") as file1:
        frame1 = file1.read()

    with open("frames/rocket_frame_2.txt") as file2:
        frame2 = file2.read()

    frame_rows, frame_columns = get_frame_size(frame1)

    max_row, max_column = canvas.getmaxyx()

    while True:
        rows_direction, columns_direction, space_pressed = read_controls(
            canvas)
        if check_ship_inside_border(current_coordinate=row,
                                    step=rows_direction,
                                    frame_size=frame_rows,
                                    border=max_row):
            row += rows_direction
        if check_ship_inside_border(current_coordinate=column,
                                    step=columns_direction,
                                    frame_size=frame_columns,
                                    border=max_column):
            column += columns_direction
        draw_frame(canvas, row, column, frame1)

        await asyncio.sleep(0)

        draw_frame(canvas, row, column, frame1, negative=True)
        draw_frame(canvas, row, column, frame2)

        await asyncio.sleep(0)

        draw_frame(canvas, row, column, frame2, negative=True)
Example #16
0
async def fill_orbit_with_garbage(canvas, coroutines, obstacles,
                                  obstacles_collisions, years):

    assert bool(years), AssertionError(
        "Years has to be initiated with int value.")
    assert years[0] >= 1957, AssertionError(
        "Years has to be at least 1957 and more.")

    _, width = canvas.getmaxyx()
    width -= 1

    garbage_frames = [(i, *get_frame_size(i))
                      for i in get_frames("frames/garbage/*.txt")]

    # waiting for garbage epoch
    while get_garbage_delay_tics(years[0]) is None:
        await asyncio.sleep(0)

    while True:

        frame = choice(garbage_frames)
        _, _, frame_width = frame
        column = randint(1, width - frame_width)
        speed = randint(1, 4) / 10

        coroutines.append(
            fly_garbage(canvas, obstacles, obstacles_collisions, column, frame,
                        speed))

        # waiting before adding new one
        await sleep_for(get_garbage_delay_tics(years[0]))
Example #17
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)
Example #18
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)
Example #19
0
async def control_spaceship(canvas, rocket_frames, row, column):
    global is_collision
    rocket_height, rocket_wide = get_frame_size(rocket_frames[0])
    rocket_frames = cycle(rocket_frames)
    row_speed = column_speed = 0

    while True:
        rocket_frame = next(rocket_frames)
        rows_direction, columns_direction, space_pressed = read_controls(
            canvas)
        if space_pressed and year >= 2020:
            coroutines.append(fire(canvas, row - 1, column + rocket_wide // 2))
        row_speed, column_speed = update_speed(row_speed, column_speed,
                                               rows_direction,
                                               columns_direction)
        row += row_speed
        column += column_speed

        if column <= 1 or column >= SCREEN_WIDE - rocket_wide:
            column -= column_speed
        if row <= 1 or row >= SCREEN_HEIGHT - rocket_height:
            row -= row_speed
        coroutines.append(draw_rocket(canvas, rocket_frame, row, column))

        for obstacle in obstacles:
            if obstacle.has_collision(row - 1, column):
                obstacles_in_last_collision.append(obstacle)
                await explode(canvas, row + 5, column + 2)
                is_collision = True
                coroutines.append(
                    show_gameover(canvas, SCREEN_HEIGHT // 2,
                                  SCREEN_WIDE // 2))
                return
        await asyncio.sleep(0)
Example #20
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)

    row = 0

    garbage_rows_size, garbage_columns_size = get_frame_size(garbage_frame)

    obstacle = Obstacle(row, column, garbage_rows_size, garbage_columns_size)
    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)

            if obstacle in hit_obstacles:
                await explode(canvas, obstacle.center_row,
                              obstacle.center_column)
                return

            row += speed
            obstacle.row = row
    finally:
        obstacles.remove(obstacle)
        if obstacle in hit_obstacles:
            hit_obstacles.remove(obstacle)
Example #21
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)
Example #22
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)
Example #23
0
async def fill_orbit_with_garbage(canvas,
                                  coros,
                                  garbage_frames,
                                  level,
                                  initial_timeout=5,
                                  complexity_factor=5,
                                  timeout_min=0.3):

    _, columns_number = canvas.getmaxyx()
    border_size = 1
    while True:
        current_trash_frame = random.choice(garbage_frames)
        _, trash_column_size = get_frame_size(current_trash_frame)
        random_column = random.randint(border_size,
                                       columns_number - border_size)
        actual_column = min(
            columns_number - trash_column_size - border_size,
            random_column + trash_column_size - border_size,
        )

        trash_coro = fly_garbage(canvas, actual_column, current_trash_frame)
        coros.append(trash_coro)
        timeout_step = level[0] / complexity_factor
        garbage_respawn_timeout = initial_timeout - timeout_step

        if garbage_respawn_timeout <= timeout_min:
            garbage_respawn_timeout = timeout_min
        await sleep(garbage_respawn_timeout)
Example #24
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
    garbage_row, garbage_column = get_frame_size(garbage_frame)

    while row < rows_number:

        draw_frame(canvas, row, column, garbage_frame)
        obstacles.append(Obstacle(row, column, garbage_row, garbage_column))
        await asyncio.sleep(0)
        draw_frame(canvas, row, column, garbage_frame, negative=True)
        obstacles.pop(0)

        # for obst in obstacles_in_last_collisions:
        #     return obst

        row += speed
Example #25
0
async def fly_garbage(canvas, column, garbage_frame, obs_id, speed=0.5):
    """ Animate garbage, flying from top to bottom. 
    Сolumn position will stay same, as specified on start."""

    frame_row, frame_column = get_frame_size(garbage_frame)

    rows_number, columns_number = canvas.getmaxyx()
    column = max(column, 0)
    column = min(column, columns_number - frame_column - 1)

    row = 1

    obs = Obstacle(row, column, frame_row, frame_column)
    obstacles[obs_id] = obs

    try:
        while row < rows_number:
            draw_frame(canvas, obs.row, obs.column, garbage_frame)
            await asyncio.sleep(0)
            draw_frame(canvas,
                       obs.row,
                       obs.column,
                       garbage_frame,
                       negative=True)
            obs.row += speed
        else:
            obstacles.pop(obs_id)
    except asyncio.CancelledError:
        draw_frame(canvas, obs.row, obs.column, garbage_frame, negative=True)
        coroutines.append(
            explode(canvas, obs.row + round(frame_row / 2),
                    obs.column + round(frame_column / 2)))
        obstacles.pop(obs_id)
        obstacles_to_stop.remove(obs_id)
        return
Example #26
0
async def run_spaceship(canvas, frames):
    height, width = get_frame_size(frames[0])
    row_max, col_max = get_canvas_max_coords(canvas)
    row = row_max - height // 2
    col = col_max // 2 - width // 2
    row_speed = col_speed = 0
    cur_frame = None

    while True:
        for obstacle in obstacles:
            if obstacle.has_collision(row, col):
                draw_frame(canvas, row, col, cur_frame, negative=True)
                await show_gameover(canvas)

        row_change, col_change, shot_made = read_controls(canvas)

        if shot_made and year >= YEAR_GUN_ON:
            fire = animate_fire(canvas, row, col + width // 2)
            coroutines.append(fire)

        row_speed, col_speed = update_speed(row_speed, col_speed, row_change, col_change)

        row += row_speed
        col += col_speed
        row = min(row, row_max - height)
        col = min(col, col_max - width)
        row = max(row, BORDER)
        col = max(col, BORDER)

        draw_frame(canvas, row, col, spaceship_frame)
        cur_frame = spaceship_frame
        await sleep()
        draw_frame(canvas, row, col, cur_frame, negative=True)
Example #27
0
def main(canvas):
    canvas.border()
    canvas.nodelay(True)
    curses.curs_set(False)
    stars_count = 100
    coroutines = []
    max_row, max_column = canvas.getmaxyx()
    space_near_border = 2
    for _ in range(stars_count):
        coroutines.append(
            blink(canvas,
                  random.randint(space_near_border,
                                 max_row - space_near_border),
                  random.randint(space_near_border,
                                 max_column - space_near_border),
                  symbol=choose_star()))
    frame_1 = get_frame('./animations/rocket_frame_1.txt')
    frame_2 = get_frame('./animations/rocket_frame_2.txt')
    spaceship_width, spaceship_height = get_frame_size(frame_1)
    rows_center = max_row // 2 - spaceship_width // 2
    columns_center = max_column // 2 - spaceship_height // 2
    spaceship = animate_spaceship(canvas, frame_1, frame_2, rows_center,
                                  columns_center)
    coroutines.append(spaceship)
    loop = 0
    loops_count = 100
    while loop < loops_count:
        loop += 1
        for coroutine in coroutines:
            coroutine.send(None)
        canvas.refresh()
        time.sleep(0.1)
Example #28
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)
Example #29
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)
    row = 1
    height, width = curses_tools.get_frame_size(garbage_frame)
    obstacle = Obstacle(row, column, rows_size=height, columns_size=width)
    _, bounding_column, bounding_frame = obstacle.dump_bounding_box()

    state.obstacles.append(obstacle)

    while obstacle.row < rows_number:
        # curses_tools.draw_frame(canvas, obstacle.row - 1, bounding_column, bounding_frame)
        curses_tools.draw_frame(canvas, obstacle.row, column, garbage_frame)
        await asyncio.sleep(0)
        curses_tools.draw_frame(canvas,
                                obstacle.row,
                                column,
                                garbage_frame,
                                negative=True)
        # curses_tools.draw_frame(canvas, obstacle.row - 1, bounding_column, bounding_frame, negative=True)
        obstacle.row += speed

        if obstacle in state.obstacles_in_last_collisions.copy():
            state.obstacles_in_last_collisions.remove(obstacle)
            state.obstacles.remove(obstacle)

            state.coroutines.append(
                explode(canvas, obstacle.row + height // 2,
                        column + width // 2))

            return
async def run_spaceship(canvas):
    # Положение корабля и отрисовка
    global spaceship_frame
    max_row, max_col = canvas.getmaxyx()
    frame_max_row, frame_max_col = get_frame_size(load_frames("rocket")[0])
    row = max_row - (frame_max_row + 1)
    col = max_col // 2
    row_speed = col_speed = 0
    while True:
        drow, dcol, space = read_controls(canvas)
        border = 1
        row_speed, col_speed = update_speed(row_speed, col_speed, drow, dcol)

        if border < (col + col_speed) < (max_col - frame_max_col - border):
            col += col_speed
        if border < (row + row_speed) < (max_row - frame_max_row - border):
            row += row_speed

        if space and (globalvars.year >= 2020):
            coroutines.append(fire(canvas, row, col + 2))

        for obstacle in obstacles:
            if obstacle.has_collision(row, col):
                coroutines.append(show_gameover(canvas))
                return

        draw_frame(canvas, row, col, spaceship_frame)
        old_frame = spaceship_frame
        await asyncio.sleep(0)
        draw_frame(canvas, row, col, old_frame, negative=True)