โŒ

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Pygame slows down after Collision Handling PacMan [duplicate]

Hey i am trying to write a pacman game, PacMan runs very smooth, but if i add the collison function which checks if theres a collision (it iterates over every block created in the map) and then tells me if there is a collison, if so we do not take this direction. If i remove the function pacman runs smooth and fast so why does he slows down heavily if collision is checked? Thank you in advanced

    def game_loop(self):
        self.clock.tick(10)
        change_x = 0
        change_y = 0
        self.x, self.y = START_POS
        while self.running:
            change_x = 0
            change_y = 0
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
                elif event.type == pygame.KEYDOWN:

                    if event.key == pygame.K_LEFT:
                        self.direction = "LEFT"

                    elif event.key == pygame.K_RIGHT:
                        self.direction = "RIGHT"

                    elif event.key == pygame.K_DOWN:
                        self.direction = "DOWN"

                    elif event.key == pygame.K_UP:
                        self.direction = "UP"

            if self.direction == 'LEFT':
                # self.x -= 3
                change_x = -3
            elif self.direction == 'RIGHT':
                # self.x += 3
                change_x = 3
            elif self.direction == 'DOWN':
                # self.y += 3
                change_y = 3
            elif self.direction == 'UP':
                # self.y -= 3
                change_y = -3
             
             # Due to adding this function pacman slows down, but collision is detected correctly
            if self.check_collision_with_rectangles(self.x + change_x, self.y + change_y, 12):
                print("Kollision")
            else:
                self.x += change_x
                self.y += change_y
                self.drawPacMan(self.x, self.y)





    def check_collision_with_rectangles(self, circle_x, circle_y, circle_radius):
        for rect in listo:
            if rect.colliderect(pygame.Rect(circle_x - circle_radius, circle_y - circle_radius, circle_radius * 2,
                                            circle_radius * 2)):
                # Kollision zwischen Kreis und Rechteck
                return True
        return False
โŒ
โŒ