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 in Python by William
• 120 points
648 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 by Roshni
• 4,030 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 in Python by anonymous
• 220 points

edited May 18 by Gitika 322 views
0 votes
1 answer

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

Hi. This error is caused because you ...READ MORE

answered May 12, 2019 in Python by Anjali
32,119 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
5,765 views
0 votes
0 answers

TypeError: unsupported operand type(s) for *: 'CommentedMap' and 'CommentedMap' Hatam bu nasıl çözebilirim ?

from ruamel import yaml from scipy import spatial import ...READ MORE

2 days ago in Python by anonymous
• 120 points

edited 2 days ago by Gitika 16 views
+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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,371 views
0 votes
0 answers
+4 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,800 points
1,014 views
+1 vote
8 answers

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

Just for the record: >>> int('55063.000000') Traceback (most recent ...READ MORE

answered Aug 17 in Python by pakainfo
• 200 points
151,175 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 6 days ago in Python by Roshni
• 4,030 points
75 views