• This repository has been archived on 24/Jan/2023
  • Stars
    star
    118
  • Rank 299,923 (Top 6 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created about 2 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple python bot (powered by computer vision) used to play a game (City Island 5). The bot is able to play the game and collect points without any human intervention.

Gaming using Computer Vision

A python bot used to play a game using computer vision and image processing techniques. The bot is able to play the game without any human intervention. It managed to make me a millionaire in the game overnight!

play-game-with-computer-vision

Here's a screen recording of the game play:

activity.mp4

Access a comprehensive guide in this article: https://paulonteri.com/thoughts/play-game-with-computer-vision.

Disclaimer - this content is for educational purposes only!


Why?

I've been playing strategy + city building + simulation? games like TownsMen 6, Clash of the Clans, SimCity for the last 10 years.

On trying out City Island 5 I found it mildly irritating that my collectables could not accumulate while I was outside the game. I might have had the best businesses, strategy, etc but I had to be in the game to ensure I collect the cash/keys/gold overtime. For example, if my bakery makes €100 per minute I would only earn €100 after leaving the game and coming back 24 hours later.

This became especially tiresome while trying to accumulate €5,000,000 required to buy the island shown below. This would take me roughly two weeks of gameplay if I don't spend any money - it's not worth it.

game_screenshot_island


How it works

1. Capture the live game feed

I needed a way to capture the live game feed.

The easiest way to to capture an in-game screenshot and pass it to the next steps in the script.

2. Identify the valuables in the screenshot

We need a way to detect a valuable in the game's feed and then return its coordinates.

OpenCv's Template Matching algorithms are perfect for this.

They are used for searching and finding the location of a template image (like a valuable) in a larger image (like the game's feed). It simply slides the template image over the input image (as in 2D convolution) and compares the template and patch of the input image under the template image. Several comparison methods are implemented in OpenCV. (You can check docs for more details). We use it in the method: cv2.matchTemplate(... )

To achieve this, I needed the template images. I took screenshots by hand and then cropped off the cash, star and key:

cash

key

star

3. Collect the valuables by clicking on them

Once we have the coordinates of an item we can try to click on it.

pyautogui's .click(x,y) function works like magic for this. It clicks the screen on the coordinates x and y where our valuable is lying. Learn more about it here.

click_cash.mp4
click_key.mp4
click_star.mp4

4. Close any popups that may appear

Our clicks above may result in popups when we are being given a reward, levelling up, etc.

We need to close it before attempting to collect valuables again. We use the same logic used in finding and clicking on valuables.

To achieve this, I needed the template images for the popups' close buttons so that they can be clicked. I took screenshots by hand and then cropped off the various close buttons:

close

continue_level

close_button.mp4

5. Repeat

We do the steps above repeatedly to collect the valuables while the script is running.

activity.mp4

Results after running overnight

I started the game with €316,415.

game_screenshot_start

The following morning I had €6,463,870.

game_screenshot_end

I made €6,147,455 overnight!

I then proceeded to buy the Island I wanted:

image


Regrets

  • This is cheating.
  • Why get a game if you're not the one playing it?

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


Maintainers

Current maintainers:


License

Apache License 2.0


Conclusion

Access the full guide in this article here: https://paulonteri.com/thoughts/play-game-with-computer-vision

This was fun!

More Repositories

1

remote-code-execution-environment

Have you ever wondered how code execution on competitive programming websites like leetcode works? Code that runs code. Tried implementing that.
JavaScript
152
star
2

data-structures-and-algorithms

My notes and solutions/code on Data Structures and Algorithms.
Python
128
star
3

django-serverless-cron

django-serverless-cron 🦑 A Django library with a simpler approach running cron jobs in a serverless environment through HTTP requests. This allows you to run any task without having to manage always-on infrastructure. https://pypi.org/project/django-serverless-cron
Python
55
star
4

ecommerce

Online Store built with Python (Django) and JavaScript (React). Live site - ecommerce.paulonteri.com
HTML
36
star
5

freelance-marketplace

This is a digital freelancing marketplace that will connect talented individuals with economic opportunities and help organisations cut down on the cost of managing human capital by getting individuals to work for them on a short-term basis.
PHP
29
star
6

shule-s-frontend

School management, E-learning, plus Powerful Data & Communication Tools For Modern Schools. https://shulesuite.com
JavaScript
12
star
7

sms-mgt-backend

SMS Backend - built with Django Rest framework. A simple web API that can manage SMS sending using the Africa's Talking SMS platform. It has contact management and stores all the SMSs sent & received in a secure way.
Python
11
star
8

livefitfood-clone

Meal delivery website clone built with NodeJS & Express. Hosted at: https://livefitfood-clone.herokuapp.com
JavaScript
8
star
9

notes

Note-taking web application with accessibility features such as dark theme, font-size change and language change. Built with Django(Python) & React(Javascript).
JavaScript
5
star
10

web-hosting-homepage

A simple website homepage for web hosting company built with HTML, CSS and a sprinkle of Javascript.
CSS
5
star
11

django-boilerplate

Django boilerplate
Python
4
star
12

django-wordpress

Fetch data from a Wordpress site and save it in Django
Python
3
star
13

algorithm-visualizer

JavaScript
3
star
14

django_azure

Serverless Python on Azure: Django hosted on Azure App Service
Python
3
star
15

school_system

School System
Python
3
star
16

backend-sms

A simple web API that can manage SMS sending using the Africa's Talking SMS platform. It has contact management and stores all the SMSs sent & received in a secure way.
Python
3
star
17

video-search-engine

Python
2
star
18

ecommerce-client

JavaScript
2
star
19

dsc-cuea-react.js-talk

JavaScript
2
star
20

google-get-ahead-africa

Python
2
star
21

django-rich-text-search-blog-post-code

Python
2
star
22

blog-posts

Blog posts on https://paulonteri.com
2
star
23

hospital-app-backend

Hospital App Backend API built with NodeJS
JavaScript
1
star
24

django-api-boilerplate

Python
1
star
25

inuua-product-detail

CSS
1
star
26

paulonteri.com

1
star
27

algoexpert

1
star
28

node-express-backend

NodeJS, Express & Express-Handlebars Template
HTML
1
star
29

connect-four-game

A web-based game built with jQuery
JavaScript
1
star
30

data-structures-and-algorithms-in-python

Simple Data Structures and Algorithms written in Python.
Python
1
star
31

Android-AndelaLearningCommunity4.0-project1

Java
1
star
32

android-talk

Code used in my Android talk on SheCode Africa
Java
1
star