# calling start_game function # to initialze the matrix mat = logic.start_game() while (True): # taking the user input # for next step x = input("Press the command : ") # we have to move up if (x == 'W' or x == 'w'): # call the move_up funtion mat, flag = logic.move_up(mat) # get the current state and print it status = logic.get_current_state(mat) print(status) # if game not ove then continue # and add a new two if (status == 'GAME NOT OVER'): logic.add_new_2(mat) # else break the loop else: break # the above process will be followed
while running: current_events = event.get() for e in current_events: if e.type == pygame.QUIT: running = False if e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False changed = False keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: board, changed = logic.move_left(board) elif keys[pygame.K_RIGHT]: board, changed = logic.move_right(board) elif keys[pygame.K_UP]: board, changed = logic.move_up(board) elif keys[pygame.K_DOWN]: board, changed = logic.move_down(board) tiles = tiles_change(board) status = logic.current_state(board) print(status) if status == 'Continue': if changed: board = logic.add_new_number(board) tiles = tiles_change(board) else: pass else: sleep(2)
def test_move_up(self): mas = [[2, 4, 0, 2], [2, 0, 2, 0], [4, 0, 2, 4], [4, 4, 0, 0]] rezult = [[4, 8, 4, 2], [8, 0, 0, 4], [0, 0, 0, 0], [0, 0, 0, 0]] self.assertEqual(move_up(mas), (rezult, 24))
running = True while running: moved = False current_events = event.get() for e in current_events: if e.type == pygame.QUIT: running = False if e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: running = False if e.key == pygame.K_LEFT: board, moved = logic.move_left(board) if e.key == pygame.K_RIGHT: board, moved = logic.move_right(board) if e.key == pygame.K_UP: board, moved = logic.move_up(board) if e.key == pygame.K_DOWN: board, moved = logic.move_down(board) tiles = tiles_change(board) if moved == True: status = logic.current_state(board) if status == 'Continue': board = logic.add_new_number(board) else: if status == 'Congratulation!': screen.blit(num.win, (0, 0)) else: screen.blit(num.lose, (0, 0))
mat = logic.start_game() print("Commands are as follows : ") print("'w' : Move Up") print("'s' : Move Down") print("'a' : Move Left") print("'d' : Move Right") print("'x' : exit") 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)