def run(self): key_actions = { 'ESCAPE': self.quit, 'LEFT': lambda:self.move(-1), 'RIGHT': lambda:self.move(+1), 'DOWN': lambda:self.drop(True), 'UP': self.rotate_stone, 'p': self.toggle_pause, 'SPACE': self.start_game, 'RETURN': self.insta_drop } self.gameover = False self.paused = False dont_burn_my_cpu = Ui.get_clock() while 1: self.screen.fill((0,0,0)) if self.gameover: self.center_msg("""Game Over!\nYour score: %d Press space to continue""" % self.score) else: if self.paused: self.center_msg("Paused") else: Ui.draw_line(self.screen, self.rlim+1, self.height-1) self.disp_msg("Next:", ( self.rlim+cell_size, 2)) self.disp_msg("Score: %d\n\nLevel: %d\ \nLines: %d" % (self.score, self.level, self.lines), (self.rlim+cell_size, cell_size*5)) self.draw_matrix(self.bground_grid, (0,0)) self.draw_matrix(self.board, (0,0)) self.draw_matrix(self.stone, (self.stone_x, self.stone_y)) self.draw_matrix(self.next_stone, (cols+1,2)) Ui.update() for event in Ui.get_events(): if Ui.is_user_event(event): self.drop(False) elif Ui.is_quit_event(event): self.quit() elif Ui.is_keydown_event(event): for key in key_actions: if Ui.is_correct_key(event, key): key_actions[key]() dont_burn_my_cpu.tick(maxfps)
def main() -> None: s = Settings() if s.display_output: win = Ui(s) else: win = None if s.verbose: print("Setup done, verbose is active") cap = Video(s) cap.detect_objects(s) while True: # cap.detect_objects(s) cap.update_live_feed(s) if s.autodetect: cap.detect_objects(s) # cv.imwrite('./frame.bmp', cap.frame) if s.display_output: status_code = win.update(s, cap) if status_code == 2: if s.verbose: print("Started new detection") cap.detect_objects(s) elif status_code == 3: win.del_extra_window() elif status_code != 0: break if win.update_detect: cap.detect_objects(s) win.update_detect = False else: cap.detect_objects(s) s.update_json() if s.display_output: del win del s del cap
def run_cli(source, sample_rate, file_length_sec, debug, display_channel, threshold_db, markfreq_hz, threshold_steps, nfft, device_name): log_dir = source if not os.path.isdir(log_dir): print('Must provide valid log directory! source=%s' % str(log_dir)) exit(2) stdscr, curses = config_curses() console_height, console_width = stdscr.getmaxyx() # setup dimensions for window columns_of_data = int(nfft / 2) min_width = columns_of_data + extra_column_buffer while min_width > console_width: nfft -= 10 columns_of_data = int(nfft / 2) min_width = columns_of_data + extra_column_buffer # make sure window is wide enough to fit the menu if min_width <= menu_column_buffer: min_width = menu_column_buffer min_height = console_height max_rows_specgram = min_height - menu_row_buffer max_rows_specgram_no_menu = min_height # create Ui object ui = Ui(min_width, min_height, time.time(), curses.color_pair, max_rows_specgram, max_rows_specgram_no_menu, file_length_sec=file_length_sec, sample_rate=sample_rate) # create specgram object specgram = Specgram(sample_rate, file_length_sec, display_channel, device_name=device_name, scale='dB', threshdb=threshold_db, threshdb_steps=threshold_steps, markfreq=markfreq_hz, nfft=nfft, max_lines=ui.specgram_max_lines, color_pair=curses.color_pair, voltage_bar_width=voltage_bar_width) # now dow stuff try: count = 0 latest_file = ui.get_file(stdscr, source) previous_file = latest_file is_dup = True current_time = time.time() previous_time = current_time # setup the ui with the curses window and specgram object stdscr, specgram = ui.spin(stdscr, specgram) while True: current_time = time.time() if debug: # start context manager for log file with open( 'log_{0}.txt'.format( unix_epoch_to_local(time.time(), no_date=True)), 'w+') as log_file: if (current_time - previous_time) > (file_length_sec * 1000): message = "iteration: {0} time: {1} last iteration happened, {2} seconds ago.\n".format( count, unix_epoch_to_local(current_time), (current_time - previous_time) * 0.001) log_file.write(message) previous_time = current_time latest_file = ui.get_file(stdscr, source) # # if DAQ isn't running, new files aren't being added to the log dir # - Let user know they are looking at the specgram of the same file over and over # - Let's user know when they are looking at new streaming data # if latest_file == previous_file: is_dup = True else: is_dup = False previous_file = latest_file # clear out data list specgram.clear() # take the file and parse into specgram object rc = specgram.parse_file(latest_file) # if rc == None: # ui.message_buffer.append('Unable to read file...') # # draw everything in the buffer # stdscr.refresh() # continue # clear curses window stdscr.erase() try: specgram.display(stdscr) ui.update(stdscr, specgram, is_dup, count) # spin ui will display menu and handle user inputs stdscr, specgram = ui.spin(stdscr, specgram) except curses.error as err: ui.hard_reset(stdscr, specgram, max_rows_specgram, max_rows_specgram_no_menu) # draw everything in the buffer stdscr.refresh() count += 1 if count % 100 == 0: ui.message_buffer = [] ui.message_buffer.append('CLEARED!') except KeyboardInterrupt: print('\n\tExiting...\n\n') exit(1) except ConfigError as err: pass finally: curses.nocbreak() stdscr.keypad(False) curses.echo() curses.endwin()
def main(): open_canvas() global mainback mainback = Mainback() global start start = False while(start == False): handle_events() clear_canvas() mainback.draw() update_canvas() delay(0.08) #클래스 선언 global hero hero = Hero() rabbit = Rabbit() rabbit_group = [Rabbit() for i in range(600)] land = Land() wood = Wood() global torch torch = Torch() global fire fire = Fire() eskimo = Eskimo() attack_fire = Attack_fire() attack_fire_group = [Attack_fire() for i in range(100)] background = BackGround() global ui ui = Ui() firewood = Firewood() firewood_group = [Firewood() for i in range(600)] #변수 선언 rabbit_group_counter = 0 rabbit_group_collision_counter = 0 attack_group_counter = 0 attack_group_update_counter = 0 attack_group_collision_counter = 0 rabbit_alive_counter = 0 attack_group_alive_check = False attack_group_alive_counter = 0 attack_group_limit = 20 rabbit_group_draw_counter = 0 firewood_num_counter = 0 firewood_num_update_counter = 0 firewood_collide_counter = 0 rabbit_group_counter2 = 0 rabbit_jump = False rabbit_num = 10 firewood_num = 10 rack_block = 0 eskimo_counter = 0 global running running = True while running: #핸들 이벤트 handle_events() #업데이트 hero.update() background.update() land.update() wood.update() fire.update() torch.update() eskimo.update() ui.update(hero.x, hero.y, hero.ability) for rabbit in rabbit_group: # 토끼 업데이트 if(rabbit_group_counter == rabbit_num): rabbit_group_counter = 0 break if(rabbit.alive): rabbit.update() rabbit_group_counter += 1 # for rabbit in rabbit_group: # 토끼 업데이트 # if(rabbit_group_counter2 == 1): # print("%d" % rabbit.x) # break # rabbit_group_counter2 += 1 for attack_fire in attack_fire_group: # 공격불 업데이트 if(attack_group_update_counter == hero.attack_num): attack_fire.init_direction() attack_fire.alive = True # 공격불이 활성화 됨 attack_fire.init_fire() attack_group_update_counter = 0 break if(attack_fire.alive): attack_fire.update() attack_group_update_counter += 1 for firewood in firewood_group: # 장작 업데이트 if(firewood_num_update_counter == firewood_num): firewood_num_update_counter = 0 break firewood.update() firewood_num_update_counter += 1 #함수 for rabbit in rabbit_group: #토끼와 히어로의 충돌체크 if(rabbit_group_collision_counter == rabbit_num): rabbit_group_collision_counter = 0 break if(collision(rabbit, hero)): rabbit.y += 50 mainback.hero_die = True mainback.die_sound() running = False rabbit_group_collision_counter += 1 for rabbit in rabbit_group: # 토끼와 공격불의 충돌체크 if(rack_block == rabbit_num): rack_block = 0 break for attack_fire in attack_fire_group: if(attack_group_collision_counter == hero.attack_num): attack_group_collision_counter = 0 break if(collision(rabbit, attack_fire) and rabbit.alive and attack_fire.alive): attack_fire.alive = False rabbit.alive = False rabbit.die = True hero.kill += 1 attack_fire.die = False attack_group_collision_counter += 1 rack_block += 1 for attack_fire in attack_fire_group: if(eskimo_counter == hero.attack_num): eskimo_counter = 0 break if(collision(eskimo, attack_fire)): attack_fire.alive = False eskimo.x -= 10 eskimo.hp -= 1 if(eskimo.hp == 0): mainback.eskimo_die = True running = False mainback.win_sound() attack_fire.die = False eskimo_counter += 1 if(collision(wood, hero)): # 나무와 주인공 충돌체크 fire.collide = True if(ui.firewood_num != 0): fire.life += ui.firewood_num*250 ui.firewood_num = 0 else: fire.collide = False if(collision(eskimo, hero)): # 주인공과 에스키모 충돌체크 mainback.hero_die = True running = False mainback.die_sound() for firewood in firewood_group: # 장작과 주인공 충돌체크 if(firewood_collide_counter == firewood_num): firewood_collide_counter = 0 break if(collision(firewood, hero) and firewood.die == False): ui.firewood_num += 1 firewood.die = True firewood_collide_counter += 1 for rabbit in rabbit_group: # 토끼 출현! if(rabbit_alive_counter == rabbit_num): break if(rabbit.die == False): rabbit.alive = True rabbit_alive_counter += 1 if(fire.die): # 불이 꺼지면 토끼들이 마구마구 몰려온다. rabbit_num = 500 for attack_fire in attack_fire_group: # 불 스킬 존재 유무 if(attack_fire.alive): attack_group_alive_counter = 0 break attack_group_alive_counter += 1 if(attack_group_alive_counter == hero.attack_num): hero.attack_num = 0 for attack_fire in attack_fire_group: # 화면 밖을 벗어나면 불 스킬 사망 판정 if(attack_fire.x >= 900 or attack_fire.x <= -100): attack_fire.alive = False print("stage = %d" % background.stage) #스테이지 if(hero.kill == 10 and background.stage == 1): print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++") rabbit_num += 20 hero.ability += 10 firewood_num += 5 background.stage = 2 if(hero.kill == 30 and background.stage == 2): rabbit_num += 30 firewood_num += 10 hero.ability += 10 background.stage = 3 background.stage3_music() if(hero.kill == 60 and background.stage == 3): rabbit_num += 40 firewood_num += 15 hero.ability += 10 background.stage = 4 if(hero.kill == 80 and background.stage == 4): rabbit_num += 50 firewood_num += 20 hero.ability += 10 background.stage = 5 eskimo.alive = True background.stage5_music() if(background.stage == 5): rabbit_jump = True if(rabbit_jump): for rabbit in rabbit_group: rabbit.y += 5 # print("%d" % hero.attack_num) print("킬 수 : %d num : %d" % (hero.kill, rabbit_num)) clear_canvas() #그리기 background.draw() fire.draw() wood.draw() torch.draw() hero.draw() eskimo.draw() # eskimo.draw_bb() # hero.draw_bb() for rabbit in rabbit_group: # 적토끼 출력 if(rabbit_group_draw_counter == rabbit_num): rabbit_group_draw_counter = 0 break if(rabbit.alive): rabbit.draw() rabbit_group_draw_counter += 1 # rabbit.draw_bb() for attack_fire in attack_fire_group: # 공격 불 출력 if(attack_group_counter == hero.attack_num): attack_group_counter = 0 break if(attack_fire.alive): attack_fire.draw() # attack_fire.draw_bb() attack_group_counter += 1 for firewood in firewood_group: # 장작 출력 if(firewood_num_counter == firewood_num): firewood_num_counter = 0 break firewood.draw() firewood_num_counter += 1 land.draw() ui.draw() update_canvas() delay(0.06) while(mainback.hero_die or mainback.eskimo_die): handle_events() clear_canvas() mainback.draw() update_canvas() delay(0.08) close_canvas()
class DialogComponent: """ A graphical Pygame component that represents a dialog tree Display this component to the screen by blitting its publicly accessible surface. Make sure to call redraw() so that the graphics are updated. Call update(ms) to have it respond to time passing. """ def __init__(self, surface: Surface, dialog_font: Font, choice_font: Font, images: Dict[str, Surface], animations: Dict[str, List[Surface]], sound_player: SoundPlayer, dialog_graph: DialogGraph, picture_size: Vec2, select_blip_sound_id: str): self._validate_inputs(dialog_graph, images, sound_player) self.surface = surface self._sound_player = sound_player self._dialog_graph = dialog_graph self._current_dialog_node = self._dialog_graph.current_node() background_id = self._dialog_graph.background_image_id background = images[background_id] if background_id else None self._ui = Ui(surface=surface, picture_size=picture_size, dialog_node=self._current_dialog_node, dialog_font=dialog_font, choice_font=choice_font, images=images, animations=animations, sound_player=sound_player, background=background, select_blip_sound_id=select_blip_sound_id) self._play_dialog_sound() @staticmethod def _validate_inputs(dialog_graph: DialogGraph, images: Dict[str, Surface], sound_player: SoundPlayer): for node in dialog_graph.nodes(): if node.graphics.image_ids: for image_id in node.graphics.image_ids: if image_id not in images: raise ValueError( f"Invalid config! Graph node '{node.node_id}' refers to missing image: '{image_id}'" ) if node.sound_id: if not sound_player.has_sound(node.sound_id): raise ValueError( f"Invalid config! Graph node '{node.node_id}' refers to missing sound: '{node.sound_id}" ) background_id = dialog_graph.background_image_id if background_id and background_id not in images: raise ValueError( f"Invalid config! Graph refers to missing background image: '{background_id}'" ) def update(self, elapsed_time: Millis): self._ui.update(elapsed_time) self._sound_player.update(elapsed_time) def skip_text(self): self._ui.skip_text() def move_choice_selection(self, delta: int): self._ui.move_choice_highlight(delta) def select_choice_at_position(self, ui_coordinates: Vec2): chosen_index = self._ui.choice_button_at_position(ui_coordinates) if chosen_index is not None: self._ui.set_highlighted_choice(chosen_index) def commit_selected_choice(self): chosen_index = self._ui.highlighted_choice() if chosen_index is not None: self._commit_choice(chosen_index) def commit_choice_at_position(self, ui_coordinates: Vec2): chosen_index = self._ui.choice_button_at_position(ui_coordinates) if chosen_index is not None: self._commit_choice(chosen_index) def _commit_choice(self, chosen_index: int): self._dialog_graph.make_choice(chosen_index) self._current_dialog_node = self._dialog_graph.current_node() self._play_dialog_sound() self._ui.set_dialog(self._current_dialog_node) def current_node_id(self) -> str: return self._current_dialog_node.node_id def _play_dialog_sound(self): self._sound_player.stop_all_playing_sounds() if self._current_dialog_node.sound_id: self._sound_player.play(self._current_dialog_node.sound_id) def redraw(self): self._ui.redraw()
def quit(self): self.center_msg("Exiting...") Ui.update() sys.exit()