pygame自定义字体将字母放入错误的p

2024-10-01 07:36:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用的IDE(pyscripter)有一些字体错误,所以我尝试使用自己的字体。 我很难把信放在我想要的地方。例如

draw_word('abab',[15,10]) 

使“abab”这个词(到目前为止我只做了a和b)。但是,如果我要这样做:

draw_word('abab',[50,10])

然后这些信就散开了。我想让这个词以x=50的速度出现在屏幕上。你知道吗

draw_word('abab',[5,10])

这会将单词向上压缩,而不是在x=5时将其放到屏幕上。你知道吗

我该如何解决这个问题?是什么导致了这个问题?你知道吗

完整代码为:

draw_word('abab',[15,10])

这就叫:

  def draw_word(word,xy):
    loc=1 # short for location
    for letter in word:
        draw_letter(letter,[(xy[0]*loc),xy[1]]) #uses loc to move the letter over
        loc+=1 #next letter

这就叫:

def draw_letter(letter,xy):
l=pygame.image.load(('letters/'+letter+'.png')).convert()
l.set_colorkey(WHITE)
screen.blit(l,xy)

Tags: for屏幕def地方错误字体ide速度
1条回答
网友
1楼 · 发布于 2024-10-01 07:36:40

for letter in word中加上print xy[0]*loc,你就会明白为什么字母放错了地方。你知道吗

例如x=50:第一个字母50*1=50,下一个字母50*2=100,下一个字母50*3=150

您需要:

def draw_word(word,xy):
    loc=0 # short for location
    for letter in word:
        draw_letter(letter,[(xy[0]+loc),xy[1]]) #uses loc to move the letter over
        loc += 20 #next letter

使用loc += 20中的其他值可以获得更好的字母间距。你知道吗


顺便说一句:你可以这样写:

def draw_word(word,xy, distance=20)
    for letter in word:
        draw_letter(letter,xy)
        xy[0] += distance

现在你可以用了

draw_word('abab',[15,10]) # distance will be 20
draw_word('abab',[15,10], 30) # distance will be 30

相关问题 更多 >