import pygame from display import Scaling class FieldSprite(pygame.sprite.Sprite): def __init__(self, area: tuple ): super().__init__() self.area = area self.update() def update(self): self.rect = Scaling.area_to_rect(self.area) self.image = pygame.Surface(self.rect.size) self.image.fill("green") class Field(): def __init__(self): self.sprites = pygame.sprite.Group() # self.updated = True self.sprites.add(FieldSprite( (0, 0, 40_000, 28_000) )) # self.areas = [(0,0,40_000,28_000)] def coordinates_inside_area(self, area, x, y) -> bool: if x < area[0]: return False if y < area[1]: return False if x >= area[0]+area[2]: return False if y >= area[1]+area[3]: return False return True def slice(self, pos: tuple, direction: bool, thickness: int) -> pygame.Rect: """ Slice one area into two areas """ # Find the overlapping area for field in self.sprites: if self.coordinates_inside_area(field.area, pos[0], pos[1]): break else: return None # remove current area ax, ay, aw, ah = field.area field.remove(self.sprites) # create new areas if there is any space # self.updated = True if direction: x1 = ax x2 = pos[0] - thickness x3 = pos[0] + thickness x4 = ax + aw if x2 > x1: self.sprites.add(FieldSprite( (x1, ay, x2-x1, ah) )) if x4 > x3: self.sprites.add(FieldSprite( (x3, ay, x4-x3, ah) )) return Scaling.area_to_rect((x2, ay, x3-x2, ah)) y1 = ay y2 = pos[1] - thickness y3 = pos[1] + thickness y4 = ay + ah if y2 > y1: self.sprites.add(FieldSprite( (ax, y1, aw, y2-y1) )) if y4 > y3: self.sprites.add(FieldSprite( (ax, y3, aw, y4-y3) )) return Scaling.area_to_rect((ax, y2, aw, y3-y2)) def __update_sprites(self): """""" # self.sprites.empty() # for area in self.areas: # self.sprites.add(FieldSprite(Scaling.area_to_rect(area))) # self.updated = False def get_sprites(self) -> pygame.sprite.Group: # if self.updated: # self.__update_sprites() return self.sprites def __del__(self): pass