Merge pull request 'mozna zgetowac tile po row i column do tego jest funkcja get_tile w grid;' (#18) from field into master

Reviewed-on: #18
This commit is contained in:
Juliusz Sadowski 2022-04-08 17:16:51 +02:00
commit c1d05eb1d1
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 import pygame
from logic.exceptions import FieldNotWalkable
class Field(pygame.sprite.Sprite): class Field(pygame.sprite.Sprite):
def __init__(self, texture_path, converted_texture, img, rect, row=0, column=0): 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': if self.texture_path == 'water.png' or self.texture_path == 'grass_with_tree.jpg':
self.busy = True self.busy = True
def step_onto(self, obj): # ustawia objekt na srodku pola def put_on(self, obj): # ustawia objekt na srodku pola
obj.rect = obj.clamp(self.rect) if not self.busy:
obj.rect = obj.rect.clamp(self.rect)
self.busy = True self.busy = True
obj.update() obj.update()
else:
raise FieldNotWalkable

View File

@ -117,6 +117,8 @@ class Game:
castle_spawn.spawn() castle_spawn.spawn()
#grid.put_on_tile(0, 0, knights_left[0])
while running: while running:
self.screen.blit(self.bg, (0, 0)) 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, \ 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 BORDER_RADIUS, NBR_OF_TREES, NBR_OF_WATER
from .exceptions import FieldNotWalkable
from .field import Field from .field import Field
@ -66,6 +67,15 @@ class Grid:
texture_index = random.randint(0, len(self.textures) - 3) texture_index = random.randint(0, len(self.textures) - 3)
return self.textures[texture_index] 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): def draw(self, screen):
bg_width = (GRID_CELL_PADDING + GRID_CELL_WIDTH) * COLUMNS + BORDER_WIDTH bg_width = (GRID_CELL_PADDING + GRID_CELL_WIDTH) * COLUMNS + BORDER_WIDTH
bg_height = (GRID_CELL_PADDING + GRID_CELL_HEIGHT) * ROWS + BORDER_WIDTH bg_height = (GRID_CELL_PADDING + GRID_CELL_HEIGHT) * ROWS + BORDER_WIDTH