def __init__(self, promt: str, pos, screen_dimensions, print_event: bool, text_color="white") -> None: self.promt = promt self.print_event = print_event # position of chatlist and chatbox self.CHAT_LIST_POS = pg.Rect((pos[0], pos[1] + 50), (screen_dimensions[0], 400)) self.CHAT_BOX_POS = pg.Rect(pos, (screen_dimensions[1], 40)) self.CHAT_LIST_MAXSIZE = 20 self._ime_editing = False self._ime_text = "" self._ime_text_pos = 0 self._ime_editing_text = "" self._ime_editing_pos = 0 self.chat_list = [] # Freetype # The font name can be a comma separated list # of font names to search for. self.FONT_NAMES = ",".join(str(x) for x in self.FONT_NAMES) self.font = freetype.SysFont(self.FONT_NAMES, 24) self.font_small = freetype.SysFont(self.FONT_NAMES, 16) self.text_color = text_color print("Using font: " + self.font.name)
def __init__(self): pg.init() self.running = True self.playing = True self.clock = pg.time.Clock() # self.screen = pg.display.set_mode((WIDTH, HEIGHT), flags=pg.NOFRAME) self.screen = pg.display.set_mode(flags=pg.NOFRAME | pg.FULLSCREEN | pg.HWSURFACE | pg.DOUBLEBUF) self.screen_size = pg.display.get_window_size() self.screen_w = self.screen_size[0] self.screen_h = self.screen_size[1] self.screen_centerx = int(self.screen_w / 2) self.screen_centery = int(self.screen_h / 2) self.one_third_y = int(self.screen_h / 3) self.one_third_y_top = (0, 0, self.screen_w, self.one_third_y) self.one_third_y_mid = (0, self.one_third_y, self.screen_w, self.one_third_y) self.one_third_y_btm = (0, self.one_third_y * 2, self.screen_w, self.screen_h - self.one_third_y * 2) self.styles = [ 'underline', 'strong', 'oblique', 'wide', 'kerning', 'vertical' ] self.text = 'Freetype Fonts' self.font_name = "resources/OvOV20.ttf" self.font = ft.Font(self.font_name, 50) self.splat_font = ft.Font(FONT_PATH + "earwig factory rg.ttf", 80) self.any_key_font = ft.SysFont("calibri", 30) self.amy_font = ft.SysFont("calibri", 15) self.font_lst = os.listdir(FONT_PATH) self.txt_grp = pg.sprite.Group() self.x, self.y, self.w, self.h = 0, 0, 0, 0 self.file_saved_flg = False self.file_saved_cnt = 1 self.file_saved_name = "" self.word_lst = None
def __createMainMenu__(self): self.mainMenu = Menu(self.w, self.h) self.mainMenu.star_bg() but_w = 300 self.mainMenu.add_label("WELCOME TO GALCON", self.w // 2, 140, font=pgfont.SysFont('Tahoma', 28)) self.mainMenu.add_status_box('name', self.user.name, self.w // 2, 180, font=pgfont.SysFont('Tahoma', 32), text_color=self.user.color) self.mainMenu.add_button( "START MULTIPLAYER", pg.Rect((self.w - but_w) // 2, 250, but_w, 50), self.show_start_mp_menu) self.mainMenu.add_button( "JOIN MULTIPLAYER", pg.Rect((self.w - but_w) // 2, 310, but_w, 50), self.show_join_mp_menu) self.mainMenu.add_button( "SETTINGS", pg.Rect((self.w - but_w) // 2, 370, but_w, 50), self.show_settings) self.mainMenu.add_button( "QUIT", pg.Rect((self.w - but_w) // 2, 430, but_w, 50), self.quit_game)
def __init__(self, width=1024, height=620, cell=30): """constructor for Game Keyword Arguments: width {number} -- window width in pixels (default: {1024}) height {number} -- window eight in pixels (default: {620}) cell {number} -- cell count (default: {30}) """ pygame.display.set_caption("python))))") pygame.display.set_icon(pygame.image.load("face.jpg")) self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode((width, height)) self.cell_size = width // cell self.grid = [width // self.cell_size, height // self.cell_size] print(self.grid) self.snake = Snake(self.grid) self.grid = [[0 for i in range(self.grid[1])] for j in range(self.grid[0])] self.face = pygame.image.load("face.jpg") self.t**s = pygame.image.load("t**s.jpg") self.ass = pygame.image.load("ass.jpg") self.face = pygame.transform.scale(self.face, (self.cell_size, self.cell_size)) self.t**s = pygame.transform.scale(self.t**s, (self.cell_size, self.cell_size)) self.ass = pygame.transform.scale(self.ass, (self.cell_size, self.cell_size)) self.font = freetype.SysFont("OpenSans", 12)
def __init__(self, surface, pos, color, text='', font='Open Sans', padding=[10, 10, 10, 10]): self.scale = 2 self.surface = surface self.text = text self.font = ft.SysFont(font, 12 * self.scale) self.display_text = self.font.render(self.text, (255, 255, 255))[0] self.text_rect = self.display_text.get_rect() self.rect = pygame.rect.Rect([0, 0, 0, 0]) self.original_color = [color[0], color[1], color[2]] self.color = color self.objects = [self] self.original_padding = np.array(padding) self.padding = self.original_padding * self.scale # top right bottom left self.prescale_pos = pos self.pos = copy.deepcopy(pos) self.focused = False self.connectable = False
def __init__(self, text_str, font_info=FontInfo()): self.text_str = text_str self.font_info = font_info self.font = freetype.SysFont(self.font_info.font_name, self.font_info.font_size) self.set_size() self.set_pos((0, 0))
def __init__(self, text_str, font_info, position=(0, 0)): self.text_str = text_str self.font_info = font_info self.position = position self.font = freetype.SysFont(self.font_info.font_name, self.font_info.font_size) self.width, self.height = self.bounds = self.get_bounds()
def draw_graph_pygame(graph, size=(500, 500)): pygame.init() screen = pygame.display.set_mode(size) white = pygame.Color('white') black = pygame.Color('black') screen.fill(white) font = freetype.SysFont("monospace", 10) done = False for v in graph.V: draw.circle(screen, black, (int(v.pos[0]), int(v.pos[1])), v.radius) font.render_to(screen, (int(v.pos[0]), int(v.pos[1])), str(v.idx)) for e in graph.E: draw.line(screen, black, (int(e.v1.pos[0]), int(e.v1.pos[1])), (int(e.v2.pos[0]), int(e.v2.pos[1]))) while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True pygame.display.flip()
def __init__(self, text_str, position=(0, 0), font_info=FontInfo()): self.text_str = text_str self.font_info = font_info self.x, self.y = self.position = position self.font = freetype.SysFont(self.font_info.font_name, self.font_info.font_size) self.w, self.h = self.size = self.font.get_rect(self.text_str).size
def print_text(text, pos, color, emoji=False): text = str(text) ft = freetype.Font("seguisym.ttf", round( size * 0.3)) if emoji else freetype.SysFont( 'Calibri', round(size * 0.2)) text = ft.render(text, fgcolor=color) surf.blit(text[0], pos)
def __init__(self, rectangle): self.updated = False self.font = freetype.SysFont('liberationsansnarrow', 24) self.font.antialiased = True self.font.pad = True self.fonts = font.get_fonts() super().__init__(rectangle.name, rectangle.x, rectangle.y, rectangle.width, rectangle.height)
def __init__(self, text_str, font_info): self.text_str = text_str self.font_name = font_info.font_name self.font_size = font_info.font_size self.font_color = font_info.font_color self.font = freetype.SysFont(self.font_name, self.font_size) self.width = self.font.get_rect(self.text_str).width self.height = self.font.get_rect(self.text_str).height
class Button(sp.DirtySprite): COLOR = (0, 255, 0) COLORO = (0, 63, 0) COLORP = (0, 127, 0) pgfont.init() FONT = pgfont.SysFont('Tahoma', 16) def __init__(self, text, rect, fn, args=tuple()): super().__init__() self.text = text self.rect = rect self.fn = fn self.args = args self.imageBase = pg.Surface((rect.width, rect.height)) self.imageMouseOver = pg.Surface((rect.width, rect.height)) self.imageMousePress = pg.Surface((rect.width, rect.height)) self.image = self.imageBase self.__createImages__() self.pressed = False def __createImages__(self): text_image, r = Button.FONT.render(self.text, Button.COLOR) text_top_left = ((self.rect.width - text_image.get_width()) // 2, (self.rect.height - text_image.get_height()) // 2) img_rect = self.image.get_rect() gfx.rectangle(self.imageBase, img_rect, Button.COLOR) self.imageBase.blit(text_image, text_top_left) self.imageMouseOver.fill(Button.COLORO) gfx.rectangle(self.imageMouseOver, img_rect, Button.COLOR) self.imageMouseOver.blit(text_image, text_top_left) self.imageMousePress.fill(Button.COLORP) gfx.rectangle(self.imageMousePress, img_rect, Button.COLOR) self.imageMousePress.blit(text_image, text_top_left) def contains_pt(self, pt): return self.rect.collidepoint(pt) def mouse_over(self): self.image = self.imageMouseOver def un_mouse_over(self): self.image = self.imageBase def press(self): self.image = self.imageMousePress self.pressed = True def unpress(self): self.image = self.imageBase self.pressed = False def release(self): self.image = self.imageBase if self.pressed: self.fn(*self.args)
def __init__(self, text_str, *args, font_info=FontInfo(), **kargs): super().__init__(*args, **kargs) self.paras = Paras(*args, **kargs) self.text_str = text_str self.font_info = font_info self.font = freetype.SysFont(self.font_info.font_name, self.font_info.font_size) self.set_size(self.font.get_rect(self.text_str).size) self.set_pos(self.paras.try_get('left_top', (0, 0)))
class MultilineLabel(sp.Sprite): COLOR = Color.GREEN pgfont.init() FONT = pgfont.SysFont('Tahoma', 20) PARA_MARGIN = 15 LINE_MARGIN = 5 def __init__(self, text, x, y, max_w): super().__init__() self.text = text self.x, self.y = x, y self.maxW = max_w self.__createImage__() def __createImage__(self): para_list = [] self.h = 0 for paragraph in self.text.splitlines(): sf, h = self.__createParagraphImage__(paragraph) para_list.append((sf, h)) self.h += h + MultilineLabel.PARA_MARGIN self.image = pg.Surface((self.maxW, self.h)) curr_y = 0 for sf, h in para_list: self.image.blit(sf, (0, curr_y)) curr_y += h + MultilineLabel.PARA_MARGIN def __createParagraphImage__(self, text): word_list = text.split() line_list = [] line_sf_list = [] h = 0 next_line = word_list.pop(0) while next_line: curr_line, next_line = next_line, "" while MultilineLabel.FONT.get_rect(curr_line).width < self.maxW: if not word_list: break curr_line += " " + word_list.pop(0) else: curr_line, next_line = curr_line.rsplit(" ", 1) line_list.append(curr_line) for l in line_list: sf, r = MultilineLabel.FONT.render(l, MultilineLabel.COLOR) line_sf_list.append((sf, r)) h += r.height + MultilineLabel.LINE_MARGIN image = pg.Surface((self.maxW, h)) curr_y = 0 for sf, r in line_sf_list: image.blit(sf, (0, curr_y)) curr_y += r.height + MultilineLabel.LINE_MARGIN return image, h @property def rect(self): return pg.Rect(self.x, self.y, self.maxW, self.h)
def __init__(self, text, font_name, font_size): self.text = text self.font_size = font_size self.font_color = Color.WHITE self.font_name = font_name self.font = freetype.SysFont(self.font_name, self.font_size) self.height = self.font.get_rect(self.text).height self.width = self.font.get_rect(self.text).width self.id = Text.ID Text.ID += 1
def __init__(self, name, font_name, font_size, filter, image_size): self.name = name self.filter = sorted(filter) if os.path.isfile(font_name): self.font = freetype.Font(font_name, font_size) else: print "Using system font" self.font = freetype.SysFont(font_name, font_size) self.font_size = font_size self.glyphs = [] self.tex_w, self.tex_h = image_size
def __init__(self): self.font = freetype.SysFont('Consolas', 25) self.score, self.missed = 0, 0 self.stop = True self.white = pg.Color('Black') self.red = pg.Color('White') self.text1 = '' self.text2 = 'INICIAR' self.r1 = self.font.get_rect('GAME OVER', size=60) self.r2 = self.font.get_rect(self.text2) self.level_list = ['GR12']
def __createMainMenu__(self): self.mainMenu = Menu(self.w, self.h) self.mainMenu.star_bg() but_w = 300 self.mainMenu.add_label("GALCON", self.w // 2, 100, font=pgfont.SysFont('Tahoma', 32)) self.mainMenu.add_button("START MULTIPLAYER", pg.Rect((self.w - but_w) // 2, 250, but_w, 50), self.show_start_mp_menu) self.mainMenu.add_button("JOIN MULTIPLAYER", pg.Rect((self.w - but_w) // 2, 310, but_w, 50), self.show_join_mp_menu) self.mainMenu.add_button("SETTINGS", pg.Rect((self.w - but_w) // 2, 370, but_w, 50), self.show_settings) self.mainMenu.add_button("QUIT", pg.Rect((self.w - but_w) // 2, 430, but_w, 50), self.quit_game)
def draw(self, size=40): """ Render the tile given its current state Returns ---------- pygame.Surface Surface with a the rendered tile """ if self.font is None: self.font = fonts.SysFont('Courier New', size / 2, True) # Create surface surf = pygame.Surface((size, size)) # Fill Surface if not self.isVisible: surf.fill((90, 90, 90)) else: surf.fill((255, 255, 255)) # Generate text for tile t_surf, t_rect = None, None if self.isVisible: # Display M on an exposed mine if self.isMine: t_surf, t_rect = self.font.render('M', (255, 0, 0)) # Display a value if value is set elif self.value != 0: t_surf, t_rect = self.font.render(str(self.value), (0, 255, 0)) elif self.isFlagged: # Draw a neat flag xLeft = size / 5 xRigh = 4 * size / 5 points = [(xRigh, 1 * size / 5), (xLeft, 3 * size / 10), (xRigh, 2 * size / 5)] pygame.draw.polygon(surf, (255, 0, 0), points) pygame.draw.line(surf, (0, 0, 0), (xRigh, 1 * size / 5), (xRigh, 4 * size / 5), 2) # Draw text on tile if t_surf is not None: y = size / 2 - t_rect[3] / 2 x = size / 2 - t_rect[2] / 2 surf.blit(t_surf, (x, y)) # Draw border pygame.draw.rect(surf, (0, 0, 0), (0, 0, size, size), 2) return surf
def __init__(self, surface, pos, color, text='', font='Open Sans', padding=[10, 10, 10, 10]): super().__init__(surface, pos, color, text, font, padding) self.is_editing = False self.cursor = 0 # Typing a letter would mean inserting the first letter self.font = ft.SysFont(font, 12 * self.scale) self.display_text = self.font.render(self.text, color)[0] self.text_rect = self.display_text.get_rect()
def draw(self): if self.font is None: self.font = fonts.SysFont('Courier New', self.size_t, True) # Create surface surf = pygame.Surface((self.size_w, self.size_h)) # Fill Surface surf.fill((255, 255, 255)) # Draw Text t_surf, t_rect = self.font.render(self.text, (0, 0, 0)) y = self.size_h / 2 - t_rect[3] / 2 x = self.size_w / 2 - t_rect[2] / 2 surf.blit(t_surf, (x, y)) return surf
def __createImage__(self): if self.planet.teamNo is None: color = *Team.NEUTRAL_COLOR, 63 else: color = *Team.teams[self.planet.teamNo].color, PlanetUnits.ALPHA count = "" if self.count is None else str(self.count) r = self.planet.r + PlanetUnits.AURA w = h = r * 2 + 1 self.image = pg.Surface((w, h), flags=pg.SRCALPHA) gfx.filled_circle(self.image, r, r, r, color) gfx.aacircle(self.image, r, r, r, color) self.font = pgfont.SysFont("Comic Sans MS", self.planet.r) self.text, rt = self.font.render(count, (0, 0, 0, 255)) self.image.blit( self.text, (r - self.text.get_width() // 2, r - self.text.get_height() // 2))
def __init__(self, pos, width, height): self.border_overlap = 5 self.pos = pos self.border_pos = (pos[0] - self.border_overlap, pos[1] - self.border_overlap) self.width = width self.border_width = width self.height = height self.border_height = height freetype.init() self.font = freetype.SysFont("Ariel", 24) self.current_text = '' self.current_colour = (0, 0, 0) self.history = deque() self.text_size = 24 self.text_space = 25
def drawCells(color): size = cellSize cell = Rect(0, 0, size, size) for i in range(9): for j in range(9): cell.left = j * cell.width cell.top = i * cell.height pg.draw.rect(screen, color, cell, 2) # Draw values font = freetype.SysFont('arial', size) value = "" if board[i][j] != 0: value = str(board[i][j]) font.render_to(screen, (j * size + (size // 4), i * size + (size // 8)), value, color)
def __init__(self, pos, size, color, text): FontObject.__init__(self) self.size = size self.pos = pos self.text = text self.image = pygame.Surface([*self.size]) self.image.fill(color) self.origin = self.image.copy() self.rect = self.image.get_rect(topleft=self.pos) self.font = freetype.SysFont("arial", 20) self.render_text(self.text) self.pointer = None self.value = None
def __init__(self, pos, size, color, threshold=""): FontObject.__init__(self) self.threshold = threshold self.text = "" self.pos = pos self.size = size self.image = pygame.Surface([*size]) self.image.fill(color) self.origin = self.image.copy() self.rect = self.image.get_rect(topleft=pos) self.font = freetype.SysFont("arial", 20) self.active = False
def __createImage__(self): if self.planet.owner.color is None: color = Color.WHITE # neutral planet else: color = self.planet.owner.color count = "" if self.count is None else str(round(self.count)) radius = self.planet.radius + PlanetUnits.AURA w = h = radius * 2 + 1 self.image = pg.Surface((w, h), flags=pg.SRCALPHA) if self.planet.selected: gfx.aacircle(self.image, radius, radius, radius, color) else: gfx.aacircle(self.image, radius, radius, radius, Color.BLACK) self.font = pgfont.SysFont("Comic Sans MS", self.planet.radius) self.text, rt = self.font.render(count, Color.WHITE) self.image.blit(self.text, (radius - self.text.get_width() // 2, radius - self.text.get_height() // 2))
def __init__(self, w, h, main_menu, user): super().__init__(w, h) self.star_bg() but_w = 300 self.add_status_box('name', user.name, self.w // 2, 180, font=pgfont.SysFont('Tahoma', 32), text_color=user.color) self.add_label("Enter the name of room to connect to:", w // 2, 225) self.add_text_box("room", pg.Rect((w - but_w) // 2, 250, but_w, 100), self.connect_to_server) self.add_status_box("status", "", w // 2, 400) self.add_button("CONNECT AND START", pg.Rect((w - but_w) // 2, 440, but_w, 50), self.connect_to_server) self.add_button("BACK", pg.Rect((w - but_w) // 2, 500, but_w, 50), main_menu)
class StatusBox(sp.DirtySprite): COLOR = Color.GREEN pgfont.init() FONT = pgfont.SysFont('Tahoma', 16) def __init__(self, text, x, y, anchor=None, font=None, text_color=None): super().__init__() self.text = text self.x, self.y = x, y self.anchor = anchor if font is None: self.font = StatusBox.FONT else: self.font = font if text_color is None: self.textColor = StatusBox.COLOR else: self.textColor = text_color self.__createImage__() def __createImage__(self): self.image, r = self.font.render(self.text, self.textColor) if self.anchor == 'N': top_left = self.x - self.image.get_width() // 2, self.y elif self.anchor == 'W': top_left = self.x, self.y - self.image.get_height() // 2 elif self.anchor == 'NW': top_left = self.x, self.y else: # default to center top_left = (self.x - self.image.get_width() // 2, self.y - self.image.get_height() // 2) self.rect = pg.Rect(*top_left, self.image.get_width(), self.image.get_height()) def update_text(self, text=None, color=None): if text is not None: self.text = text if color is not None: self.textColor = color if not (color is None and text is None): self.__createImage__()