python畫等邊三角形及彩蟒

2021-08-09 08:14:52 字數 4313 閱讀 8355

python turtle 庫 介紹

控制畫筆繪製狀態的函式

pendown() | pd() | down()

penup() | pu() | up()

pensize(wid ) | width(wid)

forward(distance) | fd(distance)

backward(distance)| bk(distance)

|back(distance)

right(angle) | rt(angle)

left(angle) | lt(angle)

setheading(to_angle)

position() | pos()

goto(x,y )

setposition(x,y ) | setpos(x,y )

circle(radius,extent ,steps )

dot(size ,*color) radians()

stamp() speed(speed )

clearstamp(stamp_id)

clearstamps(n ) undo()

speed(speed ) heading()

towards(x,y ) distance(x,y )

xcor() ycor()

setx(x) sety(y)

home() undo()

degrees(fullcircle = 360.0)

控制畫筆運動的函式

控制畫筆顏色和字型函式

color() reset()

begin_fill() end_fill()

filling() clear()

screensize()

showturtle() | st()

hideturtle() | ht()

isvisible()

write(arg,move=false,align="left"

,font =("arial",8,"normal") )

>>>import turtle

>>>from turtle import *

引入方式

bgcolor(*args)

bgpic(picname )

clearscreen()

resetscreen()

screensize(cwid ,canvh,bg )

tracer(n ,delay )

listen(xdummy ,ydummy )

onkey((fun,key)

onkeyrelease((fun,key)

onkeypress(fun,key )

onscreenclick(fun,btn=1,add )

turtlescreen/screen類的函式

getcanvas()

getshapes()

turtles()

window_height()

window_width()

bye()

exitonclick()

title(titlestring)

setup(wid=_cfg["wid"],h=_cfg["h"],

startx=_cfg["leftright"],

starty=_cfg["topbottom"])

turtle庫

python quick reference series python快速參考

python繪製等邊三角形

import turtle

import random

def drawsnake(rad):

turtle.fd(rad)

turtle.seth(120)

turtle.fd(rad)

turtle.seth(-120)

turtle.fd(rad)

def main():

turtle.setup(1300,800,0,0)

pythonsize = 30

turtle.pensize(1)

c = random.uniform(0,1)

d = random.randint(0,1)

e = random.randint(0,1)

f = (c,d,e)

turtle.pencolor(f)

turtle.seth(0)

drawsnake(pythonsize * 6)

main()

python繪製彩蟒

import turtle

import random

def drawsnake(rad,angle,len,neckrad):

for i in range(len):

c = random.uniform(0,1)

d = random.randint(0,1)

e = random.randint(0,1)

f = (c,d,e)

turtle.pencolor(f)

turtle.circle(rad,angle)

c = random.uniform(0,1)

d = random.randint(0,1)

e = random.randint(0,1)

f = (c,d,e)

turtle.pencolor(f)

turtle.circle(-rad,angle)

c = random.uniform(0,1)

d = random.randint(0,1)

e = random.randint(0,1)

f = (c,d,e)

turtle.pencolor(f)

c = random.uniform(0,1)

d = random.randint(0,1)

e = random.randint(0,1)

f = (c,d,e)

turtle.pencolor(f)

turtle.circle(rad,angle/2)

c = random.uniform(0,1)

d = random.randint(0,1)

e = random.randint(0,1)

f = (c,d,e)

turtle.pencolor(f)

turtle.fd(rad)

c = random.uniform(0,1)

d = random.randint(0,1)

e = random.randint(0,1)

f = (c,d,e)

turtle.pencolor(f)

turtle.circle(neckrad+1,180)

c = random.uniform(0,1)

d = random.randint(0,1)

e = random.randint(0,1)

f = (c,d,e)

turtle.pencolor(f)

turtle.fd(rad*2/3)

def main():

turtle.setup(1300,800,0,0)

pythonsize = 30

turtle.pensize(pythonsize)

c = random.uniform(0,1)

d = random.randint(0,1)

e = random.randint(0,1)

f = (c,d,e)

turtle.pencolor(f)

turtle.seth(-220)

drawsnake(40,80,5,pythonsize/2)

main()

turtle庫

turtle.setup(width,height,x,y)

啟動乙個圖形視窗 引數為視窗的寬度、長度,以及視窗在螢幕上的左上角

turtle.pensize(

width)

運動軌跡的寬度

turtle.pencolor(color)

運動軌跡的顏色

turtle.seth(angle)

運動啟動時的運動方向 0 表示 向東;90 向北,180 向西;270向南;負值表示反方向

turtle.circle(rad,angle)

以圓為軌跡 rad表示半斤,angle表示角度

turtle.fd(rad)

以直線為軌跡 rad為移動距離

等邊三角形

蒜頭君手上有一些小木棍,它們長短不一,蒜頭君想用這些木棍拼出乙個等邊三角形,並且每根木棍都要用到。例如,蒜頭君手上有長度為1,2,3,3的4根木棍,他可以讓長度為1,2的木棍組成一條邊,另外2跟分別組成2條邊,拼成乙個邊長為3的等邊三角形。蒜頭君希望你提前告訴他能不能拼出來,免得白費功夫。輸入格式 ...

python繪製等邊三角形

定義乙個函式,引數為三角形的邊長和顏色,並呼叫該函式繪製兩個邊長顏色不同的等邊三角形 1 defdraw l,color 2import turtle 3 turtle.setup 900,600,200,200 4 turtle.penup 筆抬起5 turtle.fd 60 6 turtle.p...

CSS做等邊三角形

這裡我列舉的兩種方法 一種用邊框也就是border實現 比較簡單 另一種是用五個div旋轉溢位隱藏實現 在我們平常使用邊框的時候往往是乙個很窄的邊框,所以在視覺效果上邊框看似為直線,事實呢?給乙個小的盒子加乙個很寬的邊框,如下 可以明顯的看出其實並不是像想的那樣邊框是直線或者矩形。其實它是體型滴 下...