TypeError: invalid destination position for blit (I looked at other solutions to this but they don't work for some reason)

So I've been trying to make a game and the TypeError: invalid destination position for blit error popped up, I have no idea how to solve it.

Here's the problematic code:

import pygame, sys
pygame.init()
    
# screen settings
screen_width = 1000
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))

class Player(pygame.sprite.Sprite):
      """
      player character
      """
      def __init__(self):
          pygame.sprite.Sprite.__init__(self)
            
          self.x = screen_width / 2
          self.y = screen_height / 2
          self.image = pygame.image.load('img/player.png').convert_alpha()
          self.rect = self.image.get_rect
    
    player = Player()
    player_list = pygame.sprite.Group()
    player_list.add(player)
    
    # game loop
    while True:
    
        for event in pygame.event.get():
    
            if event.type == pygame.QUIT:
                pygame.quit()
                
                sys.exit()
                
        player_list.draw(screen) # traceback thinks this is where the error is
    
        pygame.display.flip()
        pygame.display.update()

The code example in here isn't actually the game code, but just the part related to my problem.

I'd appreciate some help.

Upvotes: 0

Views: 57

Answers (1)

Eric He
Eric He

Reputation: 11

The problem with your code was not the player_list.draw, rather is was the pygame.display.update() messing up your code.

the other problem was the self.rect = self.image.get_rect, it should be self.rect= self.image.get_rect()

and just for future work, you should add an update function to your player class with player_list.update inside your while loop!

#fixed code

import pygame, sys
pygame.init()
    
# screen settings
screen_width = 1000
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))

class Player(pygame.sprite.Sprite):
    """player"""
    def __init__(self, window):
        pygame.sprite.Sprite.__init__(self)
        self.window = window
        self.x = screen_width / 2
        self.y = screen_height / 2
        self.image = pygame.image.load('sprites/NewGumbo.png').convert_alpha()
        self.rect = self.image.get_rect()

    
player = Player(screen)
player_list = pygame.sprite.Group()
player_list.add(player)
    
# game loop
while True:
    
    for event in pygame.event.get():
    
        if event.type == pygame.QUIT:
            pygame.quit()
                
            sys.exit()
                
    player_list.draw(screen) # traceback thinks this is where the error is
    player_list.update()
    
    pygame.display.flip()
 

Upvotes: 1

Related Questions