如何用get_rect()或Rect()设置rect的坐标?

时间:2017-10-28 20:02:48

标签: python pygame rect

这是一个有效的例子: 它会在屏幕底部加载一个红色图形。到目前为止这么好,但是 我想在特定坐标(如700(x轴)和100(y轴))上加载图形。

import pygame


class Figure:

    def __init__(self, screen2):
        """Initialize the figure and set its starting position."""
        self.screen2 = screen2

        # Load the Red figure image and get its rect.
        self.redplayer = pygame.image.load('Images/redplayer.bmp')
        self.rectredplayer = self.redplayer.get_rect()
        self.screen2_rect = screen2.get_rect()

        # Start each new redplayer at the bottom center of the screen.
        self.rectredplayer.centerx = self.screen2_rect.centerx
        self.rectredplayer.bottom = self.screen2_rect.bottom

    def blitme(self):
        """Draw the red player figure at its current location."""
        self.screen2.blit(self.redplayer, self.rectredplayer)

1 个答案:

答案 0 :(得分:0)

以这种方式设置topleft coords:

self.rectredplayer.x = 700
self.rectredplayer.y = 100
# Or just:
self.rectredplayer.topleft = (700, 100)

将矩形的中心设置为所需的坐标:

self.rectredplayer.center = (700, 100)

或使用其他rect attributes

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

您也可以将指引直接传递给Surface.get_rect

self.rectredplayer = self.redplayer.get_rect(topleft=(700, 100))