pygame "invalid destination position for blit"

Viewed 157

This has been asked by many others but i cant seem to get it to work for my code (Space invaders).

Tutorial python version 1.9.6 and i am using 3.8.5 so it's very possibly outdated code or is there a mistake. How should i do this in 3.8.5?

Tutorial: https://youtu.be/FfWpgLFMI7w?t=6485

I am making it so it would spawn in 6 enemies and i added the [i] to the end almost every enemyX, enemyY or enemy_change as i was told in a tutorial. So i got this error for line 56:

screen.blit(enemyImg[i], (x, y))

TypeError: invalid destination position for blit"

and the Pygame window closes.

Thanks for help

Here is the error related code followed by the tutorial:

import random
import math

pygame.init()

pygame.init()
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6

for i in range(num_of_enemies):
    enemyImg.append(pygame.image.load("enemy.png"))
    enemyX.append(random.randint(0, 735))
    enemyY.append(random.randint(50, 150))
    enemyX_change.append(4)
    enemyY_change.append(10)

def enemy(x, y, i):
    screen.blit(enemyImg[i], (x, y))

def isCollision(enemyX, enemyY, bulletX, bulletY):
    distance = math.sqrt((math.pow(enemyX - bulletX, 2)) + (math.pow(enemyY - bulletY, 2)))

    if distance < 27:
        return True

    else:
        return False   

    for i in range(num_of_enemies):
        enemyX[i] += enemyX_change[i]
        if enemyX[i] <= 0:
            enemyX_change[i] = 4
            enemyY[i] += enemyY_change[i]
        elif enemyX[i] >= 735:
            enemyX_change[i] = -4
            enemyY[i] += enemyY_change[i]


        collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
        if collision:
            bulletY = 480
            bullet_state = "ready"
            score += 1
            print(score)
            enemyX[i] = random.randint(0, 735)
            enemyY[i] = random.randint(50, 150)

        enemy(enemyX, enemyY, i)
1 Answers

enemyX and enemyY are list of coordinates. You pass the lists to the enemy function:

def isCollision(enemyX, enemyY, bulletX, bulletY):
   # [...]

   for i in range(num_of_enemies):
       # [...]

       enemy(enemyX, enemyY, i)

You need to get an element of the lists by subscription (x[i], y[i]):

def enemy(x, y, i):
    screen.blit(enemyImg[i], (x[i], y[i]))
Related