Exemple #1
0
 def cruising():
     time.sleep(1)
     PressKey(keys.W)
     time.sleep(3)
     ReleaseKey(keys.W)
     PressKey(keys.C)
     ReleaseKey(keys.C)
Exemple #2
0
def control_car(chosen_action):
    for action in actions_array:
        ReleaseKey(action)

    # Simplified state-action with
    # turning on constant accelerating
    # TODO remove after experimenting
    PressKey(W)

    # Perform action
    PressKey(actions_array[chosen_action])
Exemple #3
0
def right():
    ReleaseKey(A)
    ReleaseKey(S)
    ReleaseKey(W)
    #PressKey(S)
    #time.sleep(0.05)
    #ReleaseKey(S)
    PressKey(D)
    PressKey(W)
    time.sleep(0.09)
    ReleaseKey(D)
Exemple #4
0
def left():
    ReleaseKey(D)
    ReleaseKey(S)
    ReleaseKey(W)
    #PressKey(S)
    #time.sleep(0.05)
    #ReleaseKey(S)
    PressKey(A)
    PressKey(W)
    time.sleep(0.09)
    ReleaseKey(A)
Exemple #5
0
def reset_race():
    # Run keys pattern to reset race
    PressKey(ESC)
    t.sleep(keypress_pause)
    ReleaseKey(ESC)
    PressKey(ENTER)
    t.sleep(keypress_pause)
    ReleaseKey(ENTER)
    PressKey(DOWN)
    t.sleep(keypress_pause)
    ReleaseKey(DOWN)
    PressKey(ENTER)
    t.sleep(keypress_pause)
    ReleaseKey(ENTER)
    t.sleep(3 * keypress_pause)
Exemple #6
0
def none():
    ReleaseKey(D)
    ReleaseKey(S)
    ReleaseKey(W)
    ReleaseKey(A)
    PressKey(S)
    time.sleep(0.4)
    ReleaseKey(S)
Exemple #7
0
def on_gyro(data):
    global z
    global pressed
    z += data[2]
    if (z > 0.5):
        if (pressed != 'left'):
            PressKey(LEFT)
            pressed = 'left'
    elif (z < -0.5):
        if (pressed != 'right'):
            PressKey(RIGHT)
            pressed = 'right'
    else:
        if (pressed == 'left'):
            ReleaseKey(LEFT)
        elif (pressed == 'right'):
            ReleaseKey(RIGHT)

        pressed = False
    print('z: ' + str(z) + "\n")
Exemple #8
0
def on_acc(data):
    global pressed
    if (data[1] > 2):
        if (pressed == 'right'):
            ReleaseKey(RIGHT)
        if (pressed != 'left'):
            PressKey(LEFT)
            pressed = 'left'
    elif (data[1] < -2):
        if (pressed == 'left'):
            ReleaseKey(LEFT)
        if (pressed != 'right'):
            PressKey(RIGHT)
            pressed = 'right'
    else:
        if (pressed == 'left'):
            ReleaseKey(LEFT)
        elif (pressed == 'right'):
            ReleaseKey(RIGHT)
        pressed = False
    print('acc: ' + str(data) + "\n")
Exemple #9
0
def on_pressed(data):
    print("\n" + 'pressed: ' + str(data) + "\n")
    if (data == "x"):
        PressKey(X)
    elif (data == "y"):
        PressKey(Y)
    elif (data == "a"):
        PressKey(A)
    elif (data == "b"):
        PressKey(B)
    elif (data == "up"):
        PressKey(UP)
    elif (data == "down"):
        PressKey(DOWN)
    elif (data == "left"):
        PressKey(LEFT)
    elif (data == "right"):
        PressKey(RIGHT)
Exemple #10
0
def main():

    # countdown delay to get placed into the game
    for i in list(range(4))[::-1]:
        print(i + 1)
        time.sleep(1)

    last_time = time.time()
    PressKey(W)
    while (True):
        #PressKey(W)
        # 800x600 windowed mode for GTA5, at top left position of main screen
        # 40 px for title bar at top
        screen = np.array(ImageGrab.grab(bbox=(0, 40, 800, 640)))
        #print('loop took {} seconds'.format(time.time()-last_time))
        last_time = time.time()
        new_screen = process_img(screen)
        #cv2.imshow('window', cv2.cvtColor(printscreen, cv2.COLOR_BGR2RGB))
        cv2.imshow('window', new_screen)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break
