我收到此错误代码:
2017-04-13 03:04:14.958 Python[606:839244] -[SDLApplication _setup:]: unrecognized selector sent to instance 0x1007a0dd0
2017-04-13 03:04:14.971 Python[606:839244] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SDLApplication _setup:]: unrecognized selector sent to instance 0x1007a0dd0'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8d71be7b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffa22fbcad objc_exception_throw + 48
2 CoreFoundation 0x00007fff8d79dcb4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00007fff8d68dfb5 ___forwarding___ + 1061
4 CoreFoundation 0x00007fff8d68db08 _CF_forwarding_prep_0 + 120
5 Tk 0x000000010324b5e6 TkpInit + 471
6 Tk 0x00000001031c6c8d Tk_Init + 1794
7 _tkinter.cpython-36m-darwin.so 0x00000001027e7dfd Tcl_AppInit + 77
8 _tkinter.cpython-36m-darwin.so 0x00000001027e5849 _tkinter_create + 889
9 Python 0x000000010006a688 _PyCFunction_FastCallDict + 568
10 Python 0x00000001000f33e4 call_function + 612
11 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
12 Python 0x00000001000f27a0 _PyEval_EvalCodeWithName + 2720
13 Python 0x00000001000f2fab fast_function + 219
14 Python 0x00000001000f33cb call_function + 587
15 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
16 Python 0x00000001000f37cc _PyFunction_FastCallDict + 828
17 Python 0x000000010000e984 _PyObject_FastCallDict + 356
18 Python 0x000000010000eaa0 _PyObject_Call_Prepend + 208
19 Python 0x000000010000e5b3 PyObject_Call + 99
20 Python 0x0000000100089871 slot_tp_init + 81
21 Python 0x0000000100080144 type_call + 212
22 Python 0x000000010000e8d4 _PyObject_FastCallDict + 180
23 Python 0x00000001000f3225 call_function + 165
24 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
25 Python 0x00000001000f37cc _PyFunction_FastCallDict + 828
26 Python 0x000000010000e984 _PyObject_FastCallDict + 356
27 Python 0x000000010000eaa0 _PyObject_Call_Prepend + 208
28 Python 0x000000010000e5b3 PyObject_Call + 99
29 Python 0x0000000100089871 slot_tp_init + 81
30 Python 0x0000000100080144 type_call + 212
31 Python 0x000000010000e8d4 _PyObject_FastCallDict + 180
32 Python 0x00000001000f3225 call_function + 165
33 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
34 Python 0x00000001000f312e fast_function + 606
35 Python 0x00000001000f33cb call_function + 587
36 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
37 Python 0x00000001000f27a0 _PyEval_EvalCodeWithName + 2720
38 Python 0x00000001000f3636 _PyFunction_FastCallDict + 422
39 Python 0x000000010000e984 _PyObject_FastCallDict + 356
40 Python 0x000000010000eaa0 _PyObject_Call_Prepend + 208
41 Python 0x000000010000e5b3 PyObject_Call + 99
42 Python 0x0000000100089871 slot_tp_init + 81
43 Python 0x0000000100080144 type_call + 212
44 Python 0x000000010000e8d4 _PyObject_FastCallDict + 180
45 Python 0x00000001000f3225 call_function + 165
46 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
47 Python 0x00000001000f312e fast_function + 606
48 Python 0x00000001000f33cb call_function + 587
49 Python 0x00000001000f8d84 _PyEval_EvalFrameDefault + 21892
50 Python 0x00000001000f27a0 _PyEval_EvalCodeWithName + 2720
51 Python 0x00000001000f2944 PyEval_EvalCode + 100
52 Python 0x000000010012f21e PyRun_FileExFlags + 206
53 Python 0x000000010012f4bf PyRun_SimpleFileExFlags + 447
54 Python 0x0000000100148ada Py_Main + 3914
55 Python 0x0000000100000dfe Python + 3582
56 Python 0x0000000100000c34 Python + 3124
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6
我的代码是
#!/Library/Frameworks/Python.framework/Versions/3.6/bin/
import sys;
import log;
# Initialization
print(log.GetTime());
import pygame;
import turtle;
pygame = pygame;
turtle = turtle;
# After Initialization
pygame.init();
turtle.begin_fill();
player = {
'speed': {
'move': 5,
'rotate': 1
}
};
while true:
time = log.GetTime();
keys = pygame.key.get_pressed();
if keys[K_ESCAPE]: break;
if keys[K_W]: turtle.forward(player.speed.move);
if keys[K_S]: turtle.backward(player.speed.move);
if keys[K_A]: turtle.left(player.speed.rotate);
if keys[K_D]: turtle.right(player.speed.rotate);
print('Ended program!');
我对python编程非常陌生,并且没有这方面的经验。我一直在寻找解决这个问题的答案,但是当我不知道什么是错的时候我会很难,有人能告诉我如何解决这个问题吗?我做错了什么?
答案 0 :(得分:0)
在这里,我将指出您的错误以及如何解决您的问题。
true
在python中被称为True
K_ESCAPE
,K_W
...没有定义,我不知道你从哪里得到你的名字,但你必须弄明白自己。您可能意味着pygame.K_ESCAPE
,pygame.K_w
...注意:它们是较低的;
pygame = pygame
和turtle = turtle
,因为导入时带有名称以下是你如何阻止它崩溃:
import sys
import log
# Initialization
print(log.GetTime())
import pygame
import turtle
# After Initialization
turtle.begin_fill()
pygame.init()
player = {
'speed': {
'move': 5,
'rotate': 1
}
};
while True:
time = log.GetTime()
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]: break
if keys[pygame.K_W]: turtle.forward(player['speed']['move'])
if keys[pygame.K_S]: turtle.backward(player['speed']['move'])
if keys[pygame.K_A]: turtle.left(player['speed']['rotate'])
if keys[pygame.K_D]: turtle.right(player['speed']['rotate'])
print('Ended program!')
您需要与turtle.begin_fill()
和pygame.init()
交换位置,因为pygame和turtle都使用基本模块tkinter
,但只允许一个Tk()
(窗口)一次,但是龟不知道如何使用pygame创建的现有窗口,因此应用程序崩溃了。虽然pygame知道如何使用现有的窗口龟创建。
作为旁注,没有必要将pygame与turtle一起使用,只需使用tkinter
并且它具有更多功能。另外,你甚至不需要其中任何一个,龟有足够的功能来做你想做的事。