1

Pygame と Python 2.7 では、一連の座標で表される特定の点で四角形をブリットするにはどうすればよいですか?

私はこれを使用できることを知っています:

screen.blit(img.image, img.rect.topleft)

しかし、長方形を画面上の正確な位置に配置したいのです。

4

2 に答える 2

2

ポイントの長方形の左上隅が必要な場合は、次の(34,57)ことができます

screen.blit(img.image, (34,57) )

またはこれ

img.rect.topleft = (34,57)

screen.blit(img.image, img.rect)

またはこれ

img.rect.x = 34
img.rect.y = 57

screen.blit(img.image, img.rect)

ポイントの長方形の中心が必要な場合(34,57)

img.rect.center = (34,57)

screen.blit(img.image, img.rect)

画面の中央に長方形が必要な場合:
(特に、テキスト (「一時停止」など) を画面の中央に表示する必要がある場合、またはボタンを作成するために長方形の中央にテキストを表示する必要がある場合に役立ちます)

img.rect.center = screen.get_rect().center

screen.blit(img.image, img.rect)

画面の右端に触れる四角形が必要な場合:

img.rect.right = screen.get_rect().right

screen.blit(img.image, img.rect)

画面の左下隅に長方形が必要な場合:

img.rect.bottomleft = screen.get_rect().bottomleft

screen.blit(img.image, img.rect)

さらに、 pygame.Rectを参照してください。

x,y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery

上記の要素を使用しても変化せずwidthheight. (または他の値)
を変更すると、 、およびその他の新しい値が自動的に取得されます。xleftright

ところで:img.rectご覧のとおり、引数として使用できますblit()

ところで:これを行うこともできます:(たとえば__init__):

img.rect = img.image.get_rect(center=screen.get_rect().center)

オブジェクトを画面の中央に配置する

ところで:Surfaceこれを使用して、画像を他Surfaceの正確なポイントでブリットすることもできます。テキストをいくつかの表面(ボタンなど)の中央に配置し、その表面を画面の右下隅に配置できます

于 2014-07-11T15:41:56.787 に答える