Intro
I am currently trying to create something with the pygame library and python classes.
Although it is working as expected in its current state, there's an error message / hint from PyCharm which bugs me, since I don't know how to properly handle it, or how to figure out if it's a bug in PyCharm or pygame.
Problem
I created a Tile class and a TileMap class. The latter one has an attribute of a pygame.sprite.Group() - with its elements being instances of the Tile class.
The Tile class inherits from pygames sprite.Sprite() class. Adding a method in the Tile class and calling this method on a Tile instance from within the TileMap class yields the following message / error in PyCharm: [(A) in code below]
'Unresolved attribute reference 'collides_with' for class 'Sprite'
If the Tiles are stored in TileMap as an attribute with type list, no such message is shown. [(B) in code below]
Question(s)
- Is this an issue with PyCharm, confusing the class with its parent? (It does not state that the attribute is not found in class 'Tile' where it is defined, but in the parent class 'Sprite')
- If so, could i safely ignore this?
- Could this alternatively be an issue with pygames' 'Sprite' class?
- Or is this something python specific, meaning i do not understand its classes or am blind to something?
Conditions
- python 3.9.1
- pygame 2.0.2
- PyCharm 2021.2.3
- OS: macOS Catalina 10.15.7 (Same on Ubuntu 20.04)
This is the shortest i could condense the code to:
import pygame
class Tile(pygame.sprite.Sprite):
def __init__(self, size):
super().__init__()
self.size = size
@staticmethod
def collides_with():
return True
class TileMap:
def __init__(self, tile_size):
self.tile_size = tile_size
self.terrain_sprites = self.create_tile_group() <-- (A)
self.tiles = [] <-- (B)
def create_tile_group(self):
sprite_group = pygame.sprite.Group()
width, height = (10, 10)
columns = int((width / self.tile_size))
rows = int((height / self.tile_size))
for col in range(columns):
for row in range(rows):
sprite = Tile(self.tile_size)
sprite_group.add(sprite)
self.tiles.append(sprite)
return sprite_group
def update(self):
for tile in self.terrain_sprites:
tile.collides_with() <-- (A) This is where i get the error
print(type(tile))
for i in self.tiles:
i.collides_with() <-- (B) Strangely this is ok
print(type(i))
t = TileMap(1)
t.update()