无法弄清楚如何检查两个精灵之间的蒙版碰撞
我在同一组中有两个不同的精灵,变量“玩家”和“地面”。它们都是不同的类别,表面上都带有面具。这条线是他们两个班的。
self.mask = pygame.mask.from_surface(self.surface)
其表面中使用的图像使用了“convert_alpha()”,因此其中一部分是透明的,并且蒙版应该对它们起作用。地面是几个平台,我想检查碰撞,这样我就可以将玩家保持在地面上,并在它们不在不透明部分上时让它们掉落。
if pygame.sprite.collide_mask(player,ground):
print("collision")
else:
print("nope")
即使玩家精灵掉落到彩色地面精灵像素所在的位置,也会打印“不”。因此,“collide_mask()”的文档表示,当没有碰撞时,它返回“NoneType”。所以我尝试了这个。
if pygame.sprite.collide_mask(player,ground)!= NoneType:
print("collision")
无论玩家在哪里,都会打印“碰撞”(我为玩家设置了跳跃、向左和向右移动)。我昨天问了一个关于碰撞的问题,但没有得到有帮助的答案。我被告知要压缩我在问题中提交的代码,所以希望我能够很好地解释这一点,而无需发布所有 90 行。我在这里检查了很多其他问题,它们似乎都有点不同,所以我很困惑(而且相当新)。 强调两个精灵都在同一组中,因此我无法让 spritecollide() 工作。
I have two different sprites in the same group, variables 'player' and 'ground'. They both are separate classes, with a mask of their surface. This line is in both of their classes.
self.mask = pygame.mask.from_surface(self.surface)
The images used in their surfaces have 'convert_alpha()' used on them so part of them is transparent, and the mask should work on them. The ground is a few platforms, and I want to check for collision so I can keep the player on the ground, and have them fall when they are not on the non-transparent parts.
if pygame.sprite.collide_mask(player,ground):
print("collision")
else:
print("nope")
This prints "nope", even as the player sprite is falling over where the colored ground sprite pixels are. So the documentation for 'collide_mask()' says that it returns "NoneType" when there is no collision. So I tried this.
if pygame.sprite.collide_mask(player,ground)!= NoneType:
print("collision")
This prints "collision" no matter where the player is(I have jumping, left, and right movements setup for the player). I asked a question about collision yesterday with no answer that helped. And I was told to condense my code submitted in the question so hopefully I explained this well enough without posting all 90 lines. I've checked a lot of other questions on here, and they all seem to do it a little different so I'm very confused (and fairly new).
Emphasis on both sprites being in the same group, I couldn't get spritecollide() to work because of this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
精灵不仅需要
mask
属性,还需要rect
属性。mask
定义位掩码,rect
指定精灵在屏幕上的位置。请参阅pygame.sprite.collide_mask
< /a>:如果在
pygame.sprite.Group
中使用精灵,则每个精灵都应该具有image
和rect
属性。pygame.sprite.Group.draw()
和pygame.sprite.Group.update()
是pygame.sprite.Group
提供的方法。后者委托给所包含的
pygame.sprite.Sprite
s — 您必须实现该方法。请参阅pygame.sprite.Group.update( )
:前者使用包含的 pygame.sprite.Sprite 的 image 和 rect 属性来绘制对象 - 你有确保 pygame.sprite.Sprite 具有所需的属性。请参阅
pygame.sprite.Group.draw( )
:最小示例
The sprites do not only need the
mask
attribute, they also need therect
attribute. themask
defines the bitmask andrect
specifies the posiotion of the sprite on the screen. Seepygame.sprite.collide_mask
:If sprites are used in
pygame.sprite.Group
s then each sprite should haveimage
andrect
attributes.pygame.sprite.Group.draw()
andpygame.sprite.Group.update()
are methods which are provided bypygame.sprite.Group
.The latter delegates to the
update
method of the containedpygame.sprite.Sprite
s — you have to implement the method. Seepygame.sprite.Group.update()
:The former uses the
image
andrect
attributes of the containedpygame.sprite.Sprite
s to draw the objects — you have to ensure that thepygame.sprite.Sprite
s have the required attributes. Seepygame.sprite.Group.draw()
:Minimal example