def cruising(): time.sleep(1) PressKey(keys.W) time.sleep(3) ReleaseKey(keys.W) PressKey(keys.C) ReleaseKey(keys.C)
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])
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)
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)
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)
def none(): ReleaseKey(D) ReleaseKey(S) ReleaseKey(W) ReleaseKey(A) PressKey(S) time.sleep(0.4) ReleaseKey(S)
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")
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")
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)
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
def forward(): PressKey(keys.W)
def left(delay=0): PressKey(keys.A) time.sleep(delay) ReleaseKey(keys.A)
def right(delay=0): PressKey(keys.D) time.sleep(delay) ReleaseKey(keys.D)
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)
def right(): ReleaseKey(A) ReleaseKey(S) ReleaseKey(W) PressKey(D)
def left(): ReleaseKey(D) ReleaseKey(S) ReleaseKey(W) PressKey(A)
def backward(): PressKey(S) ReleaseKey(A) ReleaseKey(D) ReleaseKey(W)
def right(): PressKey(W) PressKey(D) ReleaseKey(A) time.sleep(t_time) ReleaseKey(D)
def left(): PressKey(A) PressKey(W) ReleaseKey(D) time.sleep(t_time) ReleaseKey(A)
def forward(): PressKey(W) ReleaseKey(A) ReleaseKey(D) ReleaseKey(S)
def reverse(): ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) PressKey(S)
def straight_left(): ReleaseKey(D) ReleaseKey(S) PressKey(W) PressKey(A)
def reverse_right(): ReleaseKey(A) ReleaseKey(W) PressKey(S) PressKey(D)
def reverse_left(): ReleaseKey(D) ReleaseKey(W) PressKey(S) PressKey(A)
def straight_right(): ReleaseKey(A) ReleaseKey(S) PressKey(W) PressKey(D)