为什么我的计数器表现得很奇怪?我应该如何编辑代码来修复它?

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

我正在尝试使用

turtle()
实现我自己的蛇游戏版本,它的工作几乎完美地按照我的意愿进行。然而,我的问题是计数器。我的计数器有时会增加 2,即使我的代码明确告诉它在整个过程中只增加 1...

这是我的代码:

import turtle
import random

w = 500
h = 500
food_size = 10
delay = 100

offsets = {
    "up": (0, 20),
    "down": (0, -20),
    "left": (-20, 0),
    "right": (20, 0)
}

def reset():
    global snake, snake_dir, food_position, pen, food_collision_count
    snake = [[-180, 30], [-180, 50], [-180, 70], [-180, 90], [-180, 110]]
    snake_dir = "up"
    food_position = get_random_food_position()
    food.goto(food_position)
    food_collision_count = 0
    move_snake()

def move_snake():
    global snake_dir, pen, food_position, food_collision_count

    new_head = snake[-1].copy()
    new_head[0] += offsets[snake_dir][0]
    new_head[1] += offsets[snake_dir][1]

    for i in range(2):
        if new_head[i] > w / 2:
            new_head[i] -= w
        elif new_head[i] < -w / 2:
            new_head[i] += w

    if new_head in snake[:-1]:
        reset()
        return

    snake.append(new_head)

    x = 0

    if get_distance(new_head, food_position) < 20:
        food_collision_count = food_collision_count + 1
        update_food_collision_count()
        food_position = get_random_food_position()
        food.goto(food_position)
        x += food_collision_count
    else:
        snake.pop(0)
    high_score = x

    update_snake_on_screen()

    turtle.ontimer(move_snake, delay)

def update_snake_on_screen():
    pen.clearstamps()
    for segment in snake:
        pen.goto(segment[0], segment[1])
        pen.stamp()
    screen.update()

def update_food_collision_count():
    pen_count.clear()
    pen_count.goto(0, h / 2 - 20)
    pen_count.write(f"Food Collision Count: {food_collision_count}", align="center", font=("Arial", 12, "normal"))
    screen.update()

def get_random_food_position():
    x = random.randint(-w / 2 + food_size, w / 2 - food_size)
    y = random.randint(-h / 2 + food_size, h / 2 - food_size)
    return (x, y)

def get_distance(pos1, pos2):
    x1, y1 = pos1
    x2, y2 = pos2
    distance = ((y2 - y1) ** 2 + (x2 - x1) ** 2) ** 0.5
    return distance

def update_direction(new_direction):
    global snake_dir, current_direction
    if snake_dir != opposite_direction(current_direction):
        snake_dir = new_direction

def change_direction(direction):
    global snake_dir, current_direction

    current_direction = snake_dir

    switch = {
        "up": lambda: update_direction("up"),
        "right": lambda: update_direction("right"),
        "down": lambda: update_direction("down"),
        "left": lambda: update_direction("left"),
    }

    switch.get(direction, lambda: None)()

def opposite_direction(current_direction):
    opposites = {"up": "down", "down": "up", "left": "right", "right": "left"}
    return opposites.get(current_direction, current_direction)

def handle_direction_key(direction):
    switch = {
        "Up": "up",
        "Right": "right",
        "Down": "down",
        "Left": "left",
    }

    change_direction(switch.get(direction, ""))

screen = turtle.Screen()
screen.setup(w, h)
screen.title("Snake")
screen.bgcolor("blue")
screen.tracer(0)

pen = turtle.Turtle("square")
pen.penup()

pen_count = turtle.Turtle()
pen_count.hideturtle()
pen_count.penup()

food = turtle.Turtle()
food.shape("square")
food.color("yellow")
food.shapesize(food_size / 20)
food.penup()

screen.listen()
screen.onkey(lambda: handle_direction_key("Up"), "Up")
screen.onkey(lambda: handle_direction_key("Right"), "Right")
screen.onkey(lambda: handle_direction_key("Down"), "Down")
screen.onkey(lambda: handle_direction_key("Left"), "Left")

reset()

turtle.ontimer(move_snake, delay)

turtle.done() 

我不明白这里的问题是什么。如果这看起来是一个愚蠢的问题,我想提前道歉,因为我才刚刚开始了解

turtle()

我的代码中您应该注意的部分是

move_game()
def update_food_collision_count()

我应该改变什么来确保我的计数器不会无缘无故地随机增加 2?

python counter increment python-turtle
1个回答
0
投票

正如评论中提到的,问题是你的代码启动了两个游戏循环计时器:

reset() # calls move_snake(), which calls turtle.ontimer(move_snake, delay) turtle.ontimer(move_snake, delay)
删除 

turtle.ontimer(move_snake, delay)

 调用,您的游戏似乎会正常运行。

© www.soinside.com 2019 - 2024. All rights reserved.