def brake(): PressKey(S) time.sleep(0.2) ReleaseKey(W) ReleaseKey(A) ReleaseKey(D) ReleaseKey(S)
def accelerate(): PressKey(W) time.sleep(0.2) ReleaseKey(A) ReleaseKey(D) ReleaseKey(S) ReleaseKey(W)
def right(): if random.randrange(0, 3) == 1: PressKey(W) else: ReleaseKey(W) PressKey(D) ReleaseKey(A) ReleaseKey(S)
def no_keys(): if random.randrange(0, 3) == 1: PressKey(W) else: ReleaseKey(W) ReleaseKey(A) ReleaseKey(S) ReleaseKey(D)
def right(): PressKey(D) time.sleep(0.175) PressKey(W) time.sleep(0.025) ReleaseKey(D) ReleaseKey(W) ReleaseKey(A) ReleaseKey(S)
def main(): for i in list(range(4))[::-1]: print(i+1) time.sleep(1) tic = time.time() paused = False while True: if not paused: tic = time.time() capture_region = (0,40,640,520) image = grab_screen(capture_region) image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) image = cv2.resize(image, (80,60)) cv2.imshow('Test', image) prediction = model.predict([image.reshape(WIDTH, HEIGHT,1)]) moves = np.around(prediction) # Taking the move to be arg max of predictions moves = np.int32(prediction == np.max(prediction)) print('Moves: ', moves[0], ' Prediction: ',prediction[0]) print((moves[0] == [0,1,0,0]).all()) if ((moves[0] == [1,0,0,0]).all()): print('Taking Left') left() elif ((moves[0] == [0,1,0,0]).all()): print('Going Straight') accelerate() elif ((moves[0] == [0,0,1,0]).all()): print('Taking Right') right() elif ((moves[0] == [0,0,0,1]).all()): print('Braking') brake() keys = key_check() 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)
def turn(self, action): if self.mouse_up and action >= 0.5: PressKey(W) self.mouse_up = False self.have_turned = True if not self.mouse_up and action < 0.5: ReleaseKey(W) self.mouse_up = True
def reset(self): print('reset') self.turn(-1) time.sleep(1.3) PressKey(Q) screen = self.get_screen() time.sleep(0.05) row, col, is_nan = self.process_img(screen) state = [row, col, 0, int(self.mouse_up)] self.ep_start = time.time() self.time_checked = self.ep_start self.have_turned = False self.prev_state = [row, col] ReleaseKey(Q) print('exit_reset') return state
def reverse_left(): PressKey(S) PressKey(A) ReleaseKey(W) ReleaseKey(D)
def forward_right(): PressKey(W) PressKey(D) ReleaseKey(A) ReleaseKey(S)
def forward_left(): PressKey(W) PressKey(A) ReleaseKey(D) ReleaseKey(S)
def reverse(): PressKey(S) ReleaseKey(A) ReleaseKey(W) ReleaseKey(D)
def left(): PressKey(A) PressKey(W) ReleaseKey(D)
if moves == 0: # print("straight") straight() elif moves == 1: # print("left") left() elif moves == 2: # print("right") right() # if cv2.waitKey(25) & 0xFF == ord('q'): # cv2.destroyAllWindows() # break keys = key_check() if 'Q' in keys: break # t for pause script so you can get in position again if 'T' in keys: if paused: paused = False time.sleep(1) else: paused = True ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1)
def release_all(): ReleaseKey(W) ReleaseKey(A) ReleaseKey(S) ReleaseKey(D)
def release_every_key(): for key in game_keys: ReleaseKey(key)
def pause_button(): PressKey(ESC) ReleaseKey(ESC)
def reverse_right(): PressKey(S) PressKey(D) ReleaseKey(W) ReleaseKey(A)
import os import glob import time from PIL import ImageGrab, Image from direct_keys import PressKey, ReleaseKey, W, A, S, D # Preallocating screengrab array screengrab = np.zeros((480, 640, 3)) for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) print('down') PressKey(W) time.sleep(5) ReleaseKey(A) print('up') PressKey(S) time.sleep(3) def process_screengrab(orig_image): # colored image = [[0-255],[0-255],[0-255]] GRAY = [0-255] processed_image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2GRAY) processed_image = cv2.adaptiveThreshold(processed_image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ cv2.THRESH_BINARY,15,5) # processed_image = cv2.Canny(processed_image,threshold1=100,threshold2=300) return processed_image while (True):
def press_label(label): global last_print_time if time.time() - last_print_time > 1: last_print_time = time.time() print(label) if simulate: return if label == "NO": release_all() pass # release_all() elif label == "WA": PressKey(W) PressKey(A) ReleaseKey(S) ReleaseKey(D) elif label == "WD": PressKey(W) PressKey(D) ReleaseKey(A) ReleaseKey(S) elif label == "AS": PressKey(A) PressKey(S) ReleaseKey(W) ReleaseKey(D) elif label == "SD": PressKey(S) PressKey(D) ReleaseKey(W) ReleaseKey(A) elif label == "A": PressKey(A) ReleaseKey(W) ReleaseKey(S) ReleaseKey(D) elif label == "D": PressKey(D) ReleaseKey(W) ReleaseKey(A) ReleaseKey(S) elif label == "S": PressKey(S) ReleaseKey(W) ReleaseKey(A) ReleaseKey(D) elif label == "W": PressKey(W) ReleaseKey(A) ReleaseKey(S) ReleaseKey(D)
def slow_ya_roll(): ReleaseKey(W) ReleaseKey(A) ReleaseKey(D)
def straight(): PressKey(W) ReleaseKey(A) ReleaseKey(D)
def straight(): PressKey(Z) ReleaseKey(J) ReleaseKey(L)
def right(): PressKey(D) PressKey(W) ReleaseKey(A)
def left(): PressKey(Z) PressKey(J) ReleaseKey(L)
def movimiento(): PressKey(W) print('key pressed') #time.sleep(5) ReleaseKey(W)
def right(): PressKey(Z) PressKey(L) ReleaseKey(J)