def test_render(self): """ """ f = pygame_font.Font(None, 20) s = f.render("foo", True, [0, 0, 0], [255, 255, 255]) s = f.render("xxx", True, [0, 0, 0], [255, 255, 255]) s = f.render("", True, [0, 0, 0], [255, 255, 255]) s = f.render("foo", False, [0, 0, 0], [255, 255, 255]) s = f.render("xxx", False, [0, 0, 0], [255, 255, 255]) s = f.render("xxx", False, [0, 0, 0]) s = f.render(" ", False, [0, 0, 0]) s = f.render(" ", False, [0, 0, 0], [255, 255, 255]) # null text should be 1 pixel wide. s = f.render("", False, [0, 0, 0], [255, 255, 255]) self.assertEqual(s.get_size()[0], 1) # None text should be 1 pixel wide. s = f.render(None, False, [0, 0, 0], [255, 255, 255]) self.assertEqual(s.get_size()[0], 1) # Non-text should raise a TypeError. self.assertRaises(TypeError, f.render, [], False, [0, 0, 0], [255, 255, 255]) self.assertRaises(TypeError, f.render, 1, False, [0, 0, 0], [255, 255, 255]) # is background transparent for antialiasing? s = f.render(".", True, [255, 255, 255]) self.assertEqual(s.get_at((0, 0))[3], 0) # is Unicode and bytes encoding correct? # Cannot really test if the correct characters are rendered, but # at least can assert the encodings differ. su = f.render(as_unicode("."), False, [0, 0, 0], [255, 255, 255]) sb = f.render(as_bytes("."), False, [0, 0, 0], [255, 255, 255]) self.assert_(equal_images(su, sb)) u = as_unicode(r"\u212A") b = u.encode("UTF-16")[2:] # Keep byte order consistent. [2:] skips BOM sb = f.render(b, False, [0, 0, 0], [255, 255, 255]) try: su = f.render(u, False, [0, 0, 0], [255, 255, 255]) except pygame.error: pass else: self.assert_(not equal_images(su, sb)) # If the font module is SDL_ttf based, then it can only supports UCS-2; # it will raise an exception for an out-of-range UCS-4 code point. if UCS_4 and not hasattr(f, 'ucs4'): ucs_2 = as_unicode(r"\uFFEE") s = f.render(ucs_2, False, [0, 0, 0], [255, 255, 255]) ucs_4 = as_unicode(r"\U00010000") self.assertRaises(UnicodeError, f.render, ucs_4, False, [0, 0, 0], [255, 255, 255]) b = as_bytes("ab\x00cd") self.assertRaises(ValueError, f.render, b, 0, [0, 0, 0]) u = as_unicode("ab\x00cd") self.assertRaises(ValueError, f.render, b, 0, [0, 0, 0])
class Pawn: pg.font.init() field_font = font.Font('freesansbold.ttf', 16) text = field_font.render("P", False, (0, 0, 0)) def __init__(self, base_id, pawn_id, pawn_color): self.position = -1 # pozycja na sciezce self.starting_position = 0 print(self.starting_position) self.p_coord = 0 self.base_id = base_id self.pawn_id = pawn_id self.pawn_color = pawn_color self.__in_base = True self.radius = 25 self.__is_hovered = False self.route = [] def render(self): if self.__is_hovered: pg.draw.circle(Handler.display, (255, 128, 14), self.p_coord, self.radius) else: pg.draw.circle(Handler.display, (0, 0, 0), self.p_coord, self.radius) pg.draw.circle(Handler.display, self.pawn_color, self.p_coord, self.radius - 3) Handler.display.blit(Pawn.text, (self.p_coord[0]-5, self.p_coord[1]-5)) def set_xy(self, x, y): self.p_coord = (x, y) self.starting_position = (x, y) def check_if_hovered(self): # funkcja sprawdza czy myszka najechala na pole if ((Handler.mx - self.p_coord[0]) ** 2 + (Handler.my - self.p_coord[1]) ** 2) ** 0.5 < self.radius: self.__is_hovered = True return True else: self.__is_hovered = False return False def move(self): self.position += Handler.dice_value self.p_coord = self.route[self.position].coord self.__in_base = False self.__is_hovered = False print(self.starting_position) def kill(self): print(self.starting_position) self.p_coord = self.starting_position self.__in_base = True self.position = -1 def set_route(self, route): self.route = route def is_in_base(self): return self.__in_base
def __init__(self, txt, surface, pos, name): Sprite.__init__(self) self.surface = Surface((1280, 720), SRCALPHA) self.txt = txt self.pos = pos self.font = font.Font('res/fonts/16643.otf', 24) self.screen = surface self.render(self.screen) self.first = True self.name = name
def __init__(self): State.State.__init__(self) if not Instructions.IMAGE: Instructions.IMAGE = PI.load("sprites/images/instructions.jpg") \ .convert() self.image = Instructions.IMAGE self.font = PF.Font("fonts/red_october.ttf", 42) self.back_surf = self.font.render("Back", True, (255, 0, 0)) self.back_x, self.back_y = self.back_surf.get_size()
def __init__(self, x=0, y=0, width=10, height=10, text='', text_size=0, color=(10, 20, 130)): Sprite.__init__(self) self.image = Surface((width, height)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.text = text if text_size == 0: self.font = font.Font(None, width) elif text_size > 0: self.font = font.Font(None, text_size) self.color = color
def set_title(text): ''' 根据读取的音乐文件名设置UI界面中的字幕 ''' rect = screen.get_rect() f = pygame_font.Font('./arial.ttf', 16) fsurface = f.render('NOW PLAYING: ' + text, True, (255, 255, 255)) frect = fsurface.get_rect() frect.topleft = rect.topleft return fsurface, frect
def display(self, surface): myfont = font.Font(None, 2 * self.display_weight) label = myfont.render(self.label, 1, Config.vertex_label_color) d_pos = (int(self.x) + (Config.frameWidth / 2), int(self.y) + (Config.frameHeight / 2)) circle(surface, self.color, d_pos, self.display_weight, self.width) surface.blit(label, (d_pos[0] + self.display_weight + 5, d_pos[1]))
def test_segfault_after_reinit(self): """Reinitialization of font module should not cause segmentation fault""" import gc font = pygame_font.Font(None, 20) pygame_font.quit() pygame_font.init() del font gc.collect()
def __init__(self): State.State.__init__(self) if not JoySettings.FONT: JoySettings.FONT = PF.Font("fonts/red_october.ttf", 15) if not JoySettings.SECFONT: JoySettings.SECFONT = PF.Font("fonts/red_october.ttf", 20) self.font = JoySettings.FONT self.font2 = JoySettings.SECFONT self.num_joys = PJ.get_count() self.no_joy = False if self.num_joys == 0: self.no_joy = True return joys = [] for i in range(self.num_joys): joy = PJ.Joystick(i) joy.init() joys.append(joy) self.buttons = [ "Move Up", "Move Down", "Move Left", "Move Right", "Shoot Up", "Shoot Down", "Shoot Left", "Shoot Right", "Activation Key" ] self.instructions = self.font.render( "Press buttons on gamepad to configure." "" " Press X to set to default settings.", True, (255, 255, 255)) self.surfs = [] for i in range(len(self.buttons)): self.surfs.append( self.font2.render(self.buttons[i], True, (255, 255, 255))) self.index = 0 self.selected_buttons = [] if joys[0].get_numbuttons() < 9: self.no_joy = True return for i in range(joys[0].get_numbuttons()): JoySettings.POSS_BUTTONS.append(i) self.continue_string = self.font2.render("", True, (255, 255, 255)) self.button_pos = set([(1, 0), (-1, 0), (0, 1), (0, -1)])
def __init__(self, game): self.myGame = game from pygame import font #self.myFont = pygame.font.SysFont('arial',32) self.myFont = font.Font( SCS.getFilename(os.path.join('..', 'media', 'freesansbold.ttf')), 18) #print "Points is ", self.myGame.getPoints() self.image = self.myFont.render("%d" % self.myGame.getPoints(), True, (240, 140, 140))
def __init__(self, scene, score_check, *groups): super().__init__(*groups) self.scene = scene self.score_check = score_check self.font = font.Font(font.get_default_font(), 16) self.color = (200, 0, 0) self.image = None self.rect = None self.draw_score() self.layer = Layers.UI.value
def draw_text(text, posx, posy, size=25, color=(255, 255, 255)): fuente = font.Font( resource_path("assets" + os.sep + "fonts" + os.sep + "OpenSans-Regular.ttf"), size) salida = fuente.render(text, 1, color) salida_rect = salida.get_rect() salida_rect.centerx = posx salida_rect.centery = posy return salida, salida_rect
def __init__(self, screen, settings, game_stats): self.screen = screen self.settings = settings self.screen_rect = self.screen.get_rect() self.game_stats = game_stats self.score_font = font.Font("assets/font/arcade_n.ttf", 30) self.hs_font = font.Font("assets/font/arcade_n.ttf", 35) #Board Surface & Rect self.board_surface = pygame.Surface((self.screen_rect.width, 100)) self.board_surface.fill(self.settings.bg_color) self.board_rect = self.board_surface.get_rect() self.board_rect.x = (self.screen_rect.width - self.board_surface.get_rect().width) / 2 self.board_rect.y = 0 self.title_high_score() self.prep_high_score() self.prep_score()
def update(self, hover): if (hover): self.surface.fill((200, 200, 200)) else: self.surface.fill((255, 255, 255)) self.rect = Rect(self.pos, self.size) self.buttonFont = font.Font(None, 25) self.surface.blit(self.buttonFont.render(self.option, 1, (0, 0, 0)), (5, self.size[1] / 2 - self.buttonFont.size("Lorem Ipsum")[1] / 2))
def initialize(): # Creates pygame module and intializes global variables. passed, failed = PG.init() if failed > 0: print "warning: %d PyGame modules failed to initialize" % failed Globals.SCREEN = PDI.set_mode((1280, 1020), PG.DOUBLEBUF | PG.HWSURFACE) Globals.WIDTH = Globals.SCREEN.get_width() Globals.HEIGHT = Globals.SCREEN.get_height() Globals.FONT = PF.Font(None, 48) Globals.STATE = "Title"
def __init__(self): names = {'blue': 'a', 'red': 's', 'green': 'd', 'yellow': 'f'} self.assets = {} for color, key in names.items(): surf = image.load(os.path.join("data", key + ".png")).convert() surf.set_colorkey((0, 0, 0)) self.assets[color] = surf self.font = font.Font(os.path.join("data", "DIMIS___.TTF"), self.font_size)
def __init__(self, game): from pygame import font from supercubeslide import bgmusic #self.myFont = pygame.font.SysFont('arial',32) self.myFont = font.Font( supercubeslide.SCS.getFilename( os.path.join('..', 'media', 'freesansbold.ttf')), 32) self.myGame = game bgmusic.pauseSong() pass
def test_render_ucs2_ucs4(self): """that it renders without raising if there is a new enough SDL_ttf.""" f = pygame_font.Font(None, 20) # If the font module is SDL_ttf < 2.0.15 based, then it only supports UCS-2 # it will raise an exception for an out-of-range UCS-4 code point. if hasattr(pygame_font, "UCS4"): ucs_2 = "\uFFEE" s = f.render(ucs_2, False, [0, 0, 0], [255, 255, 255]) ucs_4 = "\U00010000" s = f.render(ucs_4, False, [0, 0, 0], [255, 255, 255])
def __init__(self, width: int, height: int): super().__init__() self._width = width self._height = height self._progress = 0 self._finished = False self._data = None self._font = font.Font('freesansbold.ttf', 10) thread = SceneProgress.BackgroundGeneration(self._generation_progress, self._create_next_scene) thread.start()
def __init__(self): State.State.__init__(self) PX.stop() """self.sound = PX.Sound("music/march.wav") self.sound.play()""" Intro.FONT = PF.Font("fonts/red_october.ttf", 18) img_1 = PI.load("sprites/images/intro1.jpg").convert() img_2 = PI.load("sprites/images/intro2.jpg").convert() img_3 = PI.load("sprites/images/intro3.jpg").convert() img_4 = PI.load("sprites/images/intro4.jpg").convert() img_5 = PI.load("sprites/images/instructions.jpg").convert() self.strings1 = [] self.strings2 = [] self.images = [] self.index = -1 self.time = 0 self.strings1.append("In a secret government lab, deep " + "within a deserted Russian town,") self.strings2.append("Scientists concocted experiments on" + " a large group of specimens.") self.strings1.append("Through a series of radioactive experiments" + " and countless injections") self.strings2.append("a powerful new age weapon was created.") self.strings1.append("A fierce creature: the product of science" + " and one's worst fears") self.strings2.append("was brought into this world as a dangerous " + "and sentient being.") self.strings1.append("It is now time for you to make your escape " + "from the lab,") self.strings2.append("for you are now, the Gamma Ray Kitten.") self.strings1.append("") self.strings2.append("") self.images.append(img_1) self.images.append(img_2) self.images.append(img_3) self.images.append(img_4) self.images.append(img_5) self.surf1 = Intro.FONT.render(self.strings1[self.index], True, (255, 0, 0)) self.surf2 = Intro.FONT.render(self.strings2[self.index], True, (255, 0, 0)) self.xy1 = ((400 - self.surf1.get_width() / 2, 500 - self.surf1.get_height())) self.xy2 = ((400 - self.surf2.get_width() / 2, 500 + self.surf2.get_height())) self.fadein = 0 self.fadeout = 255 self.fade_value = 100
def __init__(self, txt, surface): Sprite.__init__(self) self.surface = Surface((1280, 720), SRCALPHA) self.txt = txt.split('&') # self.pos = [(90, x + 515) for x in range(0, 600, 25)] self.pos = [x + 515 for x in range(0, 600, 25)] self.font = font.Font('res/fonts/16643.otf', 20) self.screen = surface self.render(self.screen) self.first = True self.name = 'TextObject'
def show_codes(list_lists): ''' в данной процедуре основной текст кнопки помещается свойству alt_text, а свойству text назначаются строковые значения номеров строки и столбца нужно для того чтобы показать коды сообщений - шифруемого и зашифрованного ''' for l in list_lists: for button in l: button.alt_text = button.text button.text = str(button.row) + ' ' + str(button.col) button.font = font.Font('fonts/DS Crystal Regular.ttf', 16)
def __init__(self, que): self.screen = display.set_mode((1920, 1080)) self.font = font.Font('./Materials/Kingthings Trypewriter 2.ttf', 24) self.que = self.font.render(f'{que}?', True, Color('white')) self.yes_no = self.font.render(f'Yes/No', True, Color('white')) # центрирование по длине self.que_pos = None
def draw_header(self, p_game_screen: Surface, p_header: str): """ Draws a header text over the Status elements Coordinate """ largeText = font.Font('freesansbold.ttf', 18) text_surface = largeText.render(p_header, True, color.THECOLORS['black']) text_rectangle = text_surface.get_rect() text_rectangle.center = (self.coordinate.get_x() + 25, self.coordinate.get_y() - 20) p_game_screen.blit(text_surface, text_rectangle)
def __init__(self, position, size, passenger, resourcePath): Surface.__init__(self, size) self.position = position self.size = size self.passenger = passenger self.textFont = font.Font(None, 15) self.passengerSurface = Surface(self.size).convert() self.rect = Rect(self.position, self.size) self.optionImage = transform.scale( image.load(resourcePath + "img/optionIcon.png"), (20, 20)) self.optionRect = self.optionImage.get_rect()
def __init__(self, offset=(0, 0)): self.BG_COLOR = Settings.BG_COLOR self.BOARD_BG_COLOR = Settings.BOARD_BG_COLOR self.COLOR = Settings.COLOR self.SIDE = Settings.SQUARE_SIDE self.WIDTH = 4 * self.SIDE + 2 * self.MARGIN self.HEIGHT = self.WIDTH + self.FONT_SIZE font.init() self.font = font.Font(Settings.FONT, self.FONT_SIZE) self.font_sf = self.font.render('Next', True, self.COLOR) self.rect = Rect(offset[0], offset[1], self.WIDTH, self.HEIGHT)
def __init__(self): State.State.__init__(self) if not AdjSound.FONT: AdjSound.FONT = PF.Font("fonts/red_october.ttf", 20) self.font = AdjSound.FONT if not AdjSound.SECFONT: AdjSound.SECFONT = PF.Font("fonts/red_october.ttf", 40) self.font2 = AdjSound.SECFONT self.save_surf = self.font2.render("Save", True, (255, 255, 255)) self.save_x, self.save_y = self.save_surf.get_size() self.instructions = self.font.render("Adjust sound FX volume", True, (255, 255, 255)) self.instructions2 = self.font.render("Adjust Music Volume", True, (255, 255, 255)) self.color1 = (255, 255, 255) self.color2 = (255, 255, 255) self.color3 = (255, 255, 255) self.interval_surf = self.font.render(str(G.Globals.FX_VOL), True, (255, 255, 255)) self.time = 0
def __init__(self, x, y, w, h, text=''): super().__init__(x, y, w, h) self.COLOR_INACTIVE = Color('dodgerblue2') self.COLOR_ACTIVE = Color('black') self.FONT = font.Font(None, 32) self.ACCEPTED = string.ascii_letters + string.digits self.color = self.COLOR_INACTIVE self.text = text self.txt_surface = self.FONT.render(text, True, self.color) self.active = False self.final = False
def test_load_from_file_bytes(self): font_path = os.path.join( os.path.split(pygame.__file__)[0], pygame_font.get_default_font()) filesystem_encoding = sys.getfilesystemencoding() try: font_path = font_path.decode(filesystem_encoding, filesystem_errors) except AttributeError: pass bfont_path = font_path.encode(filesystem_encoding, filesystem_errors) f = pygame_font.Font(bfont_path, 20)
def draw(self, screen): w = self.location[0] - 40 h = self.location[1] - 40 if self.alive: screen.blit(spider_image, (w, h)) else: if self.just_consumed > 0 and self.last_score > 0: f = font.Font('freesansbold.ttf', 40) text = f.render(f"+{self.last_score}", True, GREEN) screen.blit(text, (w - 20, h)) self.just_consumed -= 1