dino_api.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:go_dino 作者: pauloalves86 项目源码 文件源码
def play_game(get_command_callback: Callable[[int, int, int], str]) -> int:
    with mss() as screenshotter:
        get_game_landscape_and_set_focus_or_die(screenshotter)
        reset_game()
        landscape = get_game_landscape_and_set_focus_or_die(screenshotter, .95)

        start_game()
        gameover_template = cv2.imread(os.path.join('templates', 'dino_gameover.png'), 0)
        start = time.time()
        last_distance = landscape['width']
        x1, x2, y1, y2 = compute_region_of_interest(landscape)
        speed = 0
        last_compute_speed = time.time()
        last_speeds = [3] * 30
        last_command_time = time.time()

        while True:
            buffer = screenshotter.grab(landscape)
            image = Image.frombytes('RGB', buffer.size, buffer.rgb).convert('L')
            image = np.array(image)
            image += np.abs(247 - image[0, x2])
            roi = image[y1:y2, x1:x2]
            score = int(time.time() - start)
            distance, size = compute_distance_and_size(roi, x2)
            speed = compute_speed(distance, last_distance, speed, last_speeds, last_compute_speed)
            last_compute_speed = time.time()
            # Check GAME OVER
            if distance == last_distance or distance == 0:
                res = cv2.matchTemplate(image, gameover_template, cv2.TM_CCOEFF_NORMED)
                if np.where(res >= 0.7)[0]:
                    reset_game()
                    return score
            last_distance = distance
            if time.time() - last_command_time < 0.6:
                continue
            command = get_command_callback(distance, size, speed)
            if command:
                last_command_time = time.time()
                pyautogui.press(command)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号