如何在海龟屏幕上输出变量? [英] How to output variable on turtle screen?

查看:37
本文介绍了如何在海龟屏幕上输出变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Turtle 屏幕上输出一个变量.但是随着变量的变化,之前的值仍然在屏幕上.因此,这些值是重叠的.方法 turtle.clear() 没有解决问题,因为它会导致值在屏幕上闪烁.

I want to output a variable on the Turtle screen. But as the variable changes, the previous value is still on the screen. Thus, the values are overlapped. Method turtle.clear() does not solve the problem, because it causes the value to flicker on the screen.

# -*- coding: utf-8 -*-

import turtle
from math import *

s = turtle.Screen()
body = turtle.Turtle()
time_t = turtle.Turtle()
time_t.penup() # For time output
time_t.ht()

def Motion(A=100, omega=5, N=2):
    n = 0
    t = 0

    def x_pos(t):
        return A*cos(0.5*omega*t)  # x

    def y_pos(t):
        return A*sin(1*omega*t)  # Кy

    body.setposition(x_pos(t),y_pos(t))
    body.pendown()

    while n<N:
        body.setposition(x_pos(t),y_pos(t))
        t = round(t + 0.01,2)
        time_t.setposition(200,200)  # In this position I want to output variable
        time_t.write("t = " + str(t))  # Show the time variable t on screen
        if int(round(t / (2*pi/omega),2)) == n + 1:
              n =  n + 1


body.penup()
body.color('red')
body.shape('circle')
Motion()
turtle.done()

推荐答案

我们可以采取一些措施来改善这种情况.第一种是利用turtle的Undo特性来去除旧的时间信息作为清除它的一种方式.其次,我们可以使用 tracer()update() 来控制动画更新以控制闪烁:

There are a couple of things we can do to improve the situation. The first is to use the Undo feature of turtle to remove the old time information as a way of clearing it. Second, we can use tracer() and update() to control animation updates to get flicker under control:

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

screen = Screen()

body = Turtle(shape="circle")
body.color('red')
body.penup()

time_t = Turtle(visible=False)  # For time output
time_t.penup()
time_t.setposition(200, 200)  # In this position I want to output variable
time_t.write("t = " + str(0))

screen.tracer(0)  # Control animation updates ourself

def Motion(A=100, omega=5, N=2):
    n = 0
    t = 0

    def x_pos(t):
        return A * cos(0.5 * omega * t)  # x

    def y_pos(t):
        return A * sin(1 * omega * t)  # Ky

    body.setposition(x_pos(t), y_pos(t))
    body.pendown()

    while n < N:
        body.setposition(x_pos(t), y_pos(t))

        t = round(t + 0.01, 2)
        time_t.undo()  # undraw the last time update
        time_t.write("t = " + str(t))  # Show the time variable t on screen
        screen.update()

        if int(round(t / (2 * pi / omega), 2)) == n + 1:
            n = n + 1

Motion()

screen.exitonclick()

顺便说一句,非常酷的乌龟使用!让我们更进一步:使运动连续;在动画过程中的任何时候启用点击退出;不要让身体乌龟在它的初始位置可见;放大字体大小,以便我们可以看到时间:

BTW, very cool use of turtle! Let's go a step further: make the motion continuous; enable the exit on click at any point during the animation; don't make the body turtle visible until it's in its initial position; bump up the font size so we can see the time:

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

def motion():
    global n, t
    body.setposition(x_pos(t), y_pos(t))

    t = round(t + 0.01, 2)
    time_t.undo()  # undraw the last time update
    time_t.write("t = " + str(t), font=font)  # Show the time variable t on screen
    screen.update()

    if int(round(t / (2 * pi / omega), 2)) == n + 1:
        n = n + 1
        if (n >= N):
            n = t = 0

    screen.ontimer(motion, 10)

def x_pos(t):
    return A * cos(0.5 * omega * t)  # x

def y_pos(t):
    return A * sin(1 * omega * t)  # Ky

A = 100
omega = 5
N = 2
n = t = 0

font = ("Arial", 12, "normal")

screen = Screen()
screen.tracer(0)  # Control animation updates ourself

body = Turtle(shape="circle", visible=False)
body.color('red')
body.penup()
body.setposition(x_pos(t), y_pos(t))
body.pendown()
body.showturtle()

time_t = Turtle(visible=False)  # For time output
time_t.penup()
time_t.setposition(200, 200)  # In this position I want to output variable
time_t.write("t = " + str(0))

screen.ontimer(motion, 100)

screen.exitonclick()

这篇关于如何在海龟屏幕上输出变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