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

标签 python pygame

我是 pygame 的新手,目前我正在创建一个内存游戏,其中计算机在随机位置显示方 block 大约一秒钟,然后用户必须点击他/她认为这些方 block 所在的位置。有点像this game :

screenshot

但是我不太确定如何让计算机显示带有字母或符号的框,例如'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(font name, size) 来获取字体。例如:

arialfont=pygame.font.Font('arial', 12)

所有可用的字体都可以从命令 pygame.font.get_fonts() 中获取。请记住在执行任何操作之前初始化 pygame (pygame.init())。

接下来,您将不得不使用 Font.render(text, antialias, color, background=None)。例如:

text=arialfont.render('Hello World!', True, (0, 0, 0))

这将返回一个表面。您可以像使用任何其他表面一样使用它。使用 text.get_rect() 获取其 rect,然后重新定位 rect 以将其放置在您想要的位置,并将其 blit 到窗口。如果你对表面物体一无所知,尽管问我。

这是一个工作代码。

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()

关于python - 如何在 Pygame 中以网格状结构显示文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14096280/

相关文章:

python - pygame 横向卷轴平台游戏中奇怪的平台运动

python - 如何在 Sprite 的底部添加额外的图像或矩形?

python-2.7 - Homebrew 软件:没有 smpeg 的公式

python - pyQTGraph设置轴值(FFT)

python - 在图形工具中更改背景颜色

python Pandas : Getting the locations of a value in dataframe

python - Ubuntu Python 3 上的 Pygame

python - 将 .py 转换为 .exe 时出现 cx_Freeze 错误

python - 在 Jupyter hubtable.js 中显示数据

python - Pytest 插件打开 GUI