diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..26c77e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,248 @@ + Created by https://www.toptal.com/developers/gitignore/api/pycharm,python +# Edit at https://www.toptal.com/developers/gitignore?templates=pycharm,python + +### PyCharm ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +.idea +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### PyCharm Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +# https://plugins.jetbrains.com/plugin/7973-sonarlint +.idea/**/sonarlint/ + +# SonarQube Plugin +# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator-enh.xml +.idea/**/markdown-navigator/ + +# Cache file creation bug +# See https://youtrack.jetbrains.com/issue/JBR-2257 +.idea/$CACHE_FILE$ + +# CodeStream plugin +# https://plugins.jetbrains.com/plugin/12206-codestream +.idea/codestream.xml + +### 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/ +pip-wheel-metadata/ +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/ +pytestdebug.log + +# 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/ +doc/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.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 + +# 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/ +pythonenv* + +# 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/ + +# profiling data +.prof + +# End of https://www.toptal.com/developers/gitignore/api/pycharm,python \ No newline at end of file diff --git a/images/Tiles/grass_01.png b/images/Tiles/grass_01.png new file mode 100644 index 0000000..3b2a585 Binary files /dev/null and b/images/Tiles/grass_01.png differ diff --git a/images/Tiles/grass_02.png b/images/Tiles/grass_02.png new file mode 100644 index 0000000..934b21a Binary files /dev/null and b/images/Tiles/grass_02.png differ diff --git a/images/Tiles/grass_mine_01.png b/images/Tiles/grass_mine_01.png new file mode 100644 index 0000000..33be860 Binary files /dev/null and b/images/Tiles/grass_mine_01.png differ diff --git a/images/Tiles/grass_mine_02.png b/images/Tiles/grass_mine_02.png new file mode 100644 index 0000000..0606764 Binary files /dev/null and b/images/Tiles/grass_mine_02.png differ diff --git a/images/Tiles/grass_rock_01.png b/images/Tiles/grass_rock_01.png new file mode 100644 index 0000000..1a6ad51 Binary files /dev/null and b/images/Tiles/grass_rock_01.png differ diff --git a/images/Tiles/grass_rock_02.png b/images/Tiles/grass_rock_02.png new file mode 100644 index 0000000..cc96ea5 Binary files /dev/null and b/images/Tiles/grass_rock_02.png differ diff --git a/images/Tiles/mud_01.png b/images/Tiles/mud_01.png new file mode 100644 index 0000000..dab0866 Binary files /dev/null and b/images/Tiles/mud_01.png differ diff --git a/images/Tiles/mud_02.png b/images/Tiles/mud_02.png new file mode 100644 index 0000000..c2d6ba6 Binary files /dev/null and b/images/Tiles/mud_02.png differ diff --git a/images/Tiles/silt_01.png b/images/Tiles/silt_01.png new file mode 100644 index 0000000..070163a Binary files /dev/null and b/images/Tiles/silt_01.png differ diff --git a/images/Tiles/silt_02.png b/images/Tiles/silt_02.png new file mode 100644 index 0000000..d653768 Binary files /dev/null and b/images/Tiles/silt_02.png differ diff --git a/images/mine_icon.png b/images/mine_icon.png new file mode 100644 index 0000000..70ae6cc Binary files /dev/null and b/images/mine_icon.png differ diff --git a/images/sapper_idle/cpt_bomba.png b/images/sapper_idle/cpt_bomba.png new file mode 100644 index 0000000..1ec8e3f Binary files /dev/null and b/images/sapper_idle/cpt_bomba.png differ diff --git a/images/sapper_idle/cpt_bomba_left.png b/images/sapper_idle/cpt_bomba_left.png new file mode 100644 index 0000000..866ddee Binary files /dev/null and b/images/sapper_idle/cpt_bomba_left.png differ diff --git a/images/sapper_idle/cpt_bomba_right.png b/images/sapper_idle/cpt_bomba_right.png new file mode 100644 index 0000000..0d55992 Binary files /dev/null and b/images/sapper_idle/cpt_bomba_right.png differ diff --git a/images/sapper_idle/idle.png b/images/sapper_idle/idle.png new file mode 100644 index 0000000..51711cc Binary files /dev/null and b/images/sapper_idle/idle.png differ diff --git a/images/sapper_run/sapper_run_01.png b/images/sapper_run/sapper_run_01.png new file mode 100644 index 0000000..edbf8be Binary files /dev/null and b/images/sapper_run/sapper_run_01.png differ diff --git a/images/sapper_run/sapper_run_02.png b/images/sapper_run/sapper_run_02.png new file mode 100644 index 0000000..4e6fdce Binary files /dev/null and b/images/sapper_run/sapper_run_02.png differ diff --git a/images/sapper_run/sapper_run_03.png b/images/sapper_run/sapper_run_03.png new file mode 100644 index 0000000..eb72fd7 Binary files /dev/null and b/images/sapper_run/sapper_run_03.png differ diff --git a/images/sapper_run/sapper_run_04.png b/images/sapper_run/sapper_run_04.png new file mode 100644 index 0000000..3e8c71b Binary files /dev/null and b/images/sapper_run/sapper_run_04.png differ diff --git a/images/sapper_run/sapper_run_05.png b/images/sapper_run/sapper_run_05.png new file mode 100644 index 0000000..4541cd4 Binary files /dev/null and b/images/sapper_run/sapper_run_05.png differ diff --git a/images/sapper_run/sapper_run_06.png b/images/sapper_run/sapper_run_06.png new file mode 100644 index 0000000..3438eb3 Binary files /dev/null and b/images/sapper_run/sapper_run_06.png differ diff --git a/images/sapper_run/sapper_run_07.png b/images/sapper_run/sapper_run_07.png new file mode 100644 index 0000000..32ce850 Binary files /dev/null and b/images/sapper_run/sapper_run_07.png differ diff --git a/images/sapper_run/sapper_run_08.png b/images/sapper_run/sapper_run_08.png new file mode 100644 index 0000000..b647522 Binary files /dev/null and b/images/sapper_run/sapper_run_08.png differ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0254322 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pygame==2.0.1 diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/agent.py b/src/agent.py new file mode 100644 index 0000000..a67276e --- /dev/null +++ b/src/agent.py @@ -0,0 +1,10 @@ +from const import SAPPER_IDLE + + +class Agent: + def __init__(self, x: int = 0, y: int = 0, x_px: int = 0, y_px: int = 0): + self.img = SAPPER_IDLE + self.x_px = x_px + self.y_px = y_px + self.x = x + self.y = y diff --git a/src/const.py b/src/const.py new file mode 100644 index 0000000..6157f57 --- /dev/null +++ b/src/const.py @@ -0,0 +1,41 @@ +from pygame import image + +WIDTH = 800 +HEIGHT = 800 + +IMAGES = [] +for img in ['grass_01', 'grass_02', 'grass_mine_01', 'grass_mine_02', 'grass_rock_01', 'grass_rock_02']: + IMAGES.append(image.load('C:/Users/matix/Documents/Python/Saper/images/Tiles/' + img + ".png")) + +ICON = 'C:/Users/matix/Documents/Python/Saper/images/mine_icon.png' +SAPPER_IDLE = image.load('C:/Users/matix/Documents/Python/Saper/images/sapper_idle/cpt_bomba.png') + +SAPPER_RUNNING = [] +"""for img in [ + 'sapper_run_01', + 'sapper_run_02', + 'sapper_run_03', + 'sapper_run_04', + 'sapper_run_05', + 'sapper_run_06', + 'sapper_run_07', + 'sapper_run_08', +]:""" +for img in [ + 'cpt_bomba_left', + 'cpt_bomba_right', +]: + SAPPER_RUNNING.append(image.load('C:/Users/matix/Documents/Python/Saper/images/sapper_idle/' + img + ".png")) + +DEFAULT_FIELD = [ + [1, 3, 4, 5, 0, 1, 2, 3, 2, 0], + [1, 5, 0, 2, 1, 0, 4, 1, 0, 0], + [1, 0, 1, 1, 5, 4, 5, 1, 5, 4], + [4, 1, 5, 1, 0, 2, 4, 0, 3, 3], + [1, 5, 0, 1, 4, 4, 1, 1, 1, 5], + [1, 4, 5, 4, 0, 5, 4, 0, 2, 1], + [0, 2, 1, 3, 0, 3, 5, 5, 5, 4], + [1, 2, 0, 2, 0, 4, 0, 2, 0, 0], + [5, 2, 0, 1, 3, 1, 2, 1, 0, 0], + [2, 5, 1, 4, 5, 1, 0, 5, 4, 0], +] diff --git a/src/environment.py b/src/environment.py new file mode 100644 index 0000000..5c4c78e --- /dev/null +++ b/src/environment.py @@ -0,0 +1,10 @@ +from typing import List + +from const import DEFAULT_FIELD + + +class Environment: + def __init__(self, field: List[List[int]] = DEFAULT_FIELD): + self.field = field + self.rows = len(field) + self.cols = len(field[0]) diff --git a/src/game_ui.py b/src/game_ui.py new file mode 100644 index 0000000..543a05c --- /dev/null +++ b/src/game_ui.py @@ -0,0 +1,30 @@ +import pygame + +from agent import Agent +from environment import Environment +from const import WIDTH, HEIGHT, IMAGES, SAPPER_RUNNING, SAPPER_IDLE + + +class GameUi: + def __init__(self, agent: Agent, env: Environment): + self.agent = agent + self.env = env + self.clock = pygame.time.Clock() + self.screen = pygame.display.set_mode((WIDTH, HEIGHT)) + + def move(self, coord: str, shift: int): + setattr(self.agent, coord, getattr(self.agent, coord) + shift) + for x in range(8): + setattr(self.agent, f'{coord}_px', getattr(self.agent, f'{coord}_px') + (shift * 10)) + self.agent.img = SAPPER_RUNNING[x % 2] + self.update() + self.clock.tick(15) + self.agent.img = SAPPER_IDLE + self.update() + + def update(self): + for x in range(10): + for y in range(10): + self.screen.blit(IMAGES[self.env.field[y][x]], (x * 80, y * 80)) + self.screen.blit(self.agent.img, (self.agent.x_px, self.agent.y_px)) + pygame.display.update() diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..efa830c --- /dev/null +++ b/src/main.py @@ -0,0 +1,40 @@ +import pygame + +from const import ICON +from agent import Agent +from game_ui import GameUi +from environment import Environment + + +def main(): + pygame.init() + pygame.display.set_caption('Super Saper') + pygame.display.set_icon(pygame.image.load(ICON)) + + env = Environment() + agent = Agent() + game_ui = GameUi(agent, env) + game_ui.update() + + running = True + while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_d and agent.x_px < 700: + game_ui.move('x', 1) + elif event.key == pygame.K_a and agent.x_px > 5: + game_ui.move('x', -1) + elif event.key == pygame.K_s and agent.y_px < 700: + game_ui.move('y', 1) + elif event.key == pygame.K_w and agent.y_px > 0: + game_ui.move('y', -1) + elif event.key == pygame.K_SPACE: + if env.field[agent.y][agent.x] in [2, 3]: + env.field[agent.y][agent.x] -= 2 + game_ui.update() + + +if __name__ == "__main__": + main()