Skip to content Skip to sidebar Skip to footer

Using A Function Closes Pygame Window

The following is the String class. Using the draw function from this class in my main loop immediately closes the game without any errors and as long i dont include it, the game ru

Solution 1:

The position (dest) argument to pygame.Surface.blit() is ought to be a tuple of 2 integral numbers. In your case self.pos[0] and/or self.pos[1] seems to be a floating point number.

You can get rid of the warning by rounding the floating point coordinates to integral coordinates (round):

D.blit(self.player, (round(self.pos[0]), round(self.pos[1])))

For the sake of completeness it has to be mentioned that the argument can also be a rectangle. A tuple with the 4 components (left, top, width, height).


Furthermore you have create a Surface with an integral length and you have to rotate the surface after it is (re)created:

classString:
    # [...]defdraw(self):

        # compute INTEGRAL length        
        length = math.hypot(self.dx, self.dy)
        length = max(1, int(length))

        # create surface
        self.string = pygame.Surface((1, length)).convert_alpha()
        self.string.fill((0, 255, 0))

        # roatet surface
        angle = pygame.transform.rotate(self.string, player.orbital_angle)

        # blit rotated surface
        D.blit(angle, (round(self.pos[0]), round(self.pos[1])))

Post a Comment for "Using A Function Closes Pygame Window"