From f65bbc1b79025672ff091d5d4454c06916649586 Mon Sep 17 00:00:00 2001 From: Jager72 <49473040+Jager72@users.noreply.github.com> Date: Wed, 13 Mar 2024 23:25:49 +0100 Subject: [PATCH] feat: Added basic program loop --- .gitignore | 160 ++++++++++++++++++++++++++++++++++++++++++ Chef64new.png | Bin 0 -> 1415 bytes READ.ME | 1 + app.py | 70 ++++++++++++++++++ classes/agent.py | 38 ++++++++++ classes/cell.py | 25 +++++++ prefs.py | 7 ++ sprites/Chef64new.png | Bin 0 -> 1415 bytes sprites/wall.png | Bin 0 -> 711 bytes test.py | 37 ++++++++++ wall.png | Bin 0 -> 711 bytes 11 files changed, 338 insertions(+) create mode 100644 .gitignore create mode 100644 Chef64new.png create mode 100644 READ.ME create mode 100644 app.py create mode 100644 classes/agent.py create mode 100644 classes/cell.py create mode 100644 prefs.py create mode 100644 sprites/Chef64new.png create mode 100644 sprites/wall.png create mode 100644 test.py create mode 100644 wall.png diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6769e21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,160 @@ +# 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 + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__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 maintained 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/ \ No newline at end of file diff --git a/Chef64new.png b/Chef64new.png new file mode 100644 index 0000000000000000000000000000000000000000..54a5459cd96c264ce9db6243200f8ae298cfcc62 GIT binary patch literal 1415 zcmV;21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U?L!8;)E8nCsbL@smwBQ!vtfq44U=@mB9QSc71u&^j>{0Fm3 V<)-xxgBJh*002ovPDHLkV1n2&ngjp< literal 0 HcmV?d00001 diff --git a/READ.ME b/READ.ME new file mode 100644 index 0000000..a9b4b0f --- /dev/null +++ b/READ.ME @@ -0,0 +1 @@ +Projekt na Sztuczną Inteligencje, semstr letnim, 2024 \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..a7b7dfb --- /dev/null +++ b/app.py @@ -0,0 +1,70 @@ +import pygame +import prefs +import random +from pygame.locals import K_w, K_s, K_a, K_d +from classes.cell import Cell +from classes.agent import Agent +pygame.init() +window = pygame.display.set_mode((prefs.WIDTH, prefs.HEIGHT)) +pygame.display.set_caption("Game Window") + +def initBoard(): + global cells + cells = [] + for i in range(prefs.GRID_SIZE): + row = [] + for j in range(prefs.GRID_SIZE): + cell = Cell(i, j) + row.append(cell) + cells.append(row) + global agent + agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells) + + # Na potrzeby prezentacji tworzę sobie prostokatne sciany na które nie da się wejść + x1 = 3 + y1 = 2 + for i in range(x1, x1+4): + for j in range(y1, y1+2): + cells[i][j].prepareTexture("wall.png") + cells[i][j].blocking_movement = True + + +def draw_grid(window, cells): + for i in range(prefs.GRID_SIZE): + for j in range(prefs.GRID_SIZE): + cells[i][j].draw(window) + +initBoard() +running = True +while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + +# takie głupie kontrolki do usunięcia potem, tylko do preznetacji + + keys = pygame.key.get_pressed() + if keys[K_w] and not agent.moved: + agent.move_up() + if keys[K_s] and not agent.moved: + agent.move_down() + if keys[K_a] and not agent.moved: + agent.move_left() + if keys[K_d] and not agent.moved: + agent.move_right() + if not any([keys[K_w], keys[K_s], keys[K_a], keys[K_d]]): + agent.moved = False + + + window.fill((255, 0, 0)) + draw_grid(window, cells) + agent.draw(window) + pygame.display.update() + +pygame.quit() + + + + + + diff --git a/classes/agent.py b/classes/agent.py new file mode 100644 index 0000000..fc2d48e --- /dev/null +++ b/classes/agent.py @@ -0,0 +1,38 @@ +import pygame +import prefs +class Agent: + def __init__(self, x, y, cells): + self.sprite = pygame.image.load("Chef64new.png").convert_alpha() + self.sprite = pygame.transform.scale(self.sprite, (prefs.CELL_SIZE, prefs.CELL_SIZE)) + self.current_cell = cells[x][y] + self.moved=False + self.last_move_time=pygame.time.get_ticks() + self.cells = cells + + def move_up(self): + if pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.Y > 0 and not self.cells[self.current_cell.X][self.current_cell.Y-1].blocking_movement: + self.current_cell = self.cells[self.current_cell.X][self.current_cell.Y-1] + self.moved=True + self.last_move_time=pygame.time.get_ticks() + + def move_down(self): + if pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.Y < prefs.GRID_SIZE-1 and not self.cells[self.current_cell.X][self.current_cell.Y+1].blocking_movement: + self.current_cell = self.cells[self.current_cell.X][self.current_cell.Y+1] + self.moved=True + self.last_move_time=pygame.time.get_ticks() + + def move_left(self): + if pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.X > 0 and not self.cells[self.current_cell.X-1][self.current_cell.Y].blocking_movement: + self.current_cell = self.cells[self.current_cell.X-1][self.current_cell.Y] + self.moved=True + self.last_move_time=pygame.time.get_ticks() + + def move_right(self): + if pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.X < prefs.GRID_SIZE-1 and not self.cells[self.current_cell.X+1][self.current_cell.Y].blocking_movement: + self.current_cell = self.cells[self.current_cell.X+1][self.current_cell.Y] + self.moved=True + self.last_move_time=pygame.time.get_ticks() + + def draw(self, surface): + surface.blit(self.sprite, (self.current_cell.X * prefs.CELL_SIZE, + self.current_cell.Y * prefs.CELL_SIZE)) diff --git a/classes/cell.py b/classes/cell.py new file mode 100644 index 0000000..f11f6cc --- /dev/null +++ b/classes/cell.py @@ -0,0 +1,25 @@ +import pygame +import prefs +class Cell: + def __init__(self, x, y, blocking_movement=False, interactableItem=False, texture=None): + self.X = x + self.Y = y + self.rect = pygame.Rect(x * prefs.CELL_SIZE, y * prefs.CELL_SIZE, prefs.CELL_SIZE, prefs.CELL_SIZE) + self.blocking_movement = blocking_movement + self.interactableItem = interactableItem + if texture: + self.prepareTexture(texture) + else: + self.texture = texture + + def prepareTexture(self, texture): + preparedTexture = pygame.image.load(texture).convert_alpha() + preparedTexture = pygame.transform.scale(preparedTexture, (prefs.CELL_SIZE, prefs.CELL_SIZE)) + self.texture = preparedTexture + def draw(self,window): + if(self.texture): + window.blit(self.texture, self.rect) + else: + pygame.draw.rect(window, prefs.COLORS[(self.X*prefs.GRID_SIZE+self.Y)%len(prefs.COLORS)], self.rect) + pygame.draw.line(window, (0, 0, 0), (self.rect.left, self.rect.top), (self.rect.right, self.rect.top)) + pygame.draw.line(window, (0, 0, 0), (self.rect.left, self.rect.top), (self.rect.left, self.rect.bottom)) diff --git a/prefs.py b/prefs.py new file mode 100644 index 0000000..fe43e61 --- /dev/null +++ b/prefs.py @@ -0,0 +1,7 @@ +WIDTH = 600 +HEIGHT = WIDTH +GRID_SIZE = 12 +CELL_SIZE = WIDTH // GRID_SIZE +SPAWN_POINT = (5, 5) +COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255), (128, 0, 128), (255, 165, 0), (0, 128, 128), (128, 128, 0)] + diff --git a/sprites/Chef64new.png b/sprites/Chef64new.png new file mode 100644 index 0000000000000000000000000000000000000000..54a5459cd96c264ce9db6243200f8ae298cfcc62 GIT binary patch literal 1415 zcmV;21$g?2P)Px)K}keGRCt{2npO5X(w z;*C-j0_Gv0NKqpN#lGo_EsD}g#BEFQmS~`2V_Pa|`w$z`rAeA=lig%l6SB85e+65yu7C)!Z0}@2Bow;d4Ab0G@4q zEaXUatTM{>KZFys;gkDc&sqLp?N$Ji$q^TzrKKqle2@x+I{C)<&~G;e-(hk2Ri|no0~H=R1MYvridbu2mqN(M!TQB zbqjzu55JwWohUDuO{DAOc70P+5$k?yeawQdrCt}AQRgDurs{r%q_ z*V;)I+1B3adi@*84GP`?fq%uO=I332?TvaQCv*1F2k!weF);yTSM%3;J_g{UrdPE4 z>L0EEaHvI{`%6x-?O+>ldluQ(t8$2*?xUfcQ1A{29S8W&{My=8_7?ZeeNjx5?(oWo zA6E_#Pl94AaQD0{7<#MxFoN@ zt^pwtcnx@GpUVCuwm)}HodW-SW!?ek0~hkI;`tzRTo7NKb^vzP6}bTK?t1~q<_=GE z2mJ;L)&Zu7cI?>hUG=#DgR@J|T|Z>bA86C({?57uSLZhi>i|$H%pMQ6_t`Q;rGbJ4y2Q9~9OBK^w+mF$dt}$&*?{ zIGC=I$V@9==`J9+c%#Sx829xf{fC8hK+uNCWKu3QC9Ynd0-*1&e}QbaD7%1vl^6Jx z7snLee-8lhTD_k|{fB~eK+uL?Tr1649-UF|1V!;f9Y?hLJHcQ7hIjUuufMRc4!GML z$lMlX@3b38{pDtlVPPF$nsD5kvjdm^(aL2`FLHnuFUWIIUh1u@RA+=m(KV)r3Rnl2 zbHKuxV*sSeYE^-JhD2&mIbqf10rmOlUL(8$>wvHXqP(DNX2A760qDye_)(l524JqF z+N-DU4jV>)kxR(|oW=c_azep7AaF+2S|0}>wpp#x$!il@wpM?yDnIePdK0u~cO3wi z{#Ne_N7HK^fXb3dcRBS1vCS2Z*B<~~mj;7=1;9EWxC0)Jtp}iLLsb1UWUn6UCyuE( z?N?T?CR*wMOiiaW=lGS+$2)R!+5z}IITrLQ0M-G)cY$J0$u&kbnLMfrS~ znoceGT-p!#c%{EJQ8U?L!8;)E8nCsbL@smwBQ!vtfq44U=@mB9QSc71u&^j>{0Fm3 V<)-xxgBJh*002ovPDHLkV1n2&ngjp< literal 0 HcmV?d00001 diff --git a/sprites/wall.png b/sprites/wall.png new file mode 100644 index 0000000000000000000000000000000000000000..954907e0e5f889112048bf5bfb40f03422faa932 GIT binary patch literal 711 zcmV;&0yzDNP)K3~{q=gyIXjMU$T`Ot@Av!j`K+P?2fK9nAAu%L zxpUXiEYWe8IG~kM3Pf>ywbU*HbWwCTPSeC@8HQn+rl!mtYR>8@$~@1m&J0Miq``&q z1Db&{IIVM;#xkWgq#B7RgR9nmJ~^F&{p&XIR&D_%Hp_alwjMo z3Q=9u_V?j%Ftf+w0n>iJzuj(tZrc`il|{}u=e*nPn6$p{pUJdiT8%4L*AaX#+p{kcawA*Wxk=##_9h^%pWuX=}5<_8;SW&R~x?C>o4YDBFaU97P zKY+sX<l0qR;{!F5NDHaQRWPs)2&CV$xMBuiA7v4LYZk1*fCcMC8zQ zM7p+aG@^0{YX%fl_3zT_Vrtxcw%YYnqU$mJw;Z&gdgpy8Hzp^gbUvS}yP-46+EF+6+HeOn%21y8^G#S{ zl??2a+s56xI#~Xc^qWt{7~$UVn|xYZZLna`GL9oM-gTXAt3@c|u%n6Bh?AQ-v!|=g tMubh95}jY>jcD7B;%(&;La(xS{{bMg9jfZZ4H*Cc002ovPDHLkV1oC|Uo`*# literal 0 HcmV?d00001 diff --git a/test.py b/test.py new file mode 100644 index 0000000..4e5ed20 --- /dev/null +++ b/test.py @@ -0,0 +1,37 @@ +# Assuming you have an image file named "sprite.png" in the same directory +import pygame + +# Initialize Pygame +pygame.init() + +# Set up the display +screen = pygame.display.set_mode((800, 600)) +pygame.display.set_caption("Sprite Example") + +# Load the image +sprite_image = pygame.image.load("Chef64new.png").convert_alpha() + +# Scale the image to 200x200 +sprite_image = pygame.transform.scale(sprite_image, (200, 200)) + +# Get the rect of the sprite image for positioning +sprite_rect = sprite_image.get_rect() + +# Main game loop +running = True +while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + + # Clear the screen + screen.fill((255, 255, 255)) + + # Draw the sprite image onto the screen at position (250, 150) + screen.blit(sprite_image, (250, 150)) + + # Update the display + pygame.display.flip() + +pygame.quit() +pygame.quit() diff --git a/wall.png b/wall.png new file mode 100644 index 0000000000000000000000000000000000000000..954907e0e5f889112048bf5bfb40f03422faa932 GIT binary patch literal 711 zcmV;&0yzDNP)K3~{q=gyIXjMU$T`Ot@Av!j`K+P?2fK9nAAu%L zxpUXiEYWe8IG~kM3Pf>ywbU*HbWwCTPSeC@8HQn+rl!mtYR>8@$~@1m&J0Miq``&q z1Db&{IIVM;#xkWgq#B7RgR9nmJ~^F&{p&XIR&D_%Hp_alwjMo z3Q=9u_V?j%Ftf+w0n>iJzuj(tZrc`il|{}u=e*nPn6$p{pUJdiT8%4L*AaX#+p{kcawA*Wxk=##_9h^%pWuX=}5<_8;SW&R~x?C>o4YDBFaU97P zKY+sX<l0qR;{!F5NDHaQRWPs)2&CV$xMBuiA7v4LYZk1*fCcMC8zQ zM7p+aG@^0{YX%fl_3zT_Vrtxcw%YYnqU$mJw;Z&gdgpy8Hzp^gbUvS}yP-46+EF+6+HeOn%21y8^G#S{ zl??2a+s56xI#~Xc^qWt{7~$UVn|xYZZLna`GL9oM-gTXAt3@c|u%n6Bh?AQ-v!|=g tMubh95}jY>jcD7B;%(&;La(xS{{bMg9jfZZ4H*Cc002ovPDHLkV1oC|Uo`*# literal 0 HcmV?d00001