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:
parent
e768a534a4
commit
deabeeac43
2
logic/exceptions.py
Normal file
2
logic/exceptions.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
class FieldNotWalkable(Exception):
|
||||||
|
pass
|
@ -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
|
||||||
|
@ -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))
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user