Pygame does not handle keyboard or mouse events properly

I recently reinstalled pygame on my Mac. I installed pygame 1.9.2a0. I have the same version on my windows and on the same version earlier on this same Mac. But I get strange results with this new setup. I noticed that all drawing commands work fine, but for some reason there is no pygame to click on the window. The window opens in the background, this is not normal, but it is not a big problem. But then all keystrokes are redirected to the terminal / IDE (of which the application has ever been run). I am enclosing below a very simple program with which I am testing. The program is simply looking for an exit key. Please note that this works fine on my windows machine.

TL DR: Why the keystroke is sent to the terminal and not to the pygame window's event loop when the window is selected.

I am confused by this question mainly because I did not have such problems before. I am not even sure how to debug this problem. If you need more information, I would be happy to provide them.

import pygame import sys from pygame.locals import * pygame.init() size = width, height = 100, 100 screen = pygame.display.set_mode(size) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == KEYDOWN: if event.key == K_ESCAPE: pygame.event.post(pygame.event.Event(QUIT)) screen.fill((30, 30, 30)) pygame.display.flip() 

For the curious:

  • MacOSX 10.10.1
  • Python 3.4.2 (via pyenv)
  • Pygame: 1.9.2a0

Update:

I had a theory that in one of the new commits there was some kind of problem. So I decided to pull out their repository and go back to previous deals. It looked pretty promising, especially since at the beginning of January 2015 there was a merger of the Big Bang with 8 or so requests. So I pulled the repository back to commit before this mass merge happened, and the problem was exactly the same. I decided to do this with a few other commits (before and after the merge place) and still no changes. Any suggestions?

+5
source share
2 answers

It looks like you are trying to make a shutdown key without using the X in the corner. This is what I did to fix it when I needed to.

 key = pygame.key.get_pressed() running = 1 while running: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() running = 0 elif key[pygame.K_ESCAPE]: pygame.quit() running = 0 

hope this helps. by the way, the escape key doesn't always work, so maybe set it to a different key binding. And for the mouse you need to check the position of the mouse.

 def run(): pos = pygame.mouse.get_pos() #check_events check_events(pos) 
0
source

If you are just trying to exit the game by pressing the escape key, you should just do the following as an event loop ...

 for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() quit() 

It will just close the escape key. You can add the "X" button later if you wish. There will be some problems between the Pygame version for Windows and Apple.

Hope this helps you solve your problem!

0
source

Source: https://habr.com/ru/post/1212156/


All Articles