Ejemplo n.º 1
0
def draw(canvas):
    canvas.border(0)
    curses.curs_set(0)
    canvas.nodelay(True)

    rows, columns = canvas.getmaxyx()
    max_row, max_column = rows - 1, columns - 1
    info_window = canvas.derwin(max_row - 1, 2)

    global coroutines
    coroutines += create_stars(canvas, STAR_COUNT)
    coroutines.append(
        run_spaceship(canvas, int(max_row / 2), int(max_column / 2)))
    coroutines.append(animate_spaceship(ship_frame1, ship_frame2, canvas))
    coroutines.append(fill_orbit_with_garbage(canvas, garbage_list))
    coroutines.append(start_time())
    coroutines.append(show_game_info(info_window))
    coroutines.append(show_obstacles(canvas, obstacles))
    while True:
        for coroutine in coroutines.copy():
            try:
                coroutine.send(None)
            except StopIteration:
                coroutines.remove(coroutine)
        canvas.refresh()
        canvas.border(0)
        time.sleep(TIC_TIMEOUT)
Ejemplo n.º 2
0
def initialize_coroutines(game_canvas, info_canvas):
    star_symbols = ['+', '*', '.', ':']

    rows_number, columns_number = game_canvas.getmaxyx()

    stars_to_canvas = [
        (
            random.randint(0, rows_number - 1),
            random.randint(0, columns_number - 1),
            random.choice(star_symbols),
        )
        for _ in range(STARS_QUANTITY)
    ]

    for star in stars_to_canvas:
        coroutines.append(animate_blink(game_canvas, *star))

    coroutines.append(game_time_line.increment_year())
    coroutines.append(game_time_line.show_year(info_canvas))
    coroutines.append(run_spaceship(game_canvas))
    coroutines.append(animate_spaceship())
    coroutines.append(fill_orbit_with_garbage(game_canvas))

    if SHOW_OBSTACLES_BORDERS:
        coroutines.append(show_obstacles(game_canvas, obstacles))

    return coroutines
Ejemplo n.º 3
0
def main(canvas):
    canvas.border()
    canvas.nodelay(True)
    curses.curs_set(0)

    global coroutines
    global obstacles

    create_stars(canvas, count=100)

    central_row, central_column = (max_coordinate // 2
                                   for max_coordinate in canvas.getmaxyx())
    coroutines.append(
        run_spaceship(canvas=canvas,
                      start_row=central_row,
                      start_column=central_column))

    coroutines.append(fill_orbit_with_garbage(canvas))
    coroutines.append(handle_year(TICS_PER_YEAR))
    coroutines.append(show_caption(canvas))
    if SHOW_OBSTACLES:
        coroutines.append((show_obstacles(canvas, obstacles)))

    while True:
        for coroutine in coroutines.copy():
            try:
                coroutine.send(None)
            except StopIteration:
                coroutines.remove(coroutine)
        canvas.refresh()
        time.sleep(TIC_TIMEOUT)
