turtle和tkinter移动的红色矩形test程序

turtle和tkinter移动的红色矩形test程序

from turtle import *
from time import sleep
from turtle import TurtleScreenBase

def _convertxy(self,x,y):
    """把角色的x,y坐标转换回画布原来以左上角为原点的坐标"""
    x, y = x/self.xscale,-y/self.yscale
    return x,y

TurtleScreenBase.convertxy = _convertxy
screen = Screen()
x1,y1 = screen.convertxy(-100,100)
x2,y2 = screen.convertxy(0,0) 

def draw_rect(width,height,direction=1,fill=True,border=0):
    if direction != 1:direction = -1
    w2 = width/2
    x1 = t.xcor() - w2
    y1 = t.ycor() + height * direction
    x2 = t.xcor() + w2
    y2 = t.ycor()
    x1,y1 = screen.convertxy(x1,y1)
    x2,y2 = screen.convertxy(x2,y2)
    if fill:
       item = t.screen.cv.create_rectangle(x1,y1,x2,y2,width=border,fill=t.fillcolor())
    else:
       item = t.screen.cv.create_rectangle(x1,y1,x2,y2,width=border)
    return item

t = Turtle()
t.color('blue','red')
r = draw_rect(50,100,-1,2,5)
canvas = t.screen.cv
for x in range(100):
    canvas.move(r,1,0)
    canvas.update()
    sleep(0.01)

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。