Python pygame.font() Examples

The following are 30 code examples of pygame.font(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pygame , or try the search function .
Example #1
Source File: scorecard.py    From alien-invasion-game with MIT License 6 votes vote down vote up
def __init__(self, ai_settings: Settings, stats: GameStats, screen: pygame.SurfaceType):

        self.screen = screen
        self.screen_rect = screen.get_rect()
        self.ai_settings = ai_settings
        self.stats = stats
        self.score_ship_size = self.ai_settings.score_ship_size  # size of ship in the scoreboard.
        self.dur_highscore_msg = 3000        # duration of highscore msg = 3 sec

        # Font settings.
        font_name = 'fonts/PoiretOne.ttf'       # try changing the font
        self.font_color = self.ai_settings.score_font_color
        self.font = pygame.font.Font(font_name, self.ai_settings.score_font_size)

        # Prepare the initial score image.
        self.prep_images() 
Example #2
Source File: scorecard.py    From alien-invasion-game with MIT License 6 votes vote down vote up
def prep_high_score(self):
        """Prepare the high score."""
        high_score = round(self.stats.high_score, -1)
        high_score_str = "High Score: {:,}".format(high_score)

        self.high_score_image = self.font.render(high_score_str, True, self.font_color)

        self.high_score_rect = self.high_score_image.get_rect()
        self.high_score_rect.top = self.score_rect.top
        self.high_score_rect.centerx = self.screen_rect.centerx

        self.new_high_score_msg = "Congratulations! New High Score."
        self.new_high_score_image = self.font.render(self.new_high_score_msg, True, self.font_color)
        self.new_high_score_rect = self.new_high_score_image.get_rect()
        self.new_high_score_rect.centerx = self.high_score_rect.centerx
        self.new_high_score_rect.y = self.high_score_rect.bottom + 4 
Example #3
Source File: lcars_widgets.py    From pi-tracking-telescope with MIT License 6 votes vote down vote up
def __init__(self, colour, pos, text, handler=None):
        self.handler = handler
        image = pygame.image.load("assets/button.png").convert()
        size = (image.get_rect().width, image.get_rect().height)
        font = Font("assets/swiss911.ttf", 19)
        textImage = font.render(text, False, colours.BLACK)
        image.blit(textImage, 
                   (image.get_rect().width - textImage.get_rect().width - 10,
                    image.get_rect().height - textImage.get_rect().height - 5))

        self.image = image
        self.colour = colour
        LcarsWidget.__init__(self, colour, pos, size)
        self.applyColour(colour)
        self.highlighted = False
        self.beep = Sound("assets/audio/panel/202.wav") 
Example #4
Source File: lcars_widgets.py    From rpi_lcars with MIT License 6 votes vote down vote up
def __init__(self, colour, pos, text, handler=None, rectSize=None):
        if rectSize == None:
            image = pygame.image.load("assets/button.png").convert_alpha()
            size = (image.get_rect().width, image.get_rect().height)
        else:
            size = rectSize
            image = pygame.Surface(rectSize).convert_alpha()
            image.fill(colour)

        self.colour = colour
        self.image = image
        font = Font("assets/swiss911.ttf", 19)
        textImage = font.render(text, False, colours.BLACK)
        image.blit(textImage, 
                (image.get_rect().width - textImage.get_rect().width - 10,
                    image.get_rect().height - textImage.get_rect().height - 5))
    
        LcarsWidget.__init__(self, colour, pos, size, handler)
        self.applyColour(colour)
        self.highlighted = False
        self.beep = Sound("assets/audio/panel/202.wav") 
Example #5
Source File: fig24_06.py    From PythonClassBook with GNU General Public License v3.0 5 votes vote down vote up
def displayMessage( message, screen, background ):
   font = pygame.font.Font( None, 48 )
   text = font.render( message, 1, ( 250, 250, 250 ) )
   textPosition = text.get_rect()
   textPosition.centerx = background.get_rect().centerx
   textPosition.centery = background.get_rect().centery
   return screen.blit( text, textPosition )

# remove outdated time display and place updated time on screen 
Example #6
Source File: fig24_06.py    From PythonClassBook with GNU General Public License v3.0 5 votes vote down vote up
def updateClock( time, screen, background, oldPosition ):
   remove = screen.blit( background, oldPosition, oldPosition )
   font = pygame.font.Font( None, 48 )
   text = font.render( str( time ), 1, ( 250, 250, 250 ),
      ( 0, 0, 0 ) )
   textPosition = text.get_rect()
   post = screen.blit( text, textPosition )
   return remove, post 
Example #7
Source File: util.py    From SwervinMervin with GNU General Public License v2.0 5 votes vote down vote up
def render_text(text, window, font, color, position):
    """Renders a font and blits it to the given window"""
    text = font.render(text, 1, color)

    window.blit(text, position)

    return text 
Example #8
Source File: cli.py    From stuntcat with GNU Lesser General Public License v2.1 5 votes vote down vote up
def checkdependencies():
    "only returns if everything looks ok"
    msgs = []

    #make sure this looks like the right directory
    if not os.path.isdir(CODEDIR):
        msgs.append('Cannot locate stuntcat modules')
    if not os.path.isdir('data'):
        msgs.append('Cannot locate stuntcat data files')

    #first, we need python >= 2.7
    if sys.hexversion < 0x2070000:
        errorbox('Requires Python-2.7 or Greater')

    #is correct pg found?
    try:
        import pygame as pg
        if pg.ver < '1.9.4':
            msgs.append('Requires pygame 1.9.4 or Greater, You Have ' + pg.ver)
    except ImportError:
        msgs.append("Cannot import pygame, install version 1.9.4 or higher")
        pg = None

    #check that we have FONT and IMAGE
    if pg:
        if not pg.font:
            msgs.append('pg requires the SDL_ttf library, not available')
        if not pg.image or not pg.image.get_extended():
            msgs.append('pg requires the SDL_image library, not available')

    if msgs:
        msg = '\n'.join(msgs)
        errorbox(msg)



#Pretty Error Handling Code... 
Example #9
Source File: cli.py    From stuntcat with GNU Lesser General Public License v2.1 5 votes vote down vote up
def __pgbox(title, message):
    try:
        import pygame as pg
        pg.quit() #clean out anything running
        pg.display.init()
        pg.font.init()
        screen = pg.display.set_mode((460, 140))
        pg.display.set_caption(title)
        font = pg.font.Font(None, 18)
        foreg, backg, liteg = (0, 0, 0), (180, 180, 180), (210, 210, 210)
        ok = font.render('Ok', 1, foreg, liteg)
        okbox = ok.get_rect().inflate(200, 10)
        okbox.centerx = screen.get_rect().centerx
        okbox.bottom = screen.get_rect().bottom - 10
        screen.fill(backg)
        screen.fill(liteg, okbox)
        screen.blit(ok, okbox.inflate(-200, -10))
        pos = [10, 10]
        for text in message.split('\n'):
            if text:
                msg = font.render(text, 1, foreg, backg)
                screen.blit(msg, pos)
            pos[1] += font.get_height()
        pg.display.flip()
        stopkeys = pg.K_ESCAPE, pg.K_SPACE, pg.K_RETURN, pg.K_KP_ENTER
        while 1:
            e = pg.event.wait()
            if e.type == pg.QUIT or \
                       (e.type == pg.KEYDOWN and e.key in stopkeys) or \
                       (e.type == pg.MOUSEBUTTONDOWN and okbox.collidepoint(e.pos)):
                break
        pg.quit()
    except pg.error:
        raise ImportError 
Example #10
Source File: scorecard.py    From alien-invasion-game with MIT License 5 votes vote down vote up
def prep_score(self):
        """Prepare the image of score."""
        rounded_score = round(self.stats.score, -1)
        score_str = "{:,}".format(rounded_score)

        self.score_image = self.font.render(score_str, True, self.font_color)

        self.score_rect = self.score_image.get_rect()
        self.score_rect.top = 10
        self.score_rect.right = self.screen_rect.right - 20 
Example #11
Source File: scorecard.py    From alien-invasion-game with MIT License 5 votes vote down vote up
def prep_level(self):
        """Prepare the image of current level."""
        level_str = "Level: {:d}".format(self.stats.level)
        self.level_image = self.font.render(level_str, True, self.font_color)
        self.level_rect = self.level_image.get_rect()
        self.level_rect.top = self.score_rect.top + 45
        self.level_rect.right = self.score_rect.right 
Example #12
Source File: scorecard.py    From alien-invasion-game with MIT License 5 votes vote down vote up
def prep_ships(self):
        """Show how many ships are left."""
        ship_str = "Ships left:  " if self.stats.ships_left > 0 else "No ships left."
        self.ship_str_image = self.font.render(ship_str, True, self.font_color)
        self.ship_str_rect = self.ship_str_image.get_rect()
        self.ship_str_rect.x = 10
        self.ship_str_rect.y = self.score_rect.top

        self.ships = Group()
        for ship_number in range(self.stats.ships_left):
            ship = Ship(self.ai_settings, self.screen, self.score_ship_size)
            ship.rect.x = self.ship_str_rect.width + ship_number * ship.rect.width
            ship.rect.centery = self.ship_str_rect.centery
            self.ships.add(ship) 
Example #13
Source File: loaders.py    From wasabi2d with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _load(self, path, fontsize=None):
        return pygame.font.Font(path, fontsize) 
Example #14
Source File: lcars_widgets.py    From pi-tracking-telescope with MIT License 5 votes vote down vote up
def __init__(self, colour, pos, message, size=1.0, background=None):
        self.colour = colour
        self.background = background
        self.font = Font("assets/swiss911.ttf", int(19.0 * size))
        
        self.renderText(message)
        # center the text if needed 
        if (pos[1] < 0):
            pos = (pos[0], 400 - self.image.get_rect().width / 2)
            
        LcarsWidget.__init__(self, colour, pos, None) 
Example #15
Source File: lcars_widgets.py    From pi-tracking-telescope with MIT License 5 votes vote down vote up
def renderText(self, message):        
        if (self.background == None):
            self.image = self.font.render(message, True, self.colour)
        else:
            self.image = self.font.render(message, True, self.colour, self.background) 
Example #16
Source File: simplecanvas.py    From yaksok with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def text(x, y, text, size, color, font = None, antialias = False):
	x=int(x)
	y=int(y)
	global surfarr, changed
	changed = True
	if font is None:
		font = pygame.font.SysFont("", size)
	else:
		font = pygame.font.Font(font, size)
	surface = font.render(text, antialias, color)
	del surfarr
	background.blit(surface, (x,y))
	surfarr = pygame.PixelArray(background) 
Example #17
Source File: interactive_control_train.py    From autonomous-rc-car with MIT License 5 votes vote down vote up
def setup_interactive_control():
    """Setup the Pygame Interactive Control Screen"""
    pygame.init()
    display_size = (300, 400)
    screen = pygame.display.set_mode(display_size)
    background = pygame.Surface(screen.get_size())
    color_white = (255, 255, 255)
    display_font = pygame.font.Font(None, 40)
    pygame.display.set_caption('RC Car Interactive Control')
    text = display_font.render('Use arrows to move', 1, color_white)
    text_position = text.get_rect(centerx=display_size[0] / 2)
    background.blit(text, text_position)
    screen.blit(background, (0, 0))
    pygame.display.flip() 
Example #18
Source File: lcars_widgets.py    From rpi_lcars with MIT License 5 votes vote down vote up
def __init__(self, colour, pos, message, size=1.0, background=None, handler=None):
        self.colour = colour
        self.background = background
        self.font = Font("assets/swiss911.ttf", int(19.0 * size))
        
        self.renderText(message)
        # center the text if needed 
        if (pos[1] < 0):
            pos = (pos[0], 400 - self.image.get_rect().width / 2)
            
        LcarsWidget.__init__(self, colour, pos, None, handler) 
Example #19
Source File: lcars_widgets.py    From rpi_lcars with MIT License 5 votes vote down vote up
def renderText(self, message):        
        if (self.background == None):
            self.image = self.font.render(message, True, self.colour)
        else:
            self.image = self.font.render(message, True, self.colour, self.background) 
Example #20
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.font = pygame.font.Font(None, 20)
        self.font.set_italic(1)
        self.color = Color('white')
        self.lastscore = -1
        self.update()
        self.rect = self.image.get_rect().move(10, 450) 
Example #21
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def update(self):
        if SCORE != self.lastscore:
            self.lastscore = SCORE
            msg = "Score: %d" % SCORE
            self.image = self.font.render(msg, 0, self.color) 
Example #22
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.font = pygame.font.Font(None, 20)
        self.font.set_italic(1)
        self.color = Color('white')
        self.lastscore = -1
        self.update()
        self.rect = self.image.get_rect().move(10, 450) 
Example #23
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def update(self):
        if SCORE != self.lastscore:
            self.lastscore = SCORE
            msg = "Score: %d" % SCORE
            self.image = self.font.render(msg, 0, self.color) 
Example #24
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.font = pygame.font.Font(None, 20)
        self.font.set_italic(1)
        self.color = Color('white')
        self.lastscore = -1
        self.update()
        self.rect = self.image.get_rect().move(10, 450) 
Example #25
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.font = pygame.font.Font(None, 20)
        self.font.set_italic(1)
        self.color = Color('white')
        self.lastscore = -1
        self.update()
        self.rect = self.image.get_rect().move(10, 450) 
Example #26
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def update(self):
        if SCORE != self.lastscore:
            self.lastscore = SCORE
            msg = "Score: %d" % SCORE
            self.image = self.font.render(msg, 0, self.color) 
Example #27
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.font = pygame.font.Font(None, 20)
        self.font.set_italic(1)
        self.color = Color('white')
        self.lastscore = -1
        self.update()
        self.rect = self.image.get_rect().move(10, 450) 
Example #28
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def update(self):
        if SCORE != self.lastscore:
            self.lastscore = SCORE
            msg = "Score: %d" % SCORE
            self.image = self.font.render(msg, 0, self.color) 
Example #29
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.font = pygame.font.Font(None, 20)
        self.font.set_italic(1)
        self.color = Color('white')
        self.lastscore = -1
        self.update()
        self.rect = self.image.get_rect().move(10, 450) 
Example #30
Source File: aliens.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.font = pygame.font.Font(None, 20)
        self.font.set_italic(1)
        self.color = Color('white')
        self.lastscore = -1
        self.update()
        self.rect = self.image.get_rect().move(10, 450)