Python 如何在Pygame中以网格状结构显示文本?

Python 如何在Pygame中以网格状结构显示文本?,python,pygame,Python,Pygame,我是pygame的新手,目前我正致力于创建一个记忆游戏,在这个游戏中,电脑会在任意位置显示一秒钟的方块,然后用户必须点击他/她认为这些方块所在的位置。这有点像: 然而,我真的不知道如何让计算机显示带有字母或符号的框,例如“T”或“%”。(我已经做了网格)。 有人能帮忙吗?非常感谢 import pygame size=[500,500] pygame.init() screen=pygame.display.set_mode(size) # Colours LIME = (0,255,0)

我是pygame的新手,目前我正致力于创建一个记忆游戏,在这个游戏中,电脑会在任意位置显示一秒钟的方块,然后用户必须点击他/她认为这些方块所在的位置。这有点像:

然而,我真的不知道如何让计算机显示带有字母或符号的框,例如“T”或“%”。(我已经做了网格)。 有人能帮忙吗?非常感谢

import pygame
size=[500,500]
pygame.init()
screen=pygame.display.set_mode(size)

# Colours
LIME = (0,255,0) 
RED = (255, 0, 0)
BLACK = (0,0,0)
PINK = (255,102,178)
SALMON = (255,192,203)
WHITE = (255,255,255)
LIGHT_PINK = (255, 181, 197)
SKY_BLUE = (176, 226, 255)
screen.fill(BLACK)

# Width and Height of game box
width=50
height=50

# Margin between each cell
margin = 5

# Create a 2 dimensional array. A two dimesional
# array is simply a list of lists.
grid=[]
for row in range(20):
    # Add an empty array that will hold each cell
    # in this row
    grid.append([])
    for column in range(20):
        grid[row].append(0) # Append a cell

# Set row 1, cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[1][5] = 1  

# Set title of screen
pygame.display.set_caption("Spatial Recall")

#Loop until the user clicks the close button.
done=False

# Used to manage how fast the screen updates
clock=pygame.time.Clock()

# -------- Main Program Loop -----------
while done==False:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done=True # Flag that we are done so we exit this loop
      if event.type == pygame.MOUSEBUTTONDOWN:
        # User clicks the mouse. Get the position
        pos = pygame.mouse.get_pos()
        # Change the x/y screen coordinates to grid coordinates
        column=pos[0] // (width+margin)
        row=pos[1] // (height+margin)
        # Sete t hat location to zero
        grid[row][column]=1
        print("Click ",pos,"Grid coordinates: ",row,column)


# Draw the grid
for row in range(10):
    for column in range(10):
        color = LIGHT_PINK
        if grid[row][column] == 1:
            color = RED
        pygame.draw.rect(screen,color,[(margin+width)*column+margin,(margin+height)*row+margin,width,height])

# Limit to 20 frames per second
clock.tick(20)

# Go ahead and update the screen with what we've drawn.
pygame.display.flip()

pygame.quit ()

为了显示文本,您必须经历一系列步骤。首先,您需要使用命令'pygame.font.font(字体名称、大小)获取字体。例如:

arialfont=pygame.font.Font('arial', 12)
text=arialfont.render('Hello World!', True, (0, 0, 0))
所有可用字体都可以通过命令
pygame.font.get\u fonts()
获取。记住在执行任何操作之前初始化pygame(
pygame.init()

接下来,您必须使用
Font.render(text,antialas,color,background=None)
。例如:

arialfont=pygame.font.Font('arial', 12)
text=arialfont.render('Hello World!', True, (0, 0, 0))
这将返回一个曲面。您可以像使用任何其他曲面一样使用它。使用
text.get_rect()
获取其rect,然后重新定位该rect以将其放置在您希望的位置,并将其放到窗口中。如果你对曲面对象一无所知,就问我

这是一个工作代码

import pygame, sys
pygame.init()#never forget this line
window=pygame.display.set_mode((100, 100))
font=pygame.font.SysFont('arial', 40)
text=font.render('@', True, (0, 0, 0))
rect=text.get_rect()
window.fill((255, 255, 255))
window.blit(text, rect)
pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()

我看不到那一页,好像在付费墙后面。你能提供一个截图吗?非常感谢你的回答,但你是对的,我从来没有被教过任何关于表面物体的东西……@nnekymoe没问题。曲面对象基本上是使用程序存储的图片。它可以绘制到其他曲面上,也可以进行Blitt,也可以使用
pygame.draw
绘制。曲面对象的一个示例是屏幕,它可以帮助您理解它们。为了blit一个曲面对象,你必须有一个矩形或矩形,你可能知道。要获取曲面的矩形,请使用
surface.get_rect()
。我将在另一篇评论中继续这一点。我没有字符了。@nnekymoe因此,当您使用
get_rect()
函数生成一个rect时,它会将它放在屏幕的左上角。若要更改该属性,请更改其属性,例如左、右、上、下、centerx、centery、中心、左上、左下、右上和右下。例如:
rect.bottom=430
rect.bottomleft=(0300)
。要将曲面对象blit显示到屏幕或任何其他曲面,请使用
screen.blit(surface,rect)
。如果你什么都不懂,我很乐意帮忙。@PgameNerd再次感谢你的回复。我尝试将对象(此符号:@)快速移动到屏幕上,但它没有给我任何东西。我也尝试过上pygame网站,但也没用。有什么我可能做错了吗?@nnekymoe我编辑了我的答案,加入了一个工作代码,用于将
@
快速发送到窗口。看看你是否能自己解决这个问题。否则,给我代码。