-1

Ok so I have this code which in def draw, in if self.part == 1: I blit the main image in the middle, but this doesn´t center the image as I want, it just makes the spawning point in the middle, and the image starts from there, so the image always appears on the bottom right side. I want it to blit it in the middle, like the whole thing:

class GameScene(Scene):
    def __init__(self, game, images, main_image, next_scene):
        super().__init__(next_scene)
        
        self.game = game
        self.main_image = main_image
        self.game_images = images

        # Fade effect set-up
        self.fade = False
        self.fade_time = 0
        self.current_alpha = 255
        self.part = 1

        self.record_text = font.render('Atiende',True, PURPLE)
        self.record_text_rect = self.record_text.get_rect(center=(SCREEN_WIDTH/2, 70))
        self.correct_image_rect = None

        # Trying to use colliderect so it doesnt overlap
        # this is the same code as before but adapted to use the gameimage class and the rects stored there
        self.rects = []

    # this is the fade stuff from before that was in draw. It really belongs here tbh
    def update(self, dt):

        if len(self.rects) < len(self.game_images):
            i = len(self.rects)
            
            x = random.randint(100,950)
            y = random.randint(100,600) 

            self.game_images[i].rect.x = x
            self.game_images[i].rect.y = y

            margin = 5
            rl = [rect.inflate(margin*2, margin*2) for rect in self.rects]
            if len(self.rects) == 0 or self.game_images[i].rect.collidelist(rl) < 0:
                self.rects.append(self.game_images[i].rect)

        if self.part == 1 and self.fade:
            self.fade_time += dt
            if self.fade_time > fade_timer:
                self.fade_time = 0
                self.main_image.set_alpha(self.current_alpha)
                self.record_text.set_alpha(self.current_alpha)

                # Speed whichin the image dissapears
                self.current_alpha -= 5
                if self.current_alpha <= 0:
                    self.fade = False
                    self.part = 2

        else:
            # we reset the main image alpha otherwise it will be invisible on the next screen (yeah, this one caught me out lol!)
            self.main_image.set_alpha(255)

    # draw is similar to before, but a bit more streamlined as the fade stuff is not in update
    def draw(self, screen):
        super().draw(screen)

        if self.part == 1:
            screen.blit(self.record_text, self.record_text_rect)
            # x = self.main_image.rect.x.center
            # y = self.main_image.rect.y.center
            screen.blit(self.main_image.image, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2)) 
        else:
            # Second half 
            text2 = font.render('¿Qué has visto?',True, PURPLE)
            screen.blit(text2, (400,5))

            # Show all similar images       
            cont = 0
            for game_image in self.game_images:
                game_image.draw(screen)
                cont += 1
            # We associate the correct rect to the correct image, to pass it later to the CORRECT Screen
            self.correct_image_rect = self.game_images[self.game_images.index(self.main_image)].rect

The thing is, that the main_image that it comes through a parameter, its a proper class:

class GameImage(object):
    def __init__(self, image):
        self.image = image
        self.rect = self.image.get_rect()

    # this class has the set_alpha so the alpha can be passed on to its image
    def set_alpha(self, alpha):
        self.image.set_alpha(alpha)

    # we pass the draw method the surface we want the image drawn on
    def draw(self, surf):
        surf.blit(self.image, self.rect)

So, as you can see, the GameImage class it´s an object that gets its rect as well, but I´m struggling to center that main image rect and blit into the screen the way I want. So yeah, I know how to do it with texts, as you can see on the self.recrod_text_rect, but I don´t know how to do it with this object and pass it to the screen.blit of the draw def.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Diego
  • 35
  • 6

1 Answers1

0

You need to set the center of the image by the center of target Surface:

class GameImage(object):
    def __init__(self, image):
        self.image = image
        self.rect = self.image.get_rect()

    # this class has the set_alpha so the alpha can be passed on to its image
    def set_alpha(self, alpha):
        self.image.set_alpha(alpha)

    # we pass the draw method the surface we want the image drawn on
    def draw(self, surf):

        self.rect.center = surf.get_rect().center # <---
 
        surf.blit(self.image, self.rect)

See also How to Center Text in Pygame

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • The thing is, that I pass all my pictures through the GameImage(object) class and if I add that, all my images spawn on the center, and I ONLY want that main image to spawn on the center, so yeah, I´m guessing I have to get the surface of the main_image that I´m getting through the parameter and center it? – Diego Jun 24 '21 at 17:41
  • Ok sorry, it´s very difficult to show or explain everything without all the code. I´m so focused on what I know that I forgot that people from the outside obviously not gonna know everything if I don´t show it. I apologize. – Diego Jun 24 '21 at 17:56