简体   繁体   English

错误:描述符“ blit”需要“ pygame.Surface”对象,但收到了“ NoneType”

[英]Error: descriptor 'blit' requires a 'pygame.Surface' object but received a 'NoneType'

I am creating a game. 我正在创建一个游戏。 I wrote this code to create a sprite and its hitbox: 我编写了以下代码来创建一个精灵及其点击框:

hg = pygame.image.load('hgrd1copy.jpg').set_colorkey(red)
hgbox = pygame.Rect(0 ,13 ,36 ,72)
pygame.Surface.blit(hg, hgbox)

I had originally put 我原来放

windowSurface.blit(hg, hgbox)

but then I got an error telling me that my argument needed to be pygame.Surface not none. 但是后来我遇到一个错误,告诉我我的论点必须是pygame.Surface不是没有。

However, when I changed the code to pygame.Surface.blit , it gives me this error code: TypeError: descriptor 'blit' requires a 'pygame.Surface' object but received a 'NoneType' What do I do? 但是,当我将代码更改为pygame.Surface.blit ,它给了我这个错误代码: TypeError: descriptor 'blit' requires a 'pygame.Surface' object but received a 'NoneType'我该怎么办?

As explained in the commemts - y9u have to load your image in one line of code, and in another line call the set_colorkey method on the image read. 如通讯中所述-y9u必须在一行代码中加载图像,而在另一行代码中,对读取的图像调用set_colorkey方法。 And them, blit should be called as a method from an existing surface - not from the Surface class. 而且,应该从现有曲面(而不是从Surface类)将blit称为方法。 (With the code as it is now in the question, how would the program "know" where to blit the image to? (有,因为它现在是在问题的代码,怎么会节目“知道” 的图像BitBlt到?

So, assuming your screen is on the windowSurface variable you describe, this should work: 因此,假设您的屏幕位于您描述的windowSurface变量上,那么这应该可以工作:

hg = pygame.image.load('hgrd1copy.jpg')
hg.set_colorkey(red)
hgbox = pygame.Rect(0, 13, 36, 72)
windowSurface.blit(hg, hgbox)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM