{"id":18851895,"url":"https://github.com/kroshechka/basketbot","last_synced_at":"2026-04-12T22:47:24.974Z","repository":{"id":158647040,"uuid":"620419127","full_name":"KroSheChKa/BasketBot","owner":"KroSheChKa","description":"A bot that plays basketball for you (OpenCV + mss).","archived":false,"fork":false,"pushed_at":"2023-08-21T18:46:48.000Z","size":210,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-30T03:36:06.507Z","etag":null,"topics":["bot","computer-vision","game","gamebot","image-processing","opencv","python","template-matching","vk-bot","vkgame","vkgamebot","vkontakte-bot","win32api"],"latest_commit_sha":null,"homepage":"https://vk.com/app6657931","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KroSheChKa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-28T16:36:59.000Z","updated_at":"2024-12-31T17:36:38.000Z","dependencies_parsed_at":"2024-11-08T03:37:12.582Z","dependency_job_id":"c04d5d8d-372c-496f-a285-8769553b143f","html_url":"https://github.com/KroSheChKa/BasketBot","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/KroSheChKa/BasketBot","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KroSheChKa%2FBasketBot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KroSheChKa%2FBasketBot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KroSheChKa%2FBasketBot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KroSheChKa%2FBasketBot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KroSheChKa","download_url":"https://codeload.github.com/KroSheChKa/BasketBot/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KroSheChKa%2FBasketBot/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266135685,"owners_count":23881803,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bot","computer-vision","game","gamebot","image-processing","opencv","python","template-matching","vk-bot","vkgame","vkgamebot","vkontakte-bot","win32api"],"created_at":"2024-11-08T03:37:05.823Z","updated_at":"2026-04-12T22:47:19.939Z","avatar_url":"https://github.com/KroSheChKa.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BasketBot\n## A simple bot that throws the ball into a basketball hoop.\n\n**The bot works in general using computer vision `(OpenCV + mss libraries)`, and mouse movement emulation.**\n\n\u003eI decided not to use the pyautogui library, as it is quite slow. It was replaced by `win32(api/con)` which are much faster and smoother than pyautogui. Also `mss library` much faster in making screenshots than method with pyautogui.\n\n![basketbot_gif](https://github.com/KroSheChKa/BasketBot/assets/104899233/dc98a915-1dc4-47bd-a097-cf7647bf0d83)\n\n\u003e **[Video](https://youtu.be/pxDiq9SRecY) in YouTube how BasketBot works!**\n\n- Use the `BasketBot` for single runs and manual checks!\n- Use the `BasketBot_v2` for idle runs and collecting data!\n\n----\n**_Overall, the idea isn't really complicated. These 3 steps are implemented in the main program:_**\n\n### 1. We get the screen coordinates of the ring and the ball with OpenCV.\n\n### 2. Calculate at what angle you need to throw the ball so it goes into the basketball hoop without hitting the ring.\n\nAt the 2d stage, I had some difficulties with the derivation of the formula, because it must include the `physical properties of the inner world of the game`. Not surprisingly that I didn't have such data. So I had to calculate the parameters of the world on my own. (Thanks to school physics, I did it.)\n\nI did some calculations on the wall in my room:\n\n![IMG_0098](https://user-images.githubusercontent.com/104899233/232847993-deb48127-827f-455e-9220-8bce9557e147.jpg)\n\u003e In the upper right corner you can see the calculation of the `gravitational acceleration (g)` and the `initial velocity (v₀)` with the only inputs that I was able to measure: `time (t)`, `height (h)` (in pixels). In the remaining part of the wall, the derivation of the full physical formula from the system of equations\n\n\u003e \u003e P.S. Actually I recalculated them on a bigger screen resolution to get more accurate data.\n\n**Main output:**\n\n$$\nformula = tan(a) * x - \\frac{g * x^2}{2 * v₀^2 * cos(a)^2} - y\n$$\n\u003e To find the right angle, the formula value must be `close to zero`\n\nImplemented formula in python:\n```python\nformula = (tan(radians(a))*x) - (g*(pow(x, 2))) / (2*(pow(v0, 2)) * pow((cos(radians(a)), 2))) - y\n```\n\n### 3. Throw the ball with the left button pressed at a modified coefficient angle.\n\nSo why do we need to enter some other coefficient when we can substitute values into the formula, find the angle, and throw?\n\n**In this game, the angle of flight of the ball is different from the input angle**\n\n- So I had to come up with a solution. First, I introduced a constant, a coefficient. This solved the problem for the most part, but there were still a lot of flaws. After observation, I noticed that the `coefficient is dynamic` and you need to increase it as the distance between the ball and the ring increases.\n\n```python\ncoefficient = 2.167\nif x + y \u003e= 955: #As long as the distance doesn't reach 955 px, coef. is static\n    coefficient = coefficient + (math.sqrt(x + y - 955) / 53) # Compensate angle\n```\n\n**Formula calculation with a material point, not with an object**\n\n- As we know, in physics we usually take objects to be material points that have `no distance` at all. In my case, however, we are throwing a ball, which has a `decent size`! This creates a big problem, because where a material point will fly, the ball won't fly.\n\n  \u003e This is often the reason for losing. The ball hits the ring and flies away\n\n![basketbot_angle](https://github.com/KroSheChKa/BasketBot/assets/104899233/2a348895-365f-4c45-a1f2-afdad140cdae)\n\n\u003e As you can see in the gif above you can see the influence of the coefficient on the ball's trajectory. The black line is the **`desired`** angle of flight of the ball, which the formula calculates. The white line is the changed cursor trajectory by coefficient, so that the ball flew with the angle of the black line.\n\n----\n\n## How to use?\n\n#### Installation steps:\n\n- Install [python](https://www.python.org/downloads/) together with `IDLE` on your computer **(you should run the code via IDLE!)**\n- Clone this project by this command somewhere on your computer:\n\u003e **Make sure you have downloaded [git](https://git-scm.com/downloads)!**\n```\ngit clone https://github.com/KroSheChKa/BasketBot.git\n```\n- Open cmd in the created folder or press RButton in the folder and click \"`Git Bash Here`\" and paste that:\n```\npip install -r requirements.txt\n```\n**Particular case:** *If you have a monitor 3440x1440, then simply place the window with the game exactly half the screen on the left, set the **window scale 150%** and run it via `Python IDLE`.*\n\nIn other cases to run this code on your computer you will have to `change values` depending on the resolution of your monitor, such as:\n\n```python \n# This area of the game depends on your monitor resolution\nplay_zone = {'left': 662,'top': 285,'width': 617,'height': 1093}\n```\n\u003e Try to make it right on the borders of the game\n\nIn `BasketBot_v2` change theese values to a place where you see the result:\n```python \n# A small area to determine the end of the game\nscore_zone = {'left': 776,'top': 295,'width': 200,'height': 50}\n``` \n```python \n# Change the second static value to where the center of the ball is relative to the y coordinate\ncenter_b = (max_loc_b[0] + basket_w // 2, 973)\n```\n*Also you may play with threshold coefficients if something goes wrong.*\n\n- It is **necessary** to have an account in a popular CIS social network [VK.com](https://vk.com)\n  \n- Then go into the [Game](https://vk.com/app6657931)\n\n- Launch the bot **via `Python IDLE`**. Move the windows that pop out away from the window with the game.\n\n### Press Q to start. Press Q to stop the bot.\n\u003e You can change the key to another using [that table](https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes)\n\nAnd that's it.\n\u003eI hope my detailed comments in the code will help you\n\n----\n\n## The only issue\nThe only reason why the bot loses is the rare `in-game cases` where the ball is in the lower left/right corner and the ring is high in the opposite corner. Developers have not provided such an outcome and the ball just gets stuck on the basketball hoop or it just doesn't fly to it and doesn't score :\\\n\n---\n\n#### I created it for my own pleasure, to learn new and interesting solutions and features, as well as to break the records of other players XD (It really makes me feel more confident and fulfilled).\n\n---\n\n*Any suggestions? You found a bug?*\n\n-\u003e Leave a comment in [Discussions](https://github.com/KroSheChKa/BasketBot/discussions)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkroshechka%2Fbasketbot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkroshechka%2Fbasketbot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkroshechka%2Fbasketbot/lists"}