From deabeeac43984add178ac91b958b60b68ecc9bf3 Mon Sep 17 00:00:00 2001 From: XsedoX Date: Fri, 8 Apr 2022 17:15:01 +0200 Subject: [PATCH] mozna zgetowac tile po row i column do tego jest funkcja get_tile w grid; step_onto -> put_on(zmiana nazwy), dziala poprawnie(maly bug byl);funkcja put_on_tile(row, column, obj) sluzy polozeniu danego obiektu na danym tilu, przyklad uzycia zakomentowany w game.py(wywolujcie po spawnerze, bo nadpisze pozycje xd); dorobilem wlasny exception, gdy tile jest zajety w pliku exceptions.py - moze sie przyda ten plik --- logic/exceptions.py | 2 ++ logic/field.py | 13 +++++++++---- logic/game.py | 2 ++ logic/grid.py | 18 ++++++++++++++---- 4 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 logic/exceptions.py diff --git a/logic/exceptions.py b/logic/exceptions.py new file mode 100644 index 0000000..560a7a5 --- /dev/null +++ b/logic/exceptions.py @@ -0,0 +1,2 @@ +class FieldNotWalkable(Exception): + pass diff --git a/logic/field.py b/logic/field.py index 5f4c7d6..0faa309 100644 --- a/logic/field.py +++ b/logic/field.py @@ -1,5 +1,7 @@ import pygame +from logic.exceptions import FieldNotWalkable + class Field(pygame.sprite.Sprite): def __init__(self, texture_path, converted_texture, img, rect, row=0, column=0): @@ -17,7 +19,10 @@ class Field(pygame.sprite.Sprite): if self.texture_path == 'water.png' or self.texture_path == 'grass_with_tree.jpg': self.busy = True - def step_onto(self, obj): # ustawia objekt na srodku pola - obj.rect = obj.clamp(self.rect) - self.busy = True - obj.update() + def put_on(self, obj): # ustawia objekt na srodku pola + if not self.busy: + obj.rect = obj.rect.clamp(self.rect) + self.busy = True + obj.update() + else: + raise FieldNotWalkable diff --git a/logic/game.py b/logic/game.py index 90621d4..436d514 100644 --- a/logic/game.py +++ b/logic/game.py @@ -117,6 +117,8 @@ class Game: castle_spawn.spawn() + #grid.put_on_tile(0, 0, knights_left[0]) + while running: self.screen.blit(self.bg, (0, 0)) diff --git a/logic/grid.py b/logic/grid.py index ce1a675..8074713 100644 --- a/logic/grid.py +++ b/logic/grid.py @@ -4,6 +4,7 @@ import pygame from common.constants import ROWS, COLUMNS, GRID_CELL_PADDING, GRID_CELL_WIDTH, GRID_CELL_HEIGHT, BORDER_WIDTH, \ BORDER_RADIUS, NBR_OF_TREES, NBR_OF_WATER +from .exceptions import FieldNotWalkable from .field import Field @@ -15,15 +16,15 @@ class Grid: self.busy_fields = [] podkladka = np.zeros((ROWS, COLUMNS)) for drzewa in range(0, NBR_OF_TREES): - rzad = random.randint(0, ROWS-1) - kolumna = random.randint(0, COLUMNS-1) + rzad = random.randint(0, ROWS - 1) + kolumna = random.randint(0, COLUMNS - 1) if podkladka[rzad][kolumna] == 0: podkladka[rzad][kolumna] = 1 else: drzewa = drzewa - 1 for i in range(0, NBR_OF_WATER): - rzad = random.randint(0, ROWS-1) - kolumna = random.randint(0, COLUMNS-1) + rzad = random.randint(0, ROWS - 1) + kolumna = random.randint(0, COLUMNS - 1) if podkladka[rzad][kolumna] == 0: podkladka[rzad][kolumna] = 2 else: @@ -66,6 +67,15 @@ class Grid: texture_index = random.randint(0, len(self.textures) - 3) return self.textures[texture_index] + def get_tile(self, row, column): + return pygame.sprite.Group.sprites(self.grid[row][column])[0] # iteruj kolumny i wiersze od 0 + + def put_on_tile(self, row, column, obj): # iteruj kolumny i wiersze od 0 + try: + self.get_tile(row, column).put_on(obj) + except FieldNotWalkable: + print("Field busy") + def draw(self, screen): bg_width = (GRID_CELL_PADDING + GRID_CELL_WIDTH) * COLUMNS + BORDER_WIDTH bg_height = (GRID_CELL_PADDING + GRID_CELL_HEIGHT) * ROWS + BORDER_WIDTH -- 2.20.1