① 如何用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)
#收尾工作
這是一個簡易版貪吃蛇的代碼,雖然結構簡單,但是該有的功能都是完整的,可玩性也不錯