inside main.py file
from Level_Class import Level
from Player_Class import Player
level = Level(level_map)
inside Level_Class.py file
class Level:
def __init__(self, map):
self.tile_list = []
for row_index, row in enumerate(map):
for col_index, col in enumerate(row):
# print(row_index, col_index, col)
if col == "X":
tile_surf = pygame.Surface((100, 100))
tile_rect = tile_surf.get_rect()
tile_surf.fill("Grey")
tile_rect.x = col_index * 100
tile_rect.y = row_index * 100
# print(tile_rect.x, tile_rect.y)
tile = (tile_surf, tile_rect)
self.tile_list.append(tile)
# the only important part is the self.tile_list
inside Player_Class.py file
from main import level
class Player():
def __init__....
def update(self):
for tile in level.tile_list:
if tile[1].colliderect(self.player_rect.x diff_x, self.player_rect.y, 100, 100):
diff_x = 0
cannot import name 'Player' from partially initialized module 'Player_Class' (most likely due to a circular import) (C:\Users\archi\PycharmProjects\5Game\Player_Class.py)
My question is how do I use an instance of the class Level in main.py and use that in another class' function which is in another file? I wonder if I can use inheritance in some way but I am not sure if that will Im kinda new to python so sorry about the weird format.
My question is how do I use an instance of the class Level in main.py and use that in another class' function which is in another file? I wonder if I can use inheritance in some way but I am not sure if that will Im kinda new to python so sorry about the weird format.
CodePudding user response:
Your Player_Class.py
file imports level from main.py
, which imports Player from Player_Class.py
. That results in circular import. You have to carefully check your imports and (if you can't remove any) place some of them in nested scope (inside class functions or somewhere)