python类font()的实例源码

inputbox.py 文件源码 项目:opseilen 作者: Baal221 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def ask(screen, question):
  "ask(screen, question) -> answer"
  pygame.font.init()
  current_string = []
  display_box(screen, question + ": " + "".join(current_string))
  while 1:
    inkey = get_key()
    if inkey == K_BACKSPACE:
      current_string = current_string[0:-1]
    elif inkey == K_RETURN:
      break
    elif inkey == K_MINUS:
      current_string.append("_")
    elif inkey <= 127:
      current_string.append(chr(inkey))
    display_box(screen, question + ": " + "".join(current_string))
  return "".join(current_string)
lcars_widgets.py 文件源码 项目:rpi_lcars 作者: tobykurien 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __init__(self, colour, pos, text, handler=None, rectSize=None):
        if rectSize == None:
            image = pygame.image.load("assets/button.png").convert()
            size = (image.get_rect().width, image.get_rect().height)
        else:
            size = rectSize
            image = pygame.Surface(rectSize).convert()
            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")
gamenews.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def buildlist(self):
        clr = 220, 230, 240
        clr2 = 140, 150, 160
        offsetx, offsety = 240, 30
        self.clearlist()
        font = fonts[0][0]
        self.gamelist = []
        cur = 0
        for opt in Options:
            if self.current == cur:
                c = clr
            else:
                c = clr2
            imgpos = font.text(c, opt, (offsetx, offsety), "midleft")
            self.gamelist.append(imgpos)
            offsety += 33
            cur += 1
txt.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def textlined(self, color, text, center=None, pos='center'):
        darkcolor = [int(c//2) for c in color]
        if text is None: text = ' '
        try:
            if gfx.surface.get_bytesize()>1:
                img1 = self.font.render(text, 1, color)
                img2 = self.font.render(text, 1, darkcolor)
            else:
                img1 = img2 = self.font.render(text, 0, color)
                img2 = self.font.render(text, 0, darkcolor)
        except (pygame.error, TypeError):
            img1 = img2 = pygame.Surface((10, 10))

        newsize = img1.get_width()+4, img1.get_height()+4
        img = pygame.Surface(newsize)
        img.blit(img2, (0, 0))
        img.blit(img2, (0, 4))
        img.blit(img2, (4, 0))
        img.blit(img2, (4, 4))
        img.blit(img1, (2, 2))
        img = img.convert()
        img.set_colorkey((0,0,0), pygame.RLEACCEL)
        r = self._positionrect(img, center, pos)
        return [img, r]
txt.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def textshadowed(self, color, text, center=None, pos='center'):
        darkcolor = [int(c//2) for c in color]
        if text is None: text = ' '
        try:
            if gfx.surface.get_bytesize()>1:
                img1 = self.font.render(text, 1, color)
                img2 = self.font.render(text, 1, darkcolor)
            else:
                img1 = img2 = self.font.render(text, 0, color)
                img2 = self.font.render(text, 0, darkcolor)
        except (pygame.error, TypeError):
            img1 = img2 = pygame.Surface((10, 10))

        newsize = img1.get_width()+2, img1.get_height()+2
        img = pygame.Surface(newsize)
        img.blit(img2, (2, 2))
        img.blit(img1, (0, 0))
        img = img.convert()
        img.set_colorkey((0,0,0), pygame.RLEACCEL)
        r = self._positionrect(img, center, pos)
        return [img, r]
aliens.py 文件源码 项目:Projects 作者: it2school 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
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)
aliens.py 文件源码 项目:Projects 作者: it2school 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def update(self):
        if SCORE != self.lastscore:
            self.lastscore = SCORE
            msg = "Score: %d" % SCORE
            self.image = self.font.render(msg, 0, self.color)
aliens.py 文件源码 项目:Projects 作者: it2school 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
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)
aliens.py 文件源码 项目:Projects 作者: it2school 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def update(self):
        if SCORE != self.lastscore:
            self.lastscore = SCORE
            msg = "Score: %d" % SCORE
            self.image = self.font.render(msg, 0, self.color)
MainMenu.py 文件源码 项目:opseilen 作者: Baal221 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def __init__(self):
                # Resolution
                self.width = 800
                self.height = 600
                self.resolution = (self.width,self.height)
                #self.fullscreen = pygame.FULLSCREEN

                pygame.init() # Makes pygame work

                # Set the resolution
                self.screen = pygame.display.set_mode(self.resolution)

                # Set Title
                self.caption = pygame.display.set_caption('Main Menu')

                # Set default font
                self.font = pygame.font.Font(None, 30)

                self.Rects = Rects(self)
inputbox.py 文件源码 项目:opseilen 作者: Baal221 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def display_box(screen, message):
  "Print a message in a box in the middle of the screen"
  fontobject = pygame.font.Font(None,18)
  pygame.draw.rect(screen, (0,0,0),
                   ((screen.get_width() / 2) - 100,
                    (screen.get_height() / 2) - 10,
                    200,20), 0)
  pygame.draw.rect(screen, (255,255,255),
                   ((screen.get_width() / 2) - 102,
                    (screen.get_height() / 2) - 12,
                    204,24), 1)
  if len(message) != 0:
    screen.blit(fontobject.render(message, 1, (255,255,255)),
                ((screen.get_width() / 2) - 100, (screen.get_height() / 2) - 10))
  pygame.display.flip()
engine.py 文件源码 项目:wargame 作者: maximinus 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def system_checks():
    # do system checks
    if not pygame.font:
        logger.error('Error: Fonts disabled')
        sys.exit(False)
    if not pygame.mixer:
        logger.error('Error: Sound disabled')
        sys.exit(False)
lcars_widgets.py 文件源码 项目:rpi_lcars 作者: tobykurien 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
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)
lcars_widgets.py 文件源码 项目:rpi_lcars 作者: tobykurien 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
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)
cli.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
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 SolarWolf modules')
    if not os.path.isdir('data'):
        msgs.append('Cannot locate SolarWolf data files')

    #first, we need python >= 2.1
    if int(sys.version[0]) < 2:
        errorbox('Requires Python-2.1 or Greater')

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

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

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



