python turtle 畫單車兩輪

2021-08-19 05:01:37 字數 2513 閱讀 3580

draw_bicycle.py

# -*- coding: cp936 -*-

# 已知單車的車輪直徑約長60cm, 兩車輪的圓心距約為100cm, 輪胎徑長為4cm或5cm

# 用turtle 畫出單車的兩個輪子, 要求圖形幾何相似

# draw_bicycle.py 18 20

import os, sys

import turtle

import math

vlist =

def p_line(t, n, length, angle):

"""draws n line segments."""

#tt.pen(fillcolor="blue", pencolor="#fe6023", pensize=5 )

for i in range(n):

t.fd(length)

t.right(angle)

def polygon(t, n, length):

"""draws a polygon with n sides."""

angle = 360.0/n

p_line(t, n, length, angle)

def draw(tt, n, l):

global x,y

tt.seth(0)

tt.pen(fillcolor="blue", pencolor="black", pensize=1 )

start_pos = tt.pos() # start_pos

polygon(tt, n, l)

r = l/2/math.sin(math.pi/n) # 半徑

r = r*math.cos(math.pi/n) # 邊心距

# 求中心點座標(x,y)

x = l/2 + start_pos[0]

y = -r + start_pos[1]

tt.goto(start_pos) # start_pos

for pos in vlist:

tt.goto(x,y)

tt.pendown()

tt.goto(pos)

tt.penup()

print x,y

# 畫輪胎

tt.goto(x,0)

tt.pen(fillcolor="blue", pencolor="black", pensize=y)

tt.pendown()

tt.circle(y,360)

tt.penup()

# 畫輪罩

tt.goto(x,abs(y))

tt.pen(fillcolor="blue", pencolor="yellow", pensize=y)

tt.pendown()

tt.circle(-(r+2*y),90) # 正負號是畫線方向

tt.penup()

tt.goto(x,abs(y))

tt.seth(180) # 轉向180度

tt.pendown()

tt.circle(r+2*y,90) # 正負號是畫線方向

tt.penup()

print 'heading:',tt.heading()

# main

if len(sys.argv) ==3:

n = int(sys.argv[1]) # n 邊形

l = float(sys.argv[2]) # 邊長

else:

print 'usage: draw_bicycle.py int length'

sys.exit(4)

if n < 3:

print 'error: n < 3 '

sys.exit(4)

if n > 36:

print 'error: n > 36 '

sys.exit(4)

window= turtle.screen() #creat a screen

window.bgcolor("white")

tt = turtle.turtle()

tt.pencolor("black")

tt.fillcolor("violet")

tt.width(1)

tt.speed(10)

tt.pu()

s = l/10 # 幾何放大係數

x = 50*s

y = 4*s # 輪胎徑長為4cm

print 'x=',x,', y=',y

tt.goto(x,-y)

tt.pd()

draw(tt,n,l)

tt.pu()

tt.goto(-x,-y)

tt.pd()

vlist =

draw(tt,n,l)

tt.pu()

window.exitonclick()

cmd

draw_bicycle.py 18 20

桔子菌用Python turtle畫的桔子

要畫桔子,先對著個桔子看幾分鐘 看完了桔子該動手了,先畫個外形 print n 歡迎來到juzicode.com import turtle turtle.setup width 700,height 500 turtle.speed 10 畫桔子本身 turtle.penup turtle.got...

Python turtle色彩控制

turtle.pencolor args 返回或設定pencolor。允許四種輸入格式 pencolor 將當前的pencolor返回為顏色規範字串或元組 參見示例 可用作另一種顏色 pencolor fillcolor呼叫的輸入。pencolor colorstring 設定pencolor到co...

Python Turtle視窗控制

turtle.bgcolor args 引數args 顏色字串或0 colormode範圍內的三個數字或此類數字的3元組 設定或返回turtlescreen的背景顏色。screen.bgcolor orange screen.bgcolor orange screen.bgcolor 800080 ...