Я пишу простую игру про змей на Python 3 с модулем черепахи, но каждый раз, когда змея ест пищу, она становится немного медленнее, и в конечном итоге в игру невозможно играть.
Я хочу сделать скорость постоянной и достаточно быстрой, чтобы вы могли играть в игру, не раздражаясь, потому что змея движется очень медленно, но я не знаю, как это сделать.
Вот код, который у меня есть, мне еще нужно сделать кое-что еще но просто игнорируйте их.
#------------------------------------------SETUP------------------------------------------
#import statements
import turtle
import random
#variables
score = 0
up = True
down = False
left = False
right = False
high_score=0
#background
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("black")
wn.setup(width = 600, height =600) #set height and width of the screen
#create snake head
head = turtle.Turtle()
head.shape("square")
head.color("green")
head.penup()
head.goto(0,0)
#food
food = turtle.Turtle()
food.shape("square")
food.color("red")
food.penup()
food.speed = "fastest"
food.goto(0,100)
#segments
segments = []
segment_locationsy=[]
segment_locationsx=[]
#create pen to write score
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("green")
pen.penup()
pen.hideturtle()
pen.goto(0,260)
pen.pendown()
pen.write(pen.write("PRESS SPACE TO START" , align = "center", font=("Courier", 24, "normal")))
#------------------------------------------FUNCTIONS------------------------------------------
# define a function for each direction
#TODO: FIX REPETITIVENESS
def go_up():
global up,down,left,right
if (down != True):
up = True
down = False
left = False
right = False
def go_down():
global up,down,left,right
if (up != True):
up = False
down = True
left = False
right = False
def go_right():
global up,down,left,right
if (left != True):
up = False
down = False
left = False
right = True
def go_left():
global up,down,left,right
if (right != True):
up = False
down = False
left = True
right = False
#create a function to make the turtle move
def move():
if up:
head.sety(head.ycor()+5)
if down:
head.sety(head.ycor()-5)
if left:
head.setx(head.xcor()-5)
if right:
head.setx(head.xcor()+5)
#function to reset the food location
def reset_food():
new_x = random.randint(-280,280)
new_y = random.randint(-280,280)
if (head.distance(new_x,new_y)
Подробнее здесь: [url]https://stackoverflow.com/questions/70645190/how-can-i-prevent-my-snake-game-from-slowing-down-every-time-a-segment-is-added[/url]
Я пишу простую игру про змей на Python 3 с модулем черепахи, но каждый раз, когда змея ест пищу, она становится немного медленнее, и в конечном итоге в игру невозможно играть. Я хочу сделать скорость постоянной и достаточно быстрой, чтобы вы могли играть в игру, не раздражаясь, потому что змея движется очень медленно, но я не знаю, как это сделать. Вот код, который у меня есть, мне еще нужно сделать кое-что еще но просто игнорируйте их. [code]#------------------------------------------SETUP------------------------------------------ #import statements import turtle import random
#variables score = 0 up = True down = False left = False right = False high_score=0
#background wn = turtle.Screen() wn.title("Snake Game") wn.bgcolor("black") wn.setup(width = 600, height =600) #set height and width of the screen
#create snake head head = turtle.Turtle() head.shape("square") head.color("green") head.penup() head.goto(0,0)
pen.write(pen.write("PRESS SPACE TO START" , align = "center", font=("Courier", 24, "normal"))) #------------------------------------------FUNCTIONS------------------------------------------ # define a function for each direction #TODO: FIX REPETITIVENESS def go_up(): global up,down,left,right if (down != True): up = True down = False left = False right = False def go_down(): global up,down,left,right if (up != True): up = False down = True left = False right = False def go_right(): global up,down,left,right if (left != True): up = False down = False left = False right = True def go_left(): global up,down,left,right if (right != True): up = False down = False left = True right = False
#create a function to make the turtle move def move(): if up: head.sety(head.ycor()+5) if down: head.sety(head.ycor()-5) if left: head.setx(head.xcor()-5) if right: head.setx(head.xcor()+5)
#function to reset the food location def reset_food(): new_x = random.randint(-280,280) new_y = random.randint(-280,280) if (head.distance(new_x,new_y)