首页 > 解决方案 > 导入文件时出现 Python 错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[SDLApplication macOSVersion]

问题描述

设备:2020 款英特尔 MacBook Pro

软件:macOS Big Sur 11.4

Python版本:3.9

我正在尝试将一个 python 文件导入另一个;两者都在同一个目录中。这适用于其他几个文件,但在一种情况下会引发错误。

bloodmoon.py自行运行良好,但当我尝试导入时抛出错误bloodmoon.py__main__.py如下所示:

血月.py

import pygame
from settings import Settings
from monster import Monster, Player, Monster_2, Monster_3
import gameFunctions as gf
import time
import sys
import pygame.freetype

# font stuff
pygame.init()
big_font = pygame.font.Font(None, 150)
small_font = pygame.font.Font(None, 75)
global success
success = None

def runGame():
    pygame.init()
    bloodmoonSettings = Settings()
    screen = pygame.display.set_mode((bloodmoonSettings.screen_width, bloodmoonSettings.screen_height))
    pygame.display.set_caption("Bloodmoon")
    coordsUpdateTimer = 0  # time since last checked player coordinates for monster movement
    coordsUpdateTimer_2 = 0
    coordsUpdateTimer_3 = 0
    timeSinceShake = 0 # time since monster shook
    timeSinceTP = 0 # time since monster teleported
    timeSinceTP_2 = 0
    timeSinceTP_3 = 0
    global score
    score = 0
    clock = pygame.time.Clock()

    # playtime clock

    monster = Monster(screen)
    monster_2 = Monster_2(screen)
    monster_3 = Monster_3(screen)
    player = Player(screen, bloodmoonSettings)


    gf.check_events(player)
    player.update()
    gf.update_screen(bloodmoonSettings, screen, monster, player, monster_2, monster_3, str(score), small_font)





    while True:
        # Watch for keyboard and mouse events; update the screen
        gf.check_events(player)
        player.update()
        gf.update_screen(bloodmoonSettings, screen, monster, player, monster_2, monster_3, str(score), small_font)
        gf.detect_collision(player, monster, monster_2, monster_3)
        if gf.dead:
            gf.game_over(screen)
            gf.print_message(screen, "GAME OVER", big_font, (300, 50))
            time.sleep(3)
            print("You died.")
            success = False
            sys.exit()


        # the clock is ticking...
        dt = clock.tick()
        coordsUpdateTimer += dt
        coordsUpdateTimer_2 += dt
        coordsUpdateTimer_3 += dt
        timeSinceShake += dt
        timeSinceTP += dt
        timeSinceTP_2 += dt
        timeSinceTP_3 += dt

        # coords updates
        if coordsUpdateTimer > 1500:
            player.find_player()
            player.printPos()
            monster.shake()
            coordsUpdateTimer = 0
            timeSinceTP = 2000
        if coordsUpdateTimer_2 > 2500:
            player.find_player_2()
            monster_2.unShake()
            player.printPos_2()
            coordsUpdateTimer_2 = 0
            timeSinceTP_2 = 3500
        if coordsUpdateTimer_3 > 5000:
            player.find_player_3()
            player.printPos_3()
            coordsUpdateTimer_3 = 0
            monster_3.unShake()
            timeSinceTP_2 = 6000

        # change to 1 for a good time
            for i in range(3):
                monster.shake()
                monster_2.shake()
                monster_3.unShake()
                gf.update_screen(bloodmoonSettings, screen, monster, player, monster_2, monster_3, str(score), small_font)
                pygame.time.wait(25)
                monster.unShake()
                monster_2.unShake()
                monster_3.shake()
                gf.update_screen(bloodmoonSettings, screen, monster, player, monster_2, monster_3, str(score), small_font)
            timeSinceShake = 0

        # teleportation
        if timeSinceTP > 3000:
            monster.teleport()
            monster_3.shake()
            timeSinceTP = 0
            score += 1
        if timeSinceTP_2 > 6000:
            monster_2.teleport()
            monster.unShake()
            timeSinceTP_2 = 0
            score += 1
        if timeSinceTP_3 > 9500:
            monster_3.teleport()
            monster_2.shake()
            timeSinceTP_3 = 0
            score += 1

        if score >= 10:
            gf.victory(screen)
            time.sleep(3)
            success = True
            sys.exit()

if __name__ == '__main__':
    runGame()

主要的.py

import bloodmoon
# snipped code

我收到以下错误:

