用python画出小人发射爱心_用python代码turtle画一个坐地铁

运行效果:知乎视频​www.zhihu.com代码:import time, mathimport turtle as tclass Human(object):def __init__(self, x0, y0, x1=None, y1=None, v0=100, v1=100):self.x0 = x0self.y0 = y0self.tx = x0self.ty = y0self.x1 = ...
摘要由CSDN通过智能技术生成

运行效果:知乎视频​www.zhihu.comzhihu-card-default.svg

代码:

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,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值