Pygame如何获取图像的左、上、宽和高

2024-04-20 15:53:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我的游戏中实现碰撞,但我不知道如何获取图像的rect属性。玩家可以选择图片在哪里生成(他把它们放在哪里),所以我不能设置一个确切的数字等等


Tags: rect图像游戏属性玩家图片数字
1条回答
网友
1楼 · 发布于 2024-04-20 15:53:44

pygame.Surface对象有一个方法get_rect,该方法返回一个pygame.Rect的实例,该实例包含{}。注意,Rect的position(xy属性)从(0,0)开始,但是您可以更改该值,并在blit到另一个曲面时引用它。在

因此,如果您有一个名为surfpygame.Surface,和一个名为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和{}方法,它们都非常自解释。在

相关问题 更多 >