How to Build a Python Bot to Automate a Simple Web Game
This tutorial explains how to use Python libraries such as autopy, PIL, and pywin32 to capture screen images, recognize game elements, and automate mouse movements and clicks, enabling a bot to automatically play a sushi‑making browser game.
Introduction
Many gamers are familiar with cheats, but this article shows how to create a simple game bot using Python. The goal is not to gain unfair advantage in large online games, but to practice programming techniques by automating a small browser game where customers order sushi.
Tools Preparation
Install the autopy, PIL (Pillow), and pywin32 packages. autopy provides cross‑platform mouse and keyboard simulation and screen capture. PIL handles image processing, and pywin32 simplifies some Windows‑specific actions.
For screen capture you can use any tool; the author uses PicPick. An editor such as Vim is used for coding.
Mouse Movement
The command autopy.mouse.move(x, y) moves the cursor to the specified screen coordinates (origin at the top‑left corner). Note that the library’s coordinate conversion may be slightly inaccurate due to integer division: point.x *= 0xFFFF / GetSystemMetrics(SM_CXSCREEN); A more precise formula would be:
point.x = point.x * 0xFFFF / GetSystemMetrics(SM_CXSCREEN);Mouse Click
Click actions are fast; insert a short time.sleep() if the game does not respond quickly enough.
Keyboard Operations
Keyboard input is not required for this particular game, so it is omitted.
Image Capture and Analysis
Capture the game screen with PIL.ImageGrab.grab() or autopy.bitmap.capture_screen(). Determine the fixed positions of customer avatars and ingredient slots relative to the game window.
Similar Image Search Principle
Instead of using autopy.bitmap.find_bitmap (which requires exact pixel matches), the tutorial implements a custom image fingerprinting method. Each ingredient image is resized to a small fixed size (e.g., 18×13) and converted to a binary hash.
The similarity between hashes is measured using Hamming distance:
def hamming_dist(self, hash1, hash2): return sum(itertools.imap(operator.ne, hash1, hash2))If the minimum distance is below a threshold (e.g., 50), the corresponding dish is identified; otherwise the region is considered empty.
Automated Cooking
Store the recipe for each of the eight dishes as a list of ingredient coordinates. The bot moves the mouse to the ingredient locations, clicks them, and drags the finished dish to the customer. The following class encapsulates the process (code omitted for brevity): # class definition image omitted Overall, the bot demonstrates basic screen‑based automation, image processing, and simple pattern matching, providing a hands‑on example for improving Python programming skills.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
