コード例 #1
0
                tty.setraw(sys.stdin.fileno())
                ch = sys.stdin.read(1)
            finally:
                termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
            return ch

    getter = _GetchUnix()

    print('Game start!')
    while True:
        canvas = update_canvas(comp, canvas)
        ih.set_data(canvas)
        fh.canvas.draw()
        i = getter()
        if i == 'a':
            comp.append_input(-1)
        elif i == 's':
            comp.append_input(0)
        elif i == 'd':
            comp.append_input(1)
        elif i == 'w':
            import pickle
            pickle.dump([comp, canvas], open('day13_save.pickle', 'wb'))

        if comp._state == RunState.FINISHED:
            i = input('To reload? (y/n)')
            if i == 'n':
                break
            else:
                import pickle
                comp, canvas = pickle.load(open('day13_save.pickle', 'rb'))
コード例 #2
0
    elif direction == 4:  # east
        new_pos = pos + [0, 1]
    return new_pos


while True:
    direction = np.random.randint(4) + 1
    if direction == 1:  # north
        new_pos = pos - [1, 0]
    elif direction == 2:  # south
        new_pos = pos + [1, 0]
    elif direction == 3:  # west
        new_pos = pos - [0, 1]
    elif direction == 4:  # east
        new_pos = pos + [0, 1]
    remote_control.append_input(direction)
    remote_control.run()
    status = remote_control.get_stdout()

    if status == 0:
        map[tuple(new_pos)] = 0
    elif status == 1:
        map[tuple(new_pos)] = 1
        pos = new_pos
    elif status == 2:
        map[tuple(new_pos)] = 2
        pos = new_pos
        break
    if n_steps % 10000 == 0:
        print(n_steps)
        ih.set_data(map)