I am trying to make a chess game in python using pygame. In my 'piece' file I have written a dictionary for pieces with their image links in piece.img. After that I have written a function called 'fit_to_square' to try to load each image and fit them to the squares on my board. Finally, I have called this function in draw_squares in mainRewrote.py to try to blit these images on to my board. The problem is that I get this error:
Traceback (most recent call last):
File "D:\Documents\Python Files\EX_Chess\Chess Rewrote\mainRewrote.py", line 3, in <module>
import piece
File "D:\Documents\Python Files\EX_Chess\Chess Rewrote\piece.py", line 3, in <module>
import mainRewrote
File "D:\Documents\Python Files\EX_Chess\Chess Rewrote\mainRewrote.py", line 4, in <module>
from piece import fit_to_square
ImportError: cannot import name 'fit_to_square' from partially initialized module 'piece' (most likely due to a circular import) (D:\Documents\Python Files\EX_Chess\Chess Rewrote\piece.py)
#piece.py
import pygame
import engine
import mainRewrote
from mainRewrote import draw_pieces
#class containing information realted to the pieces
class piece():
def __init__(self):
self.piece_img = {
'wP': 'images/wP.png',
'wR': 'images/wR.png',
'wN': 'images/wN.png',
'wB': 'images/wB.png',
'wQ': 'images/wQ.png',
'wK': 'images/wK.png',
'bP': 'images/bP.png',
'bR': 'images/bR.png',
'bN': 'images/bN.png',
'bB': 'images/bB.png',
'bQ': 'images/bQ.png',
'bK': 'images/bK.png'
}
#test code to see if we can print the dictionary
print(piece().piece_img)
#test code to check if we can access the links
for key in piece().piece_img:
print(piece().piece_img[key]) #we can only use 'key' here to print the links
#code to load images from links and fit them to the square
def fit_to_square():
for key in piece().piece_img:
piece_imgs = pygame.transform.scale(pygame.image.load(piece().piece_img[key]), (square_size, square_size)) #fits image to square
#we need to load pictures
#mainRewrote.py
import pygame
import engine
import piece
from piece import fit_to_square
import sys
# screen config
height = 500
width = 500
dimension = 8 #dimension of an 8x8 chess board
square_size = height//dimension
FPS = 30
#pygame settings
pygame.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Tapton Chess Club')
clock = pygame.time.Clock()
screen.fill(pygame.Color("white"))
board_format = engine.game_logic()
pieces = piece.piece()
def draw_board(screen, board_format):
draw_squares(screen)
draw_pieces(screen, board_format.board)
#draw squares on screen using 2d array
def draw_squares(screen):
square_colours = [pygame.Color("#e8ebef"), pygame.Color("#7d8796")]
for row in range(dimension):
for column in range(dimension):
sq_colour = square_colours[((row+column)%2)] #used to find the colour of the square
pygame.draw.rect(screen, sq_colour, pygame.Rect(row*square_size, column*square_size, square_size, square_size))
def draw_pieces(screen, board):
for row in range(dimension):
for column in range(dimension):
blit_pos = board[row][column]
if blit_pos != '--':
screen.blit(fit_to_square().piece_imgs, pygame.Rect(column*square_size, row*square_size, square_size, square_size))
#pygame event loop to register user input
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
draw_board(screen, board_format)
clock.tick(FPS)
pygame.display.flip()