import pygame
WINDOWWIDTH = 1080
WINDOWHEIGHT = 1080
Window = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), pygame.FULLSCREEN)
class CustomFont: #The letterimage file must consist of equally-sized letters, each of 'lettersize', arranged in a single line, left-to-right. The letterstring is a string representation of the letters in the image (in the same order).
def __init__(self, letterstring, lettersize, letterimagefile):
self.letterstring = tuple(letterstring)
self.lettersize = lettersize
self.letterimagefile = letterimagefile
def write (self, string, position):
stringseparated = tuple(string)
for i in range(len(stringseparated)):
letter = stringseparated[i]
for j in range(len(self.letterstring)):
if self.letterstring[j] == letter: #j gives us the index number of the letter in our original string
grabletterbox = ((self.lettersize[0] * j), 0, self.lettersize[0], self.lettersize[1]) #this defines the position of the rectangle that will grab the letter from our letterimage file, as well as the size of said rectangle
Window.blit(self.letterimagefile, ((position[0] + (i * self.lettersize[0])),position[1]) , grabletterbox)
ActiveNumbersSprite = pygame.image.load('.\images\ActiveNumbers.png') #The image used for the numbers
ActiveNumbers = CustomFont('0123456789', (36 , 60), ActiveNumbersSprite) #This tells the program what characters the ActiveNumbersSprite image contains, in what order, how big each character is (as (x,y), and the name of the image file
...
我没有找到支持颜色的位图字体格式(更不用说python3.4/pygame了),但是下面user3191557提供的另一种解决方案让我走上了正确的道路。在
最后,我创建了一个png图像,其中包含我需要的字符串(图像中所有字符的间距/大小都相等),从左到右。对我来说,我只需要0到9之间的数字。以下是我使用的代码(希望我没有遗漏任何相关内容):
有了它,我就可以用ActiveNumbers.write(string,(x,y))在窗口中的任何x,y位置用我的字体写任何我想要的字符串(当然,假设我有所需字符的图像)。很有魅力。在
我不知道PyGame,但OpenType有一些规范可以满足您的需要。唯一的问题是:呈现字体的东西必须支持这些new and bleeding edge specs。例如,在浏览器领域,这些技术中的一些在Firefox中有效,有些在IE11中,还有一些只有在Mac上作为系统字体安装时才起作用。在
但你可以做colorful game fonts like these :)
大多数“早期”字体格式都是基于位图字体的,所以你可能会很幸运。在
对于一个简单的例子,pygame here有一个“位图字体”片段。这不实现(单词)包装和其他花哨的东西。在
但是,您也可以或多或少地轻松地滚动:
希望有帮助
相关问题 更多 >
编程相关推荐