예제 #1
0
def right():
    if random.randrange(0, 3) == 1:
        PressKey(W)
    else:
        ReleaseKey(W)
        PressKey(D)
        ReleaseKey(A)
        ReleaseKey(S)
예제 #2
0
def no_keys():

    if random.randrange(0, 3) == 1:
        PressKey(W)
    else:
        ReleaseKey(W)
        ReleaseKey(A)
        ReleaseKey(S)
        ReleaseKey(D)
예제 #3
0
def reverse():
    PressKey(S)
    ReleaseKey(A)
    ReleaseKey(W)
    ReleaseKey(D)
예제 #4
0
def straight():
    PressKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
    ReleaseKey(S)
예제 #5
0
def main():
    last_time = time.time()
    for i in list(range(4))[::-1]:
        print(i + 1)
        time.sleep(1)

    paused = False
    mode_choice = 0

    screen = grab_screen(region=(0, 0, GAME_WIDTH, GAME_HEIGHT))
    screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
    prev = cv2.resize(screen, (WIDTH, HEIGHT))

    t_minus = prev
    t_now = prev
    t_plus = prev

    while (True):

        if not paused:
            screen = grab_screen(region=(0, 0, GAME_WIDTH, GAME_HEIGHT))
            screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)

            last_time = time.time()
            screen = cv2.resize(screen, (WIDTH, HEIGHT))

            delta_count_last = motion_detection(t_minus, t_now, t_plus)

            t_minus = t_now
            t_now = t_plus
            t_plus = screen
            t_plus = cv2.blur(t_plus, (4, 4))

            x_array = np.array(screen)
            x_array = x_array.reshape(-1, WIDTH, HEIGHT, 3)
            x_array = x_array.astype('float32')
            prediction = model.predict(x_array)
            #prediction = np.array(prediction) * np.array([0.1, 0.1, 0.1, 0.1,  0.1,   0.1, 0.1, 0.1, 0])#np.array([4.5, 0.1, 0.1, 0.1,  1.8,   1.8, 0.5, 0.5, 0.2])

            mode_choice = np.argmax(prediction)

            if mode_choice == 0:
                straight()
                choice_picked = 'straight'

            elif mode_choice == 1:
                reverse()
                choice_picked = 'reverse'

            elif mode_choice == 2:
                left()
                choice_picked = 'left'
            elif mode_choice == 3:
                right()
                choice_picked = 'right'
            elif mode_choice == 4:
                forward_left()
                choice_picked = 'forward+left'
            elif mode_choice == 5:
                forward_right()
                choice_picked = 'forward+right'
            elif mode_choice == 6:
                reverse_left()
                choice_picked = 'reverse+left'
            elif mode_choice == 7:
                reverse_right()
                choice_picked = 'reverse+right'
            elif mode_choice == 8:
                no_keys()
                choice_picked = 'nokeys'

            motion_log.append(delta_count_last)
            motion_avg = round(mean(motion_log), 3)
            print('loop took {} seconds. Motion: {}. Choice: {}'.format(
                round(time.time() - last_time, 3), motion_avg, choice_picked))
            print('prediction array: {}'.format(prediction))

            if motion_avg < motion_req and len(motion_log) >= log_len:
                print(
                    'WERE PROBABLY STUCK FFS, initiating some evasive maneuvers.'
                )

                # 0 = reverse straight, turn left out
                # 1 = reverse straight, turn right out
                # 2 = reverse left, turn right out
                # 3 = reverse right, turn left out

                quick_choice = random.randrange(0, 4)

                if quick_choice == 0:
                    reverse()
                    time.sleep(random.uniform(1, 2))
                    forward_left()
                    time.sleep(random.uniform(1, 2))

                elif quick_choice == 1:
                    reverse()
                    time.sleep(random.uniform(1, 2))
                    forward_right()
                    time.sleep(random.uniform(1, 2))

                elif quick_choice == 2:
                    reverse_left()
                    time.sleep(random.uniform(1, 2))
                    forward_right()
                    time.sleep(random.uniform(1, 2))

                elif quick_choice == 3:
                    reverse_right()
                    time.sleep(random.uniform(1, 2))
                    forward_left()
                    time.sleep(random.uniform(1, 2))

                for i in range(log_len - 2):
                    del motion_log[0]

        keys = keycheck()

        # p pauses game and can get annoying.
        if 'T' in keys:
            if paused:
                paused = False
                time.sleep(1)
            else:
                paused = True
                ReleaseKey(A)
                ReleaseKey(W)
                ReleaseKey(D)
                time.sleep(1)
예제 #6
0
def right():
    ReleaseKey(A)
    PressKey(D)
예제 #7
0
def reverse_right():
    PressKey(S)
    PressKey(D)
    ReleaseKey(W)
    ReleaseKey(A)
예제 #8
0
def brake():
    ReleaseKey(A)
    ReleaseKey(W)
    ReleaseKey(D)
    PressKey(S)
예제 #9
0
def no_keys():
    ReleaseKey(W)
    ReleaseKey(A)
    ReleaseKey(S)
    ReleaseKey(D)
예제 #10
0
def left():
    ReleaseKey(W)
    ReleaseKey(S)
    ReleaseKey(D)
    PressKey(A)
예제 #11
0
def right():
    ReleaseKey(W)
    ReleaseKey(A)
    ReleaseKey(S)
    PressKey(D)
