Reputation: 1
I'm currently new to python programming. Nowadays I'm building a snake game using the turtle module. I want to refresh the screen after every piece of snake object parts has moved. So I turned off the tracer and use the update function after the for loop.
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. 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.
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()
Upvotes: 0
Views: 94
Reputation: 41925
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. Let's rework your code using ontimer()
events and make the snake's basic movement a method of the snake itself:
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()
Upvotes: 0