在海龟图形中使用 clear() 函数时如何停止闪烁

How to stop flickering when using the clear() function in turtle graphics

我正在编写一个练习程序,该练习模拟射弹发射,同时每秒跟踪每个 x 和 y 位置。

虽然该程序在技术上是可行的,并且它确实会追踪每个 x 和 y 位置,但追踪 x 和 y 位置的文本会在每一步中闪烁。

我知道问题是我正在使用 .clear() 函数(顾名思义,该函数清除海龟生成的文本)。但是,我相信该程序只能正常运行。

这是我的代码:

"""Program to track specific location of turtle (for every step)"""

from turtle import *
from math import *

def cball_graphics():

    leonardo = Turtle()
    leonardo.color("dark blue")
    leonardo.shape("turtle")
    leonardo.speed(1)

    return leonardo

def show_position():

    pos = Turtle()
    pos.color("white")
    pos.goto(30, -50)
    pos.color("red")

    return pos

class cannon_ball:

    def __init__(self, angle, vel, height, time):

        self.x_pos = 0
        self.y_pos = height
        theta = pi * angle / 180
        self.x_vel = vel * cos(theta)
        self.y_vel = vel * sin(theta)
        self.time = time

    def update_time(self):

        self.x_pos += self.time * self.x_vel
        y_vel1 = self.y_vel - 9.8 * self.time
        self.y_pos += self.time * (self.y_vel + y_vel1) / 2
        self.y_vel = y_vel1

    def get_x(self):

        return self.x_pos

    def get_y(self):

        return self.y_pos

def variables():

    angle = 55
    vel = 10
    height = 100
    time = .01

    return cannon_ball(angle, vel, height, time)

def main():

    leonardo = cball_graphics()

    """pos is a variable that writes the position on the screen using x and y pos"""
    pos = show_position()
    pos.hideturtle()
    projectile = variables()

    while projectile.y_pos >= 0:

        pos.write(f"{'%0.0f' % projectile.x_pos}, {'%0.0f' % projectile.y_pos}")

        projectile.update_time()
        leonardo.goto(projectile.x_pos, projectile.y_pos)

        pos.clear()

main()

相信龟屏的tracer()update()方法可以解决你的问题。它们允许您写入后备存储,然后在按照您想要的方式将其复制到屏幕上。对于消除闪烁特别有用:

from turtle import Screen, Turtle
from math import pi, sin, cos

FONT = ('Arial', 18, 'normal')

def cball_graphics():

    leonardo = Turtle()
    leonardo.color('dark blue')
    leonardo.shape('turtle')

    return leonardo

def show_position():

    pos = Turtle()
    pos.hideturtle()
    pos.goto(30, -50)
    pos.color('red')

    return pos

class cannon_ball:

    def __init__(self, angle, velocity, height, time):

        self.x_pos = 0
        self.y_pos = height

        theta = pi * angle / 180

        self.x_vel = velocity * cos(theta)
        self.y_vel = velocity * sin(theta)

        self.time = time

    def update_time(self):

        self.x_pos += self.time * self.x_vel
        y_vel1 = self.y_vel - 9.8 * self.time
        self.y_pos += self.time * (self.y_vel + y_vel1) / 2
        self.y_vel = y_vel1

def freefall():

    if projectile.y_pos >= 0:

        pos.clear()
        pos.write("({:0.0f}, {:0.0f})".format(projectile.x_pos, projectile.y_pos), font=FONT)

        projectile.update_time()

        leonardo.goto(projectile.x_pos, projectile.y_pos)

        screen.update()
        screen.ontimer(freefall)

variables = {
    'angle': 55,
    'velocity': 10,
    'height': 100,
    'time': 0.01,
}

screen = Screen()
screen.tracer(False)

leonardo = cball_graphics()

# pos is a turtle that writes the position on the screen using x and y pos
pos = show_position()

projectile = cannon_ball(**variables)

freefall()

screen.mainloop()

我还对代码进行了一些样式更改...