Exemple #11
0
 def forward():
     PressKey(keys.W)
Exemple #12
0
 def left(delay=0):
     PressKey(keys.A)
     time.sleep(delay)
     ReleaseKey(keys.A)
Exemple #13
0
 def right(delay=0):
     PressKey(keys.D)
     time.sleep(delay)
     ReleaseKey(keys.D)
Exemple #14
0
def changeKeyState(newPressConfig):
    global speed, frame

    pressed = {up: False, down: False, left: False, right: False, nitro: False}
    if (speed < speedLimit * 8 / 10 and len(road) > 0 and abs(road[0]) < 2):
        # current speed is much lower than our desired speed, nitro up !
        PressKey(up)
        PressKey(nitro)
        ReleaseKey(down)
        pressed[up] = True
        pressed[nitro] = True
    elif (speed < speedLimit):
        # push the pedal but no nitro
        PressKey(up)
        ReleaseKey(down)
        ReleaseKey(nitro)
        pressed[up] = True

    elif (speed < speedLimit * 12 / 10):
        # release both buttons, stop speeding
        ReleaseKey(up)
        ReleaseKey(down)
        ReleaseKey(nitro)
    else:
        # slow down
        ReleaseKey(up)
        PressKey(down)
        ReleaseKey(nitro)
        pressed[down] = True

    # lastKeypressConfig = what was pressed in the last frame
    for key in lastKeypressConfig:
        if not lastKeypressConfig[key] and key not in newPressConfig:
            continue

        # fresh press
        elif not lastKeypressConfig[key]:
            lastKeypressConfig[key] = newPressConfig[key]
            PressKey(key)
            pressed[key] = True
            continue

        # force delayBetweenInputs
        elif lastKeypressConfig[key] + delayBetweenInputs < time.time():
            if key in newPressConfig and newPressConfig[key]:
                lastKeypressConfig[key] = newPressConfig[key]
                PressKey(key)
                pressed[key] = True
            else:
                ReleaseKey(key)
                lastKeypressConfig[key] = False
            continue
        # delayBetweenInputs ended, release
        elif lastKeypressConfig[key] < time.time():
            if ReleaseKey(key):
                pass

    font = cv2.FONT_HERSHEY_SIMPLEX
    fontScale = 0.8
    fontColor = (255, 255, 255)
    lineType = 3

    # visualize what buttons are pressed
    visualButtons = [('up', (400, 450), pressed[up]),
                     ('down', (400, 500), pressed[down]),
                     ('left', (325, 500), pressed[left]),
                     ('right', (500, 500), pressed[right]),
                     ('nitro', (575, 475), pressed[nitro]),
                     (str(speed) + ' km/h', (625, 450), False)]
    for visual in visualButtons:

        cv2.putText(frame, visual[0], visual[1], font, fontScale,
                    (255, 0, 0) if visual[2] else (255, 255, 255), lineType)
Exemple #15
0
def right():
    ReleaseKey(A)
    ReleaseKey(S)
    ReleaseKey(W)
    PressKey(D)
Exemple #16
0
def left():
    ReleaseKey(D)
    ReleaseKey(S)
    ReleaseKey(W)
    PressKey(A)
Exemple #17
0
def backward():
    PressKey(S)
    ReleaseKey(A)
    ReleaseKey(D)
    ReleaseKey(W)
Exemple #18
0
def right():
    PressKey(W)
    PressKey(D)
    ReleaseKey(A)
    time.sleep(t_time)
    ReleaseKey(D)
Exemple #19
0
def left():
    PressKey(A)
    PressKey(W)
    ReleaseKey(D)
    time.sleep(t_time)
    ReleaseKey(A)
Exemple #20
0
def forward():
    PressKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
    ReleaseKey(S)
Exemple #21
0
def reverse():
    ReleaseKey(A)
    ReleaseKey(W)
    ReleaseKey(D)
    PressKey(S)
Exemple #22
0
def straight_left():
    ReleaseKey(D)
    ReleaseKey(S)
    PressKey(W)
    PressKey(A)
Exemple #23
0
def reverse_right():
    ReleaseKey(A)
    ReleaseKey(W)
    PressKey(S)
    PressKey(D)
Exemple #24
0
def reverse_left():
    ReleaseKey(D)
    ReleaseKey(W)
    PressKey(S)
    PressKey(A)
Exemple #25
0
def straight_right():
    ReleaseKey(A)
    ReleaseKey(S)
    PressKey(W)
    PressKey(D)