At the end of yesterday’s article I admitted defeat. I’d developed a script to render chess positions, using a suitable font as a source of scalable bitmasks to represent the pieces. Sadly, you could clearly see the board through the pieces, which meant white pieces on black squares looked wrong. I couldn’t see an easy fix.
Happily one of my readers was more resourceful:
You can make white pieces by drawing a “black” piece in white, then overlaying that with a “white” piece in black.
This is a clever trick which I wish I’d thought of! The redrawn pictures do look better.
We need three more lines of code and comments apiece.
def chess_position_font(fen, font_file, sq_size): .... for sq, piece in filter(not_blank, zip(sqs, pieces)): if is_white_piece(piece): # Use the equivalent black piece, drawn white, # for the 'body' of the piece, so the background # square doesn't show through. filler = unichr_pieces[piece.lower()] put_piece(sq, filler, fill='white', font=font) put_piece(sq, unichr_pieces[piece], fill='black', font=font) return board
Note, in passing, that I don’t think comments can or should be entirely eliminated from source code — here’s a case where they help.
Even after this hack, the pictures aren’t pixel perfect. But I do like the grey mane you get when a white knight occupies a dark square.