python - (Pygame/Python) 如何根据对象矩形的中间位置将对象放置在屏幕上?

标签 python pygame

我想我可以简单地这样做:

self.rect = self.image.get_rect(center=((self.pos_x, self.pos_y)))

但这似乎具有相同的效果:

self.rect = self.image.get_rect(bottomleft=((self.pos_x, self.pos_y)))

我错过了什么?两个对象都进入,就好像 (pos_x, pos_y) 元组描述其左下角一样。

最佳答案

不要将参数传递给方法self.image.get_rect(),它返回一个pygame.Rect对象,该对象具有许多属性:top,下、左、右、 topleft、topright、bottomleft、bottomright、size、width、height、center、centerx、centery、midleft、midright、midtop、midbottom #(this is from pygame docs) 你可以用它来将你的 Sprite 放置在任何位置在屏幕中,例如在这里,您将 Sprite 放置在屏幕中间:

def __init__(self, screen): 
    #Load image
    #...
    self.screen = screen

    self.rect =self.image.get_rect()
    self.screen_rect = self.screen.get_rect()

    self.rect.centerx = self.screen_rect.centerx
    self.rect.centery = self.screen_rect.centery

我希望这有帮助。

关于python - (Pygame/Python) 如何根据对象矩形的中间位置将对象放置在屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36378185/

相关文章:

python - 改变 numpy 数组中一致值的比例

python - 如果我想在 Apache 上使用 pylons 应用程序,我应该使用 mod_wsgi 还是代理来粘贴?

python - 当我点击移动时,如何为 pygame 角色行走设置动画

python - 没有 while True 循环的 Pygame 播放列表

python - 从Pygame/PyOpenGL到iOS的最简单路径。

python - Pygame 窗口在 Mac 上无法关闭的问题

python - 在Python中创建一系列弧度值

python - pip 是否将软件包安装到我的 conda 环境中?

python - 将回归树输出转换为 Pandas 表

python - 如何在pygame中反转图像的颜色?