async def fill_orbit_with_garbage(canvas): global coroutines global obstacles global year while True: current_year = year.get('current_year') await_time = utils.get_garbage_delay_tics(current_year) await utils.wait_time(await_time) garbage = get_trash(canvas) coroutines.append(garbage)
async def run_spaceship(canvas): global year global spaceship_frame global coroutines global obstacles global spaceship_frame max_available_row, max_available_column = utils.get_terminal_size() row, column = max_available_row - 10, max_available_column / 2 row_speed = column_speed = 0 while True: current_year = year.get('current_year') row_frame, column_frame = get_frame_size(spaceship_frame) prev_sprite_row, prev_sprite_column = row, column prev_spaceship_frame = spaceship_frame canvas.nodelay(True) row_pos, column_pos, space = read_controls(canvas) row_speed, column_speed = update_speed(row_speed, column_speed, row_pos, column_pos) row += row_pos + row_speed column += column_pos + column_speed if space and current_year >= 2020: # for gun position in the center of the spaceship column_for_fire = column + 2 fire_animation = fire(canvas, row, column_for_fire, rows_speed=FIRE_SPEED) coroutines.append(fire_animation) row = correct_row(max_available_row, row, row_frame) column = correct_column(max_available_column, column, column_frame) for obstacle in obstacles: if obstacle.has_collision(row, column): draw_frame(canvas, prev_sprite_row, prev_sprite_column, prev_spaceship_frame, negative=True) coroutines.append(utils.show_gameover(canvas)) return await asyncio.sleep(0) draw_frame(canvas, prev_sprite_row, prev_sprite_column, prev_spaceship_frame, negative=True) draw_frame(canvas, row, column, spaceship_frame, negative=False)
def add_stars_coros(canvas): #def add_stars_coros(canvas): '''Function adds to coroutines list blinking stars coroutines.''' global coroutines y_max, x_max = canvas.getmaxyx() #stars should not intersect window border #max values decreased border_offset = 3 y_max -= border_offset x_max -= border_offset val = int(x_max * y_max / 20) # define number of stars for i in range(val): row = random.randint(2, y_max) column = random.randint(2, x_max) symbol = random.choice('+:*.') coroutines.append(blink(canvas, row, column, symbol)) return coroutines
def main(canvas, rocket_frame_1, rocket_frame_2): global coroutines global year curses.curs_set(False) year_change = change_year_data(canvas) coroutines.append(year_change) spaceship_frame = animate_spaceship([rocket_frame_1, rocket_frame_2]) coroutines.append(spaceship_frame) spaceship = run_spaceship(canvas) coroutines.append(spaceship) stars = get_stars(canvas, STARS_COUNT) coroutines += stars while True: for coro in coroutines: try: coro.send(None) except StopIteration: coroutines.remove(coro) except _curses.error: continue utils.refresh_draw(canvas) time.sleep(TIC_TIMEOUT)
async def change_year_data(canvas): global year max_y, max_x = utils.get_terminal_size() while True: current_year = year.get('current_year') previous_message = utils.get_message(current_year - 1) utils.draw_frame(canvas, round(max_y - 2), round(2), str(previous_message), negative=True) message = utils.get_message(current_year) utils.draw_frame(canvas, round(max_y - 2), round(2), str(message)) if current_year == 1961: orbit_with_garbage = fill_orbit_with_garbage(canvas) coroutines.append(orbit_with_garbage) if current_year == 2020: fire_animation = get_fire(canvas) coroutines.append(fire_animation) await utils.wait_time(CHANGE_YEAR_DELAY) year['current_year'] += 1
async def fill_orbit_with_garbage(canvas): '''Function controlls coroutines with garbage flying.''' global coroutines global year frames = [] y_max, x_max = canvas.getmaxyx() #read files with trash frames trash_dir = 'frames/trash_frames/' onlyfiles = [f for f in listdir(trash_dir) if isfile(join(trash_dir, f))] for fl in onlyfiles: with open(trash_dir + fl, 'r') as f: frames.append(f.read()) while True: #add random fly garbage coroutine after random sleep delay = get_garbage_delay_tics(year) #delay for garbage spawning if delay: coroutines.append( fly_garbage(canvas, random.randint(1, x_max), random.choice(frames))) await sleep(rnd(delay * 1, delay * 3)) await sleep()
async def run_spaceship(canvas, start_row, start_column): """Function provides ship control for a player.""" global spaceship_frame global coroutintes global obstacles global year row_speed, column_speed = 0, 0 score_counter = count_score() coroutines.append(score_counter) while True: #remembering current frame current_frame = spaceship_frame #get position and control data (start_row, start_column, row_speed, column_speed, space) = get_ship_control(canvas, start_row, start_column, row_speed, column_speed, spaceship_frame) #add fire if space were pressed if space and year >= 2020: coroutines.append(fire(canvas, start_row, start_column, -1, 0)) #coroutines.append(fire(canvas,start_row,start_column,-1,1)) #coroutines.append(fire(canvas,start_row,start_column,-1,-1)) draw_frame(canvas, start_row, start_column, current_frame) await sleep(1) draw_frame(canvas, start_row, start_column, current_frame, negative=True) #stop game after collision for obs in obstacles: if obs.has_collision(start_row, start_column, 5, 4): coroutines.append(show_gameover(canvas)) coroutines.remove(score_counter) return
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)