Ejemplo n.º 4
0
def main(canvas, obstacles_visible=False):
    canvas.nodelay(True)
    curses.curs_set(False)
    stars_count = 200
    global year
    global coroutines
    global obstacles
    space_near_border = 2
    max_row, max_column = canvas.getmaxyx()
    sub_canvas_columns = max_column - space_near_border * 2
    sub_canvas_rows = 3
    sub_canvas_start_row = max_row - 5
    sub_canvas_start_column = space_near_border + 1
    sub_canvas = canvas.derwin(sub_canvas_rows, sub_canvas_columns,
                               sub_canvas_start_row, sub_canvas_start_column)
    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),
                  random.randint(1, 20),
                  symbol=choose_star()))
    frame_1 = get_frame('./animations/rocket/rocket_frame_1.txt')
    frame_2 = get_frame('./animations/rocket/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
    coroutines.append(fill_orbit_with_garbage(canvas, max_column))

    spaceship_animation = animate_spaceship(frame_1, frame_2)
    coroutines.append(spaceship_animation)

    spaceship = run_spaceship(canvas, rows_center, columns_center)
    coroutines.append(spaceship)

    year_counter = count_years()
    coroutines.append(year_counter)

    event_printer = print_event(sub_canvas)
    coroutines.append(event_printer)
    if obstacles_visible:
        run_obstacles = show_obstacles(canvas, obstacles)
        coroutines.append(run_obstacles)

    while True:
        for coroutine in coroutines:
            try:
                coroutine.send(None)
            except StopIteration:
                coroutines.remove(coroutine)
            except GameOver:
                return
        canvas.border()
        sub_canvas.refresh()
        canvas.refresh()
        time.sleep(0.1)
Ejemplo n.º 5
0
def draw(canvas):

    global coroutines
    global obstacles
    global obstacles_in_last_collisions
    global year

    curses.curs_set(False)
    canvas.nodelay(1)  #will be non-blocking.

    space_ship_animate = animate_spaceship()
    space_ship_run = run_spaceship(canvas, 15, 20)
    year_control = count_year()
    year_drawing = draw_year_label(canvas)
    labels_control = draw_phrases(canvas)
    garbage_anim = fill_orbit_with_garbage(canvas)
    #score_counter = count_score()
    score_drawer = draw_score(canvas)

    coroutines.append(space_ship_animate)
    coroutines.append(space_ship_run)
    coroutines.append(labels_control)
    coroutines.append(year_drawing)
    coroutines.append(year_control)
    coroutines.append(garbage_anim)
    #coroutines.append(score_counter)
    coroutines.append(score_drawer)
    coroutines = add_stars_coros(canvas)

    #obstacles debug
    if SHOW_OBSTACLES:
        coroutines.append(show_obstacles(canvas, obstacles))
        coroutines.append(show_obstacles(canvas, obstacles_in_last_collisions))
    #run event loop
    while True:
        for coroutine in coroutines:
            try:
                coroutine.send(None)
            except StopIteration:
                coroutines.remove(coroutine)

        canvas.border()
        canvas.refresh()
        time.sleep(0.05)
Ejemplo n.º 6
0
def draw(canvas):
    canvas.border()
    canvas.nodelay(True)

    rocket_frames = load_frames(path=os.path.join('frames', 'rocket'))
    garbage_frames = load_frames(path=os.path.join('frames', 'garbage'))

    if DEBUG_MODE:
        COROUTINES.append(show_obstacles(canvas, OBSTACLES))

    coroutines_garbage = fill_orbit_with_garbage(canvas=canvas,
                                                 garbage_frames=garbage_frames)

    stars, rows, columns, max_rows, max_columns = create_stars_params(
        canvas=canvas, star_num=STAR_NUM)
    center_row, center_column = round(max_rows / 2), round(max_columns / 2)

    coroutines_blink_stars = [
        blink(canvas=canvas,
              row=rows[i],
              column=columns[i],
              symbol=stars[i],
              offset_tics=OFFSET_TICS) for i in range(STAR_NUM)
    ]

    coroutine_ship = run_spaceship(canvas=canvas,
                                   row=center_row,
                                   column=center_column,
                                   frames=[
                                       rocket_frames[0], rocket_frames[0],
                                       rocket_frames[1], rocket_frames[1]
                                   ])

    canvas_for_phrase = canvas.derwin(max_rows - 2, 10)
    coroutine_year_phrase = show_phrase(canvas=canvas_for_phrase)

    temp_coroutines = [
        *coroutines_blink_stars, coroutine_ship, coroutines_garbage,
        change_year(), coroutine_year_phrase
    ]
    COROUTINES.extend(temp_coroutines)

    while COROUTINES:
        for coroutine in COROUTINES.copy():
            try:
                coroutine.send(None)
            except StopIteration:
                COROUTINES.remove(coroutine)
        canvas.border()
        canvas.refresh()
        time.sleep(TIC_TIMEOUT)
Ejemplo n.º 7
0
async def fill_orbit_with_garbage(canvas, garbage, status):

    if SHOW_OBSTACLES_ENABLED:
        status.coroutines.append(show_obstacles(canvas, status.obstacles))

    _, max_column = canvas.getmaxyx()
    while True:
        delay = get_garbage_delay_tics(status.year)
        if delay != None and status.garbage_switcher == True:
            status.coroutines.append(
                fly_garbage(canvas, random.randint(1, max_column - 2),
                            random.choice(garbage), status))
            await sleep(delay)
        else:
            await sleep()
Ejemplo n.º 8
0
def draw(canvas):
    """The main function of displaying all elements on the canvas."""
    global coroutines, obstacles

    canvas.border()
    canvas.nodelay(True)
    curses.curs_set(False)
    canvas_height, canvas_width = canvas.getmaxyx()
    center_row_of_canvas, center_column_of_canvas = (canvas_height // 2,
                                                     canvas_width // 2)
    canvas_for_phrase = canvas.derwin(canvas_height - 2, canvas_width // 2)

    coroutines += create_stars(canvas, canvas_height, canvas_width, 100)

    frames = (
        get_frame('rocket_frame_1.txt'),
        get_frame('rocket_frame_2.txt'),
    )
    trash_frames = (
        get_frame('duck.txt'),
        get_frame('hubble.txt'),
        get_frame('lamp.txt'),
        get_frame('trash_large.txt'),
        get_frame('trash_small.txt'),
        get_frame('trash_xl.txt'),
    )

    coroutines += [
        fire(canvas, center_row_of_canvas, center_column_of_canvas),
        animate_spaceship(frames),
        run_spaceship(canvas, center_row_of_canvas, center_column_of_canvas),
        fill_orbit_with_garbage(canvas, canvas_width, trash_frames),
        count_years(),
        display_info_about_the_current_year(canvas_for_phrase),
    ]

    if DEBUG_MODE:
        coroutines += [show_obstacles(canvas, obstacles)]

    while True:
        for coroutine in coroutines.copy():
            try:
                coroutine.send(None)
            except StopIteration:
                coroutines.remove(coroutine)

        canvas.refresh()
        time.sleep(TIC_TIMEOUT)
Ejemplo n.º 9
0
def draw(canvas):
    curses.curs_set(False)
    canvas.border()
    canvas.refresh()
    canvas.nodelay(True)
    max_row, max_column = get_correct_window_dimensions(canvas)
    center_row, center_col = max_row // 2, max_column // 2

    info_window = canvas.derwin(
        1, INFO_WINDOW_WIDTH, max_row - FRAME_BORDER_SIZE, center_col)

    global coroutines
    coroutines = [blink(canvas,
                        random.randint(1, max_row),
                        random.randint(1, max_column),
                        random.choice(list(STARTS_SIMBOLS)))
                  for _ in range(STARS_NUMBER)]

    coroutines.append(animate_spaceship())
    coroutines.append(run_spaceship(canvas, center_row, center_col))
    coroutines.append(fill_orbit_with_garbage(canvas, max_column))
    coroutines.append(show_messages(info_window))
    coroutines.append(change_years())

    if DEV_MODE:
        coroutines.append(show_obstacles(canvas, obstacles))

    canvas.refresh()
    while True:
        exhausted_coroutines = []
        for coroutine in coroutines:
            try:
                coroutine.send(None)
            except StopIteration:
                exhausted_coroutines.append(coroutine)

        canvas.border()
        canvas.refresh()
        info_window.refresh()
        time.sleep(TIC_TIMEOUT)
        for coroutine_to_remove in exhausted_coroutines:
            coroutines.remove(coroutine_to_remove)
Ejemplo n.º 10
0
def draw(canvas):
    curses.curs_set(False)  # hide cursor

    max_row, max_col = canvas.getmaxyx()
    num_stars = random.randint(MIN_NUM_STARS, MAX_NUM_STARS)

    state.coroutines.append(tick_game_time())
    state.coroutines.append(show_year_info(canvas.derwin(max_row - PADDING,
                                                         0)))
    state.coroutines.append(fire(canvas, max_row // 2, max_col // 2))
    state.coroutines += [
        blink(canvas,
              random.randint(PADDING, max_row - PADDING),
              random.randint(PADDING, max_col - PADDING),
              symbol=random.choice(STARS_SYMBOLS)) for _ in range(num_stars)
    ]

    spaceship_frames = get_frames_from_files(SPACESHIP_FRAMES_PATHS)
    state.coroutines.append(animate_spaceship(spaceship_frames))
    state.coroutines.append(run_spaceship(canvas, max_row // 2, max_col // 2))

    garbage_frames = get_frames_from_files(GARBAGE_FRAMES_PATHS)
    state.coroutines.append(fill_orbit_with_garbage(canvas, garbage_frames))

    if SHOW_OBSTACLES:
        state.coroutines.append(show_obstacles(
            canvas, state.obstacles))  # for debugging purpose
    canvas.nodelay(True)

    while True:
        for coroutine in state.coroutines.copy(
        ):  # using .copy() because list can be modified
            try:
                coroutine.send(None)
            except StopIteration:
                state.coroutines.remove(coroutine)
        canvas.refresh()
        time.sleep(TIC_TIMEOUT)
Ejemplo n.º 11
0
def draw(canvas):
    curs_set(False)
    canvas.nodelay(True)
    canvas.border()
    row, col = get_window_size()
    coroutines.extend([
        blink(canvas, randint(1, row - 2), randint(1, col - 2), choice("+*.:"))
        for _ in range(STARS_AMOUNT)
    ])
    coroutines.append(animate_spaceship())
    coroutines.append(start_gameplay(canvas))
    coroutines.append(run_spaceship(canvas))
    if SHOW_OBSTACLES_BORDERS:
        coroutines.append(show_obstacles(canvas, obstacles))

    while True:
        for coroutine in coroutines:
            try:
                coroutine.send(None)
            except StopIteration:
                coroutines.remove(coroutine)
        canvas.refresh()
        time.sleep(TIC_TIMEOUT)
Ejemplo n.º 12
0
def draw(canvas):
    curses.curs_set(False)
    canvas.nodelay(True)

    rocket_frames = get_frames_from_files('rocket_frames')
    garbage_frames = get_frames_from_files('garbage')

    #  canvas.getmaxyx() return tuple of height and width (https://docs.python.org/2/library/curses.html#curses.window.getmaxyx)
    height, width = canvas.getmaxyx()
    max_x, max_y = height - 1, width - 1

    canvas_for_phrase = canvas.derwin(max_x - 2, max_y // 2)

    COROUTINES.extend(
        [blink(canvas, *get_random_xy(max_x, max_y), random.randint(0, 10), random.choice(SYMBOLS)) for _ in
         range(STARS_AMOUNT)]
    )
    COROUTINES.extend([
        animate_spaceship(rocket_frames),
        run_spaceship(canvas, max_x // 2, max_y // 2),
        fill_orbit_with_garbage(canvas, garbage_frames),
        show_phrase(canvas_for_phrase),
        change_year()
    ])
    if DEBUG:
        COROUTINES.append(show_obstacles(canvas, OBSTACLES))  # добавление рамок для мусора

    while COROUTINES:
        for coroutine in COROUTINES.copy():
            canvas.border()
            try:
                coroutine.send(None)
            except StopIteration:
                COROUTINES.remove(coroutine)

        canvas.refresh()
        time.sleep(TIC_TIMEOUT)
Ejemplo n.º 13
0
async def fill_orbit_with_garbage(canvas):
    """Control space debris."""
    global coroutines, obstacles, year

    garbage_frames = []

    for filename in os.listdir(GARBAGE_FRAMES_DIR):
        with open(os.path.join(GARBAGE_FRAMES_DIR, filename), "r") as garbage_file:
            garbage_frames.append(garbage_file.read())

    rows_number, columns_number = canvas.getmaxyx()

    while True:
        # Create debris coroutines with random garbage type, random column and
        # delay depends on current year. Also create obstacles bounds.
        if get_garbage_delay_tics(year) is not None:
            column = random.randint(0, columns_number)
            garbage_frame = random.choice(garbage_frames)
            garbage_speed = random.randint(2, 10) * GARBAGE_SPEED
            coroutines.append(fly_garbage(canvas, column, garbage_frame, garbage_speed))
            if SHOW_OBSTACLES_BORDERS:
                coroutines.append(show_obstacles(canvas, obstacles))
            await sleep(get_garbage_delay_tics(year))
        await asyncio.sleep(0)
Ejemplo n.º 14
0
def build_game(canvas):
    """ main draw """
    global coroutines, obstacles, obstacles_in_last_collisions
    # sky config
    star_qty = 113
    left_space = 2
    blink_value = 10
    star_symbols = '+*.:'

    # spaceship move speed config
    ship_speed_row = 1  # Y
    ship_speed_column = 1  # X

    # fire speed config
    fire_speed_row = -2.0
    fire_speed_column = 0

    spaceship_files = [
        'animation/rocket_frame_1.txt', 'animation/rocket_frame_2.txt'
    ]
    spaceship_frames = list()
    for file in spaceship_files:
        spaceship_frames.append(load_frame(file))

    garbage_files = [
        'animation/trash_small.txt',
        'animation/trash_large.txt',
        'animation/trash_xl.txt',
        'animation/lamp.txt',
        'animation/hubble.txt',
        'animation/duck.txt',
    ]

    garbage_frames = list()
    for file in garbage_files:
        garbage_frames.append(load_frame(file))

    obstacles = list()
    obstacles_in_last_collisions = list()

    canvas.nodelay(True)
    canvas.border()
    curses.curs_set(False)

    max_row, max_column = canvas.getmaxyx()
    center_row = max_row // 2
    center_col = max_column // 2

    frame_row, frame_column = get_frame_size(spaceship_frames[0])
    ship_center_col = frame_column / 2

    border_row = max_row - frame_row
    border_column = max_column - frame_column

    coroutines = list()
    for star in range(star_qty):
        row = random.randint(left_space, max_row - left_space)
        column = random.randint(left_space, max_column - left_space)
        symbol = random.choice(star_symbols)
        blink_delay = random.randint(0, blink_value)
        coroutines.append(blink(canvas, row, column, blink_delay, symbol))

    ship = animate_spaceship(canvas, center_row, center_col, ship_center_col,
                             spaceship_frames, border_row, border_column,
                             ship_speed_row, ship_speed_column, fire_speed_row,
                             fire_speed_column)
    coroutines.append(ship)

    fill = fill_orbit_with_garbage(canvas, garbage_frames, max_column,
                                   left_space)
    coroutines.append(fill)

    obst = show_obstacles(canvas, obstacles)
    coroutines.append(obst)

    # game engine
    custom_event_loop(canvas)
Ejemplo n.º 15
0
def play_the_game(canvas_init, tic, print_obstacles=False):

    assert tic > 0, AssertionError("Tic interval has to be more that 0")

    border = ord('|')

    # set main canvas
    height_init, width_init = canvas_init.getmaxyx()
    canvas = canvas_init.derwin(height_init - 2, width_init, 0, 0)
    height, width = canvas.getmaxyx()

    # set years canvas
    canvas_year = canvas_init.derwin(height_init - 1, 0)

    # number of starts covers 4% of canvas square
    num_stars = round(height * width * 0.04)

    coroutines = []
    # obstacles
    obstacles = set()
    obstacles_collisions = set()
    years = [1957]

    # fill coroutines
    # years
    coroutines.append(years_increment(years))
    coroutines.append(show_years(canvas_year, years))
    # stars
    coroutines.extend(list(get_stars(canvas, num_stars)))
    # rocket
    coroutines.extend(
        get_rocket_handlers(canvas, coroutines, obstacles,
                            obstacles_collisions, years, 1))
    # garbage handler
    coroutines.append(
        fill_orbit_with_garbage(canvas, coroutines, obstacles,
                                obstacles_collisions, years))
    # print garbage borders
    if print_obstacles:
        coroutines.append(show_obstacles(canvas, obstacles))
    # explosion
    coroutines.append(
        fire(canvas, obstacles, obstacles_collisions, height // 2, width // 2))

    # canvas stuff
    canvas.keypad(True)
    canvas.border(border, border)
    curses.curs_set(False)
    canvas.nodelay(True)

    # loop
    while coroutines:

        for i, coroutine in enumerate(coroutines):

            try:
                coroutine.send(None)

            except StopIteration:
                coroutines.pop(i)

        canvas.refresh()
        canvas_year.refresh()

        time.sleep(tic)
Ejemplo n.º 16
0
def draw(canvas):
    max_y, max_x = canvas.getmaxyx()
    curses.curs_set(False)
    canvas.nodelay(True)

    game_progress_window = canvas.derwin(
        GameProgressWindowSettings.HEIGHT,
        GameProgressWindowSettings.WIDTH,
        GameProgressWindowSettings.get_row_start(max_y),
        GameProgressWindowSettings.get_col_start(),
    )

    global sleeping_events
    global obstacles
    global year

    # STARS
    star_cores = get_star_coroutines(canvas, 300)
    sleeping_events += [[0, star] for star in star_cores]

    # GAME PROGRESS BAR
    sleeping_events.append([0, show_game_progress(game_progress_window)])

    # YEAR TIMER
    sleeping_events.append([0, tick_time()])

    # SPACESHIP
    sleeping_events.append(
        [0, animate_spaceship(canvas, max_y / 2, max_x / 2)])

    # SPACE GARBAGE
    sleeping_events.append([0, fill_orbit_with_garbage(canvas)])

    if DEBUG:
        # SHOW OBSTACLES
        sleeping_events.append([0, show_obstacles(canvas, obstacles)])

    while True:
        min_delay, _ = min(sleeping_events, key=lambda event: event[0])
        _sleeping_events = [[timeout - min_delay, event]
                            for timeout, event in sleeping_events]
        time.sleep(min_delay)

        # split events on active and sleeping
        active_events = [[timeout, event]
                         for timeout, event in _sleeping_events
                         if timeout <= 0]
        _sleeping_events = [[timeout, event]
                            for timeout, event in _sleeping_events
                            if timeout > 0]

        # for save sleeping_events's original global id
        sleeping_events_cores = [event[1] for event in _sleeping_events]
        for event in sleeping_events[:]:
            if event[1] not in sleeping_events_cores:
                sleeping_events.remove(event)

        # recalc sleep time for sleeping events
        for event in sleeping_events:
            for sl_event in _sleeping_events:
                if event[1] == sl_event[1]:
                    event[0] = sl_event[0]

        for i, event in enumerate(active_events[:]):
            try:
                sleep_command = event[1].send(None)
            except StopIteration:
                pass
            else:
                seconds_to_sleep = getattr(sleep_command, 'seconds',
                                           TIC_TIMEOUT)
                sleeping_events.append([seconds_to_sleep, event[1]])

            canvas.refresh()
            game_progress_window.refresh()