I’m making a chess game with python 3.10.7 in pycharm and using pygame 2.1.3. This is some of my code in my main() function. I’m trying to get the position of the pieces the user clicks on and it works, but the number position of the notation is wrong. For example, if I click on the white rook on the bottom left it says the user clicked on a8 but it’s supposed to be a1. How do I fix this?
running = True
while running:
for event in p.event.get():
if event.type == p.QUIT:
running = False
if event.type == p.MOUSEBUTTONDOWN:
mousePosition = event.pos
column, row == mousePosition[0] // SQUARE_SIZE, mousePosition[1] // SQUARE_SIZE
columnName, rowName = chr(ord(‘a’) column), str(row 1)
print(f”Clicked on {columnName rowName}”)
CodePudding user response:
The y component of the board as far as pygame is concerned starts with 0 at the top and 7 at the bottom. This is the row
variable in your code.
Since you want 1 at the bottom and 8 at the top you need to subtract the row
from 8 to get your rowName
So line 9 of your code above would be
columnName, rowName = chr(ord(‘a’) column), str(8-row)