예제 #12
0
def main():
    '''
    for i in list(range(7))[::-1]:
        print(i+1)
        time.sleep(1)
    '''

    paused = False
    mode_choice = 0

    while (True):

        if not paused:
            screen = grab_screen(region=(0, 0, GAME_WIDTH, GAME_HEIGHT))
            screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
            screen = cv2.resize(screen, (WIDTH, HEIGHT))
            x_array = np.array(screen)
            x_array = x_array.reshape(-1, WIDTH, HEIGHT, 3)
            #float_x_array = (tf.cast, tf.float32)
            x_array = x_array.astype('float32')
            prediction = model.predict(x_array)
            pred = np.argmax(prediction)
            #prediction = np.array(prediction) * np.array([0.1,0.5,0.5,0.5,1.8,1.8,0.5,0.5,0.1])

            mode_choice = np.argmax(prediction)
            print('Choice made: {}'.format(mode_choice))
            print('Prediction : {}'.format(pred))

            if mode_choice == 0:
                straight()
                choice_picked = 'straight'
            elif mode_choice == 1:
                brake()
                choice_picked = 'reverse'
            elif mode_choice == 2:
                left()
                choice_picked = 'left'
            elif mode_choice == 3:
                right()
                choice_picked = 'right'
            elif mode_choice == 4:
                forward_left()
                choice_picked = 'forward+left'
            elif mode_choice == 5:
                forward_right()
                choice_picked = 'forward+right'
            elif mode_choice == 6:
                reverse_left()
                choice_picked = 'reverse+left'
            elif mode_choice == 7:
                reverse_right()
                choice_picked = 'reverse+right'
            elif mode_choice == 8:
                no_keys()
                choice_picked = 'nokeys'

        keys = keycheck()

        # p pauses game and can get annoying.
        if 'T' in keys:
            if paused:
                paused = False
                time.sleep(1)
            else:
                paused = True
                ReleaseKey(A)
                ReleaseKey(W)
                ReleaseKey(D)
                ReleaseKey(S)
                time.sleep(1)
예제 #13
0
async def on_message(message):
    if message.channel.id == None:  # Enter channel ID to listen, must be an integer
        if message.content.lower() == 'up':
            PressKey(UpArrow)
            log.debug('up')
            await asyncio.sleep(0.1)
            ReleaseKey(UpArrow)

        if message.content.lower() == 'down':
            PressKey(DownArrow)
            log.debug('down')
            await asyncio.sleep(0.1)
            ReleaseKey(DownArrow)

        if message.content.lower() == 'left':
            PressKey(LeftArrow)
            log.debug('left')
            await asyncio.sleep(0.1)
            ReleaseKey(LeftArrow)

        if message.content.lower() == 'right':
            PressKey(RightArrow)
            log.debug('right')
            await asyncio.sleep(0.1)
            ReleaseKey(RightArrow)

        if message.content.lower().startswith('z'):
            PressKey(Z)
            log.debug('z')
            await asyncio.sleep(0.1)
            ReleaseKey(Z)

        if message.content.lower().startswith('x'):
            PressKey(X)
            log.debug('x')
            await asyncio.sleep(0.1)
            ReleaseKey(X)

        if message.content.lower().startswith('q'):
            PressKey(Q)
            log.debug('q')
            await asyncio.sleep(0.1)
            ReleaseKey(Q)

        if message.content.lower().startswith('w'):
            PressKey(W)
            log.debug('w')
            await asyncio.sleep(0.1)
            ReleaseKey(W)

        # Control running
        if message.content.lower().startswith('up!'):
            PressKey(LShift)
            PressKey(UpArrow)
            log.debug('run up')
            await asyncio.sleep(0.1)
            ReleaseKey(LShift)
            ReleaseKey(UpArrow)

        if message.content.lower().startswith('down!'):
            PressKey(LShift)
            PressKey(DownArrow)
            log.debug('run down')
            await asyncio.sleep(0.1)
            ReleaseKey(LShift)
            ReleaseKey(DownArrow)

        if message.content.lower().startswith('left!'):
            PressKey(LShift)
            PressKey(LeftArrow)
            log.debug('run left')
            await asyncio.sleep(0.1)
            ReleaseKey(LShift)
            ReleaseKey(LeftArrow)

        if message.content.lower().startswith('right!'):
            PressKey(LShift)
            PressKey(RightArrow)
            log.debug('run right')
            await asyncio.sleep(0.1)
            ReleaseKey(LShift)
            ReleaseKey(RightArrow)

        if message.content.lower().startswith('shift'):
            PressKey(LShift)
            log.debug('shift')
            await asyncio.sleep(0.1)
            ReleaseKey(LShift)
예제 #14
0
def stop():
    ReleaseKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
예제 #15
0
def forward_left():
    PressKey(W)
    PressKey(A)
    ReleaseKey(D)
    ReleaseKey(S)
예제 #16
0
def reverse_left():
    PressKey(S)
    PressKey(A)
    ReleaseKey(W)
    ReleaseKey(D)
예제 #17
0
def forward_right():
    PressKey(W)
    PressKey(D)
    ReleaseKey(A)
    ReleaseKey(S)
예제 #18
0
def straight():
    ReleaseKey(A)
    ReleaseKey(D)
    PressKey(W)