简体   繁体   English

pygame.QUIT() - TypeError: 'int' object 不可调用

[英]pygame.QUIT() - TypeError: 'int' object is not callable

I'm fairly new to python. I'm writing a code for a simple alien invasion game but I'm getting this error.我是 python 的新手。我正在为一个简单的外星人入侵游戏编写代码,但出现此错误。

import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf

def run_game():
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Game")
    ship = Ship(screen)
    bg_color = (230, 230, 230)

    while True:
        gf.check_events(ship)
        ship.update()
        gf.update_screen(ai_settings, screen, ship)
        for event in pygame.event.get():
            if event.type==pygame.QUIT():
                sys.exit()
        screen.fill(ai_settings.bg_color)
        ship.blitme()
        pygame.display.flip()
run_game()

I'm aware that I'm accidentally calling some integer value but I have no clue where I'm going wrong.我知道我不小心调用了一些 integer 值,但我不知道哪里出错了。

I have also checked these lines我也检查了这些行

File "C:/Users/Areeb Irfan/.PyCharmCE2018.3/config/scratches/AlienGame.py", line 25, in run_game()文件“C:/Users/Areeb Irfan/.PyCharmCE2018.3/config/scratches/AlienGame.py”,第 25 行,在 run_game() 中
File "C:/Users/Areeb Irfan/.PyCharmCE2018.3/config/scratches/AlienGame.py", line 16, in run_game gf.check_events(ship)文件“C:/Users/Areeb Irfan/.PyCharmCE2018.3/config/scratches/AlienGame.py”,第 16 行,在 run_game gf.check_events(ship)
File "C:\Users\Areeb Irfan.PyCharmCE2018.3\config\scratches\game_functions.py", line 5, in check_events if event.type==pygame.QUIT():文件“C:\Users\Areeb Irfan.PyCharmCE2018.3\config\scratches\game_functions.py”,第 5 行,在 check_events if event.type==pygame.QUIT() 中:
TypeError: 'int' object is not callable TypeError: 'int' object 不可调用

As shown in your error message, this line event.type==pygame.QUIT() is the issue. 如错误消息中所示,此行event.type==pygame.QUIT()是问题所在。 The error TypeError: 'int' object is not callable means that you are trying to call an int, which means that you are trying to treat an int as a function, which in turn means that you have parentheses () after an int value. 错误TypeError: 'int' object is not callable意味着您正在尝试调用一个int,这意味着您试图将一个int视为一个函数,这又意味着您在int值后有括号() The only place you have parentheses in that line is afer pygame.QUIT , so just remove the parentheses: pygame.QUIT在该行中唯一带有括号的pygame.QUIT ,因此只需删除括号即可:

if event.type==pygame.QUIT:

To fix that, remove the parentheses () at pygame.QUIT.要解决此问题,请删除 pygame.QUIT 中的括号 ()。 Get rid of them, and run the code again.摆脱它们,然后再次运行代码。 It should work, as long as you don't have that same "if" somewhere else.它应该可以工作,只要您在其他地方没有相同的“如果”。

There is a flaw in your code.您的代码中存在缺陷。 You have written as:你写成:

if event.type == pygame.QUIT():
        quit()

Better try using this code:最好尝试使用此代码:

if event.type == pygame.QUIT():
        quit()

(or) (或者)

if event.type == pygame.QUIT():
        sys.exit()

This happens because pygame.QUIT is an integer data.发生这种情况是因为 pygame.QUIT 是一个 integer 数据。 Integer data is not followed by parantheses. Integer 数据后面没有括号。 But enclosing it with parantheses makes it to be a function which is wrong.但是用括号将它括起来会使它成为错误的 function。 Hope this helps you!希望这对你有帮助!

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

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