I am making a chess game in Python with PyGame using the python-chess library.
When I implement my program in PyGame the window only shows the empty board without the pieces. However, if I manually open the svg file that my code generates it shows correctly, with the pieces on the board. The problem is - when using blit
to display the same image in PyGame I get an empty board.
To be clear, the issue is not a general 'loading SVG in PyGame' question. I am successfully displaying my SVG in this example. The question is a PyGame/python-chess question:
Why is PyGame loading the image that has the pieces in it as an empty board if it's the same file?
Code:
import pygame as pg
import chess
import chess.svg
import chess.pgn
WIDTH, HEIGHT = 900, 500
WINDOW = pg.display.set_mode((WIDTH,HEIGHT))
pg.display.set_caption('Opening mastery')
FPS = 60
board = chess.BaseBoard()
def get_board_img(brd):
SVG = chess.svg.board(board=brd)
f = open("image.svg", "w")
f.write(SVG)
f.close()
image = ('image.svg')
return image
def main():
clock = pg.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
image = get_board_img(board)
WINDOW.blit(pg.image.load(image),(0,0))
pg.display.update()
pg.quit()
if __name__ == '__main__':
main()
This is the generated SVG file opened manually (it has the pieces)
This is the result (no pieces on the board)