⑴ 請問一下網友老鐵們 美國國旗用python怎麼做呀 求其代碼 謝謝拉
參考下五星紅旗
<code>#!/usr/bin/env python
# -*- coding: utf-8 –*-
''' 對於turtle類的一些封裝方法,包括畫正多邊形,正多角形和五星紅旗。'''
__author__ = 'Hu Wenchao'
import turtle
import math
def draw_polygon(aTurtle, size=50, n=3):
''' 繪制正多邊形
args:
aTurtle: turtle對象實例
size: int類型,正多邊形的邊長
n: int類型,是幾邊形
'''
for i in xrange(n):
aTurtle.forward(size)
aTurtle.left(360.0/n)
def draw_n_angle(aTurtle, size=50, num=5, color=None):
''' 繪制正n角形,默認為黃色
args:
aTurtle: turtle對象實例
size: int類型,正多角形的邊長
n: int類型,是幾角形
color: str, 圖形顏色,默認不填色
'''
if color:
aTurtle.begin_fill()
aTurtle.fillcolor(color)
for i in xrange(num):
aTurtle.forward(size)
aTurtle.left(360.0/num)
aTurtle.forward(size)
aTurtle.right(2*360.0/num)
if color:
aTurtle.end_fill()
def draw_5_angle(aTurtle=None, start_pos=(0,0), end_pos=(0,10), radius=100, color=None):
''' 根據起始位置、結束位置和外接圓半徑畫五角星
args:
aTurtle: turtle對象實例
start_pos: int的二元tuple,要畫的五角星的外接圓圓心
end_pos: int的二元tuple,圓心指向的位置坐標點
radius: 五角星外接圓半徑
color: str, 圖形顏色,默認不填色
'''
aTurtle = aTurtle or turtle.Turtle()
size = radius * math.sin(math.pi/5)/math.sin(math.pi*2/5)
aTurtle.left(math.degrees(math.atan2(end_pos[1]-start_pos[1], end_pos[0]-start_pos[0])))
aTurtle.penup()
aTurtle.goto(start_pos)
aTurtle.fd(radius)
aTurtle.pendown()
aTurtle.right(math.degrees(math.pi*9/10))
draw_n_angle(aTurtle, size, 5, color)
def draw_5_star_flag(times=20.0):
''' 繪制五星紅旗
args:
times: 五星紅旗的規格為30*20, times為倍數,默認大小為10倍, 即300*200
'''
width, height = 30*times, 20*times
# 初始化屏幕和海龜
window = turtle.Screen()
aTurtle = turtle.Turtle()
aTurtle.hideturtle()
aTurtle.speed(10)
# 畫紅旗
aTurtle.penup()
aTurtle.goto(-width/2, height/2)
aTurtle.pendown()
aTurtle.begin_fill()
aTurtle.fillcolor('red')
aTurtle.fd(width)
aTurtle.right(90)
aTurtle.fd(height)
aTurtle.right(90)
aTurtle.fd(width)
aTurtle.right(90)
aTurtle.fd(height)
aTurtle.right(90)
aTurtle.end_fill()
# 畫大星星
draw_5_angle(aTurtle, start_pos=(-10*times, 5*times), end_pos=(-10*times, 8*times), radius=3*times, color='yellow')
# 畫四個小星星
stars_start_pos = [(-5, 8), (-3, 6), (-3, 3), (-5, 1)]
for pos in stars_start_pos:
draw_5_angle(aTurtle, start_pos=(pos[0]*times, pos[1]*times), end_pos=(-10*times, 5*times), radius=1*times, color='yellow')
# 點擊關閉窗口
window.exitonclick()
if __name__ == '__main__':
draw_5_star_flag()
</code>
⑵ 急求!這是一個用python畫國旗的程序,請求大神解釋一下每一步是幹嘛的
import turtle //導入模塊
import time
import os
def draw_square(org_x, org_y, x, y): //定義紅旗繪制函數
turtle.setpos(org_x, org_y) //定義畫筆初始位置
turtle.color('red', 'red') //顏色
turtle.begin_fill() //開始繪制
turtle.fd(x) //繪制偏轉方向和角度
turtle.lt(90)
turtle.fd(y)
turtle.lt(90)
turtle.fd(x)
turtle.lt(90)
turtle.fd(y)
turtle.end_fill() //繪制結束
def draw_star(center_x, center_y, radius): //定義星星繪制函數
print(center_x, center_y) //顯示位置
turtle.pencolor('black') //畫筆軌跡顏色
turtle.setpos(center_x, center_y) //中心點位置
pt1 = turtle.pos() //偏轉角度計算
turtle.circle(-radius, 360 / 5)
pt2 = turtle.pos()
turtle.circle(-radius, 360 / 5)
pt3 = turtle.pos()
turtle.circle(-radius, 360 / 5)
pt4 = turtle.pos()
turtle.circle(-radius, 360 / 5)
pt5 = turtle.pos()
turtle.color('yellow', 'yellow') //星星顏色
turtle.begin_fill() //開是繪制
turtle.goto(pt3)
turtle.goto(pt1)
turtle.goto(pt4)
turtle.goto(pt2)
turtle.goto(pt5)
turtle.end_fill() //繪制結束
print(turtle.pos())
turtle.pu() //隱藏畫筆軌跡
draw_square(-320, -260, 660, 440) //繪制紅旗
star_part_x = -320 //自定義星星大小等屬性
star_part_y = -260 + 440
star_part_s = 660 / 30
center_x, center_y = star_part_x + star_part_s * 5, star_part_y - star_part_s * 5 //計算星星中心點位置
turtle.setpos(center_x, center_y)
turtle.lt(90)
draw_star(star_part_x + star_part_s * 5, star_part_y - star_part_s * 2, star_part_s * 3) //繪制星星
turtle.goto(star_part_x + star_part_s * 10, star_part_y - star_part_s * 2) //同上
turtle.lt(round(turtle.towards(center_x, center_y)) - turtle.heading())
turtle.fd(star_part_s)
turtle.rt(90)
draw_star(turtle.xcor(), turtle.ycor(), star_part_s)
turtle.goto(star_part_x + star_part_s * 12, star_part_y - star_part_s * 4)
turtle.lt(round(turtle.towards(center_x, center_y)) - turtle.heading())
turtle.fd(star_part_s)
turtle.rt(90)
draw_star(turtle.xcor(), turtle.ycor(), star_part_s)
turtle.goto(star_part_x + star_part_s * 12, star_part_y - star_part_s * 7)
turtle.lt(round(turtle.towards(center_x, center_y)) - turtle.heading())
turtle.fd(star_part_s)
turtle.rt(90)
draw_star(turtle.xcor(), turtle.ycor(), star_part_s)
turtle.goto(star_part_x + star_part_s * 10, star_part_y - star_part_s * 9)
turtle.lt(round(turtle.towards(center_x, center_y)) - turtle.heading())
turtle.fd(star_part_s)
turtle.rt(90)
draw_star(turtle.xcor(), turtle.ycor(), star_part_s)
turtle.ht()
time.sleep(5) //設置掛起時間
os._exit(1)
⑶ 如何用python turtle畫奧運五環
首先畫第一環,用虛線畫出半徑,取該半徑的中點,然後從此中點延長虛線,且廷長到的終點是第一環的半徑長度。終點便是第二環的圓心,半徑為這條延長線。第三環同用此法。下面要說說第二行第一個環,因為上面已有兩個環啦,而且兩環的半徑之間已有一條虛線,那麼就在這條虛線做垂直平分線,然後做一個倒的等腰三角形,它的腰是圓的半徑,它的頂點是該環的圓心,第五環皆用此法。
⑷ 如何用Python畫澳大利亞國旗
把整個國旗換成直角坐標系。
在Python中繪制標准國旗並不簡單,我們採用的方法在數學上稱為解析法。把整個國旗換成直角坐標系,中心坐標為(0,0)。每個小格邊長20,則國旗左上角坐標為(-300,200)、國旗長600,高400。Turtle是小海龜繪圖庫,Math是數學庫,要用到裡面的三角函數和反三角函數,以及圓周率pi值。
Python是一種解釋型、面向對象、動態數據類型的高級程序設計語言。 Python於1989年底發明,第一個公開發行版發行於1991年。
⑸ 法國的國旗怎麼畫
材料准備:一張A4紙、一支黑大頭筆、一支紅色彩筆、一直藍色彩筆、尺子