0

I am following a youtube tutorial making my first platformer in pygame, and I have run into the error:

Traceback (most recent call last):
  File "/Users/I can't show this part/PycharmProjects/game/main.py", line 25, in <module>
    level.run()
  File "/Users/I can't show this part/PycharmProjects/game/Level.py", line 144, in run
    self.scroll_x()
  File "/Users/I can't show this part/PycharmProjects/game/Level.py", line 85, in scroll_x
    player_x = player.rect.centerx
AttributeError: 'NoneType' object has no attribute 'rect'

I have tried researching this and I have found nothing. I have checked for spelling errors and there are none. This was working before, but I have created a tile map using the Tiled editor, and I started making my game work with the csv files from Tiled. After importing csv and writing some other lines of code to make my tile map show up and work, I got this error.

Here is my function in my Level class that I am getting the error on:

    def scroll_x(self):
        player = self.player.sprite
        player_x = player.rect.centerx
        direction_x = player.direction.x

        if player_x < WIDTH / 4 and direction_x < 0:
            self.world_shift = 8
            player.speed = 0
        elif player_x > WIDTH - (WIDTH / 4) and direction_x > 0:
            self.world_shift = -8
            player.speed = 0
        else:
            self.world_shift = 0
            player.speed = 8

Edit: I have looked at every post and solution online and I could not find any answer. I know what a NoneType error is, but I do not know how to fix it.

YREX4LIFE
  • 1
  • 1
  • There's something wrong when you initializing your self.player. Because in self.player.sprite - None. Try to use debugger to see why sprite is None – Artem Strogin Feb 23 '23 at 18:34

0 Answers0