/usr/local/bin/python3.9 "/Users/petersalmon/PycharmProjects/Command Line Game/__main__.py"
pygame 2.0.1 (SDL 2.0.14, Python 3.9.4)
Hello from the pygame community. https://www.pygame.org/contribute.html
2021-07-06 11:24:49.455 Python[3125:202473] -[SDLApplication macOSVersion]: unrecognized selector sent to instance 0x7ff96be4c710
2021-07-06 11:24:49.457 Python[3125:202473] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SDLApplication macOSVersion]: unrecognized selector sent to instance 0x7ff96be4c710'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff2048587b __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff201bdd92 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff2050838d -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x00007fff203ed90b ___forwarding___ + 1448
    4   CoreFoundation                      0x00007fff203ed2d8 _CF_forwarding_prep_0 + 120
    5   libtk8.6.dylib                      0x000000010d501af0 GetRGBA + 282
    6   libtk8.6.dylib                      0x000000010d501522 SetCGColorComponents + 124
    7   libtk8.6.dylib                      0x000000010d501925 TkpGetColor + 543
    8   libtk8.6.dylib                      0x000000010d46193b Tk_GetColor + 143
    9   libtk8.6.dylib                      0x000000010d454792 Tk_Get3DBorder + 118
    10  libtk8.6.dylib                      0x000000010d45461a Tk_Alloc3DBorderFromObj + 127
    11  libtk8.6.dylib                      0x000000010d462c0f DoObjConfig + 970
    12  libtk8.6.dylib                      0x000000010d46272a Tk_InitOptions + 333
    13  libtk8.6.dylib                      0x000000010d462622 Tk_InitOptions + 69
    14  libtk8.6.dylib                      0x000000010d489c44 CreateFrame + 1557
    15  libtk8.6.dylib                      0x000000010d489ecd TkListCreateFrame + 137
    16  libtk8.6.dylib                      0x000000010d4826c5 Initialize + 2002
    17  _tkinter.cpython-39-darwin.so       0x000000010d2df724 Tcl_AppInit + 84
    18  _tkinter.cpython-39-darwin.so       0x000000010d2da0fa Tkapp_New + 602
    19  _tkinter.cpython-39-darwin.so       0x000000010d2d9a01 _tkinter_create + 561
    20  Python                              0x000000010ccd21bf cfunction_vectorcall_FASTCALL + 95
    21  Python                              0x000000010cd7f1f9 call_function + 168
    22  Python                              0x000000010cd7cad8 _PyEval_EvalFrameDefault + 45319
    23  Python                              0x000000010cd70889 _PyEval_EvalCode + 403
    24  Python                              0x000000010cc87a5c _PyFunction_Vectorcall + 376
    25  Python                              0x000000010cd7f1f9 call_function + 168
    26  Python                              0x000000010cd7cad8 _PyEval_EvalFrameDefault + 45319
    27  Python                              0x000000010cc879a4 _PyFunction_Vectorcall + 192
    28  Python                              0x000000010cc8706b _PyObject_FastCallDictTstate + 87
    29  Python                              0x000000010ccf6167 slot_tp_init + 192
    30  Python                              0x000000010ccff885 type_call + 272
    31  Python                              0x000000010cc86e3e _PyObject_MakeTpCall + 129
    32  Python                              0x000000010cd7f267 call_function + 278
    33  Python                              0x000000010cd7cb14 _PyEval_EvalFrameDefault + 45379
    34  Python                              0x000000010cc879a4 _PyFunction_Vectorcall + 192
    35  Python                              0x000000010cc8706b _PyObject_FastCallDictTstate + 87
    36  Python                              0x000000010ccf6167 slot_tp_init + 192
    37  Python                              0x000000010ccff885 type_call + 272
    38  Python                              0x000000010cc86e3e _PyObject_MakeTpCall + 129
    39  Python                              0x000000010cd7f267 call_function + 278
    40  Python                              0x000000010cd7cb14 _PyEval_EvalFrameDefault + 45379
    41  Python                              0x000000010cc879a4 _PyFunction_Vectorcall + 192
    42  Python                              0x000000010cd7f1f9 call_function + 168
    43  Python                              0x000000010cd7cad8 _PyEval_EvalFrameDefault + 45319
    44  Python                              0x000000010cd70889 _PyEval_EvalCode + 403
    45  Python                              0x000000010cdd3aed run_eval_code_obj + 126
    46  Python                              0x000000010cdd3a3d run_mod + 96
    47  Python                              0x000000010cdd1332 pyrun_file + 173
    48  Python                              0x000000010cdd0d3d pyrun_simple_file + 272
    49  Python                              0x000000010cdd0c07 PyRun_SimpleFileExFlags + 67
    50  Python                              0x000000010cdef954 pymain_run_file + 327
    51  Python                              0x000000010cdef0a4 pymain_run_python + 460
    52  Python                              0x000000010cdeee9c Py_RunMain + 23
    53  Python                              0x000000010cdf01b9 pymain_main + 35
    54  Python                              0x000000010cdf048f Py_BytesMain + 42
    55  libdyld.dylib                       0x00007fff2032df5d start + 1
)
libc++abi: terminating with uncaught exception of type NSException

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

我在网上找到的很少Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SDLApplication macOSVersion],所以我想知道是否有解决此错误的方法。

标签: pythonpython-3.xpygame

解决方案


推荐阅读