I am making a chess game and need to select a square and then move the piece on that square to a square of my choice.
I can correctly click on a square and found out what piece I've clicked on, but struggle on how to then set the pieces to the coordinates of the square selected after.
Each piece's position is based on a variable named pawn8whitepos
, or queen1blackpos
etc, where its value will be [80,240]
etc. In my code to identify the piece, I made a function that only returns either pawn8white
, or queen1black
, etc.
I'd obviously then like to put a "pos" onto the end of that, so I can then do pawn8whitepos = mousecoords
.
I've attempted this:
piecetobemoved = str(whatpiece(x)+"pos")
#whatpiece() is a function that returns the piece on a square when given the square.
piecetobemoved = mousecoords2
But this just makes the variable piecetobemoved
into say [240,60]
, and not pawn8blackpos
into [240,60]
How can I use string manipulation to get the pos variable for the piece I've identifed, so I can then set the pieces position to mousecoords
?
I know I could create 32 if statements like:
if whatpiece(x) == "pawn7black":
pawn7blackpos = mousecoords
But I'd like a more elegant solution if possible
EDIT
All relevant code can be found here, if I've posted this wrong please say as its my first time using github https://github.com/Fred330/Chess/commit/824bc8a57fcc4a173afcc28eb8b19eb664fcac0f