2024-04-20 15:53:44 发布
网友
我试图在我的游戏中实现碰撞,但我不知道如何获取图像的rect属性。玩家可以选择图片在哪里生成(他把它们放在哪里),所以我不能设置一个确切的数字等等
pygame.Surface对象有一个方法get_rect,该方法返回一个pygame.Rect的实例,该实例包含{}。注意,Rect的position(x和y属性)从(0,0)开始,但是您可以更改该值,并在blit到另一个曲面时引用它。在
pygame.Surface
get_rect
pygame.Rect
Rect
x
y
因此,如果您有一个名为surf的pygame.Surface,和一个名为screen的屏幕表面,您可以这样做:
surf
screen
surf_rect = surf.get_rect() # move the surface 20 units to the right, and 15 units down surf_rect.x += 20 surf_rect.y += 15 # now draw this surface to the screen based on the surf_rect's position screen.blit(surf, (surf_rect.x, surf_rect.y))
http://www.pygame.org/docs/ref/surface.html#pygame.Surface.get_rect
另外,pygame.Surface也有get_width和{}方法,它们都非常自解释。在
get_width
pygame.Surface
对象有一个方法get_rect
,该方法返回一个pygame.Rect
的实例,该实例包含{Rect
的position(x
和y
属性)从(0,0)开始,但是您可以更改该值,并在blit到另一个曲面时引用它。在因此,如果您有一个名为
surf
的pygame.Surface
,和一个名为screen
的屏幕表面,您可以这样做:http://www.pygame.org/docs/ref/surface.html#pygame.Surface.get_rect
另外,}方法,它们都非常自解释。在
pygame.Surface
也有get_width
和{相关问题 更多 >
编程相关推荐