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

问题描述

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

虽然该程序在技术上可以正常工作,并且确实可以跟踪每个x和y位置,但是跟踪x和y位置的文本在每一步都会闪烁。

我知道问题在于我正在使用c.execute("INSERT INTO inventory VALUES(NULL,:c_name,:p_no,:t_no,:d_item,:c_code,:l_item,:b_code,:n_comm)",{ 'c_name' : c_name.get(),'p_no' : p_no.get(),'t_no':t_no.get(),'d_item':d_item.get(),'c_code':c_code.get(),'l_item':l_item.get(),'b_code':b_code.get(),'n_comm':n_comm.get(),}) 函数(顾名思义,该函数清除了由乌龟生成的文本)。但是,我相信该程序只能与它一起正常运行。

这是我的代码:

.clear()

解决方法

我相信乌龟屏幕的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()

在编写代码时,我还对代码进行了一些样式更改...

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...