Python pygame-如何将文本写入特定位置?还有,如何使用坐标

Python pygame-如何将文本写入特定位置?还有,如何使用坐标,python,pygame,python-3.3,Python,Pygame,Python 3.3,我一直在四处搜寻,我真的找不到任何关于如何写文本的好资源?我尝试过对文本使用rendering命令,但它对我不起作用 还有,当你在这里的时候;有没有一种“叠加”类型的东西,你可以看到坐标的确切位置?除了每次使用坐标来精确定位一个区域时使用试错法外,我真的不知道整个坐标系是如何工作的 非常感谢,surf=pygame.display.set\u模式((宽度、高度)) ... #获取字体对象 font=pygame.font.SysFont('Arial',12,bold=True) #将给定字体渲

我一直在四处搜寻,我真的找不到任何关于如何写文本的好资源?我尝试过对文本使用rendering命令,但它对我不起作用

还有,当你在这里的时候;有没有一种“叠加”类型的东西,你可以看到坐标的确切位置?除了每次使用坐标来精确定位一个区域时使用试错法外,我真的不知道整个坐标系是如何工作的

非常感谢,

surf=pygame.display.set\u模式((宽度、高度))
...
#获取字体对象
font=pygame.font.SysFont('Arial',12,bold=True)
#将给定字体渲染为图像
img=font.render('Text to write',True,
pygame.Color(字体和颜色),
pygame.Color(字体\背景\颜色))
#最后把它放在表面上。
#下面的代码将文本图像居中
surf.blit(img,((surf.get_width()-img.get_width())/2,
(冲浪高度()-img.冲浪高度())/2)
surf=pygame.display.set_模式((宽度、高度))
...
#获取字体对象
font=pygame.font.SysFont('Arial',12,bold=True)
#将给定字体渲染为图像
img=font.render('Text to write',True,
pygame.Color(字体和颜色),
pygame.Color(字体\背景\颜色))
#最后把它放在表面上。
#下面的代码将文本图像居中
surf.blit(img,((surf.get_width()-img.get_width())/2,
(冲浪高度()-img.冲浪高度())/2)
这里还有一些帮助:

对于您的具体问题,请阅读以下内容:

向下滚动至“像素坐标”,了解pygames中的坐标系

在第2章中进一步向下滚动,了解字体以及如何呈现字体

这里还有一些帮助:

对于您的具体问题,请阅读以下内容:

向下滚动至“像素坐标”,了解pygames中的坐标系


在第2章中进一步向下滚动,了解字体以及如何呈现字体

呈现字体非常简单

import pygame
from pygame.locals import * 

# Initialize the font system and create the font and font renderer
pygame.font.init()
default_font = pygame.font.get_default_font()
font_renderer = pygame.font.Font(default_font, size)

# To create a surface containing `Some Text`
label = font_renderer.render(
    "Some Text",   # The font to render
    1,             # With anti aliasing
    (255,255,255)) # RGB Color
这将返回一个曲面,该曲面可以blit到另一个曲面。该表面还包含一个具有所述字体尺寸的矩形

# To apply this surface to another you can do the following
another_surface.blit(
    label,  # The text to render
    (0,0))  # Where on the destination surface to render said font
现在坐标系非常简单

你这样创造你的屏幕

import pygame
from pygame.locals import * 
screen_dimensions = width, height = 800, 600
screen = pygame.display.set_mode(screen_dimensions)
       800    
    -----------
    -         -
600 -         -
    -         -
    -----------
在pygame中,这将创建一个这样的屏幕

import pygame
from pygame.locals import * 
screen_dimensions = width, height = 800, 600
screen = pygame.display.set_mode(screen_dimensions)
       800    
    -----------
    -         -
600 -         -
    -         -
    -----------
最左上角是坐标
0,0
,最右下角是
800600

如果您想在左上角使用5px的填充来blit字体,您可以执行以下操作:

# Using the label created above
screen.blit(label, (5, 5))
我在PyGame中编写了一个生命克隆游戏,您可以查看源代码并准确了解我如何使用字体渲染它们


至于叠加图,可以看到物体的精确坐标。您可以通过捕获鼠标位置
pygame.mouse.get_pos()
并将其推到您在曲面上渲染的标签上,轻松完成此操作

呈现字体非常简单

import pygame
from pygame.locals import * 

# Initialize the font system and create the font and font renderer
pygame.font.init()
default_font = pygame.font.get_default_font()
font_renderer = pygame.font.Font(default_font, size)

# To create a surface containing `Some Text`
label = font_renderer.render(
    "Some Text",   # The font to render
    1,             # With anti aliasing
    (255,255,255)) # RGB Color
这将返回一个曲面,该曲面可以blit到另一个曲面。该表面还包含一个具有所述字体尺寸的矩形

# To apply this surface to another you can do the following
another_surface.blit(
    label,  # The text to render
    (0,0))  # Where on the destination surface to render said font
现在坐标系非常简单

你这样创造你的屏幕

import pygame
from pygame.locals import * 
screen_dimensions = width, height = 800, 600
screen = pygame.display.set_mode(screen_dimensions)
       800    
    -----------
    -         -
600 -         -
    -         -
    -----------
在pygame中,这将创建一个这样的屏幕

import pygame
from pygame.locals import * 
screen_dimensions = width, height = 800, 600
screen = pygame.display.set_mode(screen_dimensions)
       800    
    -----------
    -         -
600 -         -
    -         -
    -----------
最左上角是坐标
0,0
,最右下角是
800600

如果您想在左上角使用5px的填充来blit字体,您可以执行以下操作:

# Using the label created above
screen.blit(label, (5, 5))
我在PyGame中编写了一个生命克隆游戏,您可以查看源代码并准确了解我如何使用字体渲染它们


至于叠加图,可以看到物体的精确坐标。您可以通过捕获鼠标位置
pygame.mouse.get_pos()
并将其推到您在曲面上渲染的标签上,轻松完成此操作

你读了吗?有帮助吗?如果你仍然有问题,为什么不用更多的信息更新你的问题?您标记了一个答案,然后将其删除。对不起,我想我不小心误点击了您答案上的“答案”按钮。现在又是答案:)你读了吗?有帮助吗?如果你仍然有问题,为什么不用更多的信息更新你的问题?您标记了一个答案,然后将其删除。对不起,我想我不小心误点击了您答案上的“答案”按钮。现在又是答案:)