python - 检测 pygame 中的用户事件

标签 python pygame

我有以下代码,我尝试在 pygame 中对用户事件使用react:

import pygame
from pygame.locals import *
from pygame.time import set_timer
from sys import exit

def timerFunc():
    print "Timer CallBack"

pygame.init()

screen = pygame.display.set_mode((640,480),0,32)
set_timer(USEREVENT+1, 1000)

while True:
    pressed_keys = pygame.key.get_pressed()

    if pressed_keys[K_SPACE]:
        exit()

    for event in pygame.event.get():
        print event
        if event == QUIT:
            exit()
        if event == USEREVENT+1:
            timerFunc()

不幸的是,timerFunc() 没有被执行,事件由 set_timer() 函数传播,因为在输出中我得到:

<Event(25-UserEvent {'code': 0})

最佳答案

您需要使用event.type属性:

for event in pygame.event.get():
    print event
    if event.type == QUIT:
        exit()
    if event.type == USEREVENT+1:
        timerFunc()

关于python - 检测 pygame 中的用户事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24265820/

相关文章:

python - 如何在 python 3.5+ 中在 except block 中引发异常而无需原始回溯

python - 比较除一列之外的所有列的值

python - 如何在pygame中启用中文输入?

python - 在pygame中旋转图像

python - 对输入中的字符串使用递归时进行计数

Python Pandas : apply a function to dataframe. 滚动()

python - 循环返回错误,表示级数的真实值不明确

python,if/elif/else 语法被注释分割?

python - pygame中的平滑运动

python - 如何在贪吃蛇游戏中碰撞后让 Sprite 移动到不同的位置