TypeError: Invalid destination position for blit, Pygame

Viewed 94

So my code here has 2 goblins()'s bouncing left and right on the game window, and I have to try to get player() to the princess() without touching a goblin(). However, when I create instances of goblin(), they go to the right and bounce off the window given that goblinX_change is set to 0.3, but when they make it to the left side of the window the game crashes and I get TypeError: Invalif destination position for blit.

import pygame

# Initialize the pygame
pygame.init()

# Setting up the screen and background
screen = pygame.display.set_mode((800,600))

# Title and Icon of window
pygame.display.set_caption("Get Princess")

icon = pygame.image.load('knight.png')
pygame.display.set_icon(icon)

#Player Image
playerImg = pygame.image.load('knight.png')
playerImg = pygame.transform.scale(playerImg, (50,50))
playerX = 360
playerY = 520
playerX_change = 0
playerY_change = 0

#Princess Image
princessImg = pygame.image.load('princess.png')
princessImg = pygame.transform.scale(princessImg, (50,50))
princessX = 360
princessY = 20

def player(x,y):
    screen.blit(playerImg, (x, y))

def princess(x,y):
    screen.blit(princessImg, (x, y))


class goblin():
    def __init__(self, goblinX, goblinY, goblinX_change):
        self.goblinX = goblinX
        self.goblinY = goblinY
        self.goblinX_change = goblinX_change
        self.goblinImg = pygame.image.load('goblin.png')
        self.goblinImg = pygame.transform.scale(self.goblinImg,(50,50))
    
    def draw(self):
        screen.blit(self.goblinImg, (self.goblinX, self.goblinY))
        
    
    def movement(self):
        self.goblinX += self.goblinX_change
        if self.goblinX <= 0:
            self.goblinX_change += self.goblinX_change
        elif self.goblinX >= 750:
            self.goblinX_change = -self.goblinX_change


g = goblin(360,250, 0.3)
g1 = goblin(360, 280, 0.3)
running = True
while running:

    screen.fill((50,0,0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
                    

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                playerY_change = -0.4
            if event.key == pygame.K_DOWN:
                playerY_change = 0.4
            if event.key == pygame.K_LEFT:
                playerX_change = -0.4
            if event.key == pygame.K_RIGHT:
                playerX_change = 0.4
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                playerY_change = 0

    playerX += playerX_change
    playerY += playerY_change
    
    if playerX <= 0:
        playerX = 0
    elif playerX >= 750:
        playerX = 750
    if playerY <= 0:
        playerY = 0
    elif playerY >= 550:
        playerY = 550


    player(playerX,playerY)
    princess(princessX, princessY)
    
    
    g.movement()
    g.draw()
    g1.movement()
    g1.draw()


    pygame.display.update()
2 Answers

When the goblinX reaches 0, you need to reverse the direction:

def movement(self):
    self.goblinX += self.goblinX_change
    if self.goblinX <= 0:
        self.goblinX_change = -self.goblinX_change   #  update this line
        self.goblinX = 0
    elif self.goblinX >= 750:
        self.goblinX_change = -self.goblinX_change

When the goblin is at the left border, the movement must become positive (abs(self.goblinX_change)) and when the goblin is at the right border, the movement must become negative (-abs(self.goblinX_change)):

class goblin():
    # [...]

    def movement(self):
        self.goblinX += self.goblinX_change
        if self.goblinX <= 0:
            self.goblinX_change = abs(self.goblinX_change)
        elif self.goblinX >= 750:
            self.goblinX_change = -abs(self.goblinX_change)

Alternatively you can change the direction, if the goblin is at the left border and moves to the left, or is at the right border and moves to the right:

class goblin():
    # [...]

    def movement(self):
        self.goblinX += self.goblinX_change
        if (self.goblinX <= 0 and self.goblinX_change < 0) or \
           (self.goblinX >= 750 and self.goblinX_change > 0):
            self.goblinX_change *= -1
Related