Record: 1.pygame exit -- press Q key to exit

By the books, just write as follows:

 elif event.key == pygame.K_q:
     sys.exit()

After pressing the Q key, the window created by pygame is just that the things in it don't move, instead of closing the window.

When changing q to Q, the window closes, but an error occurs:

        AttributeError: module 'pygame' has no attribute 'K_Q'

Current solution: Add statement: pygame.quit()

        elif event.key == pygame.K_q:
            sys.exit()
            pygame.quit()

Note: If sys.exit() is commented out at this time, an error will be reported:

pygame.error: display Surface quit

Currently there is a problem:

1. When exiting the window, you must change the keyboard to uppercase mode before you can exit the window, and the responsibility will still make the game just become immobile

2. If you are in lowercase mode, press q, even if you change to uppercase mode at this time, you still cannot close the window

3. I feel that the lowercase q is pressed to pause? Although I don’t know why, after pressing the window to minimize, reopen it, the window can be moved again, and you can capitalize Q to exit

Guess you like

Origin blog.csdn.net/weixin_45314061/article/details/129940052