def key_down(self, event): key = repr(event.char) if key in self.commands: self.matrix, changed, score = self.commands[repr(event.char)]( self.matrix) if changed: logic.add_new_2(self.matrix) self.update_grid_cells() changed = False elif key == c.KEY_AI: while logic.get_current_state(self.matrix) == 'GAME NOT OVER': self.matrix, valid_game = ai_logic.ai_move(self.matrix, 45, 35) if valid_game: self.matrix = logic.add_new_2(self.matrix) self.update_grid_cells() self.update_grid_cells() time.sleep(3) if logic.get_current_state(self.matrix) == 'WON': self.grid_cells[1][1].configure(text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure(text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) elif logic.get_current_state(self.matrix) == 'LOST': self.grid_cells[1][1].configure(text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure(text="Lose!", bg=c.BACKGROUND_COLOR_CELL_EMPTY)
def key_down(self,event): key=repr(event.char) if key in self.commands: self.matrix,change=self.commands[repr(event.char)](self.matrix) if change: logic.add_new_2(self.matrix) self.update_grid_cells() change=False if logic.game_status(self.matrix)=="WON": self.grid_cells[1][1].configure(text="YOU",bg=c.BG_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure(text="WON!",bg=c.BG_COLOR_CELL_EMPTY) if logic.game_status(self.matrix)=="LOSE": self.grid_cells[1][1].configure(text="YOU",bg=c.BG_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure(text="LOST!",bg=c.BG_COLOR_CELL_EMPTY) pass
def key_down(self, event): key = repr(event.char) if key in self.commands: self.matrix, changed = self.commands[repr(event.char)](self.matrix) if changed: logic.add_new_2(self.matrix) self.update_grid_cells() changed = False if logic.get_current_state(self.matrix) == "Congratulations, you won the game.": self.grid_cells[1][1].configure(text='You', bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure(text='Win', bg=c.BACKGROUND_COLOR_CELL_EMPTY) if logic.get_current_state(self.matrix) == 'Lost': self.grid_cells[1][1].configure(text='You', bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure(text='Lose', bg=c.BACKGROUND_COLOR_CELL_EMPTY)
def init_matrix(self): self.matrix = logic.start_game() logic.add_new_2(self.matrix) logic.add_new_2(self.matrix)
pprint(mat, indent=0, width=20) # 1차원 행렬을 2차원으로 표시한다. while True: #pynput을 통한 키입력 with keyboard.Events() as events: event = events.get(9999) # 입력을 오래 기다린다. if event.key == keyboard.KeyCode.from_char( 'w'): # w 커맨드를 입력하면 로직이 작동한다. changed = logic.move_up(mat) mat, flag = logic.move_up(mat) status = logic.get_current_state(mat) print(status) #로직 작동후 현재상태를 출력한다. if (status == '' and changed): logic.add_new_2(mat) # 빈공간을 계속 진행으로 정의하였다. elif (status != ''): break elif event.key == keyboard.KeyCode.from_char( 's'): # s 커맨드를 입력하면 로직이 작동한다. changed = logic.move_down(mat) mat, flag = logic.move_down(mat) status = logic.get_current_state(mat) print(status) if (status == '' and changed): logic.add_new_2(mat) elif (status != ''):