#Pretty Error Handling Code...
cli.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __pygamebox(title, message):
    try:
        import pygame
        pygame.quit() #clean out anything running
        pygame.display.init()
        pygame.font.init()
        screen = pygame.display.set_mode((460, 140))
        pygame.display.set_caption(title)
        font = pygame.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()
        pygame.display.flip()
        stopkeys = pygame.K_ESCAPE, pygame.K_SPACE, pygame.K_RETURN, pygame.K_KP_ENTER
        while 1:
            e = pygame.event.wait()
            if e.type == pygame.QUIT or \
                       (e.type == pygame.KEYDOWN and e.key in stopkeys) or \
                       (e.type == pygame.MOUSEBUTTONDOWN and okbox.collidepoint(e.pos)):
                break
        pygame.quit()
    except pygame.error:
        raise ImportError
gamenews.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 112 收藏 0 点赞 0 评论 0
def rendertext(self, font, text):
        return fonts[font][0].text(fonts[font][1], text)
txt.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def initialize():
    pygame.font.init()
    return 1
txt.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def render(self, *args):
        return self.font.render(*args)
txt.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def set_underline(self, *args):
        return self.font.set_underline(*args)
txt.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def set_italic(self, *args):
        return self.font.set_italic(*args)
txt.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def set_bold(self, *args):
        return self.font.set_bold(*args)
txt.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def get_height(self):
        return self.font.get_height()
txt.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def text(self, color, text, center=None, pos='center', bgd=(0,0,0)):
        if text is None: text = ' '
        try:
            if gfx.surface.get_bytesize()>1:
                img = self.font.render(text, 1, color, bgd)
                img.set_colorkey(bgd, pygame.RLEACCEL)
            else:
                img = self.font.render(text, 0, color)
        except (pygame.error, TypeError):
            img = pygame.Surface((10, 10))
        img = img.convert()
        r = self._positionrect(img, center, pos)
        return [img, r]
txt.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def textbox(self, color, text, width, bgcolor, topmargin=6):
        sidemargin = 6
        lines = []
        for line in text.splitlines():
            cursize = 0
            build = ''
            for word in line.split():
                wordspace = word + ' '
                size = self.font.size(wordspace)[0]
                if size + cursize >= width:
                    lines.append(build)
                    cursize = size
                    build = wordspace
                else:
                    build += wordspace
                    cursize += size
            lines.append(build)

        lineheight = self.font.get_linesize()
        height = len(lines) * lineheight + topmargin + sidemargin
        width += sidemargin * 2
        surf = pygame.Surface((width, height))
        surf.fill(bgcolor)
        pos = topmargin
        for line in lines:
            if line:
                img = self.font.render(line, 1, color, bgcolor)
                img.set_colorkey(bgcolor)
                surf.blit(img, (sidemargin, pos))
            pos += lineheight

        return surf
errorbox.py 文件源码 项目:solarwolf 作者: pygame 项目源码 文件源码 阅读 45 收藏 0 点赞 0 评论 0
def __pygame(title, message):
    try:
        import pygame, pygame.font
        pygame.quit() #clean out anything running
        pygame.display.init()
        pygame.font.init()
        screen = pygame.display.set_mode((460, 140))
        pygame.display.set_caption(title)
        font = pygame.font.Font(None, 18)
        foreg = 0, 0, 0
        backg = 200, 200, 200
        liteg = 255, 255, 255
        ok = font.render('Ok', 1, foreg)
        screen.fill(backg)
        okbox = ok.get_rect().inflate(20, 10)
        okbox.centerx = screen.get_rect().centerx
        okbox.bottom = screen.get_rect().bottom - 10
        screen.fill(liteg, okbox)
        screen.blit(ok, okbox.inflate(-20, -10))
        pos = [20, 20]
        for text in message.split('\n'):
            msg = font.render(text, 1, foreg)
            screen.blit(msg, pos)
            pos[1] += font.get_height()

        pygame.display.flip()
        while 1:
            e = pygame.event.wait()
            if (e.type == pygame.QUIT or e.type == pygame.MOUSEBUTTONDOWN or
                        pygame.KEYDOWN and e.key
                        in (pygame.K_ESCAPE, pygame.K_SPACE, pygame.K_RETURN)):
                break
        pygame.quit()
    except pygame.error:
        raise ImportError
aliens.py 文件源码 项目:AIFun 作者: Plottel 项目源码 文件源码 阅读 41 收藏 0 点赞 0 评论 0
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)
aliens.py 文件源码 项目:AIFun 作者: Plottel 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def update(self):
        if SCORE != self.lastscore:
            self.lastscore = SCORE
            msg = "Score: %d" % SCORE
            self.image = self.font.render(msg, 0, self.color)
aliens.py 文件源码 项目:AIFun 作者: Plottel 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
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)
aliens.py 文件源码 项目:AIFun 作者: Plottel 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def update(self):
        if SCORE != self.lastscore:
            self.lastscore = SCORE
            msg = "Score: %d" % SCORE
            self.image = self.font.render(msg, 0, self.color)


问题


面经


文章

微信
公众号

扫码关注公众号