简体   繁体   中英

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.

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.

I have also checked these lines

File "C:/Users/Areeb Irfan/.PyCharmCE2018.3/config/scratches/AlienGame.py", line 25, in run_game()
File "C:/Users/Areeb Irfan/.PyCharmCE2018.3/config/scratches/AlienGame.py", line 16, in 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():
TypeError: 'int' object is not callable

As shown in your error message, this line event.type==pygame.QUIT() is the issue. 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. The only place you have parentheses in that line is afer pygame.QUIT , so just remove the parentheses:

if event.type==pygame.QUIT:

To fix that, remove the parentheses () at 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. Integer data is not followed by parantheses. But enclosing it with parantheses makes it to be a function which is wrong. Hope this helps you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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