Ошибка при импортировании класса Python
Делаю игру на Pygame. Есть код игрока:
import pygame
from settings import *
class Player:
def __init__(self):
self.x = HALF_WIDTH
self.y = HALF_HEIGHT
self.player_right = pygame.image.load('knightRight.png')
self.player_left = pygame.image.load('knightLeft.png')
self.current_picture = self.player_left
@property
def pos(self):
return self.x, self.y
def movement(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.y -= player_speed
if keys[pygame.K_s]:
self.y += player_speed
if keys[pygame.K_a]:
self.current_picture = self.player_left
self.x -= player_speed
if keys[pygame.K_d]:
self.current_picture = self.player_right
self.x += player_speed
Есть код оружия:
import pygame
from settings import *
from main import player
class BadPistol:
def __init__(self):
self.x = HALF_WIDTH
self.y = HALF_HEIGHT
self.bad_pistol_pic = pygame.image.load('bad_pistol.png')
self.colorkey = self.bad_pistol_pic.get_at((0, 0))
self.bad_pistol_pic.set_colorkey(self.colorkey)
@property
def pos(self):
return self.x, self.y
def move(self):
self.x = player.x
self.y = player.y
main.py:
import pygame
from settings import *
from player import Player
from bad_pistol import BadPistol
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
player = Player()
bad_pistol = BadPistol()
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
player.movement()
bad_pistol.move()
screen.fill(WHITE)
screen.blit(player.current_picture, (player.x, player.y))
screen.blit(bad_pistol.bad_pistol_pic, (bad_pistol.x, bad_pistol.y))
pygame.display.flip()
clock.tick(FPS)
При запуске ошибка:
"C:\Users\andre\PycharmProjects\Soul Knight\venv\Scripts\python.exe" "C:/Users/andre/PycharmProjects/Soul Knight/main.py"
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:/Users/andre/PycharmProjects/Soul Knight/main.py", line 4, in <module>
from bad_pistol import BadPistol
File "C:\Users\andre\PycharmProjects\Soul Knight\bad_pistol.py", line 3, in <module>
from main import player_pos
File "C:\Users\andre\PycharmProjects\Soul Knight\main.py", line 4, in <module>
from bad_pistol import BadPistol
ImportError: cannot import name 'BadPistol' from partially initialized module 'bad_pistol' (most likely due to a circular import) (C:\Users\andre\PycharmProjects\Soul Knight\bad_pistol.py)
Process finished with exit code 1
Что делать?