From ae75adf143410fbcb2d5c97cd47790eab6d9d196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Sura=C5=BCy=C5=84ski?= Date: Sat, 17 Apr 2021 21:18:20 +0200 Subject: [PATCH] refactored --- src/__pycache__/config.cpython-38.pyc | Bin 0 -> 504 bytes src/config.py | 24 ++ src/entitie/Obs.py | 11 + src/entitie/Space.py | 12 + src/entitie/Trash.py | 26 ++ src/entitie/Truck.py | 98 +++++ src/entitie/__pycache__/Obs.cpython-38.pyc | Bin 0 -> 660 bytes src/entitie/__pycache__/Space.cpython-38.pyc | Bin 0 -> 666 bytes src/entitie/__pycache__/Trash.cpython-38.pyc | Bin 0 -> 1125 bytes src/entitie/__pycache__/Truck.cpython-38.pyc | Bin 0 -> 3306 bytes src/main.py | 432 ++++++------------- 11 files changed, 311 insertions(+), 292 deletions(-) create mode 100644 src/__pycache__/config.cpython-38.pyc create mode 100644 src/config.py create mode 100644 src/entitie/Obs.py create mode 100644 src/entitie/Space.py create mode 100644 src/entitie/Trash.py create mode 100644 src/entitie/Truck.py create mode 100644 src/entitie/__pycache__/Obs.cpython-38.pyc create mode 100644 src/entitie/__pycache__/Space.cpython-38.pyc create mode 100644 src/entitie/__pycache__/Trash.cpython-38.pyc create mode 100644 src/entitie/__pycache__/Truck.cpython-38.pyc diff --git a/src/__pycache__/config.cpython-38.pyc b/src/__pycache__/config.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5882154eeb831be37563ea627c4888dea696b7c8 GIT binary patch literal 504 zcmYk2!A|2a5Qd#JZPGS@3UTJ%14M8}2-CPEQb?lMRi(KYR0k+ff*d$>_pr~iujDJI zyaE@nTaXxQzVZLd9*t%e1OudFdHnbKpM}u75x>LM;#_BwY6t{KSYQbo?8>U_eFUBF z>S(_!I?xjixS|W5=s{n&Fc2OLMIU@IfItjkNBFQS0*b~v?SXFDn&$1uz|k0Y+D~0% z_Xo;nC8jaI$QetC>TlHqXRUsLT0JsSZpq8!GG?k*@{~-Oa!NicjEeGkmW@ZMGcBmv zi^vquQpPc*gmIbxQKKL43Kv@^m?xS3#$&Q+>3_?460sQE0!LAjT|%&#gHS%nXBkt& zd7Ln!MVgngXJ&UbU@$ES;k3XL0$zSW8J?s>nuW`ufo~9K{6})%+zmcX!uxtvg^%*j zx>;6_VXxXLb+JtxYAx^?ZF2Y6qe7n3mdRZ&)bXAMDr|Qfsq%n(iY{znJ O&+;@k$JA|Fj`am4uyqvx literal 0 HcmV?d00001 diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..1dac554 --- /dev/null +++ b/src/config.py @@ -0,0 +1,24 @@ +FPS = 30 + +DEFAULT_ASSET_SIZE = 64 +DEFAULT_MAP_SIZE = 16 + +WINDOW_HEIGHT = DEFAULT_ASSET_SIZE * DEFAULT_MAP_SIZE +WINDOW_WIDTH = DEFAULT_ASSET_SIZE * DEFAULT_MAP_SIZE +PADDING = 64 + +TRASHES_COUNT = 4 + +WHITE_COLOR = (255, 255, 255) + +TRASH_BOTTLE = "TRASH_BOTTLE" +TRASH_CAN = "TRASH_CAN" + +FREE_SPACE = 0 +OBSTACLE = 1 + +R_RIGHT = "R_RIGHT" +R_LEFT = "R_LEFT" +R_UP = "R_UP" +R_DOWN = "R_DOWN" + diff --git a/src/entitie/Obs.py b/src/entitie/Obs.py new file mode 100644 index 0000000..692e953 --- /dev/null +++ b/src/entitie/Obs.py @@ -0,0 +1,11 @@ +import pygame +from src.config import DEFAULT_ASSET_SIZE + +class Obs: + def __init__(self, x, y): + self.abstractX = x + self.abstractY = y + self.object = pygame.Rect(self.abstractX * DEFAULT_ASSET_SIZE, self.abstractY * DEFAULT_ASSET_SIZE, + DEFAULT_ASSET_SIZE, DEFAULT_ASSET_SIZE) + self.asset = pygame.Surface((64, 64)) + self.asset.fill((171, 48, 74)) \ No newline at end of file diff --git a/src/entitie/Space.py b/src/entitie/Space.py new file mode 100644 index 0000000..25a3b07 --- /dev/null +++ b/src/entitie/Space.py @@ -0,0 +1,12 @@ +import pygame +from src.config import DEFAULT_ASSET_SIZE + + +class Space: + def __init__(self, x, y): + self.abstractX = x + self.abstractY = y + self.object = pygame.Rect(self.abstractX * DEFAULT_ASSET_SIZE, self.abstractY * DEFAULT_ASSET_SIZE, + DEFAULT_ASSET_SIZE, DEFAULT_ASSET_SIZE) + self.asset = pygame.Surface((64, 64)) + self.asset.fill((21, 30, 40)) diff --git a/src/entitie/Trash.py b/src/entitie/Trash.py new file mode 100644 index 0000000..3d46030 --- /dev/null +++ b/src/entitie/Trash.py @@ -0,0 +1,26 @@ +import os +import random +import pygame + +from src.config import * + + +class Trash: + def __init__(self, x, y): + self.abstractX = x + self.abstractY = y + self.generate_random_trash(x, y) + + def generate_random_trash(self, x, y): + random_index = random.randint(1, 2) + + if random_index == 1: + self.type = TRASH_BOTTLE + asset_path = os.path.join(os.path.dirname(__file__), '..', '..', 'assets', 'trashes', 'plastic-bottle.png') + else: + self.type = TRASH_CAN + asset_path = os.path.join(os.path.dirname(__file__), '..', '..', 'assets', 'trashes', 'can.png') + + self.asset = pygame.transform.scale(pygame.image.load(asset_path), (DEFAULT_ASSET_SIZE, DEFAULT_ASSET_SIZE)) + self.object = pygame.Rect(self.abstractX * DEFAULT_ASSET_SIZE, self.abstractY * DEFAULT_ASSET_SIZE, + DEFAULT_ASSET_SIZE, DEFAULT_ASSET_SIZE) diff --git a/src/entitie/Truck.py b/src/entitie/Truck.py new file mode 100644 index 0000000..aa8e3be --- /dev/null +++ b/src/entitie/Truck.py @@ -0,0 +1,98 @@ +import os + +import pygame + +from src.config import * +from src.entitie.Obs import Obs +from src.entitie.Space import Space + + +class Truck: + def __init__(self, x=0, y=0): + self.abstractX = x + self.abstractY = y + self.direction = R_RIGHT + asset_path = os.path.join(os.path.dirname(__file__), '..', '..', 'assets', 'garbage-truck.png') + self.backAsset = pygame.transform.scale(pygame.image.load(asset_path), (DEFAULT_ASSET_SIZE, DEFAULT_ASSET_SIZE)) + self.asset = pygame.transform.scale(pygame.image.load(asset_path), (DEFAULT_ASSET_SIZE, DEFAULT_ASSET_SIZE)) + self.object = pygame.Rect(self.abstractX * DEFAULT_ASSET_SIZE, self.abstractY * DEFAULT_ASSET_SIZE, + DEFAULT_ASSET_SIZE, DEFAULT_ASSET_SIZE) + self.velocity = DEFAULT_ASSET_SIZE + + def rotate_up(self): + self.asset = self.backAsset + self.asset = pygame.transform.rotate(self.asset, 90) + self.direction = R_UP + + def rotate_down(self): + self.asset = self.backAsset + self.asset = pygame.transform.rotate(self.asset, -90) + self.direction = R_DOWN + + def rotate_right(self): + self.asset = self.backAsset + self.direction = R_RIGHT + + def rotate_left(self): + self.asset = self.backAsset + self.asset = pygame.transform.rotate(self.asset, 180) + self.asset = pygame.transform.flip(self.asset, False, True) + self.direction = R_LEFT + + def move(self, map): + if self.direction == R_UP: + self.move_up(map) + if self.direction == R_DOWN: + self.move_down(map) + if self.direction == R_RIGHT: + self.move_right(map) + if self.direction == R_LEFT: + self.move_left(map) + + def replace(self, map, oldX, oldY): + map[oldX][oldY] = Space(oldX, oldY) + map[self.abstractX][self.abstractY] = self + + def move_up(self, map): + if self.abstractY == 0: + return + oldX = self.abstractX + oldY = self.abstractY + if isinstance(map[self.abstractX][self.abstractY - 1], Obs): + return + self.abstractY -= 1 + self.object.y -= self.velocity + self.replace(map, oldX, oldY) + + def move_down(self, map): + if self.abstractY == DEFAULT_MAP_SIZE - 1: + return + oldX = self.abstractX + oldY = self.abstractY + if isinstance(map[self.abstractX][self.abstractY + 1], Obs): + return + self.abstractY += 1 + self.object.y += self.velocity + self.replace(map, oldX, oldY) + + def move_right(self, map): + if self.abstractX == DEFAULT_MAP_SIZE - 1: + return + oldX = self.abstractX + oldY = self.abstractY + if isinstance(map[self.abstractX + 1][self.abstractY], Obs): + return + self.abstractX += 1 + self.object.x += self.velocity + self.replace(map, oldX, oldY) + + def move_left(self, map): + if self.abstractX == 0: + return + oldX = self.abstractX + oldY = self.abstractY + if isinstance(map[self.abstractX - 1][self.abstractY], Obs): + return + self.abstractX -= 1 + self.object.x -= self.velocity + self.replace(map, oldX, oldY) diff --git a/src/entitie/__pycache__/Obs.cpython-38.pyc b/src/entitie/__pycache__/Obs.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..eef15dceeb3b9a0ec4f99f3d420baf77e4bc6a27 GIT binary patch literal 660 zcmYjP!EVz)5S>|XoHR-$1mPEue4s{LxFD1WQdEROg{NsqGUK zkfMfEXw>2wh%X>M>4SF@n{z35!t0RqC8<3mSm6p3tOOxCaTBcI9GvU}S!C;^lWR%Q z7DYpiYtC;|w0+ajVvR^m(NV!T5}N_>8A%WYo1bJhPZ-|tk3GMaO{_Cn?mqlqe)Qqx za+cMFm+y<*8E(mk&E%XM-%b{0n&pLWW!4tX%V}9vQQ)O5s;S2>9+yABJfJOqp2cTP zUBnmWYH7>jB0gT`XPK#wO~Xt!o;8n$N3k_|T+ChR$|9z@99%AaN2zjNI;C1fWA70F zhV=DOfcP7V9$xi0^)78pGo?LqDp@JtRjO|EqT+Q=sn3h7x{a8JM6@k!2l$!{&z~n` uxz!GC{YV;qOKF!1Jjk2*w47}xu?_AJaI1z{eCPS?xi9GDfn~l9_QXHRXq7qu literal 0 HcmV?d00001 diff --git a/src/entitie/__pycache__/Space.cpython-38.pyc b/src/entitie/__pycache__/Space.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d3556dae640c1eb174700ebed291dfb38425ce88 GIT binary patch literal 666 zcmYjP!EVz)5S>|XoHR-if^g-C@PS$!kq}A*DXN5`3bzvYvbbI+b?d}ocO&7F8~u>x zz=2=dE2sPd7nn&@k+J5@j%Rju=Ix#z9(IAmy!`&b_5#$U=ZJ(fk z6g8wmqZYS7^#ZCVeJfdHd69E7_rmLt^ew49CRpJL6s!axI&l}Q;0m1V16gG2q?2n& z(H2ERjcd+tQ?z~8(PE8AW6@#3I1-xy)iaVH3N}y3Y@RYa=N||DAiJ>6WV!qFfBD&m zi)Nb53NJqvxidVJ59^C7a(p{knhEvqTbZ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g0^t1Nh zFYLctEoDc54?`iP)v7nc4w3s{`EK=Wg9hX_gL*=d7-u2Uc$nkeae8TuPjn70>YMm! zld9;DNeSOFqiR+e96!mq0E-oIUZ#eZM%l`F%ijle(dh%Q}5Cu zU%Q+Z;iCSKf{!prsJffaVSLY+XFYQf1W?s)aEw^PSj2HRFv^s)UD635iT8ag&Db00 z*Q}^19&7pJIM$2|I!&B@pJQD=0Fl0v{Qyko#vDbj8OB}%19FrgM->STp?ii?rNMCK z5b^<99M%`vJWF7Yyp^uBXs2Kx7oTED1;2h=oFHNPD2Ls&KA*|L zsX3A|z=t`~&kitv+xj`g%uP-N=YqBMf$Lz zZ#8_Q4w!LOLBj2O-ROIAU5>zea0tqTaX}(f!N{U8F-jIZ9Bz<)v zUm+b;qdb246DrH|6ImO#m36Xt$O*dj8n8lNTKYpr 0: if next <= pygame.time.get_ticks(): @@ -360,32 +235,5 @@ class Environment: ev = pygame.event.Event(pygame.KEYDOWN, {"key": pygame.K_SPACE}) pygame.event.post(ev) - def graphsearch(self, fringe, explored, istate, succ, goaltest): - fringe.append(Node(istate)) - - while True: - if len(fringe) == 0: - return False - - elem = fringe.pop() - - if goaltest(elem.state): - actions_pack = [] - while (elem.parent): - print(elem.action) - actions_pack.append(elem.action) - elem = elem.parent - - return actions_pack - - explored.append(elem) - - for [action, state] in succ(elem.state): - if not self.is_state_belongs(fringe, explored, state): - x = Node(state) - x.parent = elem - x.action = action - fringe.append(x) - environment = Environment()