如何修复错误“TypeError:参数 1 必须是 pygame.surface.Surface,而不是元组”

问题描述 投票:0回答:1

我是 pygame 的新手,正在尝试制作一个带有一些文本的游戏,但是当我运行时,我收到错误“TypeError:参数 1 必须是 pygame.surface.Surface,而不是 tuple”。这是我的代码:

1 import sys
2 import pygame
3 pygame.init()
4 # Make The Screen
5 screen = pygame.display.set_mode((1200,800))
6 screen_rect = screen.get_rect()
7 screen.fill((135, 206, 235))
8 # Font
9 font = pygame.freetype.SysFont('Times New Roman', 30)
10while True:
11    # make it work
12    for event in pygame.event.get():
13        if event.type == pygame.QUIT:
14            sys.exit()
15        screen.fill((250, 250, 0))
16        # Try to make text happen
17        ammo_text = font.render(f'Hello World', (0, 0, 100))
18        screen.blit(ammo_text, (40, 250))
19        pygame.display.flip() 

错误发生在第 18 行,我尝试 blit 文本。

我尝试了几种不同的方法,但出现了同样的错误。我对想法持开放态度。

pygame pygame-surface
1个回答
0
投票

您遇到的问题是由于 pygame.Font.render()pygame.freetype.Font.render().

之间的差异造成的

freetype 字体对象返回一个元组,一个

Surface
rect
。因此,您可以更改代码以忽略返回:

ammo_text, _ = font.render(f'Hello World', (0, 0, 100))
© www.soinside.com 2019 - 2024. All rights reserved.