我创建了一个名为message_display(text)的定义,但是当我在代码中使用该定义时,文本会彼此重叠in the lower left corner you see that hello and world is printed on top of eacht other。感谢您的帮助
import pygame
import random
import sys
pygame.init()
win = pygame.display.set_mode((800,700))
pygame.display.set_caption('Knights Of Dungeons')
gb = pygame.image.load('background.png')
font= pygame.font.SysFont('Gabriola', 30, False,True)
game = True
roll_dice = font.render('Press Enter To roll the dice' ,10,(0,0,0))
def dice():
x = random.randint(1,6)
return x
def message_display(text):
dis_text = font.render(text, 10, (0,0,0))
win.blit(dis_text,(10,650))
pygame.display.update()
player_1 = font.render('Name: Kaan '+ 'Health: 100
' + 'Damage: 0 ' + 'Armour: 0 ')
while game:
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = False
keys = pygame.key.get_pressed()
if keys[pygame.K_RETURN]:
num_thrown = dice()
roll_dice = font.render( 'The number you have throwen is:
'+str(0+num_thrown) ,20,(0,0,0))
win.blit(gb,(0,15))
win.blit(player_1 ,(0,100))
win.blit(roll_dice,(455,650))
message_display('Hello')
message_display('world')
pygame.display.update()
pygame.quit()
答案 0 :(得分:0)
当我使用pygame时,我希望使文本显示功能更加通用。像这样:
import pygame as pg
def draw_text(surface, text, size, color, x, y):
"""Draw text to surface
surface - Pygame surface to draw to
text - string text to draw
size - font size
color - color of text
x - x position of text on surface
y - y position of text on surface
"""
font = pg.font.Font(font_name, size)
text_surf = font.render(str(text), True, color)
text_rect = text_surf.get_rect()
text_rect.topleft = (x, y) # I use topleft here because that makes sense to me
# for English (unless you want it centered).
# But you can use any part of the rect to start drawing the text
surface.blit(text_surf, text_rect)
请注意,您还必须设置font_name
。您可以在函数内部或外部执行此操作(如果您只想要一个)。我已经针对以下用例在全局范围内完成了此操作:
font_name = pg.font.match_font('arial')