简体   繁体   English

我的海龟模块代码正在执行第一部分而不是第二部分。 两个部分都可以自己正常工作

[英]My turtle module code is executing the first part but not the second part. Both parts are working fine on their own

I'm making a simple snake game in python.我正在用python制作一个简单的蛇游戏。 I am in the earlier stages of just making the snake move at this point.在这一点上,我正处于让蛇移动的早期阶段。 So I have 3 files, main.py, Turtle_Skin.py and Turtle_Control.py The first part (In Turtle_Skin.py) is working just fine where I need to make the snake take the starting position, however even if I try migrating the code from Turtle_Control.py to main.py (to make sure it executes and doesn't get left behind while importing), it won't execute所以我有 3 个文件,main.py、Turtle_Skin.py 和 Turtle_Control.py 第一部分(在 Turtle_Skin.py 中)在我需要让蛇占据起始位置的地方工作得很好,但是即使我尝试迁移代码从 Turtle_Control.py 到 main.py (以确保它执行并且在导入时不会被遗忘),它不会执行

My Code with file names:我的带有文件名的代码:

main.py:主要.py:

from Turtle_Control import *
from Turtle_Skin import *
positions_goto()

Turtle_Skin.py: Turtle_Skin.py:

from turtle import *

screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Snake_Food_Game")
screen.tracer(1)
Baby_Turtle = Turtle()
Mommy_Turtle = Turtle()
Daddy_Turtle = Turtle()

All_Turtles = [Baby_Turtle, Mommy_Turtle, Daddy_Turtle]

for turtle in All_Turtles:
    turtle.shape("square")
    turtle.pencolor("white")
    turtle.color("white")


def positions_goto():
    Daddy_Turtle.penup()
    Daddy_Turtle.goto(x=-40, y=0)
    Mommy_Turtle.penup()
    Mommy_Turtle.goto(x=-20, y=0)
    Baby_Turtle.penup()


positions_goto()

screen.exitonclick()

Turtle_Control.py Turtle_Control.py

from Turtle_Skin import *
import time
positions_goto()

is_on = True
while is_on:
    screen.update()
    time.sleep(0.1)

    for part_num in range(len(All_Turtles) - 1, 0, -1):
        xcord = All_Turtles[part_num - 1].xcor()
        ycord = All_Turtles[part_num - 1].ycor()
        All_Turtles[part_num].goto(x=xcord, y=ycord)
    Baby_Turtle.forward(20)

screen.exitonclick() blocks your code, running the main turtle loop, until you click the screen. screen.exitonclick()阻塞你的代码,运行主乌龟循环,直到你点击屏幕。

Tracing the code execution:跟踪代码执行:

  1. main.py runs from Turtle_Control import * on line 1 main.py from Turtle_Control import *在第 1 行运行
  2. Turtle_Control.py runs from Turtle_Skin import * on line 1 Turtle_Control.py from Turtle_Skin import *在第 1 行运行
  3. Turtle_Skin.py runs most of the turtle code, then blocks at screen.exitonclick() . Turtle_Skin.py运行大部分海龟代码,然后在screen.exitonclick()处阻塞。 After you click, only then does from Turtle_Skin import * on line 1 of Turtle_Control.py resolve so that line 2, import time , can continue.点击后,只有在 Turtle_Control.py 的第 1 行from Turtle_Skin import * Turtle_Control.py解析,以便第 2 行import time可以继续。 But by then the window's been destroyed so the while loop is much too late.但是到那时窗口已被破坏,因此while循环为时已晚。

A good way to figure out what's going on with this behavior is to add print() s to your code to see if the code you care about is even executing, and if so, when.弄清楚这种行为发生了什么的一个好方法是将print()添加到您的代码中,以查看您关心的代码是否正在执行,如果是,何时执行。 Creating a minimal example of the problem would make the issue obvious:创建问题的最小示例将使问题变得明显:

import turtle

turtle.exitonclick() # => blocks until the screen is clicked
print("hi") # => only executes after the screen was clicked

The original code organization doesn't make much sense.原始代码组织没有多大意义。 Modules have no obvious responsibility.模块没有明显的责任。 positions_goto() is called in many different locations. positions_goto()在许多不同的位置被调用。 The main code that initializes turtles and runs the game loop is spread across a few files seemingly haphazardly.初始化海龟和运行游戏循环的主要代码看似随意地分布在几个文件中。

With such a small amount of code, creating modules seems premature here.使用如此少量的代码,在这里创建模块似乎还为时过早。 I'd put all of the code into one file until you have things working ("I am in the earlier stages of just making the snake move at this point") and really need obvious separation of concerns.我会将所有代码放在一个文件中,直到您可以正常工作为止(“我此时正处于使蛇移动的早期阶段”)并且确实需要明显的关注点分离。 When you do, I'd create different files for different classes (things/entities in the game), primarily.当你这样做时,我会主要为不同的类(游戏中的事物/实体)创建不同的文件。 snake.py with class Snake: would be one example.带有 Snake 类的snake.py class Snake:就是一个例子。 food.py with class Food: might be another potential file. food.py with class Food:可能是另一个潜在的文件。

There should be no "loose" code in the global scope in each file other than a class or function or two.除了一个或两个类或函数之外,每个文件的全局范围内都不应该有“松散”的代码。 Main-line code (particularly if non- idempotent ) in modules should be in an if __name__ == "__main__": block so that it's not invoked simply because the module was imported (which might happen multiple times in an app, as is the case here).模块中的主线代码(尤其是非幂等的)应该在if __name__ == "__main__":块中,这样它就不会因为模块被导入而被调用(这可能在应用程序中发生多次,就像案例在这里)。

If you want to separate the whole game from main, that's fine, but keep the set up and main loop intact so they execute as a unit.如果您想将整个游戏与主游戏分开,那很好,但请保持设置和主循环完好无损,以便它们作为一个单元执行。

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

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