Python rect usage, function

There are two ways to use the rect object

1. Import from outside

First load the required image to make python accept the image information

self.image = pygame.image.load('图片位置')

 When python receives the image, use the get_rect() function to get the attribute rect, which is the bounding rectangle of the accepted image.

self.rect = self.image.get_rect()

2. Create by yourself

self.rect = pygame.Rect(left, top, width, heitht)

The rect object has many properties:

1. Return a coordinate number

centerx、centerysize

top、left、bottom、right

x、y

2. Return an array of (x, y) coordinates

topleft (upper left)
bottomleft (lower left)
bottomright (lower right)
midtop (upper middle)
midleft (middle left)
midbottom (middle bottom)
midright (middle right)
center (center point coordinates)

Notice:

self.rect.x, self.rect.y can only store integers, which respectively represent the coordinates x and y of the upper left vertex of the rectangle.

self.rect.size Through this method, the width and height of the rectangle can be obtained directly, and the return value is a tuple.

1.self.x = float(self.rect.x)

2.self.x += speed

3.self.rect.x = self.x

Through steps 1 and 2, you can store the decimal to get the exact position, and step 3 gets the integer part, ignoring the decimal. Approximation is achieved by moving the graph with decimals.

Guess you like

Origin blog.csdn.net/weixin_43843289/article/details/127331097