1

I am following a tutorial about pygame and I am trying to get an image of a spaceship to appear on the pygame window using "screen.blit" but it doesn't work and I can not understand why.

import pygame

# initialize the pygame
pygame.init()

# create the screen
screen = pygame.display.set_mode((800,  600))

# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load("ufo-flying.png.png")
pygame.display.set_icon(icon)

# Player
playerImg = pygame.image.load('player.png')
playerX = 370
playerY = 480


def player():
    screen.blit(playerImg, playerX, playerY)


# Game Loop
running = True
while running:
    # RGB
    screen.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    player()
    pygame.display.update()

Here is the code, please help me to fing the error, the spaceship doesn't appear and this is on my terminal:

Traceback (most recent call last):
  File "C:\Users\hp\PycharmProjects\pythonProject5\main.py", line 33, in <module>
    player()
  File "C:\Users\hp\PycharmProjects\pythonProject5\main.py", line 21, in player
    screen.blit(playerImg, playerX, playerY)
TypeError: invalid destination position for blit

Process finished with exit code 1

Yomypoto
  • 11
  • 2

1 Answers1

1

See pygame.Surface.blit. The position is specified by one argument, which is a tuple with two components, but not 2 separate arguments:

screen.blit(playerImg, playerX, playerY)

screen.blit(playerImg, (playerX, playerY))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174