def show(self): screen = display.get_surface() draw.rect(screen, self._color, (self._x, self._y, self._width, self._height)) if not self.__controller.stopped: cell_x = self._x cell_y = self._y for line in self.__model.data: for cell in line: if cell == 1: draw.rect(screen, self.__living_cell_color, (cell_x, cell_y, self.__cell_size, self.__cell_size)) cell_x = cell_x + self.__cell_size cell_y = cell_y + self.__cell_size cell_x = self._x for child in self._children: child.show() else: font = SysFont('', 60) current_strategy_text = self.__current_strategy_text + self.__controller.strategy welcome_text = font.render(self.__welcome_message, True, PG_COLORS.get('white')) current_strategy = font.render(current_strategy_text, True, PG_COLORS.get('white')) instruction_text = font.render(self.__instruction_text, True, PG_COLORS.get('white')) text_width, text_height = font.size(self.__welcome_message) cs_text_width, cs_text_height = font.size(current_strategy_text) i_text_width, i_text_height = font.size(self.__instruction_text) screen.blit(welcome_text, (self._x + self._width / 2 - text_width / 2, self._y + self._height / 2 - text_height * 2.5)) screen.blit(instruction_text, (self._x + self._width / 2 - i_text_width / 2, self._y + self._height / 2 - text_height)) screen.blit(current_strategy, (self._x + self._width / 2 - cs_text_width / 2, self._y + self._height / 2 + text_height / 2))
def generateNewTextImage(self) -> NoReturn: from Mangers.graph_manager import DimensionsManger diments = DimensionsManger() fontInit() font = SysFont(get_default_font(), diments.VerticesDiments.fontSize) keyImage = font.render(str(self.name), True, Colors.VerticesColors.OnVertexDefaultColor) wText, hText = font.size(str(self.name)) self.wTextHalf, self.hTextHalf = wText // 2, hText // 2 self.textImage = keyImage self.isMoved = True
class Text(Component): def __init__(self, container, text="", fontType="", x=0, y=0, textSize=0, color=COLORS.WHITE, onActivate=None): self.text = text self.textSize = textSize self.color = color self.font = SysFont(fontType, self.textSize) self.size = self.font.size(self.text) self.surface = self.font.render(self.text, True, self.color) super().__init__(container, x, y, self.size, surface=self.surface, onActivate=onActivate) def setColor(self, color): color = [a if a < 255 else 255 for a in color] color = [a if a > 0 else 0 for a in color] self.color = color self.update() def setText(self, text): self.text = text self.update() def getText(self): return self.text def update(self): self.surface = self.font.render(self.text, True, self.color) self.rect = self.surface.get_rect() def select(self): self.setColor(COLORS.GREY) def unSelect(self): self.setColor(COLORS.BLACK)
def display(self, g): g.win().blit(self.background, (self.x - self.offsetX, self.y - self.offsetY)) g.win().blit(self.talker.pic, (self.x - self.offsetX, self.y - self.offsetY)) w = self.talker.pic.get_width() x_text, y_text = self.x + w + 10, self.y + 5 font = SysFont("arial_unicode_ms", 40) words = [line.split(" ") for line in self.msg.splitlines()] space = font.size(' ')[0] for line in words: for word in line: word_surface = font.render(word, 1, (0, 0, 0)) word_width, word_height = word_surface.get_size() if x_text + word_width + self.offsetX >= self.background.get_width( ) + self.x: x_text = self.x + w + 10 # Reset the x. y_text += word_height # Start on new row. g.win().blit(word_surface, (x_text, y_text)) x_text += word_width + space x_text = self.x + w + 10 # Reset the x. y_text += word_height # Start on new row.
class Window: # A Window represents a display window with a title bar, # close box and interior drawing surface. def __init__(self, title, width, height): # Create and open a window to draw in. # - self is the new Window # - title is the str title of the window # - width is the int pixel width of the window # - height is the int pixel height of the window init() self.__surface__ = set_mode((width, height), 0, 0) set_caption(title) self.__font_name__ = '' self.__font_size__ = 18 self.__font__ = SysFont(self.__font_name__, self.__font_size__, True) self.__font_color__ = 'white' self.__bg_color__ = 'black' self.__auto_update__ = True try: self.stream = Stream() self.stream.start() except: pass def close(self): # Close the window # - self is the Window self.stream.stop() quit() def set_font_name(self, name): # Set the name of the window font used to draw strings # - self is the Window # - name is the str name of the font self.__font_name__ = name self.__font__ = SysFont(self.__font_name__, self.__font_size__, True) def set_font_size(self, point_size): # Set the point size of the window font used to draw strings # - self is the Window # - point_size is the int point size of the font self.__font_size__ = point_size self.__font__ = SysFont(self.__font_name__, self.__font_size__, True) def set_font_color(self, color_string): # Set the font color used to draw in the window # - self is the Window # - color_string is the str name of the font color self.__font_color__ = color_string def set_bg_color(self, color_string): # Set the background color used to draw in the window # - self is the Window # - color_string is the str name of the background color self.__bg_color__ = color_string def set_auto_update(self, true_false): # Set the background color used to draw in the window # - self is the Window # - true_or_false is a Boolean indicating if auto update should be on or off self.__auto_update__ = true_false def get_font_height(self): # Return the int pixel height of the current font. # - self is the Window return self.__font__.size('')[1] def get_font_color(self): # Return a str that represents the current window # font color # - self is the Window return self.__font_color__ def get_bg_color(self): # Return a str that represents the current window # background color # - self is the Window return self.__bg_color__ def set_bg_image(self, image_png, width, height): image = pygame.image.load(image_png) image = pygame.transform.scale(image, (self.get_width(), self.get_height())) self.__surface__.blit(image, [0, 0]) def get_width(self): # Return the int pixel width of the window's drawable # interior surface # - self is the Window return self.__surface__.get_width() def get_height(self): # Return the int pixel height of the window's drawable # interior surface # - self is the Window return self.__surface__.get_height() def clear(self): # Erase the window contents # - self is the Window if self.__auto_update__: update() def get_surface(self): # Return the Pygame.Surface object that represents the # interior drawing surface of the window # - self is the Window return self.__surface__ def draw_string(self, string, x, y, bg): # Draw a string in the window using the current font and # colors # - self is the Window # - string is the str object to draw # - x is the int x coord of the upper left corner of the # string in the window # - y is the int y coord of the upper left corner of the # string in the window text_image = self.__font__.render(string, True, Color(self.__font_color__), bg) self.__surface__.blit(text_image, (x, y)) if self.__auto_update__: text_rect = Rect((x, y), text_image.get_size()) update(text_rect) def input_string(self, prompt, x, y): # Draw a prompt string in the window using the current font # and colors. Check keys pressed by the user until an enter # key is pressed and return the sequence of key presses as a # str object. # - self is the Window # - prompt is the str to display # - x is the int x coord of the upper left corner of the # string in the window # - y is the int y coord of the upper left corner of the # string in the window key = K_SPACE answer = '' while key != K_RETURN: self.draw_string(prompt + answer + ' ', x, y) if not self.__auto_update__: update() key = self._get_key() key_state = get_pressed() if (K_SPACE <= key <= K_z): if key == K_SPACE: letter = ' ' else: letter = name(key) if key_state[K_LSHIFT] or key_state[K_RSHIFT] or key_state[ K_CAPSLOCK]: letter = letter.upper() answer = answer + letter if key == K_BACKSPACE: answer = answer[0:len(answer) - 1] return answer def get_string_width(self, string): # Return the int pixel width of the string using the current # font. # - self is the Window # - string is the str object return self.__font__.size(string)[0] def update(self): # Update the window by copying all drawn objects from the # frame buffer to the display. # - self is the Window update() def _get_key(self): # Poll the events until the user presses a key and return it. # Discard all other events. # - self is the Window event = poll() while event.type != KEYUP: event = poll() return event.key
class Window: # A Window represents a display window with a title bar, # close box and interior drawing surface. def __init__(self, title, width, height): # Create and open a window to draw in. # - self is the new Window # - title is the str title of the window # - width is the int pixel width of the window # - height is the int pixel height of the window init() self.__surface__ = set_mode((width, height), 0, 0) set_caption(title) self.__font_name__ = '' self.__font_size__ = 18 self.__font__ = SysFont(self.__font_name__, self.__font_size__, True) self.__font_color__ = 'white' self.__bg_color__ = 'black' self.__auto_update__ = True def close(self): # Close the window quit() def set_font(self, font): """ set the font to the pygame.Font object specified """ # assert isinstance(font, SysFont) self.__font = font self.__font_name = "" # I don't know how to get font name from font... def set_font_name(self, name): # Set the name of the window font used to draw strings # - name is the str name of the font self.__font_name__ = name self.__font__ = SysFont(self.__font_name__, self.__font_size__, True) def get_font(self): """ gets and returns the pygame.Font object of the active font. """ return self.__font__ def get_font_name(self): # gets and returns the string name of the active font. return self.__font_name__ def set_font_size(self, point_size): # Set the point size of the window font used to draw strings # - point_size is the int point size of the font self.__font_size__ = point_size self.__font__ = SysFont(self.__font_name__, self.__font_size__, True) def set_font_color(self, given_color): # Set the font color used to draw in the window. # Will accept colors as either a tuple of RGB values, # pygame-compatible color names as strings, or pygame.color() objects. if type(given_color) == Color: self.__font_color__ = given_color return self.__font_color__ = Color(given_color) def set_bg_color(self, given_color): # Set the background color used to draw in the window. # Will accept colors as either a tuple of RGB values, # pygame-compatible color names as strings, or pygame.color() objects. if type(given_color) == Color: self.__bg_color__ = given_color return self.__bg_color__ = Color(given_color) def set_auto_update(self, true_false): # Set the background color used to draw in the window # - true_or_false is a Boolean indicating if auto update should be on or off self.__auto_update__ = true_false def get_font_height(self): # Return the int pixel height of the current font. return self.__font__.size('')[1] def get_font_color(self): # Returns the pygame.Color object for the font. return self.__font_color__ def get_bg_color(self): # Returns the pygame.Color object for the background. return self.__bg_color__ def get_width(self): """ Return the int pixel width of the window's drawable interior surface. """ return self.__surface__.get_width() def get_height(self): """ Return the int pixel height of the window's drawable interior surface. """ return self.__surface__.get_height() def clear(self): """ Erase the window contents """ self.__surface__.fill(self.__bg_color__) if self.__auto_update__: update() def get_surface(self): """ Return the Pygame.Surface object that represents the interior drawing surface of the window. """ return self.__surface__ def draw_string(self, string, x, y): """ Draw a string in the window using the current font and colors. - string is the str object to draw - x is the int x coord of the upper left corner of the string in the window - y is the int y coord of the upper left corner of the string in the window """ # text_image = self.__font__.render(string, True, Color(self.__font_color__), Color(self.__bg_color__)) text_image = self.__font__.render(string, True, self.__font_color__, self.__bg_color__) self.__surface__.blit(text_image, (x, y)) if self.__auto_update__: text_rect = Rect((x, y), text_image.get_size()) update(text_rect) def input_string(self, prompt, x, y): """ Draw a prompt string in the window using the current font and colors. Check keys pressed by the user until an enter key is pressed and return the sequence of key presses as a str object. - self is the Window - prompt is the str to display - x is the int x coord of the upper left corner of the string in the window - y is the int y coord of the upper left corner of the string in the window """ key = K_SPACE answer = '' while key != K_RETURN: self.draw_string(prompt + answer + ' ', x, y) if not self.__auto_update__: update() key = self._get_key() key_state = get_pressed() if (K_SPACE <= key <= K_z): if key == K_SPACE: letter = ' ' else: letter = name(key) if key_state[K_LSHIFT] or key_state[K_RSHIFT] or key_state[ K_CAPSLOCK]: letter = letter.upper() answer = answer + letter if key == K_BACKSPACE: answer = answer[0:len(answer) - 1] return answer def get_string_width(self, string): """ Return the int pixel width of the string using the current font. - self is the Window - string is the str object """ return self.__font__.size(string)[0] def update(self): """ Update the window by copying all drawn objects from the frame buffer to the display. - self is the Window """ update() def _get_key(self): # Poll the events until the user presses a key and return it. # Discard all other events. # - self is the Window event = poll() while event.type != KEYUP: event = poll() return event.key
class Button: def __init__( self, surface, x: int, y: int, click_handler: Callable = lambda: None, text="", width=0, height=0, color: Tuple[int] = None, border_width=0, hover_color=None, clicked_color=None, border_radius=0, border_color=None, font: pygame.font.Font = None, font_color=None ): self.surface = surface self.x = x self.y = y self.click_handler = click_handler self.color = color or (224, 224, 224) self.border_width = border_width self.hover_color = hover_color self.clicked_color = clicked_color self.border_radius = border_radius self.text = text if font is None: self.font = SysFont('couriernew', 20) text_size = self.font.size(text) self.width = width or text_size[0] + self.border_width + 2 self.height = height or text_size[1] + self.border_width + 2 self.font_color = font_color or (0, 0, 0) self.hovered = False self.clicked = False self.rect = pygame.Rect(self.x, self.y, self.width, self.height) def __repr__(self): return f'<Button "{self.text}" at ({self.x}, {self.y})>' def __contains__(self, point: Tuple[int]): return self.rect.collidepoint(point) def draw(self): color = self.color if self.clicked and self.clicked_color: color = self.clicked_color elif self.hovered and self.hover_color: color = self.hover_color if not self.border_width: _round_rect(self.surface, self.rect, color, self.border_radius) else: _round_rect(self.surface, self.rect, (0, 0, 0), self.border_radius) _round_rect( self.surface, self.rect.inflate(-self.border_width, -self.border_width), color, self.border_radius ) text = self.font.render(self.text, 1, self.font_color) place = text.get_rect(center=self.rect.center) self.surface.blit(text, place) def handle_event(self, event): if event.type == pygame.MOUSEMOTION: self.hovered = event.pos in self elif event.type == pygame.MOUSEBUTTONDOWN and event.pos in self: self.clicked = True self.click_handler() elif event.type == pygame.MOUSEBUTTONUP: self.clicked = False def handle_events(self, event_list): for event in event_list: self.handle_event(event)
class Button: """ Button class Contructor params: surface: surface to display button x, y: top-left coordinates click_handler: function that is called when the button is clicked, default is lambda, doing nothing text: text to display on button width, height: button size. If not provided, calculated by size of text color: button color, default is (224, 224, 224) hover_color: button color when hovered, if None (default), no effect clicked_color: button color when clicked, if None (default), no effect border_color, border_width, border_radius - border params font: text font, pygame.font.Font instance, default is Courier New 20 font_color: default is black Methods defined: draw(): draws the button on given surface handle_event(event): handles mouse events (hover and click). Must be called in main loop. handle_events(event_list): handles all events in event_list To check if given point is inside the button, use 'in' operator: if (120, 50) in button: ... """ def __init__(self, surface: pygame.surface.Surface, x: int, y: int, click_handler: Callable = lambda: None, text="", width=0, height=0, color: Tuple[int] = None, border_width=0, hover_color=None, clicked_color=None, border_radius=0, border_color=None, font: pygame.font.Font = None, font_color=None): self.surface = surface self.x = x self.y = y self.click_handler = click_handler self.color = color or (224, 224, 224) self.border_width = border_width self.hover_color = hover_color self.clicked_color = clicked_color self.border_radius = border_radius self.text = text if font is None: self.font = SysFont('couriernew', 20) text_size = self.font.size(text) self.width = width or text_size[0] + self.border_width + 2 self.height = height or text_size[1] + self.border_width + 2 self.font_color = font_color or (0, 0, 0) self.hovered = False self.clicked = False self.rect = pygame.Rect(self.x, self.y, self.width, self.height) def __repr__(self): return f'<Button "{self.text}" at ({self.x}, {self.y})>' def __contains__(self, point: Tuple[int]): return self.rect.collidepoint(point) def draw(self): color = self.color if self.clicked and self.clicked_color: color = self.clicked_color elif self.hovered and self.hover_color: color = self.hover_color if not self.border_width: _round_rect(self.surface, self.rect, color, self.border_radius) else: _round_rect(self.surface, self.rect, (0, 0, 0), self.border_radius) _round_rect( self.surface, self.rect.inflate(-self.border_width, -self.border_width), color, self.border_radius) text = self.font.render(self.text, 1, self.font_color) place = text.get_rect(center=self.rect.center) self.surface.blit(text, place) def handle_event(self, event): if event.type == pygame.MOUSEMOTION: self.hovered = event.pos in self elif event.type == pygame.MOUSEBUTTONDOWN and event.pos in self: self.clicked = True self.click_handler() elif event.type == pygame.MOUSEBUTTONUP: self.clicked = False def handle_events(self, event_list): for event in event_list: self.handle_event(event)
class Text: def __init__(self, **kw): """ wrapper for pygame's font class font_name -> name of the font text -> the text value size -> font size bold -> specifies if the font is bold italic -> specifies if the font is italic antialias -> specifies if the font has antialiasing color -> the font color background -> the font background (color?) """ self._text = kw.get('text', '') self._antialias = kw.get('antialias', False) self._font_name = kw.get('font_name', 'Arial') self._bold = kw.get('bold', False) self._italic = kw.get('italic', False) self._color = kw.get('color', Color('white')) self._size = kw.get('size', 16) self._background = kw.get('background') self._render: Surface = None self._needs_sysfont_update = True self._needs_render_update = True self.last_text = self.text self.font: PygameFont = None self.update_sysfont() self.update_render() def update_sysfont(self): self.font = SysFont(self.font_name, self.size, self.bold, self.italic) self.needs_sysfont_update = False def update_render(self): self._render = self.font.render(self.text, self.antialias, self.color, self.background) self._needs_render_update = False @property def text_size(self): return self.font.size(self.text) @property def render(self): if self._needs_render_update: self.update_render() if self.needs_sysfont_update: self.update_sysfont() return self._render @property def needs_sysfont_update(self): return self._needs_sysfont_update @needs_sysfont_update.setter def needs_sysfont_update(self, value): assert isinstance( value, bool), 'new needs_sysfont_update value must be a bool' if not self._needs_render_update and value: self._needs_render_update = value self._needs_sysfont_update = value @property def text(self): return self._text @text.setter def text(self, value): if not isinstance(value, str): value = str(value) self._text = value self._needs_render_update = True @property def size(self): return self._size @size.setter def size(self, value): assert isinstance( value, (int, float)), 'new size value must be a int or float' self._size = value self.needs_sysfont_update = True @property def color(self): return self._color @color.setter def color(self, value): assert isinstance( value, (Color, tuple, list)), 'new color value must be a Color, or RBG tuple' self._color = value self._needs_render_update = True @property def italic(self): return self._italic @italic.setter def italic(self, value): assert isinstance(value, bool), 'new italic value must be a bool' self._italic = value self.needs_sysfont_update = True @property def bold(self): return self._bold @bold.setter def bold(self, value): assert isinstance(value, bool), 'new bold value must be a bool' self._bold = value self.needs_sysfont_update = True @property def font_name(self): return self._font_name @font_name.setter def font_name(self, value): assert isinstance(value, str), 'new font_name must be a str' self._font_name = value self.needs_sysfont_update = True @property def antialias(self): return self._antialias @antialias.setter def antialias(self, value): assert isinstance(value, bool), 'new antialias value must be a bool' self._antialias = value self.needs_sysfont_update = True @property def background(self): return self._background @background.setter def background(self, value): assert isinstance( value, (Color, tuple, list)), 'new background must be a Color/RBG tuple or list' self._background = value self._needs_render_update = True
class Window: """A Window represents a display window with a title bar, close box and interior drawing surface.""" def __init__(self, title, width, height): """Create and open a window to draw in. :param title: the str title of the window :param width: the int pixel width of the window :param height: the int pixel height of the window """ init() self.__surface__ = set_mode((width, height), 0, 0) set_caption(title) self.__font_name__ = '' self.__font_size__ = 18 self.__font__ = SysFont(self.__font_name__, self.__font_size__, True) self.__font_color__ = 'white' self.__bg_color__ = 'black' self.__auto_update__ = True def close(self): """Close the window.""" quit() def set_font_name(self, name): """Set the name of the window font used to draw strings. :param name: the str name of the font """ self.__font_name__ = name self.__font__ = SysFont(self.__font_name__, self.__font_size__, True) def set_font_size(self, point_size): """Set the point size of the window font used to draw strings. :param point_size: the int point size of the font """ self.__font_size__ = point_size self.__font__ = SysFont(self.__font_name__, self.__font_size__, True) def set_font_color(self, color_string): """Set the font color used to draw in the window. :param color_string: the str name of the font color """ self.__font_color__ = color_string def set_bg_color(self, color_string): """Set the background color used to draw in the window. :param color_string: the str name of the background color """ self.__bg_color__ = color_string def set_auto_update(self, true_false): """Set the background color used to draw in the window. :param true_false: a Boolean indicating if auto update should be on or off """ self.__auto_update__ = true_false def get_font_height(self): """Return the int pixel height of the current font. :return: the int pixel height of the current font """ return self.__font__.size('')[1] def get_font_color(self): """Return a str that represents the current window font color. :return: a str that represents the current window font color """ return self.__font_color__ def get_bg_color(self): """Return a str that represents the current window. :return: a str that represents the current window """ return self.__bg_color__ def get_width(self): """Return the int pixel width of the window's drawable interior surface. :return: the int pixel width of the window's drawable interior surface """ return self.__surface__.get_width() def get_height(self): """Return the int pixel height of the window's drawable interior surface. :return: the int pixel height of the window's drawable interior surface """ return self.__surface__.get_height() def clear(self): """Erase the window contents""" self.__surface__.fill(Color(self.__bg_color__)) if self.__auto_update__: update() def get_surface(self): """Return the Pygame.Surface object that represents the interior drawing surface of the window. :return: the Pygame.Surface object that represents the interior drawing surface of the window """ return self.__surface__ def draw_string(self, string, x, y): """Draw a string in the window using the current font and colors. :param string: the str object to draw :param x: the int x coord of the upper left corner of the string in the window :param y: is the int y coord of the upper left corner of the string in the window """ text_image = self.__font__.render(string, True, Color(self.__font_color__), Color(self.__bg_color__)) self.__surface__.blit(text_image, (x, y)) if self.__auto_update__: text_rect = Rect((x, y), text_image.get_size()) update(text_rect) def input_string(self, prompt, x, y): """Draw a prompt string in the window using the current font and colors. Check keys pressed by the user until an enter key is pressed and return the sequence of key presses as a str object. :param prompt: the str to display :param x: the int x coord of the upper left corner of the string in the window :param y: the int y coord of the upper left corner of the string in the window """ key = K_SPACE answer = '' while key != K_RETURN: self.draw_string(prompt + answer + ' ', x, y) if not self.__auto_update__: update() key = self._get_key() key_state = get_pressed() if (K_SPACE <= key <= K_z): if key == K_SPACE: letter = ' ' else: letter = name(key) if key_state[K_LSHIFT] or key_state[K_RSHIFT] or key_state[ K_CAPSLOCK]: letter = letter.upper() answer = answer + letter if key == K_BACKSPACE: answer = answer[0:len(answer) - 1] return answer def get_string_width(self, string): """Return the int pixel width of the string using the current font. :param string: the str object """ return self.__font__.size(string)[0] def update(self): """Update the window by copying all drawn objects from the frame buffer to the display.""" update() def _get_key(self): """Poll the events until the user presses a key and return it. Discard all other events. :return: the user-pressed keys """ event = poll() while event.type != KEYUP: event = poll() return event.key