Nejk
Nejk

Reputation: 25

Pygame - TypeError: invalid rect assigment

So I'm trying to make a platformer game. Since I'm new to pygame library, I'm following a video tutorial(https://www.youtube.com/playlist?list=PL8ui5HK3oSiGXM2Pc2DahNu1xXBf7WQh-). And I ran to a problem. I checked documentation, tutorials, I even found somebody here that asked the same question. But nothing worked. So what did I do wrong and how can I repair it?

Player class:

import pygame

class Player(pygame.sprite.Sprite):
    def __init__(self,pos):
        super().__init__()
        self.image = pygame.Surface((32,32))
        self.image.fill((0,0,255))
        self.rect = self.image.get_rect(topleft = pos)

This is level code in which I'm drawing the player now. I will change it.

import pygame
from tiles import Tile
from settings import tileSize
from player import Player

class Level:
    #setup
    def __init__(self, levelData, surface):
        self.displaySurface = surface
        self.setupLevel(levelData)
        self.worldShift = 0
    
    def setupLevel(self,layout):
        self.tiles = pygame.sprite.Group()
        self.player = pygame.sprite.GroupSingle()

        for rowIndex, row in enumerate(layout):
            for colIndex, col in enumerate(row):
                x = colIndex * tileSize
                y = row * tileSize
                if col == 'X':
                    tile = Tile((x,y),tileSize)
                    self.tiles.add(tile)
                if col == 'P':
                    playerSprite = Player((x,y))
                    self.player.add(playerSprite)
    def run(self):
        #level tiles
        self.tiles.update(self.worldShift)
        self.tiles.draw(self.displaySurface)
        #player
        self.player.draw(self.displaySurface)

This is error message:

Hello from the pygame community. https://www.pygame.org/contribute.htmlTraceback (most recent call last):
  File "d:\AdkoaMisko-game\main\window.py", line 10, in <module>     
    level = Level(levelMap,screen)
  File "d:\AdkoaMisko-game\main\level.py", line 10, in __init__      
    self.setupLevel(levelData)
  File "d:\AdkoaMisko-game\main\level.py", line 25, in setupLevel    
    playerSprite = Player((x,y))
  File "d:\AdkoaMisko-game\main\player.py", line 8, in __init__      
    self.rect = self.image.get_rect(topleft = pos)
TypeError: invalid rect assignment

Upvotes: 1

Views: 206

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

row is a list of columns. The index of the row is rowIndex:

y = row * tileSize

y = rowIndex * tileSize

Upvotes: 1

Related Questions