Skip to content

Instantly share code, notes, and snippets.

@nileshtrivedi
Created June 9, 2024 06:18
Show Gist options
  • Save nileshtrivedi/cb403f90ecbdb03431a89a3d85fcf2be to your computer and use it in GitHub Desktop.
Save nileshtrivedi/cb403f90ecbdb03431a89a3d85fcf2be to your computer and use it in GitHub Desktop.
Generating a 4-player chess board SVG using Python
import svgwrite
def create_4player_chess_board():
# Dimensions and colors
margin = 10
square_size = 40
board_size = 14 * square_size + 2*margin
light_color = '#ebecd0'
dark_color = '#739552'
light_extra_color = '#fdfee3'
dark_extra_color = '#a5c785'
# Create an SVG drawing
dwg = svgwrite.Drawing('4_player_chess_board.svg', size=(2* board_size, board_size))
def draw_square(x, y, color):
dwg.add(dwg.rect((x, y), (square_size, square_size), fill=color))
def draw_board(dwg, offset_x, offset_y):
# Draw the main 8x8 grid
for row in range(8):
for col in range(8):
color = light_color if (row + col) % 2 == 0 else dark_color
draw_square(offset_x+3*square_size + col*square_size, offset_y+3*square_size + row*square_size, color)
# Draw the extra grids
for row in range(3):
for col in range(8):
top_color = light_extra_color if (row + col) % 2 != 0 else dark_extra_color
bottom_color = light_extra_color if (row + col) % 2 == 0 else dark_extra_color
draw_square(offset_x+3*square_size + col*square_size, offset_y+row*square_size, top_color)
draw_square(offset_x+3*square_size + col*square_size, offset_y+(11 + row)*square_size, bottom_color)
for row in range(8):
for col in range(3):
left_color = light_extra_color if (row + col) % 2 != 0 else dark_extra_color
right_color = light_extra_color if (row + col) % 2 == 0 else dark_extra_color
draw_square(offset_x+col*square_size, offset_y+3*square_size + row*square_size, left_color)
draw_square(offset_x+(11 + col)*square_size, offset_y+3*square_size + row*square_size, right_color)
dwg.add(dwg.rect((offset_x + 3*square_size, offset_y), (8*square_size, 3*square_size), stroke='#349434', stroke_width=3, fill='none'))
dwg.add(dwg.rect((offset_x + 3*square_size, offset_y + 11*square_size), (8*square_size, 3*square_size), stroke='#349434', stroke_width=3, fill='none'))
dwg.add(dwg.rect((offset_x, offset_y + 3*square_size), (3*square_size, 8*square_size), stroke='#349434', stroke_width=3, fill='none'))
dwg.add(dwg.rect((offset_x + 11*square_size, offset_y + 3*square_size), (3*square_size, 8*square_size), stroke='#349434', stroke_width=3, fill='none'))
# dwg.add(dwg.rect((offset_x + 3*square_size, offset_y + 3*square_size), (8*square_size, 8*square_size), stroke='#349434', stroke_width=3, fill='none'))
# dwg.add(dwg.rect((offset_x, offset_y), (14*square_size, 14*square_size), stroke='#000000', stroke_width=4, fill='none'))
draw_board(dwg, margin, margin)
draw_board(dwg, board_size + margin, margin)
# Save the drawing to a file
dwg.save()
# Call the function to create the SVG file
create_4player_chess_board()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment