在 pygame 中,对于 event.type == ACTIVEEVENT,有关不同 event.state 和 event.gain 参数含义的文档在哪里?

Gle*_*osh 2 python pygame

我正在用 pygame 编写一个游戏,遇到了需要发现游戏何时获得和失去焦点的情况。这由 event.type 指示ACTIVEEVENT,它有两个参数event.stateevent.gain。然而,在浏览完所有内容之后,我只能找到一些关于这些含义的信息。ACTIVEEVENT至少state可以有 6 个不同的值,并且每个状态可以有多个gain

到处都有片段,但我实际上无法在任何地方找到有关它的实际文档。https://www.pygame.org/docs/ref/ACTIVEEVENT中有一些参考资料,但没有实际信息。

我并不是在寻找人们告诉我他们所发现的关于它如何工作的点点滴滴,我已经在点点滴滴中找到了它,并基于此和一些实验,我弄清楚了我需要什么。(ACTIVEEVENT状态2,增益0似乎意味着窗口失去焦点,ACTIVEEVENT状态6,增益1似乎意味着窗口重新获得焦点)。

我想知道哪里有关于 ACTIVEEVENT 事件的实际文档和/或者如果我不知何故错过了它,我可以在 pygame 文档中找到它吗?

slo*_*oth 5

由于 pygame 是一个精简的 SQL 包装器,因此查看libsdl本身通常会有所帮助:

/** @name The available application states */
/*@{*/
#define SDL_APPMOUSEFOCUS   0x01        /**< The app has mouse coverage */
#define SDL_APPINPUTFOCUS   0x02        /**< The app has input focus */
#define SDL_APPACTIVE       0x04        /**< The application is active */
/*@}*/

/* Function prototypes */
/** 
 * This function returns the current state of the application, which is a
 * bitwise combination of SDL_APPMOUSEFOCUS, SDL_APPINPUTFOCUS, and
 * SDL_APPACTIVE.  If SDL_APPACTIVE is set, then the user is able to
 * see your application, otherwise it has been iconified or disabled.
 */
extern DECLSPEC Uint8 SDLCALL SDL_GetAppState(void);
Run Code Online (Sandbox Code Playgroud)

这是一个小 pygame 脚本,用于查看这些值的实际效果:

import pygame

pygame.init()

screen = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()

APPMOUSEFOCUS = 1
APPINPUTFOCUS = 2
APPACTIVE     = 4

def main():
  while True:
    for e in pygame.event.get():
      if e.type == pygame.QUIT: 
          return
      if e.type == pygame.ACTIVEEVENT:
          if e.state & APPMOUSEFOCUS == APPMOUSEFOCUS:
              print ('mouse focus ' + ('gained' if e.gain else 'lost'))
          if e.state & APPINPUTFOCUS == APPINPUTFOCUS:
              print ('input focus ' + ('gained' if e.gain else 'lost'))
          if e.state & APPACTIVE == APPACTIVE:
              print('app is ' + ('visibile' if e.gain else 'iconified'))
    screen.fill((30, 30, 30))
    pygame.display.flip()
    clock.tick(60)

main()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

请注意,SDL 2 不再使用这些值,而是使用类似的事件

SDL_WINDOWEVENT_ENTER
SDL_WINDOWEVENT_LEAVE
SDL_WINDOWEVENT_FOCUS_GAINED
SDL_WINDOWEVENT_FOCUS_LOST
SDL_WINDOWEVENT_MINIMIZED
SDL_WINDOWEVENT_RESTORED
Run Code Online (Sandbox Code Playgroud)

如果您将来想使用 pygame 2,这可能会有意义。