Draw things in pygame

Asked

Viewed 42 times

0

I’m reading a python book had a code I can’t understand: my doubt

Doubts:

1.What is this get_rect() ?

2.How does it work to treat things like rectangle ?

3.What is self.screen_rect = screen.get_rect() ?

4.Which is self.rect.centerx = self.screen_rect_centerx ,self.rect.bottom = self.screen_rect.bottom ?

Get me those doubts pfv.

import pygame

class nave():
    
    def __init__(self,screen):

        self.tela = tela
        
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        
        self.screen_rect = screen.get_rect()

        self.rect.centerx = self.screen_rect_centerx
        self.rect.bottom = self.screen_rect.bottom

    def blitme(self):
        self.screen.blit(self.image,self.rect)
    

1 answer

0

Hello, with get_rect() you receive the object coordinates on the screen.

by exe:

# Aqui recebes o tamanho da imagem
tamanho = self.image.get_size()
print(tamanho)

You will receive (length, height) from the image.

# Aqui recebes as coordenadas da imagem.
coordenadas = self.image.get_rect()
print(coordenadas)

You will receive (left, up, length, height) that is (x, y, width, height).

x and y is equal to a point on the screen, the first point of the rect that is in the upper left corner, x is the count of pixels from left to right and y is the count from top to bottom.

In this case you receive width and height are not absolute coordinates of another point of the rect on the screen like x and y.

But if it were right and bottom (x, y, right, bottom) it is the second point of the rect that is in the lower right corner.

Imagine the lines coming out of those two dots.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.