grid.set_drawaction('=', partial(grid.draw_background, color=(70, 70, 70))) grid.set_drawaction(':', partial(grid.draw_background, color=(140, 120, 80))) grid.set_drawaction('\\', drawing.draw_down_line) grid.set_drawaction('O', drawing.draw_boulder) grid.set_drawaction('/', drawing.draw_up_line) grid.set_drawaction('!', drawing.draw_bomb) grid.set_drawaction('*', drawing.draw_money) grid.set_drawaction('^', drawing.draw_balloon) grid.set_drawaction('@', drawing.draw_hero) grid.set_drawaction('M', drawing.draw_monster) grid.set_drawaction('S', drawing.draw_baby_monster) grid.set_drawaction('+', drawing.draw_cage) grid.set_drawaction('<', partial(grid.draw_character_cell, character='←')) grid.set_drawaction('>', partial(grid.draw_character_cell, character='→')) return grid if __name__ == '__main__': grid = setup_grid() game = Game(grid) grid.set_timer_action(partial(game.timer_step)) grid.set_key_action(partial(key_action, game=game)) grid.set_mouse_click_action(partial(mouse_click, game=game)) grid.set_update_sidebar_action(game.update_sidebar) pygame.key.set_repeat(100, 60) game.start_level() grid.run()
# [0, 0, 0, 0], # [0, 0, 2, 0], # [2, 2, 4, 4]] game.slide_down() result_board = game.get_board() assert(result_board[0]==[0,0,0,0]) assert(result_board[1]==[0,0,0,0]) assert(result_board[2]==[0,0,2,0]) assert(result_board[3]==[2,2,4,4]) print("Passed!") ## # Run the tests ## test_game = Game(0, 4) test_init(test_game) test_get_board(test_game) test_clear_board(test_game) test_get_empty_squares(test_game) test_has_valid_move(test_game) test_spawn_tile(test_game) test_slide_left(test_game) test_slide_right(test_game) test_slide_up(test_game) test_slide_down(test_game)
import queue as q import threading from command import Commands from game_logic import Game from speech_detector import SpeechDetector commands_queue = q.Queue() speech_detector = SpeechDetector(commands_queue) game = Game(commands_queue) game_thread = threading.Thread(target=game.game_loop) game_thread.start() speech_thread = threading.Thread(target=speech_detector.start_stream) speech_thread.start()
from turtle import Screen, Turtle from game_logic import Game game = Game() turtle = Turtle() turtle.hideturtle() turtle.penup() screen = Screen() screen.setup(height=491, width=725) screen.title("U.S. States Game") screen.bgpic("blank_states_img.gif") while game.score < 50: try: guess = screen.textinput( title=f"{game.score}/50 states correct", prompt="Guess a State").title() except AttributeError: continue if guess == "Exit": game.generate_states_to_learn_csv() break state_coords = game.take_guess(guess) if state_coords: turtle.goto(state_coords) turtle.write(guess)
def handle_threads(self): try: is_everyone_disconnected = False all_players_connected = False sleep(2) connected_players = 0 for _ in range(5): connected_players = 0 for conn_thread in self.connections_threads: if conn_thread.thread.is_connected(): connected_players = connected_players + 1 is_everyone_disconnected = False sleep(0.5) all_players_connected = connected_players == len(self.connections_threads) if all_players_connected: break if all_players_connected: players_names = [] # clear all message bins for conn_thread in self.connections_threads: self.logger.debug(conn_thread.status.player_name) conn_thread.status.msg_to_send = '' conn_thread.status.received_msg = '' players_names.append(conn_thread.status.player_name) app = App(self.username, players_names) # initialize list and lock for keyPress event pressed_keys_list = [] pressed_keys_lock = threading.Lock() main_game = Game(pressed_keys_list, pressed_keys_lock) app.set_game_thread(main_game) # wait for app to bootstrap while not app.is_running: pass # initialize players handlers for easier access to board/game_view/connection_thread players_handlers = [] for idx, player_board in enumerate(app.other_players_boards): game_view = GameView(player_board.player_name_value) player_handler = PlayerHandler(player_board, game_view, self.connections_threads[idx]) players_handlers.append(player_handler) # make handlers immutable players_handlers = tuple(players_handlers) # start all game threads for player in players_handlers: player.game_view.start() main_game.start() # while not app.is_screen_destroyed and not app.is_end_game: end_game = False game_view_end = False while not app.is_screen_destroyed and not app.is_end_game: if end_game: break # set new values on host board app.main_player_board.set_player_combo(main_game.get_combo()) app.main_player_board.set_player_score(main_game.get_score()) app.main_player_board.set_moles_status(main_game.get_board()) # get new package to send to other players package = main_game.create_bits_package() for conn_thread in self.connections_threads: # set package to send conn_thread.thread.set_message_to_send(package) for idx, player in enumerate(players_handlers): if player.connection_thread.thread.disconnected: player.board.disconnected = True # set received package in game_view for view update player.game_view.set_package(player.connection_thread.thread.get_received_message()) player.board.set_player_combo(player.game_view.get_combo()) player.board.set_player_score(player.game_view.get_score()) player.board.set_moles_status(player.game_view.get_board()) if player.game_view.get_end(): # set if any of game views has end_state game_view_end = True if main_game.get_end() or game_view_end: # if any game has ended, wait until all players know about endgame end_game = True if not main_game.get_end(): main_game.set_end() for idx, player in enumerate(players_handlers): if not player.connection_thread.thread.disconnected: # await only connected players if not player.game_view.get_end(): end_game = False # close GUI after ending game # after that, App thread automatically shuts down app.callback() if end_game: # Get the scoreboard players_scores = [] players_scores.append([self.username, int(main_game.get_score())]) for idx, player in enumerate(players_handlers): players_scores.append([player.board.player_name_value, int(player.game_view.get_score())]) players_scores.sort(key=lambda x:x[1], reverse=True) print('---- Scoreboard ----') for idx, player_score in enumerate(players_scores): print(f'{idx+1}. {player_score[0]} - {player_score[1]}') print('---- Scoreboard ----') else: print('Disconnected!') # independent of errors in app mainloop - set all games to end, so threads can be closed main_game.end = True for player in players_handlers: player.game_view.end = True # kill all connections for connection_thread in self.connections_threads: connection_thread.status.to_kill = True # close all threads self.logger.debug('Joining main_game') app.join() main_game.join() for player in players_handlers: self.logger.debug(f'Joining {player.board.player_name_value} game_view') player.game_view.join() self.logger.debug(f'Joining {player.board.player_name_value} conn_thread') player.connection_thread.thread.join() return else: print('There were some problems with connection. Shutdown...') for connection_thread in self.connections_threads: connection_thread.status.to_kill = True for connection_thread in self.connections_threads: connection_thread.thread.join() except KeyboardInterrupt as e: self.logger.debug('Interrupt in Client') self.logger.debug(e)
def init(data): data.game = Game(0, 4) data.width = 400 data.height = 400 data.gameFinished = False