gpt4 book ai didi

python - 如何将表面的右上角传递给矩形?

转载 作者:行者123 更新时间:2023-12-01 00:43:02 26 4
gpt4 key购买 nike

我正在制作一个迷宫游戏,我控制一个玩家,我必须逃离向我移动的外星人。我利用寻路器让外星人向我移动,但我注意到外星人总是穿过迷宫,但只显示了外星人的一小部分。由于外星人的尺寸是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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com