Soni
Soni

Reputation: 77

Getting 'TypeError: invalid destination position for blit' in python

I'm new to python, and can't fix this. When trying to run my game, I get this error:

Traceback (most recent call last):
  File "C:\Users\soni801\Documents\GitHub\volleyball\Player.py", line 15, in draw
    window.blit(pygame.image.load("player.png"), 100, 100)
TypeError: invalid destination position for blit

I know other people that have done very similar code without getting this error, and I have no idea why i am getting this.

How do I fix this? I've tried changing literally every single variable, with no luck. Any help appreciated. Here is my code:

In Game.py:

import pygame

from Player import Player

running = True
clock = pygame.time.Clock()

# Images
background = pygame.image.load("bg.png")

# Global variables
WIDTH = 800
HEIGHT = 600

# Player variables
player_size = 70
player_pos = [(WIDTH / 4) - (player_size / 2), HEIGHT - player_size]
player_speed = 5
jump_height = 22

jumping = False
jump_time = -jump_height

window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Volleyball")

player = Player(player_pos, player_size, player_speed, jump_height, pygame.image.load("player.png"))


def tick():
    player.movement()


def render():
    window.blit(background, (0, 0))

    player.draw(window)

    pygame.display.update()


if __name__ == '__main__':
    while running:
        clock.tick(60)

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

        tick()
        render()

pygame.quit()

In Player.py:

import pygame


class Player(object):
    def __init__(self, pos, size, speed, jump_height, image):
        self.jump_height = jump_height
        self.speed = speed
        self.pos = pos
        self.image = image

        self.jumping = False
        self.jump_time = -jump_height

    def draw(self, window):
        window.blit(self.image, self.pos[0], self.pos[1])

    def movement(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_a] and self.pos[0] > 0:
            self.pos[0] -= self.speed
        if keys[pygame.K_d] and self.pos[0] < 800 - self.pos:
            self.pos[0] += self.speed
        if not self.jumping:
            if keys[pygame.K_SPACE]:
                self.jumping = True
        else:
            if self.jump_time <= self.jump_height:
                self.pos[1] += self.jump_time
                self.jump_time += 1
            else:
                self.jumping = False
                self.jump_time = -self.jump_height

Upvotes: 2

Views: 171

Answers (1)

Whud
Whud

Reputation: 714

I haven't tested yet but I think you just need to tuple the pos in your draw function:

not this:

    def draw(self, window):
        window.blit(self.image, self.pos[0], self.pos[1])

this:

    def draw(self, window):
        window.blit(self.image, (self.pos[0], self.pos[1]))

Upvotes: 3

Related Questions