salem
salem

Reputation: 23

in pygame i have that problem: unsupported operand type(s) for +=: 'int' and 'list'

class Nave(pygame.sprite.Sprite):
   def __init__(self):
      pygame.sprite.Sprite.__init__(self)
      self.image = pygame.image.load('images/nave.png')
      self.rect = self.image.get_rect()
      self.rect.centerx = width / 2
      self.rect.centery = height / 2 
      self.speed = [0,0]

   def move(self,time,keys):
      if self.rect.right >= 0 :
         if keys[K_RIGHT]:
            self.rect.centerx += self.speed * time 
      if self.rect.left <= width:
         if keys[K_LEFT]:
            self.rect.centerx -= self.speed * time

in the function move, in line 3 "self.rect.centerx += self.speed * time" console say me :

TypeError: unsupported operand type(s) for +=: 'int' and 'list'

Upvotes: 1

Views: 430

Answers (2)

ikibir
ikibir

Reputation: 466

self.speed is a list you cant add list and int together.

self.rect.centerx += self.speed[0] * time

Use it like that.

Upvotes: 1

Rabbid76
Rabbid76

Reputation: 211135

self.speed is a list:

self.speed = [0,0]

You've to address an element of the list (e.g. self.speed[0] rather than self.speed):

self.rect.centerx += self.speed[0] * time 

Upvotes: 1

Related Questions