① 如何用python的turtle库写出自己的名字
用python编写一个程序,输出你的姓名和学号的方法。
1.输入命令提示符,输入python并按Enter进入python交互模式。
② 怎么用python程序从键盘录入四个图形名称计算机自动调用turtle画板依次画出四
摘要 用户选择尺寸、填充颜色和线条颜色
③ 如何采用Python语言绘制一条彩色的蟒蛇
采用的是Python shell来实现的,也就是大家所说的IDLE插件。
END
2.建立脚本
1
单击shell界面上面工具栏中的“file”-“new file”。
2
如下图所示,我们建立了一个脚本文件。
END
3.保存文件
1
单击脚本文件上侧的“file”-“save”,来实现对文件的保存。
2
选定上述命令后,会出现一个保存设置界面,将我们的脚本保存到相应的位置,并给其命名,然后,单击“保存”,完成文件保存。
END
4.编写代码
1
1.脚本结构
根据需求,本例需要建立两个函数一个是main()函数,一个是drawSnake函数。
2
2.编写drawSnake:
该函数数用来实现画蛇的具体操作,主要涉及到到画图的方法;对于该函数,我们首先得确定画蛇的基本策略,该函数中用到了很多的turtle包中的子函数,具体可参见该包的帮助文档,这里贴出代码:
def drawSnake(rad,angle,len,neckrad):
mycolor=["black","red","red","blue","yellow"]
yocolor=["yellow","green","yellow","red","red"]
for i in range(len):
turtle.pencolor(mycolor[i])
turtle.circle(rad,angle)
turtle.pencolor(yocolor[i])
turtle.circle(-rad,angle)
turtle.pencolor("green")
turtle.circle(rad,angle/2)
turtle.pencolor("yellow")
turtle.fd(rad)
turtle.pencolor("red")
turtle.circle(neckrad+1,180)
turtle.pencolor("green")
turtle.fd(rad*2/3)
3
3.编写main函数:
代码贴出如下,具体代码意思可参见turtle的帮助文档:
def main():
turtle.setup(1300,800,0,0)
pythonsize=30
turtle.pensize(pythonsize)
turtle.seth(-40)
drawSnake(40,80,5,pythonsize/2)
4
4.执行main:
与其它不同的是我们需要在最后,将我们所定义的代码进行执行,不然就是空的。就是图中的main()
END
5.编译运行
单击脚本界面上的“run”-“run mole”,就可以运行我们的画图程序了。
执行效果,如下,虽然看起来有点不好看,但它把我们的真个执行过程给展现出来了。
④ python 怎样画两只乌龟
应当是版本的问题
turtle.forward(50)
这样使用这个函数就可以了
⑤ 我用python画皮卡丘,没有错误出现,我也打开才cmd窗口输入了page22.py,前面也加入了import turtle,求
命令行输入:python page22.py
如果python不是环境变量,请添加环境变量
⑥ Python turtle海龟制图 求代码
# coding:utf-8
import turtle as t
# 绘制小猪佩奇
# =======================================
t.pensize(4)
t.hideturtle()
t.colormode(255)
t.color((255, 155, 192), "pink")
t.setup(840, 500)
t.speed(10)
# 鼻子
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
t.begin_fill()
a = 0.4
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.08
t.lt(3)
t.fd(a)
t.end_fill()
t.pu()
t.seth(90)
t.fd(25)
t.seth(0)
t.fd(10)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()
t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()
# 头
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300, -30)
t.circle(100, -60)
t.circle(80, -100)
t.circle(150, -20)
t.circle(60, -95)
t.seth(161)
t.circle(-300, 15)
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
a = 0.4
for i in range(60):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.08
t.lt(3)
t.fd(a)
t.end_fill()
# 耳朵
t.color((255, 155, 192), "pink")
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.end_fill()
t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 56)
t.end_fill()
# 眼睛
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
t.color((255, 155, 192), "white")
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()
t.color("black")
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()
# 腮
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()
# 嘴
t.color(239, 69, 19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30, 40)
t.circle(40, 80)
# 身体
t.color("red", (255, 99, 71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100, 10)
t.circle(300, 30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300, 30)
t.circle(100, 3)
t.color((255, 155, 192), (255, 100, 100))
t.seth(-135)
t.circle(-80, 63)
t.circle(-150, 24)
t.end_fill()
# 手
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300, 15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20, 90)
t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300, 15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20, 90)
# 脚
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color("black")
t.pensize(15)
t.fd(20)
# 尾巴
t.pensize(4)
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)
t.done()
⑦ 怎么用python的turtle库画出这个图案,要代码
import turtle as t
def quad(color):
t.begin_fill()
t.color(color)
t.forward(100)
t.left(36)
t.forward(100)
t.left(36*4)
t.forward(100)
t.left(36)
t.forward(100)
t.end_fill()
t.left(36*3)
for i in range(10):
if i%2:
quad('#99c8de')
else:
quad('#e5b9c4')
两三年没碰海龟了,觉得没啥用,看你赏金又提了就回去学了学
⑧ python有趣的编程代码
classPoint:
row=0
col=0
def__init__(self,row,col):
self.row=row
self.col=col
def(self):
returnPoint(row=self.row,col=self.col)
#初始框架
importpygame
importrandom
#初始化
pygame.init()
W=800
H=600
ROW=30
COL=40
size=(W,H)
window=pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
bg_color=(255,255,255)
snake_color=(200,200,200)
head=Point(row=int(ROW/2),col=int(COL/2))
head_color=(0,128,128)
snakes=[
Point(row=head.row,col=head.col+1),
Point(row=head.row,col=head.col+2),
Point(row=head.row,col=head.col+3)
]
#生成食物
defgen_food():
while1:
pos=Point(row=random.randint(0,ROW-1),col=random.randint(0,COL-1))
#
is_coll=False
#是否跟蛇碰上了
ifhead.row==pos.rowandhead.col==pos.col:
is_coll=True
#蛇身子
forsnakeinsnakes:
ifsnake.row==pos.rowandsnake.col==pos.col:
is_coll=True
break
ifnotis_coll:
break
returnpos
#定义坐标
food=gen_food()
food_color=(255,255,0)direct='left'#left,right,up,down
#
defrect(point,color):
cell_width=W/COL
cell_height=H/ROW
left=point.col*cell_width
top=point.row*cell_height
pygame.draw.rect(
window,color,
(left,top,cell_width,cell_height)
)
pass
#游戏循环
quit=True
clock=pygame.time.Clock()
whilequit:
#处理事件
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
quit=False
elifevent.type==pygame.KEYDOWN:
ifevent.key==273orevent.key==119:
ifdirect=='left'ordirect=='right':
direct='up'
elifevent.key==274orevent.key==115:
ifdirect=='left'ordirect=='right':
direct='down'
elifevent.key==276orevent.key==97:
ifdirect=='up'ordirect=='down':
direct='left'
elifevent.key==275orevent.key==100:
ifdirect=='up'ordirect=='down':
direct='right'
#吃东西
eat=(head.row==food.rowandhead.col==food.col)
#重新产生食物
ifeat:
food=gen_food()
#处理身子
#1.把原来的头,插入到snakes的头上
snakes.insert(0,head.())
#2.把snakes的最后一个删掉
ifnoteat:
snakes.pop()
#移动
ifdirect=='left':
head.col-=1
elifdirect=='right':
head.col+=1
elifdirect=='up':
head.row-=1
elifdirect=='down':
head.row+=1
#检测
dead=False
#1.撞墙
ifhead.col<0orhead.row<0orhead.col>=COLorhead.row>=ROW:
dead=True
#2.撞自己
forsnakeinsnakes:
ifhead.col==snake.colandhead.row==snake.row:
dead=True
break
ifdead:
print('死了')
quit=False
#渲染——画出来
#背景
pygame.draw.rect(window,bg_color,(0,0,W,H))
#蛇头
forsnakeinsnakes:
rect(snake,snake_color)
rect(head,head_color)
rect(food,food_color)
#
pygame.display.flip()
#设置帧频(速度)
clock.tick(8)
#收尾工作
这是一个简易版贪吃蛇的代码,虽然结构简单,但是该有的功能都是完整的,可玩性也不错