- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个迷宫游戏,我控制一个玩家,我必须逃离向我移动的外星人。我利用寻路器让外星人向我移动,但我注意到外星人总是穿过迷宫,但只显示了外星人的一小部分。由于外星人的尺寸是15x15,我想我只能看到1x15,所以不是很清楚。我了解到发生这种情况是因为我将外星人的左上角作为 x 坐标和 y 坐标传递。
我想找到从右上角、右下角和左下角传递的方法。
我尝试查看有关 Sprite 和曲面的 python 文档,但它们仅显示如何通过左上角。
我看过 YouTube 视频,但同样只看过左上角。
我看过https://www.pygame.org/docs/ref/rect.html它显示了:左上、左下、右上、右下。我认为它不相关,因为这是 pygame.Rect 但我正在使用 pygame.sprite.Sprite。
class MyAlien(pygame.sprite.Sprite):
def __init__(self, x, y, health):
super().__init__()
self.image = pygame.Surface([15, 15]) # object 15px wide and 15px high
self.image.fill(RED) # fill in red color
# top-left corner is our passed in location
self.rect = self.image.get_rect()
self.rect.y = y # set x coord
self.rect.x = x # set y coord
我期望 self.rect = self.image.get_rect() 相当于外星人的右上角、右下角和左下角。
最佳答案
如果你想设置右上角坐标,那么你必须设置.topright
而不是 .x
和 .y
。
只需将一个元组分配给 self.rect.topright
:
self.rect = self.image.get_rect()
self.rect.topright = (x, y) # set top right
注意,pygame.Rect
提供虚拟属性来获取和设置 .topleft
、.bottomleft
、.topright
、.bottomright
等。 .x
设置左侧坐标,.y
设置顶部坐标。
I don't think it is relevant since this is pygame.Rect but I am using pygame.sprite.Sprite.
当然,设置.rect
属性的坐标是相关的,因为pygame.sprite.Sprite
对象使用此属性来存储其位置。 .rect
属性在 .draw()
中读取将 .image
放在其位置。
关于python - 如何将表面的右上角传递给矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57205717/
我是一名优秀的程序员,十分优秀!