TypeError unsupported operand type s for - list and int

0 votes
import pygame

import random

import math

# Setting

pygame.init()

screen = pygame.display.set_mode((800, 600))

pygame.display.set_caption("William's First Game")

icon = pygame.image.load('money.png')

pygame.display.set_icon(icon)

background = pygame.image.load('colorful.jpg')


# Player

playerImg = pygame.image.load('space-invaders.png')

playerX = 370

playerY = 480

playerX_change = 0


# Enemy

enemyImg = []

enemyX = []

enemyY = []

enemyX_change = []

enemyY_change = []

num_of_enemies = 6


for i in range(num_of_enemies):

    enemyImg.append(pygame.image.load('monster.png'))

    enemyX.append(random.randint(1, 734))

    enemyY.append(random.randint(50, 150))

    enemyX_change.append(1)

    enemyY_change.append(40)


# Bullet

bulletImg = pygame.image.load('bullet.png')

bulletX = 0

bulletY = 480

bulletX_change = 0

bulletY_change = 6

bullet_state = "ready"


score = 0


def fire_bullet(x, y):

    global bullet_state

    bullet_state = "fire"

    screen.blit(bulletImg, (x, y - 20))


def player(x, y):

    screen.blit(playerImg, (x, y))


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


# Game Loop

running = True

while running:

    # RGB

    screen.fill((128, 128, 128))

    screen.blit(background, (0, 0))

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            running = False


        # if keystroke is pressed check left or right

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_LEFT:

                playerX_change = -4

            if event.key == pygame.K_RIGHT:

                playerX_change = 4

            if event.key == pygame.K_SPACE:

                if bullet_state is "ready":

                    bulletX = playerX

                    fire_bullet(bulletX, bulletY)


        if event.type == pygame.KEYUP:

            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:

                playerX_change = 0


    playerX += playerX_change


    if playerX <= 0:

        playerX = 0

    elif playerX >= 736:

        playerX = 736


    for i in range(num_of_enemies):

        enemyX[i] += enemyX_change[i]

        if enemyX[i] <= 0:

            enemyX_change[i] = 1

            enemyY[i] += enemyY_change[i]

        elif enemyX[i] >= 736:

            enemyX_change[i] = -1

            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(1, 734)

            enemyY[i] = random.randint(50, 150)


        enemy(enemyX[i], enemyY[i], i)


    if bulletY <= -80:

        bulletY = 480

        bullet_state = "ready"

    if bullet_state is "fire":

        fire_bullet(bulletX, bulletY)

        bulletY -= bulletY_change


    collision = isCollision(enemyX, enemyY, bulletX, bulletY)

    if collision:

        bulletY = 480

        bullet_state = "ready"

        score += 1

        print(score)

        enemyX = random.randint(1, 734)

        enemyY = random.randint(50, 150)


    player(playerX, playerY)

    pygame.display.update()
Mar 10, 2020 in Python by William
• 120 points
3,522 views

1 answer to this question.

0 votes
Hey, @William

For me the code is working fine, when are you getting this error? Can you elaborate a bit?
answered Mar 11, 2020 by Roshni
• 10,480 points

I have the same problem 

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

ERROR

line 61, in isCollision
    distance = math.sqrt((math.pow(enemyX - bulletX, 2)) + (math.pow(enemyY - bulletY, 2)))
TypeError: unsupported operand type(s) for -: 'list' and 'int'

Hi, @There,

Try enemyX[-1], enemyY[-1] to get the enemy's latest position in your collision function

Related Questions In Python

+1 vote
0 answers

TypeError: unsupported operand type(s) for /: 'list' and 'list'

This is my part of code, why ...READ MORE

May 17, 2020 in Python by anonymous
• 220 points

edited May 18, 2020 by Gitika 2,941 views
0 votes
0 answers

How can I solve it? TypeError: unsupported operand type(s) for /: 'float' and 'list'

I am trying to calculate numerical integration ...READ MORE

Nov 17, 2020 in Python by asimsek
• 120 points
13,842 views
0 votes
3 answers

TypeError: unsupported operand type(s) for -: 'str' and 'str'

& is "bitwise and" operand in Python, you ...READ MORE

answered Dec 11, 2020 in Python by Rajiv
• 8,870 points
80,688 views
0 votes
0 answers

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

Hi guys i have below code . ...READ MORE

Oct 30, 2019 in Python by sumanth
• 190 points
12,023 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,467 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,127 views
+2 votes
10 answers

How to fix this? ValueError: invalid literal for int() with base 10 error in Python

The following are totally acceptable in python: passing ...READ MORE

answered Nov 16, 2018 in Python by Nymeria
• 3,560 points
409,379 views
0 votes
1 answer

how to solve this IndexError: list index out of range in python and streamlit?

Hi,  @Erick, To solve the “index error: list ...READ MORE

answered Sep 25, 2020 in Python by Roshni
• 10,480 points
28,482 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP