mercredi 23 juin 2021

Issue while saving rects, it crash my game

I have this code that saves the position and the rect from an object (usally small, like a drawing) and then spawns them (blits) in my screen. I encountered that if I put too many objects or too big, I´m guessing the rects collide and then the game crashes, but also, sometimes, even if I have not many objects, it can crash because this occurance.

How could I solve this problem? I´m guessing adding an if sentence so it checks that is not as near as to crash the game or something like that, where I save the rects of the images is in the for i in self.game_images: :

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.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 = []
        for i in self.game_images:
            position_set = False 
            while not position_set:
                x = random.randint(100,950)
                y = random.randint(100,600) 

                i.rect.x = x
                i.rect.y = y

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

        # this makes a number and object pair, and allows us to set the correct rects for the correct gameimage classes
        for i, rect in enumerate(self.rects):
            self.game_images[i].rect = rect

    # this is the fade stuff from before that was in draw. It really belongs here tbh
    def update(self, dt):
        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, (550, 20))
            screen.blit(self.main_image.image, (580, 280)) 
        else:
            # Second half 
            text2 = font.render('¿Qué has visto?',True, PURPLE)
            screen.blit(text2, (400,5))

            # Show all similar images      
            for game_image in self.game_images:
                game_image.draw(screen)

            # 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

    # again we pass the event to the game object the same as with the other classes
    def get_event(self, event):
        if self.part == 2:
            if self.game.level == 13:
                self.game.game_over = True
            if self.correct_image_rect.collidepoint(event.pos):
                return 'CORRECT'
            for rect in self.rects:
                if not self.correct_image_rect.collidepoint(event.pos) and rect.collidepoint(event.pos):
                    return 'INCORRECT'    




Aucun commentaire:

Enregistrer un commentaire