def __init__(self, surface: Surface, position: (int, int)): GameObject.__init__(self, surface) # Save sprite self._original_image = surface self._disabled_image = surface.copy() self._disabled_image.fill((255, 255, 255, 128), None, pygame.BLEND_RGBA_MULT) self.move(Vector2(position[0], position[1])) self._player: Player = None
def build_tile_surface_dict(dir_path, size, file_suffix="svg"): front_tile_paths = get_all_tile_front_paths(dir_path, file_suffix) back_path = os.path.join(dir_path, "Front." + file_suffix) back_base = Surface(size, pygame.SRCALPHA) load_image(back_path, back_base, size) result_dict = dict() for key, value in front_tile_paths.items(): surface = back_base.copy() load_image(value, surface, size) result_dict[key] = surface.convert_alpha() return result_dict
def add_color(src_surface: Surface, color: tuple, alpha: int = 128) -> Surface: """ Накладывает color на переданный surface, добавляя цвет к исходному изображению. :param src_surface: Исходное изображение. :param color: Накладываемый цвет. :param alpha: Прозрачность накладываемого цвета. :return: Изменённое изображение. """ src_size = src_surface.get_size() return_surface = Surface(src_size, SRCALPHA) return_surface.blit(src_surface, (0, 0)) # Исходное изображение add_surface = src_surface.copy() # Копия изображения fill_color(add_surface, color, max_alpha=alpha) # Заливаем копию нужным цветом return_surface.blit( add_surface, (0, 0)) # Накладываем копию поверх исходного изображения return return_surface
class Table: """ table with a title containing several columns and rows, only columns as objects """ def __init__(self, title, number_of_columns, number_of_rows, column_titles, field_size): """ :param title: str; tables title, dsipalyed on top of the table :param number_of_columns: int; number of columns in the table :param number_of_rows: int; number of rows in the table :param column_titles: list[str, ...]; list containg all columns' titles :param field_size: float; size of a virtual field that is determined by the size of the window that inhabits the GUI """ self.title = title font = SysFont(None, int(field_size * 3 / 2)) # sets font self.writing = Writing( title, font, DARKGREEN, (field_size, field_size / 5)) # initializes title as writing self.fake_size = [number_of_columns, number_of_rows] # sets size in rows and colums column_list = [ ] # creates a lsit later containg every column inhabiting the table for i in range(number_of_columns): column_list.append( Column(number_of_rows, column_titles[i], i, title)) # creates a column self.columns = column_list self.refresh_loc(field_size) # initializes coordiantes and locations def refresh_loc(self, field_size): """ updates coordinates on teh table surface :param field_size: float; size of a virtual field that is determined by the size of the window that inhabits the GUI """ # gets surface with fitting size if self.title == "Remaining Ships": self.location = [field_size * 25, field_size * 5 ] # updates location on the game window column_width = 3 else: self.location = [field_size * 10, field_size * 5.5] column_width = 3.75 self.surf = Surface((self.fake_size[0] * 4 * field_size, self.fake_size[1] * 3 * field_size)) self.writing.font = SysFont(None, int(field_size * 3 / 2)) # resizes title's font # calculates title's center self.writing.top_left_corner = [ field_size * self.columns.__len__() * column_width / 2, field_size * 3 / 4 ] for column in self.columns: # goes through every column column.refresh_loc(field_size) # updates its locations def draw_outline(self, table_surf): """ does nothing, use to draw outline? :param table_surf: Surface; surface the table is shown on """ pass def draw(self, language, screen, ships=[[[0]]]): """ dsiplays table on table surface and table surface on the game window :param language: str; language the program's texts are currently displayed in :param screen: Surface; surface that covers the whole window :param ships: list[list[Ship, ..], list]; list containing all ships that are on the board """ table_surf = self.surf.copy( ) # gets a new surface to prevent overlaying writings table_surf.set_colorkey( BLACK ) # sets a colorkey to make table surface transparent apart from its content self.writing.content = translate_title( language, self.title) # translates title if neccessary self.writing.draw(table_surf, True) # displays its title self.draw_outline( table_surf) # does nothing, potentially display an outline here? for column in self.columns: # goes through every column column.draw(language, table_surf, ships) # displays it screen.blit(table_surf, self.location) # displays table surface on game window
class BaseScreen(): """ Base Class Screen. Use to manage screen of games Use for exec main_loop, update screen. Enable to switch screen easier. Example : +------+ +------+ | Game | <----> | Menu | +------+ +------+ ^^ || vv +-------+ | Pause | +-------+ Raise a ChangeScreenException to change screen. """ def __init__(self, width, height, background=None): """ Init of baseScreen. Save the background : - BEFORE init_entities_before ( sprite ) - AFTER init_entities_after (Attach entities or text to background) so init_entities save elements with background init_entities """ self.surface = Surface((width, height)) if background is not None: self.surface.blit(background, background.get_rect()) self.init_entities_before(self.surface) self.background = self.surface.copy() self.init_entities_after(self.surface) def init_entities_after(self, surface): """ Create entities after saving background. Need to be redefined""" pass def init_entities_before(self, surface): """ Create entities before saving background. Need to be redefined""" pass def main_loop(self): """ Function to use in main loop. Return update list of coordinates to refresh. """ self.execute(self.surface) self.erase_all_map() return self.draw(self.surface) def execute(self, surface): """ Exec and Update entities of the screen. Need to be redefined. """ pass def erase_all_map(self): """ Erase all background Can redefine this function """ self.surface = self.background.copy() def draw(self, surface): """ Draw entities in the surface. Need to be redefined. """ pass