2016-11-04 4 views
1

タートル画面で変数を出力したい。しかし変数が変わると、以前の値はまだ画面に表示されます。したがって、値は重複しています。方法turtle.clear()は、画面上で値がちらつくため、問題は解決しません。タートル画面に変数を出力する方法は?

# -*- 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() 
+0

ちらつきが見えない場合は、何を期待していますか?あなたの要件は何ですか?2.51の最終的な 't'値を出力しますか? –

答えて

2

状況を改善するためにできることがいくつかあります。 1つ目は、カメの元に戻す機能を使用して古い時間情報を消去する方法です。第二に、私たちは制御の下でちらつきを取得するには、アニメーションの更新を制御するためにtracer()update()を使用することができます。

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() 

ところで、亀の非常にクールな使用を!さらなるステップを進めてみましょう:モーションを連続的にする。クリック時にの終了を有効にするアニメーション中はいつでも終了できます。最初の位置に来るまで体のカメを見ることはできません。フォントサイズをバンプアップして、時刻を確認してください。

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() 
関連する問題