Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
7523ff6
Update predict.py
Jan 25, 2021
30a0fff
Update ai.py
Jan 25, 2021
4ac1270
Update get_model.py
Jan 25, 2021
d6ce8cf
Update create_dataset.py
Jan 25, 2021
7a46e63
Update get_dataset.py
Jan 25, 2021
cd04260
Update train.py
Jan 25, 2021
56d4c2b
Update game_control.py
Jan 25, 2021
51a9585
Delete database_process.py
Jan 25, 2021
ee3fe77
Update ai.py
Jan 25, 2021
9a4794c
Update get_model.py
Jan 25, 2021
4c3213e
Update game_control.py
Jan 25, 2021
32f568c
Update game_control.py
Jan 25, 2021
9b55584
Update train.py
Jan 25, 2021
eba2b0c
Update get_model.py
Jan 25, 2021
8989439
Update get_dataset.py
Jan 25, 2021
101befc
Update create_dataset.py
Jan 31, 2021
946e90e
Update predict.py
Jan 31, 2021
7a64b46
Update game_control.py
Pawan-Gupta10 Sep 30, 2021
3f283ae
Merge pull request #1 from BunnieCodeX/patch-2
Nov 6, 2021
50e6872
Update README.md
Nov 10, 2021
5cfccec
Update get_dataset.py
Nov 12, 2021
7251626
Update game_control.py
Nov 12, 2021
c472ca6
Update ai.py
Nov 12, 2021
dc4394c
Update predict.py
Nov 12, 2021
7e03444
Update create_dataset.py
Nov 12, 2021
9514d03
Update get_model.py
Nov 12, 2021
61555bc
Update train.py
Nov 12, 2021
6c3d933
Update README.md
Nov 12, 2021
51a81fe
Update requirements.txt
Nov 12, 2021
de393d2
Update README.md
Nov 12, 2021
f261de4
Refractored ai.py and updated the gitignore file.
Feb 1, 2022
05ba17b
Refractored create_databse.py
Feb 1, 2022
6977d09
Refractored game_control.py. Changed that we no longer use pynput for…
Feb 1, 2022
acb96eb
Refractored get_dataset.py and changed it from scipy.misc.imread and …
Feb 1, 2022
de3be91
Refractored it and made it more correct for pep8.
Feb 1, 2022
0146b33
Refractored train.py
Feb 1, 2022
bd3e4fe
Refractoring work on predict and added missing modules to requirement…
Feb 1, 2022
30d5d6e
Changed the keras... inports to tensorflow.keras....
Feb 1, 2022
f18ccf4
Merge pull request #2 from benni347/master
Feb 3, 2022
9b077f1
Fixed the create_datset file and added more allowed keys to game_cont…
Feb 3, 2022
567badc
Merge branch 'FidgetySo:master' into master
benni347 Feb 3, 2022
67c565b
Fixed a typo in README.md
Feb 3, 2022
7f3d9f1
Merge branch 'master' of github.com:benni347/Game-Bot-1
Feb 3, 2022
220c73f
Merge pull request #3 from benni347/master
Mar 24, 2022
f612ca9
Update create_dataset.py
Apr 12, 2022
cfaa532
Update create_dataset.py
FidgetySo May 27, 2023
3d775ca
Update get_dataset.py
FidgetySo May 27, 2023
53e53f3
Update get_dataset.py
FidgetySo May 27, 2023
560b2b7
Update train.py
FidgetySo May 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 162 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,162 @@
__pycache__
.DS_Store
Data

# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# End of https://www.toptal.com/developers/gitignore/api/python

.idea/
*.png
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ Deep Learning is a subfield of machine learning with neural networks inspired by
2. Run `python3 ai.py` command in terminal.

### Creating Training Dataset:
1. Change the desired kets in the game_control.py file's get keys function, by changing return keys to desired ones
1. Run `python3 create_dataset.py` command in terminal.
2. Play your desired game.
3. Stop `create_dataset` program with `Cntrl-C` in terminal.
3. Stop `create_dataset` program with `ctrl-C` in terminal.

### Model Training:
`python3 train.py`
Expand Down
101 changes: 63 additions & 38 deletions ai.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,83 @@
# Arda Mavi
import os
import platform
"""
This is the main file for the AI.

Author: Arda Mavi
"""
import pickle

import time
import numpy as np
from time import sleep
from PIL import ImageGrab
from game_control import *
from PIL import Image
from tensorflow.keras.models import model_from_json
from mss import mss

from game_control import get_key, press, release, click
from predict import predict
from game_control import *
from keras.models import model_from_json


def main():
# Get Model:
model_file = open('Data/Model/model.json', 'r')
model = model_file.read()
model_file.close()
"""
Main function.

:return: None
"""
with open("Data/Model/model.json", "r") as model_file:
model = model_file.read()
model = model_from_json(model)
model.load_weights("Data/Model/weights.h5")

print('AI start now!')

print("AI starting now!")
with open("listfile.data", "rb") as filehandle:
# read the data as binary data stream
places_list = pickle.load(filehandle)
while 1:
# Get screenshot:
screen = ImageGrab.grab()
# Image to numpy array:
screen = np.array(screen)
with mss() as sct:
monitor = sct.monitors[1]
sct_img = sct.grab(monitor)
# Convert to PIL/Pillow Image
screen = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw",
"BGRX")
screen = np.array(screen)[
:, :, :3
] # Get first 3 channel from image as numpy array.
# 4 channel(PNG) to 3 channel(JPG)
Y = predict(model, screen)
if Y == [0,0,0,0]:
y_ai = predict(model, screen)
print(y_ai)
y_ai = places_list[y_ai]
y_ai = [int(i) for i in y_ai]
print(y_ai)
if y_ai == [0, 0, 0, 0]:
# Not action
continue
elif Y[0] == -1 and Y[1] == -1:
if y_ai[0] == -1 and y_ai[1] == -1:
# Only keyboard action.
key = get_key(Y[3])
if Y[2] == 1:
# Press:
press(key)
else:
# Release:
release(key)
elif Y[2] == 0 and Y[3] == 0:
# Only mouse action.
click(Y[0], Y[1])
else:
# Mouse and keyboard action.
# Mouse:
click(Y[0], Y[1])
# Keyboard:
key = get_key(Y[3])
if Y[2] == 1:
key = get_key(y_ai[3])
if y_ai[2] == 1:
# Press:
press(key)
else:
# Release:
release(key)
elif y_ai[2] == 0 and y_ai[3] == 0:
# Click action.
click(y_ai[0], y_ai[1])

# else:
# # Mouse and keyboard action.
# # Mouse:
# click(int(y_ai[0]), int(y_ai[1]))
# # Keyboard:
# key = get_key(int(y_ai[3]))
# if y_ai[2] == 1:
# # Press:
# press(key)
# else:
# # Release:
# release(key)

time.sleep(0.005)


if __name__ == '__main__':
if __name__ == "__main__":
main()
Loading