为什么蛇长大后速度会加快。 [Python]

问题描述 投票:0回答:1

我正在用Python开发著名的蛇游戏,我偶然发现了一个小故障,蛇在吃食物时开始做事。蛇在整个游戏过程中应保持恒定的速度,并且各个部分之间的距离应相等。但发生的情况是,每次蛇吃食物时,它的速度都会增加,并且各部分开始相互远离。如果有人可以提供帮助,这是代码的一部分。

my_screen = Screen()
my_screen.setup(width=1000, height=600)
my_screen.bgcolor("black")
my_screen.title("My Snake Game")
my_screen.tracer(0)
my_snake = Snake()
food = Food()
scoreboard = Scoreboard()
my_screen.listen()
my_screen.onkey(my_snake.up, "Up")
my_screen.onkey(my_snake.down, "Down")
my_screen.onkey(my_snake.left, "Left")
my_screen.onkey(my_snake.right, "Right")
game_over = False
while not game_over:
    my_screen.update()
    time.sleep(0.1)
    my_snake.move()
    # Detect collision with the food
    if my_snake.head.distance(food) < 15:
        food.refresh_food()
        my_snake.grow()
    # Detect collision with wall
    if my_snake.head.xcor() < -480 or my_snake.head.xcor() > 480 or my_snake.head.ycor() < -280 or my_snake.head.ycor() > 280:
        game_over = True
    # Detect collision with tail
    for segment in my_snake.segments[2:]:
        if my_snake.head.distance(segment) < 10:
            game_over = True
class Snake:
    def __init__(self):
        self.segments = []
        self.create_snake()
        self.head = self.segments[0]
    def create_snake(self):
        for position in POSITIONS:
            self.add_segment(position)
    def add_segment(self, position):
        new_segment = Turtle("square")
        new_segment.color("white")
        new_segment.penup()
        new_segment.goto(position)
        self.segments.append(new_segment)
    def move(self):
        for seg_num in range(len(self.segments) - 1, 0, -1):
            self.head.forward(MOVE_DISTANCE) # Moving distance of head
            new_x = self.segments[seg_num - 1].xcor()
            new_y = self.segments[seg_num - 1].ycor()
            self.segments[seg_num].goto(new_x, new_y)
    def grow(self):
        self.add_segment(self.segments[-1].position())
    def up(self):
        if self.head.heading() != 270:
            self.head.setheading(90)
    def down(self):
        if self.head.heading() != 90:
            self.head.setheading(270)
    def left(self):
        if self.head.heading() != 0:
            self.head.setheading(180)
    def right(self):
        if self.head.heading() != 180:
            self.head.setheading(0)
class Food(Turtle):
    def __init__(self):
        super().__init__()
        self.shape("circle")
        self.penup()
        self.shapesize(stretch_len=0.5, stretch_wid=0.5)
        self.color("red")
        self.speed("fastest")
        self.refresh_food()
    def refresh_food(self):
        rand_x_cor = random.randint(-480, 480)
        rand_y_cor = random.randint(-260, 260)
        self.goto(rand_x_cor, rand_y_cor)

我尝试减少蛇头的移动距离

self.head.forward(MOVE_DISTANCE) # Moving distance of head

也尝试了不同的方法来移动蛇,但也没有成功。

python pygame python-turtle
1个回答
0
投票

您的

move
方法看起来不正确:

def move(self):
    for seg_num in range(len(self.segments) - 1, 0, -1):
        self.head.forward(MOVE_DISTANCE)  # Moving distance of head
        new_x = self.segments[seg_num - 1].xcor()
        new_y = self.segments[seg_num - 1].ycor()
        self.segments[seg_num].goto(new_x, new_y)

我认为你不想为每个尾部移动头部。相反,您想要移动一次头部,然后移动所有尾部一次:

def move(self):
    self.head.forward(MOVE_DISTANCE)  # Moving distance of head
    for seg_num in range(len(self.segments) - 1, 0, -1):
        new_x = self.segments[seg_num - 1].xcor()
        new_y = self.segments[seg_num - 1].ycor()
        self.segments[seg_num].goto(new_x, new_y)
© www.soinside.com 2019 - 2024. All rights reserved.