简体   繁体   English

pygame,如果event.type == pygame.KEYDOWN()TypeError:'int'对象不可调用

[英]Pygame if event.type == pygame.KEYDOWN() TypeError: 'int' object is not callable

The code is meant to quickly exit pygame by tapping escape. 该代码旨在通过点击转义快速退出pygame。

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN():
            if event.key == pygame.K_ESCAPE:
                running = False
                pygame.quit()
                sys.exit()

This code errors with this: 此代码与此错误:

Message File Name   Line    Position    
Traceback               
    <module>    G:\Code\JonSocket\keyboard.py   19      
TypeError: 'int' object is not callable             

Very similar code works when, 非常相似的代码在以下情况下有效:

event.type == pygame.QUIT

Is there a difference between pygame.QUIT + pygame.KEYDOWN? pygame.QUIT + pygame.KEYDOWN之间有区别吗?

Thanks. 谢谢。

Your problem is in the line 你的问题就在这里

if event.type == pygame.KEYDOWN():

pygame.KEYDOWN is not a function but just an integer constant. pygame.KEYDOWN不是函数,而只是一个整数常量。

Change it to 更改为

if event.type == pygame.KEYDOWN:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM