how do i fix the notation of my chess board?

Question:

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}”)
Asked By: technialogy

||

Answers:

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)

Answered By: Adam
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.