简体   繁体   English

pygame与龟崩溃

[英]pygame with turtle crashing

I get this error code: 我收到此错误代码:

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

My code is 我的代码是

#!/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!');

I am very very new to python programming, and have no experience in what this is. 我对python编程非常陌生,并且没有这方面的经验。 I have tried looking for answers to fix this, but quite hard when I don't know what's wrong, could anyone tell me how I could fix the issue? 我一直在寻找解决这个问题的答案,但是当我不知道什么是错的时候很难,有人能告诉我如何解决这个问题吗? What did I do wrong? 我做错了什么?

Here I will point out your mistakes and how to solve your problem. 在这里,我将指出您的错误以及如何解决您的问题。

  1. true is called True in python true在python中称为True
  2. K_ESCAPE , K_W ... isn't defined, I don't know where you got your names from but you gotta figure that out yourself. K_ESCAPEK_W ......没有定义,我不知道你从哪里得到你的名字,但你必须弄明白自己。 You probably meant pygame.K_ESCAPE , pygame.K_w ...note: they are lower cased 你可能意味着pygame.K_ESCAPEpygame.K_w ......注意:它们是较低的
  3. python does not require ; python不需要; at every line breaks 在每一个休息时间
  4. you do not need pygame = pygame and turtle = turtle since the import comes with the names 你不需要pygame = pygameturtle = turtle因为导入带有名字
  5. getting item from dict uses [] not . 从dict获取项目使用[]不是。
  6. calling turtle's left/right turns the cursor by that angle not move left/right 调用乌龟的左/右转动光标不会向左/向右移动

And here's how you can stop it from crashing: 以下是如何阻止它崩溃:

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!')

You will need to swap places with turtle.begin_fill() and pygame.init() since both pygame and turtle uses the base module tkinter , but only one Tk() (the window) is allowed at a time, but turtle does not know how to use the existing window that pygame created, so the app crashed. 您将需要交换与地方turtle.begin_fill()pygame.init()因为两者pygame的和龟使用基本模块tkinter ,但只有一个Tk()窗口)被允许在一段时间,但龟不知道如何使用pygame创建的现有窗口,因此应用程序崩溃了。 While pygame knows how to use the existing window turtle created. 虽然pygame知道如何使用现有的窗口龟创建。

As a side note, it's not necessary to use pygame with turtle, just use tkinter and it has way more functionalities. 作为旁注,没有必要使用pygame和turtle,只需使用tkinter ,它有更多的功能。 Plus, you don't even need either of them, turtle have enough functions to do what you wanted. 另外,你甚至不需要其中任何一个,龟有足够的功能来做你想要的。

EDIT: pygame and turtle will never work well together. 编辑:pygame和龟永远不会很好地一起工作。 recommend using tkinter. 建议使用tkinter。

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

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