运行效果:知乎视频www.zhihu.com
代码:
import time, math
import turtle as t
class Human(object):
def __init__(self, x0, y0, x1=None, y1=None, v0=100, v1=100):
self.x0 = x0
self.y0 = y0
self.tx = x0
self.ty = y0
self.x1 = x0 if x1 is None else x1
self.y1 = y0 if y1 is None else y1
self.v0 = v0
self.v1 = v1
self.tv = v0
self.in_metro=False
self.need_get_on=True
self.should_remove=False
self.door_index=0
def move(self,back=False):
if self.need_get_on:
if back:
self.get_on_back()
else:
self.get_on()
else:
self.get_off()
def get_on(self):
if self.ty < -5:
self.ty+=3
elif self.ty < 0:
if self.tx
elif self.tx>self.x1:self.tx-=1
elif self.ty
self.ty+=2
elif self.ty
self.ty+=3
if self.ty>=self.y1:
self.in_metro=True
self.should_remove=True
def get_on_back(self):
if self.ty > self.y0:
self.ty-=1
else:
if self.tx > self.x0: self.tx-=1
elif self.tx
def get_off(self):
if self.ty>self.y1:
self.ty-=1
else:
self.in_metro=False
if self.tv>self.v1:
self.ty-=1
self.tv-=2
if self.tv<=self.v1:
self.should_remove=True
def draw(self):
if self.in_metro:
return
x=self.tx
y=self.ty
clr=(1-self.tv*0.01,1-self.tv*0.01,1-self.tv*0.01)
t.pencolor(clr)
goto_with_penup(x, y)
t.pensize(1)
t.begin_fill()
t.fillcolor(clr)
t.circle(4)
t.end_fill()
ty = 2
t.sety(y - ty)
t.goto(x - 6, y - ty - 2)
t.goto(x, y - ty)
t.goto(x + 6, y - ty - 2)
t.goto(x, y - ty)
ty = 6
t.sety(y - ty)
t.goto(x - 2, y - ty - 8)
t.goto(x, y - ty)
t.goto(x + 2, y - ty - 8)
t.goto(x, y - ty)
def fill_rect(w,