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
This commit is contained in:
XsedoX 2022-04-08 17:15:01 +02:00
parent e768a534a4
commit deabeeac43
4 changed files with 27 additions and 8 deletions

2
logic/exceptions.py Normal file
View File

@ -0,0 +1,2 @@
class FieldNotWalkable(Exception):
pass

View File

@ -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

View File

@ -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))

View File

@ -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