用Python動態地畫一個房子


用Python動態畫一個房子,我們可從上向下畫,先畫房頂,再依次畫閣樓窗戶、房屋主體、屋門及主屋窗戶。

運行本程序前請確保已安裝turtle

在這里插入圖片描述

  1 import turtle as t
  2 
  3 t.pensize(2)
  4 
  5 t.speed(1)     #設置畫畫的速率
  6 
  7 t.colormode(255)
  8 
  9 t.pencolor("black")
 10 
 11 t.begin_fill()
 12 
 13 #房頂
 14 
 15 t.fillcolor(0,245,255)
 16 
 17 for i in range(3):
 18 
 19     t.forward(240)
 20 
 21     t.left(120)
 22 
 23 t.end_fill()
 24 
 25 #房頂閣樓窗戶外框
 26 
 27 t.penup()
 28 
 29 t.goto(80,20)
 30 
 31 t.pendown()
 32 
 33 t.begin_fill()
 34 
 35 t.fillcolor("white")
 36 
 37 for i in range(4):
 38 
 39     t.forward(80)
 40 
 41     t.left(90)
 42 
 43 t.end_fill()
 44 
 45 #閣樓窗戶內部的橫線
 46 
 47 t.penup()
 48 
 49 t.goto(80,60)
 50 
 51 t.pendown()
 52 
 53 t.forward(80)
 54 
 55 #閣樓窗戶內部的豎線
 56 
 57 t.penup()
 58 
 59 t.goto(120,100)
 60 
 61 t.pendown()
 62 
 63 t.right(90)
 64 
 65 t.forward(80)
 66 
 67 t.right(90)
 68 
 69 t.forward(80)
 70 
 71 #房屋主體
 72 
 73 t.left(90)
 74 
 75 t.penup()
 76 
 77 t.goto(0,0)
 78 
 79 t.pendown()
 80 
 81 t.begin_fill()
 82 
 83 t.fillcolor(255,165,0)
 84 
 85 for i in range(2):
 86 
 87     t.forward(240)
 88 
 89     t.left(90)
 90 
 91     t.forward(240)
 92 
 93     t.left(90)
 94 
 95 t.end_fill()
 96 
 97 #屋門
 98 
 99 t.penup()
100 
101 t.goto(30,-180)
102 
103 t.pendown()
104 
105 t.begin_fill()
106 
107 t.fillcolor("blue")
108 
109 for i in range(2):
110 
111     t.forward(50)
112 
113     t.left(90)
114 
115     t.forward(100)
116 
117     t.left(90)
118 
119 t.end_fill()
120 
121 #窗框
122 
123 t.penup()
124 
125 t.goto(140,-90)
126 
127 t.pendown()
128 
129 t.begin_fill()
130 
131 t.fillcolor("white")
132 
133 for i in range(4):
134 
135     t.forward(70)
136 
137     t.left(90)
138 
139 t.end_fill()
140 
141 #窗戶上的豎線
142 
143 t.penup()
144 
145 t.goto(175,-90)
146 
147 t.pendown()
148 
149 t.left(90)
150 
151 t.forward(70)
152 
153 t.hideturtle()

有興趣的朋友可對程序進行改造,可嘗試將屋頂畫成圓的。
更多Python源代碼,歡迎微信關注Python代碼大全。
在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM