简体   繁体   English

Python海龟模块

[英]Python turtle module

I'm currently new to python programming.我目前是 python 编程的新手。 Nowadays I'm building a snake game using the turtle module.现在我正在使用 turtle 模块构建一个贪吃蛇游戏。 I want to refresh the screen after every piece of snake object parts has moved.我想在每条蛇 object 部分移动后刷新屏幕。 So I turned off the tracer and use the update function after the for loop.所以我关闭了跟踪器并在 for 循环之后使用更新 function。

But to do that I must import the time module and use the time.sleep() function.If I don't use it the python turtle module begins to not respond.但要做到这一点,我必须导入时间模块并使用 time.sleep() function。如果我不使用它,python 海龟模块将开始不响应。 I want to know what is the reason why I must use the time function and why I can't simply use sc.update directly without time function.我想知道为什么我必须使用时间 function 以及为什么我不能在没有时间 function 的情况下直接使用 sc.update 的原因。

here is my code这是我的代码



from turtle import *
from snake import *
import time

sc = Screen()
sc.bgcolor('black')
sc.setup(width=600, height=600)
sc.tracer(0)

# diyego is our snake name
diyego = Snake(10)

run = 1
while run:
#here is the problem 
    sc.update()
    time.sleep(1) #used time.sleep
    for i in range(len(diyego.snake_object_list)-1, 0, -1):
        infront_item_position = diyego.snake_object_list[i - 1].pos()
        diyego.snake_object_list[i].goto(infront_item_position)

    diyego.snake_head.forward(10)


sc.exitonclick()




#Snake module

from turtle import *


class Snake():
    def __init__(self, number_of_parts):
        """Should pass the lenght of snake"""
        self.snake_object_list = []
        self.create_snake_parts(number_of_parts)
        self.snake_head = self.snake_object_list[0]

    def create_snake_parts(self, number_of_parts):
        """ Get number of parts which snake shuld have and create snake it"""
        x_cor = 0
        for i in range(number_of_parts):
            snake = Turtle()
            snake.speed(0)
            snake.shape("circle")
            snake.color('white')
            snake.penup()
            snake.setx(x=x_cor)
            self.snake_object_list.append(snake)
            x_cor += -20


I just want to know why the turtle gets not respond when I remove the time.sleep()我只想知道为什么当我删除 time.sleep() 时乌龟没有响应

What you describe is possible, but the problem isn't lack of use of the sleep() function but rather your use of (effectively) while True: which has no place in an event-driven world like turtle.您所描述的是可能的,但问题不在于缺少sleep() function 的使用,而是您(有效地)使用while True:它在像海龟这样的事件驱动世界中没有立足之地。 Let's rework your code using ontimer() events and make the snake's basic movement a method of the snake itself:让我们使用ontimer()事件重新编写您的代码,并使蛇的基本运动成为蛇本身的方法:

from turtle import Screen, Turtle

CURSOR_SIZE = 20

class Snake():
    def __init__(self, number_of_parts):
        """ Should pass the length of snake """
        self.snake_parts = []
        self.create_snake_parts(number_of_parts)
        self.snake_head = self.snake_parts[0]

    def create_snake_parts(self, number_of_parts):
        """ Get number of parts which snake should have and create snake """
        x_coordinate = 0

        for _ in range(number_of_parts):
            part = Turtle()
            part.shape('circle')
            part.color('white')
            part.penup()
            part.setx(x_coordinate)

            self.snake_parts.append(part)
            x_coordinate -= CURSOR_SIZE

    def move(self):
        for i in range(len(self.snake_parts) - 1, 0, -1):
            infront_item_position = self.snake_parts[i - 1].position()
            self.snake_parts[i].setposition(infront_item_position)

        self.snake_head.forward(CURSOR_SIZE)

def slither():
    diyego.move()
    screen.update()
    screen.ontimer(slither, 100)  # milliseconds

screen = Screen()
screen.bgcolor('black')
screen.setup(width=600, height=600)
screen.tracer(0)

diyego = Snake(10)

slither()

screen.exitonclick()

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

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