Python Pygame 字体 x 坐标
Python Pygame Font x coordinate
我正在尝试在 pygame 中制作一个二维密码生成器,我必须询问用户并从用户那里获取有关他希望密码的数字长度的文本输入。
我知道文本输入会很短但是如果用户键入一个大字符串表明文本输入超过我的屏幕宽度怎么办所以我的问题是如何检测字体是否超过我的屏幕宽度所以从现在开始无论用户输入什么显示在新行中,因为如果文本不在换行符中,我的用户将无法看到他现在输入的内容。
这是我的文本输入函数:
def text_input(x, y, color, char=None, char_x=None, char_y=None, char_color=None, fill=(0, 0, 0), thing=None,
thing_x=None, thing_y=None):
"""
:param x: Defines the x axis or the width position where the text input should be drawn.
:param y: Defines the y axis or the height position where the text input should be drawn.
:param color: Defines the color of the text input.
:param char: If the parameter char in not entered it does not do anything otherwise it display's the the parameter
char's text on the screen
:param char_x: If the parameter char is entered char_x Defines the x axis or the width position where the text
should be drawn.
:param char_y: If the parameter char is entered char_y Defines the y axis or the height position where the text
should be drawn.
:param char_color: Defines the color of the text of the parameter char in rgb color pixels.
:param fill: If the parameter fill is entered it fills the display with the tuple of the rgb color pixels which the
user entered. But if the parameter fill is not entered it fills the display with the rgb color code
black.
:param thing: If the user wants some thing to been drawn on the display the fill the parameter thing with the object
they want to draw. But if the parameter thing is not entered. The thing parameter should be a pygame
Surface.
:param thing_x: If the parameter thing is entered thing_x Defines the x axis or the width position where the
parameter thing should be drawn
:param thing_y: If the parameter thing is entered thing_y Defines the y axis or the height position where the
parameter thing should be drawn.
:return: It Display's the text input and the other parameters which were entered on the window and the updates it.
"""
characters = ""
WINDOW.fill(fill)
if thing is not None:
draw(thing, thing_x, thing_y)
if char is not None:
char_text = str(char)
char_text_surface = FONT.render(char_text, True, char_color)
draw(char_text_surface, char_x, char_y, True)
pygame.display.update()
while True:
for detect in pygame.event.get():
if detect.type == QUIT:
exit()
elif detect.type == KEYDOWN:
if detect.key == K_BACKSPACE:
characters = characters[:-1]
elif detect.key == K_RETURN:
global text
text = characters
return text
else:
characters = characters + detect.unicode
WINDOW.fill(fill)
if thing is not None:
draw(thing, thing_x, thing_y)
if char is not None:
char_text = str(char)
char_text_surface = FONT.render(char_text, True, char_color)
draw(char_text_surface, char_x, char_y, True)
text = FONT.render(f"{characters}", True, color)
print(char_x)
draw(text, x, y, True)
如果您想知道绘制函数是什么,请查看它的代码:
def draw(thing, x, y, update=None):
"""
:param thing: The object what needs to be drawn on the screen
:param x: Defines the x axis or the width position where the object should be drawn.
:param y: Defines the y axis or the height position where the object should be drawn.
:param update: If the parameter update is not passed we don't update the display.
But if the parameter update is passed as True we update the display.
:return: It returns the object on the display at its specific place or it returns
the object on the display at its specific place and updates and updates the display based on the parameter
update.
"""
if update is True:
WINDOW.blit(thing, (x, y))
return pygame.display.update()
else:
return WINDOW.blit(thing, (x, y)
如果你想知道我的完整代码就问我我会编辑这个post:
使用 pygame.font.Font.size
确定渲染文本所需的 space 数量:
text_width, text_height = FONT.size(text)
字体的第space行可以用get_linesize
得到
使用此方法逐行呈现文本:
def render_text(font, text, color, x, y, max_length = 100, update=False):
line_y = y
si = 0
ei = 1
while ei < len(text)+1:
text_width, text_height = font.size(text[si:ei])
if text_width > max_length:
text_surf = font.render(text[si:ei-1], True, color)
WINDOW.blit(text_surf, (x, line_y))
line_y += font.get_linesize()
si = ei-1
ei += 1
if si < len(text):
text_width, text_height = font.size(text[si:])
text_surf = font.render(text[si:], True, color)
WINDOW.blit(text_surf, (x, line_y))
if update:
pygame.display.update()
def text_input(x, y, color, char=None, char_x=None, char_y=None, char_color=None, fill=(0, 0, 0), thing=None,
thing_x=None, thing_y=None):
# [...]
while True:
for detect in pygame.event.get():
if detect.type == QUIT:
exit()
elif detect.type == KEYDOWN:
# [...]
render_text(FONT, characters, color, x, y, 100, True)
每个 character_text_surface 都有一个宽度,您可以通过以下方式获得:
character_text_surface.get_width()
所以你需要做的是有一个 total_width 变量,并且对于 blit 的每个字符,将该字符的宽度添加到 total_width。然后你可以这样测试它:
if start_of_input_x + total_width >= screenwidth:
y += character_text_surface.get_height()
我正在尝试在 pygame 中制作一个二维密码生成器,我必须询问用户并从用户那里获取有关他希望密码的数字长度的文本输入。 我知道文本输入会很短但是如果用户键入一个大字符串表明文本输入超过我的屏幕宽度怎么办所以我的问题是如何检测字体是否超过我的屏幕宽度所以从现在开始无论用户输入什么显示在新行中,因为如果文本不在换行符中,我的用户将无法看到他现在输入的内容。
这是我的文本输入函数:
def text_input(x, y, color, char=None, char_x=None, char_y=None, char_color=None, fill=(0, 0, 0), thing=None,
thing_x=None, thing_y=None):
"""
:param x: Defines the x axis or the width position where the text input should be drawn.
:param y: Defines the y axis or the height position where the text input should be drawn.
:param color: Defines the color of the text input.
:param char: If the parameter char in not entered it does not do anything otherwise it display's the the parameter
char's text on the screen
:param char_x: If the parameter char is entered char_x Defines the x axis or the width position where the text
should be drawn.
:param char_y: If the parameter char is entered char_y Defines the y axis or the height position where the text
should be drawn.
:param char_color: Defines the color of the text of the parameter char in rgb color pixels.
:param fill: If the parameter fill is entered it fills the display with the tuple of the rgb color pixels which the
user entered. But if the parameter fill is not entered it fills the display with the rgb color code
black.
:param thing: If the user wants some thing to been drawn on the display the fill the parameter thing with the object
they want to draw. But if the parameter thing is not entered. The thing parameter should be a pygame
Surface.
:param thing_x: If the parameter thing is entered thing_x Defines the x axis or the width position where the
parameter thing should be drawn
:param thing_y: If the parameter thing is entered thing_y Defines the y axis or the height position where the
parameter thing should be drawn.
:return: It Display's the text input and the other parameters which were entered on the window and the updates it.
"""
characters = ""
WINDOW.fill(fill)
if thing is not None:
draw(thing, thing_x, thing_y)
if char is not None:
char_text = str(char)
char_text_surface = FONT.render(char_text, True, char_color)
draw(char_text_surface, char_x, char_y, True)
pygame.display.update()
while True:
for detect in pygame.event.get():
if detect.type == QUIT:
exit()
elif detect.type == KEYDOWN:
if detect.key == K_BACKSPACE:
characters = characters[:-1]
elif detect.key == K_RETURN:
global text
text = characters
return text
else:
characters = characters + detect.unicode
WINDOW.fill(fill)
if thing is not None:
draw(thing, thing_x, thing_y)
if char is not None:
char_text = str(char)
char_text_surface = FONT.render(char_text, True, char_color)
draw(char_text_surface, char_x, char_y, True)
text = FONT.render(f"{characters}", True, color)
print(char_x)
draw(text, x, y, True)
如果您想知道绘制函数是什么,请查看它的代码:
def draw(thing, x, y, update=None):
"""
:param thing: The object what needs to be drawn on the screen
:param x: Defines the x axis or the width position where the object should be drawn.
:param y: Defines the y axis or the height position where the object should be drawn.
:param update: If the parameter update is not passed we don't update the display.
But if the parameter update is passed as True we update the display.
:return: It returns the object on the display at its specific place or it returns
the object on the display at its specific place and updates and updates the display based on the parameter
update.
"""
if update is True:
WINDOW.blit(thing, (x, y))
return pygame.display.update()
else:
return WINDOW.blit(thing, (x, y)
如果你想知道我的完整代码就问我我会编辑这个post:
使用 pygame.font.Font.size
确定渲染文本所需的 space 数量:
text_width, text_height = FONT.size(text)
字体的第space行可以用get_linesize
使用此方法逐行呈现文本:
def render_text(font, text, color, x, y, max_length = 100, update=False):
line_y = y
si = 0
ei = 1
while ei < len(text)+1:
text_width, text_height = font.size(text[si:ei])
if text_width > max_length:
text_surf = font.render(text[si:ei-1], True, color)
WINDOW.blit(text_surf, (x, line_y))
line_y += font.get_linesize()
si = ei-1
ei += 1
if si < len(text):
text_width, text_height = font.size(text[si:])
text_surf = font.render(text[si:], True, color)
WINDOW.blit(text_surf, (x, line_y))
if update:
pygame.display.update()
def text_input(x, y, color, char=None, char_x=None, char_y=None, char_color=None, fill=(0, 0, 0), thing=None,
thing_x=None, thing_y=None):
# [...]
while True:
for detect in pygame.event.get():
if detect.type == QUIT:
exit()
elif detect.type == KEYDOWN:
# [...]
render_text(FONT, characters, color, x, y, 100, True)
每个 character_text_surface 都有一个宽度,您可以通过以下方式获得:
character_text_surface.get_width()
所以你需要做的是有一个 total_width 变量,并且对于 blit 的每个字符,将该字符的宽度添加到 total_width。然后你可以这样测试它:
if start_of_input_x + total_width >= screenwidth:
y += character_text_surface.get_height()