我不知道如何修复这个错误:TypeError:blit的目标位置无效



我一直有这个错误,我不知道如何修复它:TypeError:blit 的目标位置无效

我正试图构建一个标签游戏,在一个屏幕上有两个玩家,我需要一个窗口来显示谁是"it">

这是我的代码:

import pygame as pyg
import pygame.gfxdraw
import time

# Screen
pygame.init()
screen = pyg.display.set_mode((1280, 720), pygame.RESIZABLE)
width,height= screen.get_size()
pygame_icon = pyg.image.load('icon.png')
pyg.display.set_icon(pygame_icon)
pyg.display.set_caption("taggy")
clock = pyg.time.Clock()
black = (0,0,0)
background = black

tikker = 0
PlayerNames = ['Player 1', 'Player 2']
test_font = pyg.font.SysFont("Comic Sans MS", 50)
text = pyg.surface, test_font.render(PlayerNames[0], True, black)
while True:
for event in pyg.event.get():
if event.type == pyg.QUIT:
quit()
# Code
screen.fill((background))
screen.blit(screen, text, (width/2), 50)

pyg.display.flip()
clock.tick(60)

有人知道怎么修吗?我使用vscode和python 3.9.9

谢谢你抽出时间!:]

我想我找到了一个解决方案。函数blit请求强制项:pygame.suface和position,所以类似于以下内容:screen.blit(picture, (x, y))。您不需要进入屏幕,因为它已经在命令中了。程序认为屏幕就是你的图像,文本就是位置。所以你只需要从函数中删除屏幕,所以把screen.blit(screen, text, (width/2), 50)替换为screen.blit( text, (width/2), 50),我希望它能帮助你。完整的好代码:

import pygame as pyg
import pygame.gfxdraw
import time
import sys

# Screen
pygame.init()
pygame.font.init()
screen = pyg.display.set_mode((1280, 720), pygame.RESIZABLE)
width,height= screen.get_size()
pyg.display.set_caption("taggy")
clock = pyg.time.Clock()
black = (0,0,0)
white = (255, 255, 255)
background = black

tikker = 0
PlayerNames = ['Player 1', 'Player 2']
test_font = pyg.font.SysFont("Comic Sans MS", 50)
text = test_font.render(PlayerNames[0], True, white)
while True:
for event in pyg.event.get():
if event.type == pyg.QUIT:
pygame.quit()
sys.exit()
# Code
screen.fill((background))
screen.blit(text, (800, 600))

pyg.display.flip()
clock.tick(60)

我编辑了一点文本的中心线,你可能需要更改它。如果你将使用宽度和高度作为中心线,文本将离开屏幕。

最新更新