《Python程序設計(第3版)》[美] 約翰·策勒(John Zelle) 第 4 章 答案


判斷對錯

1、利用 grAphiCs.py 可以在 Python 的 shell 窗口中繪制圖形。
2、傳統上,圖形窗口的左上角坐標為(0,0)。
3、圖形屏幕上的單個點稱為像素。
4、創建類的新實例的函數稱為取值方法。
5、實例變量用於在對象內存儲數據。
6、語句 myShApe.move(10,20) 將 myShApe 移動到點(10,20)。
7、如果兩個變量引用同一個對象,就產生了別名。
8、提供 Copy 方法是用於生成圖形對象的副本。
9、圖形窗口的標題總是“GrAphiCs WinDow”。
10、grAphiCs 庫中,用於獲取鼠標點擊的方法是 reADMouse。
解答

 1 F(是在 GrAphWin() 函數創建的窗口中繪制圖形)
 2 T(p.55 “(傳統上,圖形程序員將點定位在窗口的左上角。”)
 3 T(p.55 “圖形窗口實際上是一些小點的集合,這些小點稱為“像素”(“圖像元素”的縮寫)”)
 4 F(p.57 “要創建一個類的新實例,我們使用一個特殊操作,稱為“構造函數”,p.58 “這些方法有時被稱為“取值方法”,因為它們允許我們從對象的實例變量訪問信息。” p.76 “取值方法返回有關對象的實例變量的信息。”)
 5 T(p.57 “這些值作為“實例變量”存儲在對象內。”)
 6 F(p.58 “。所有圖形對象都有 move 方法。下面是規格說明:move(Dx,Dy):讓對象在 x 方向上移動 Dx 單位,在 y 方向上移動 Dy 單位。”)
 7 T(p.59 “……兩個變量引用同一個對象稱為“別名”……”,p.76 “兩個變量引用同一對象的情況稱為別名。”)
 8 F(p.59 “grAphiCs 庫提供了更好的解決方案,所有圖形對象都支持復制對象的 Clone 方法。”)
 9 F(p.61 “……GrAphWin 構造函數允許一個可選參數指定窗口的標題。”)
10 F(p.67 “如果在 GrAphWin 上調用 getMouse,程序將暫停,並等待用戶在圖形窗口中某處單擊鼠標。”,p.71 “getMouse() 暫停等待用戶在窗口中單擊鼠標,並用 Point 對象返回鼠標單擊的位置。”)

多項選擇
1、返回對象的實例變量的值的方法稱為____。
A. 設值方法
B. 函數
C. 構造方法
D. 取值方法

2、改變對象狀態的方法稱為____。
A. 狀態方法
B. 設值方法
C. 構造方法
D. 變更方法

3、____圖形類最適合繪制一個正方形。
A. Square
B. Polygon
C. Line
D. Rectangle

4、____命令會將 win 的坐標設置變為左下角是(0,0),右上角是(10,10)。
A. win.setCoords(Point(0,0), Point(10,10))
B. win.setCoords((0,0), (10,10))
C. win.setCoords(0, 0, 10, 10)
D. win.setCoords(Point(10,10), Point(0,0))

5、表達式____將創建從(2,3)到(4,5)的線段。
A. Line(2, 3, 4, 5)
B. Line((2,3), (4,5))
C. Line(2, 4, 3, 5)
D. Line(Point(2,3), Point(4,5))

6、命令____可以將圖形對象 shApe 繪制到圖形窗口 win 中。
A. win.draw(shape)
B. win.show(shape)
C. shape.draw()
D. shape.draw(win)

7、表達式____計算點 p1 和 p2 之間的水平距離。
A. abs(p1-p2)
B. p2.getX() - p1.getX()
C. abs(p1.getY() - p2.getY())
D. abs(p1.getX() - p2.getX())

8、對象____可以用來在圖形窗口中獲取文本輸入。
A. Text
B. Entry
C. Input
D. Keyboard

9、圍繞視覺元素和用戶動作組織的用戶界面被稱為____。
A. GUI
B. Application
C. windower
D. API

10、Color_rgb(0,255,255) 是____。
A. 黃色
B. 青色
C. 品紅色
D. 橙色

解答

 1 D
 2 B
 3 D
 4 C
 5 D
 6 D
 7 D
 8 B
 9 A
10 B

討論

  1. 選擇一個有趣的現實世界對象的例子,通過列出它的數據(屬性,它“知道什么”)及方法(行為,它可以“做什么”),將它描述為一個編程對象。

解答

冰箱
數據:容積
方法:開門()、放入()、關門()
要往冰箱里放入大象:
冰箱.開門()
冰箱.放入(大象)
冰箱.關門()

 2、用你自己的話描述 graphics 模塊的下列操作產生的每個對象,盡可能精確。務必描述各種對象的大小、位置和外觀等。如果需要,可以畫草圖。

 1 a. Point(130,130)
 2 
 3 b. c = Circle(Point(30,40),25)
 4    c.setFill("blue")
 5    c.setOutline("red")
 6 
 7 c. r = Rectangle(Point(20,20), Point(40,40))
 8    r.setFill(color_rgb(0,255,150))
 9    r.setWidth(3)
10 
11 d. l = Line(Point(100,100), Point(100,200))
12    l.setOutline("red4")
13    l.setArrow("first")
14 
15 e. Oval(Point(50,50), Point(60,100))
16 
17 f. shape = Polygon(Point(5,5), Point(10,10), Point(5,10), Point(10,5))
18    shape.setFill("orange")
19 
20 g. t = Text(Point(100,100), "Hello World!")
21    t.setFace("courier")
22    t.setSize(16)
23    t.setStyle("italic")

解答

a. 一個坐標為 (130,130) 的點
b. 一個圓心為 (30,40)、半徑為 25 像素的紅色輪廓的藍色圓圈
c. 一個邊長為 20 像素、中心點為 (30,30)、有 3 像素寬的黑色輪廓的藍綠色正方形
d. 一條底部位於點 (100,200)、長度為 100 像素的暗紅色線,其;上端有一個向上指的箭頭
e. 一個寬度為 10、高度為 50、中心為 (55,75) 的未填充的 “豎立的” 橢圓形
f. 一個以點 (7.5,7.5) 為中心、黑色輪廓的橙色沙漏
g. 一個以 (100,100) 為中心、以 16 點式斜體字形顯示的 “Hello World!” 文本

3、描述以下交互式圖形程序運行時會發生什么:

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin()
 5     shape = Circle(Point(50,50), 20)
 6     shape.setOutline("red")
 7     shape.setFill("red")
 8     shape.draw(win)
 9     for i in range(10):
10         p = win.getMouse()
11         c = shape.getCenter()
12         dx = p.getX() - c.getX()
13         dy = p.getY() - c.getY()
14         shape.move(dx,dy)
15     win.close()
16 main()

解答

程序在默認圖形窗口中繪制一個圓心在 (50,50)、半徑為 20 像素的紅色圓圈,然后等待用戶單擊鼠標 10 次。每次用戶點擊時,圓圈都會移動到點擊的點(以其為圓心)。點擊 10 次后,窗口消失。

編程練習

1、修改上一個討論問題的程序,做到:
  a.使它繪制正方形而不是圓。
  b.每次連續點擊在屏幕上繪制一個額外的方塊(而不是移動已有的方塊)。

解答

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin()
 5     shape = Rectangle(Point(30,30), Point(70,70))
 6     shape.setOutline("red")
 7     shape.setFill("red")
 8     shape.draw(win)
 9     for _ in range(10):
10         p = win.getMouse()
11         c = shape.getCenter()
12         dx = p.getX() - c.getX()
13         dy = p.getY() - c.getY()
14         shape = shape.clone()
15         shape.move(dx,dy)
16         shape.draw(win)
17     win.close()
18 
19 main()

2、箭靶的中心圓為黃色,圍繞着紅色、藍色、黑色和白色的同心環。每個環具有相同
的寬度,與黃色圓的半徑相同。編寫一個繪制這種箭靶的程序。(提示:稍后繪制的對象將
出現在先前繪制的對象的上面。)

解答

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin("Archery Target", 360, 360)
 5     win.setCoords(-6, -6, 6, 6)
 6     win.setBackground("gray")
 7     center = Point(0,0)
 8 
 9     c1 = Circle(center, 5)
10     c1.setFill("white")
11     c1.draw(win)
12 
13     c2 = Circle(center, 4)
14     c2.setFill("black")
15     c2.draw(win)
16 
17     c3 = Circle(center, 3)
18     c3.setFill("blue")
19     c3.draw(win)
20 
21     c4 = Circle(center, 2)
22     c4.setFill("red")
23     c4.draw(win)
24 
25     c5 = Circle(center, 1)
26     c5.setFill("yellow")
27     c5.draw(win)
28 
29     win.getMouse()
30     win.close()
31 
32 main()

  1. 編寫一個繪制某種面孔的程序。

解答

  1 # Author: Cody Leistikow (10/4/02)
  2 
  3 from graphics import *
  4 
  5 def main():
  6     win=GraphWin('Face',400,420)
  7     win.setBackground("white")
  8     neck = Polygon(Point(177,352),Point(176,371),Point(162,398),Point(170,400),Point(185,391),Point(231,373),Point(260,368),Point(287,357),Point(312,333),Point(235,311))
  9     neck.setFill(color_rgb(239,222,215))
 10     head = Polygon(Point(136,228),Point(107,260),Point(110,279),Point(129,302),Point(140,321),Point(175,350),Point(193,348),Point(234,312),Point(238,296),Point(255,291),Point(272,266),Point(271,240),Point(264,226),Point(253,224),Point(239,239),Point(243,193))
 11     head.setFill(color_rgb(239,222,215))
 12     headdark1 = Polygon(Point(184,212),Point(190,217),Point(194,261),Point(173,286),Point(198,284),Point(204,310),Point(203,321),Point(200,333),Point(192,346),Point(202,352),Point(206,351),Point(206,372),Point(225,339),Point(218,377),Point(236,371),Point(252,344),Point(258,342),Point(257,369),Point(286,358),Point(294,339),Point(300,343),Point(312,334),Point(295,319),Point(282,313),Point(276,292),Point(258,289),Point(242,296),Point(236,291),Point(240,284),Point(242,278),Point(247,278),Point(249,268),Point(240,265),Point(242,258),Point(234,261),Point(258,185))
 13     headdark1.setFill(color_rgb(226,179,154))
 14     headdark2 = Polygon(Point(133,231),Point(123,241),Point(112,273),Point(117,275),Point(148,227))
 15     headdark2.setFill(color_rgb(226,179,154))
 16     headdark3 = Polygon(Point(177,218),Point(161,279),Point(168,221))
 17     headdark3.setFill(color_rgb(226,179,154))
 18     neckline1 = Line(Point(182,365),Point(185,390))
 19     neckline2 = Line(Point(199,385),Point(219,350))
 20     neckline3 = Line(Point(219,350),Point(230,324))
 21     neckline4 = Line(Point(225,374),Point(235,340))
 22     neckline5 = Line(Point(282,313),Point(288,334))
 23     throat = Polygon(Point(189,366),Point(192,381),Point(186,373))
 24     throat.setFill(color_rgb(117,110,105))
 25     headline1 = Line(Point(193,349),Point(212,334))
 26     headline2 = Line(Point(212,334),Point(234,312))
 27     headline3 = Line(Point(234,312),Point(241,294))
 28     headline4 = Line(Point(135,279),Point(140,292))
 29     headline5 = Line(Point(149,279),Point(154,289))
 30     innerear = Polygon(Point(263,249),Point(262,240),Point(257,234),Point(241,259),Point(242,267),Point(248,269),Point(249,277),Point(242,279),Point(241,283),Point(260,271),Point(263,265),Point(253,260),Point(254,251))
 31     innerear.setFill(color_rgb(201,153,145))
 32     earline1 = Line(Point(247,249),Point(258,249))
 33     earline2 = Line(Point(248,268),Point(255,264))
 34     earline3 = Line(Point(241,261),Point(255,261))
 35     earline4 = Line(Point(255,261),Point(257,272))
 36     earline5 = Line(Point(257,272),Point(265,264))
 37     chin = Polygon(Point(161,326),Point(176,327),Point(179,330),Point(165,330))
 38     chin.setFill(color_rgb(215,178,182))
 39     mouthline1 = Line(Point(154,320),Point(158,322))
 40     mouthline2 = Line(Point(158,322),Point(171,318))
 41     mouthline3 = Line(Point(171,318),Point(179,318))
 42     noseline1 = Line(Point(146,303),Point(146,311))
 43     noseline2 = Line(Point(146,311),Point(141,319))
 44     noseline3 = Line(Point(141,319),Point(156,312))
 45     lefteye = Polygon(Point(120,282),Point(140,293),Point(128,293))
 46     lefteye.setFill(color_rgb(255,255,255))
 47     righteye = Polygon(Point(163,286),Point(166,289),Point(197,277),Point(199,264))
 48     righteye.setFill(color_rgb(255,255,255))
 49     leftiris = Polygon(Point(128,287),Point(131,291),Point(137,290))
 50     leftiris.setFill(color_rgb(100,150,131))
 51     rightiris = Polygon(Point(168,282),Point(172,284),Point(179,275))
 52     rightiris.setFill(color_rgb(100,150,131))
 53     lefteyebrow = Polygon(Point(111,272),Point(140,290),Point(140,292),Point(111,276))
 54     lefteyebrow.setFill(color_rgb(247,218,116))
 55     righteyebrow = Polygon(Point(156,288),Point(209,251),Point(212,258),Point(157,288))
 56     righteyebrow.setFill(color_rgb(247,218,116))    
 57     hairbg = Polygon(Point(49,110),Point(74,155),Point(89,103),Point(117,43),Point(127,96),Point(144,65),Point(198,21),Point(180,70),Point(230,32),Point(276,13),Point(241,77),Point(335,34),Point(310,74),Point(323,71),Point(331,65),Point(332,68),Point(363,45),Point(340,136),Point(356,125),Point(350,144),Point(381,124),Point(337,222),Point(309,240),Point(359,249),Point(294,276),Point(334,286),Point(275,297),Point(258,287),Point(272,264),Point(268,233),Point(264,225),Point(252,225),Point(239,238),Point(226,230),Point(228,219),Point(197,260),Point(210,221),Point(210,205),Point(175,217),Point(166,248),Point(142,294),Point(162,221),Point(136,230),Point(106,261),Point(110,277),Point(114,289),Point(116,317),Point(93,288),Point(94,278),Point(75,318),Point(76,274),Point(62,273),Point(69,247),Point(18,253),Point(67,215),Point(53,191))
 58     hairbg.setFill(color_rgb(247,218,116))
 59     hairdark1 = Polygon(Point(50,114),Point(74,156),Point(71,177))
 60     hairdark1.setFill(color_rgb(235,161,56))
 61     hairdark2 = Polygon(Point(47,243),Point(93,225),Point(89,239))
 62     hairdark2.setFill(color_rgb(235,161,56))
 63     hairdark3 = Polygon(Point(128,214),Point(94,256),Point(75,317),Point(99,270))
 64     hairdark3.setFill(color_rgb(235,161,56))
 65     hairdark4 = Polygon(Point(105,266),Point(116,313),Point(114,283))
 66     hairdark4.setFill(color_rgb(235,161,56))
 67     hairdark5 = Polygon(Point(119,45),Point(107,106),Point(112,170),Point(124,203),Point(129,200),Point(130,164),Point(121,139),Point(125,91),Point(117,43))
 68     hairdark5.setFill(color_rgb(235,161,56))
 69     hairdark6 = Polygon(Point(173,126),Point(145,193),Point(157,201),Point(157,216),Point(169,204),Point(186,176),Point(201,161),Point(213,122),Point(223,79),Point(234,55),Point(204,109),Point(160,186),Point(155,187),Point(179,117))
 70     hairdark6.setFill(color_rgb(235,161,56))
 71     hairdark7 = Polygon(Point(176,217),Point(253,115),Point(225,194),Point(212,192),Point(203,207))
 72     hairdark7.setFill(color_rgb(235,161,56))
 73     hairdark8 = Polygon(Point(222,86),Point(272,19),Point(240,77))
 74     hairdark8.setFill(color_rgb(235,161,56))
 75     hairdark9 = Polygon(Point(217,111),Point(284,65),Point(209,138))
 76     hairdark9.setFill(color_rgb(235,161,56))
 77     hairdark10 = Polygon(Point(217,192),Point(219,230),Point(230,216),Point(224,193))
 78     hairdark10.setFill(color_rgb(235,161,56))
 79     hairdark11 = Polygon(Point(216,109),Point(252,84),Point(283,66),Point(208,141))
 80     hairdark11.setFill(color_rgb(235,161,56))
 81     hairdark12 = Polygon(Point(238,161),Point(274,140),Point(228,185))
 82     hairdark12.setFill(color_rgb(235,161,56))
 83     hairdark13 = Polygon(Point(248,144),Point(298,73),Point(335,33),Point(307,77),Point(303,97))
 84     hairdark13.setFill(color_rgb(235,161,56))
 85     hairdark14 = Polygon(Point(249,205),Point(300,142),Point(331,67),Point(321,152))
 86     hairdark14.setFill(color_rgb(235,161,56))
 87     hairdark15 = Polygon(Point(321,152),Point(324,137),Point(357,65),Point(340,135))
 88     hairdark15.setFill(color_rgb(235,161,56))
 89     hairdark16 = Polygon(Point(142,138),Point(160,77),Point(180,48),Point(166,87),Point(166,114))
 90     hairdark16.setFill(color_rgb(235,161,56))
 91     hairdark17 = Polygon(Point(226,198),Point(252,183),Point(242,203),Point(246,216),Point(301,184),Point(282,219),Point(331,174),Point(349,144),Point(322,210),Point(358,174),Point(338,221),Point(308,240),Point(294,239),Point(283,256),Point(357,250),Point(276,282),Point(331,286),Point(276,296),Point(258,287),Point(273,265),Point(270,239),Point(265,224),Point(254,224),Point(240,238),Point(227,231),Point(228,220),Point(227,207))
 92     hairdark17.setFill(color_rgb(235,161,56))
 93     hairline1 = Line(Point(72,176),Point(84,206))
 94     hairline2 = Line(Point(68,215),Point(84,206))
 95     hairline3 = Line(Point(84,206),Point(109,208))
 96     hairline4 = Line(Point(70,246),Point(86,246))
 97     hairline5 = Line(Point(98,214),Point(77,276))
 98     hairline6 = Line(Point(98,214),Point(146,194))
 99     hairline7 = Line(Point(128,196),Point(133,147))
100     hairline8 = Line(Point(133,147),Point(207,72))
101     hairline9 = Line(Point(207,72),Point(235,53))
102     hairline10 = Line(Point(137,230),Point(158,215))
103     hairline11 = Line(Point(127,95),Point(141,140))
104     hairline12 = Line(Point(180,70),Point(174,104))
105     hairline13 = Line(Point(22,252),Point(94,225))
106     hairline14 = Line(Point(202,160),Point(254,113))
107     hairline15 = Line(Point(173,125),Point(210,79))
108     hairline16 = Line(Point(175,217),Point(171,205))
109     hairline17 = Line(Point(171,205),Point(165,208))
110     hairline18 = Line(Point(305,96),Point(332,64))
111     hairline19 = Line(Point(275,139),Point(315,94))
112     hairline20 = Line(Point(98,112),Point(91,174))
113     hairline21 = Line(Point(91,174),Point(99,208))
114     hairline22 = Line(Point(128,214),Point(156,201))
115     hairline23 = Line(Point(132,113),Point(163,50))
116     hairline24 = Line(Point(185,92),Point(240,32))
117     hairline25 = Line(Point(271,245),Point(295,233))
118     hairline26 = Line(Point(295,233),Point(322,209))
119     hairline27 = Line(Point(227,190),Point(286,139))
120     hairline28 = Line(Point(302,183),Point(326,164))
121 
122     neck.draw(win)
123     head.draw(win)
124     headdark1.draw(win)
125     headdark2.draw(win)
126     headdark3.draw(win)
127     headline1.draw(win)
128     headline2.draw(win)
129     headline3.draw(win)
130     headline4.draw(win)
131     headline5.draw(win)
132     neckline1.draw(win)
133     neckline2.draw(win)
134     neckline3.draw(win)
135     neckline4.draw(win)
136     neckline5.draw(win)
137     throat.draw(win)
138     innerear.draw(win)
139     earline1.draw(win)
140     earline2.draw(win)
141     earline3.draw(win)
142     earline4.draw(win)
143     earline5.draw(win)
144     chin.draw(win)
145     mouthline1.draw(win)
146     mouthline2.draw(win)
147     mouthline3.draw(win)
148     noseline1.draw(win)
149     noseline2.draw(win)
150     noseline3.draw(win)
151     lefteye.draw(win)
152     righteye.draw(win)
153     leftiris.draw(win)
154     rightiris.draw(win)
155     lefteyebrow.draw(win)
156     righteyebrow.draw(win)
157     hairbg.draw(win)
158     hairdark1.draw(win)
159     hairdark2.draw(win)
160     hairdark3.draw(win)
161     hairdark4.draw(win)
162     hairdark5.draw(win)
163     hairdark6.draw(win)
164     hairdark7.draw(win)
165     hairdark8.draw(win)
166     hairdark9.draw(win)
167     hairdark10.draw(win)
168     hairdark11.draw(win)
169     hairdark12.draw(win)
170     hairdark13.draw(win)
171     hairdark14.draw(win)
172     hairdark15.draw(win)
173     hairdark16.draw(win)
174     hairdark17.draw(win)
175     hairline1.draw(win)
176     hairline2.draw(win)
177     hairline3.draw(win)
178     hairline4.draw(win)
179     hairline5.draw(win)
180     hairline6.draw(win)
181     hairline7.draw(win)
182     hairline8.draw(win)
183     hairline9.draw(win)
184     hairline10.draw(win)
185     hairline11.draw(win)
186     hairline12.draw(win)
187     hairline13.draw(win)
188     hairline14.draw(win)
189     hairline15.draw(win)
190     hairline16.draw(win)
191     hairline17.draw(win)
192     hairline18.draw(win)
193     hairline19.draw(win)
194     hairline20.draw(win)
195     hairline21.draw(win)
196     hairline22.draw(win)
197     hairline23.draw(win)
198     hairline24.draw(win)
199     hairline25.draw(win)
200     hairline26.draw(win)
201     hairline27.draw(win)
202     hairline28.draw(win)
203     win.getMouse()
204     
205 main()

4、編寫一個用聖誕樹和雪人繪制冬季場景的程序。

解答

  1 # By: Megan Neuendorf
  2 
  3 from graphics import *
  4 
  5 def main():
  6     win=GraphWin("Winter Scene",500,500)
  7     win.setCoords(0,0,200,200)
  8     win.setBackground("blue")
  9 
 10     # Body of the snowman
 11     c1 = Circle(Point(50,40),40)
 12     c1.draw(win)
 13     c1.setFill("white")
 14     c1.setOutline("white")
 15 
 16     c2 = Circle(Point(50,100),30)
 17     c2.draw(win)
 18     c2.setFill("white")
 19     c2.setOutline("white")
 20 
 21     c3 = Circle(Point(50,145),20)
 22     c3.draw(win)
 23     c3.setFill("white")
 24     c3.setOutline("white")
 25 
 26     # Top Hat of Snowman
 27     r1 = Rectangle(Point(30,160),Point(70,165))
 28     r1.draw(win)
 29     r1.setFill("black")
 30 
 31     r2 = Rectangle(Point(40,165),Point(60,185))
 32     r2.draw(win)
 33     r2.setFill("black")
 34 
 35     # Eyes of the Snowman
 36     e1 = Circle(Point(42.5,150),2.5)
 37     e1.draw(win)
 38     e1.setFill("black")
 39 
 40     e2 = Circle(Point(57.5,150),2.5)
 41     e2.draw(win)
 42     e2.setFill("black")
 43 
 44     # Nose of the Snowman
 45     n = Polygon(Point(50,142.5),Point(50,137.5),Point(57.5,140))
 46     n.draw(win)
 47     n.setOutline("orange")
 48     n.setFill("orange")
 49 
 50     # Mouth of the Snowman
 51     m1 = Circle(Point(40,135),1)
 52     m1.draw(win)
 53     m1.setFill("black")
 54 
 55     m2 = Circle(Point(45,130),1)
 56     m2.draw(win)
 57     m2.setFill("black")
 58 
 59     m3 = Circle(Point(50,127.5),1)
 60     m3.draw(win)
 61     m3.setFill("black")
 62 
 63     m4 = Circle(Point(55,130),1)
 64     m4.draw(win)
 65     m4.setFill("black")
 66 
 67     m5 = Circle(Point(60,135),1)
 68     m5.draw(win)
 69     m5.setFill("black")
 70 
 71     # Buttons on the Snowman
 72     b1 = Circle(Point(50,115),3)
 73     b1.draw(win)
 74     b1.setFill("black")
 75 
 76     b2 = Circle(Point(50,105),3)
 77     b2.draw(win)
 78     b2.setFill("black")
 79 
 80     b3 = Circle(Point(50,95),3)
 81     b3.draw(win)
 82     b3.setFill("black")
 83 
 84     # Christmas Tree
 85     rect1 = Rectangle(Point(140,0),Point(160,25))
 86     rect1.draw(win)
 87     rect1.setOutline("brown")
 88     rect1.setFill("brown")
 89     
 90     t1 = Polygon(Point(100,25),Point(200,25),Point(150,65))
 91     t1.draw(win)
 92     t1.setOutline("forest green")
 93     t1.setFill("forest green")
 94 
 95     t2 = Polygon(Point(110,60),Point(190,60),Point(150,100))
 96     t2.draw(win)
 97     t2.setOutline("forest green")
 98     t2.setFill("forest green")
 99 
100     t3 = Polygon(Point(120,90),Point(180,90),Point(150,120))
101     t3.draw(win)
102     t3.setOutline("forest green")
103     t3.setFill("forest green")
104 
105     t4 = Polygon(Point(130,115),Point(170,115),Point(150,135))
106     t4.draw(win)
107     t4.setOutline("forest green")
108     t4.setFill("forest green")
109 
110     t5 = Polygon(Point(135,132.5),Point(165,132.5),Point(150,155))
111     t5.draw(win)
112     t5.setOutline("forest green")
113     t5.setFill("forest green")
114 
115     # Star on the Christmas Tree
116     n = Polygon(Point(150,152.5),Point(147.5,160),Point(140,162.5),Point(147.5,165),Point(150,172.5),Point(152.5,165),Point(160,162.5),Point(152.5,160))
117     n.draw(win)
118     n.setOutline("gold")
119     n.setFill("gold")
120 
121     # Circular Ornaments on Christmas Tree
122     o1 = Circle(Point(150,142.5),2.5)
123     o1.draw(win)
124     o1.setOutline("red")
125     o1.setFill("gold")
126 
127     o2 = Circle(Point(135,115),2.5)
128     o2.draw(win)
129     o2.setOutline("red")
130     o2.setFill("gold")
131 
132     o3 = Circle(Point(165,115),2.5)
133     o3.draw(win)
134     o3.setOutline("red")
135     o3.setFill("gold")
136 
137     o4 = Circle(Point(150,95),2.5)
138     o4.draw(win)
139     o4.setOutline("red")
140     o4.setFill("gold")
141 
142     o5 = Circle(Point(135,75),2.5)
143     o5.draw(win)
144     o5.setOutline("red")
145     o5.setFill("gold")
146 
147     o6 = Circle(Point(165,75),2.5)
148     o6.draw(win)
149     o6.setOutline("red")
150     o6.setFill("gold")
151 
152     o7 = Circle(Point(115,60),2.5)
153     o7.draw(win)
154     o7.setOutline("red")
155     o7.setFill("gold")
156 
157     o8 = Circle(Point(185,60),2.5)
158     o8.draw(win)
159     o8.setOutline("red")
160     o8.setFill("gold")
161 
162     o9 = Circle(Point(150,30),2.5)
163     o9.draw(win)
164     o9.setOutline("red")
165     o9.setFill("gold")
166 
167     # Diamond Ornaments on Christmas Tree
168     d1 = Polygon(Point(140,135),Point(142.5,132.5),Point(140,130),Point(137.5,132.5))
169     d1.draw(win)
170     d1.setOutline("gold")
171     d1.setFill("red")
172 
173     d2 = Polygon(Point(160,135),Point(162.5,132.5),Point(160,130),Point(157.5,132.5))
174     d2.draw(win)
175     d2.setOutline("gold")
176     d2.setFill("red")
177 
178     d3 = Polygon(Point(150,122.5),Point(152.5,120),Point(150,117.5),Point(147.5,120))
179     d3.draw(win)
180     d3.setOutline("gold")
181     d3.setFill("red")
182 
183     d4 = Polygon(Point(125,92.5),Point(127.5,90),Point(125,87.5),Point(122.5,90))
184     d4.draw(win)
185     d4.setOutline("gold")
186     d4.setFill("red")
187 
188     d5 = Polygon(Point(175,92.5),Point(177.5,90),Point(175,87.5),Point(172.5,90))
189     d5.draw(win)
190     d5.setOutline("gold")
191     d5.setFill("red")
192 
193     d6 = Polygon(Point(150,67.5),Point(152.5,65),Point(150,62.5),Point(147.5,65))
194     d6.draw(win)
195     d6.setOutline("gold")
196     d6.setFill("red")
197 
198     d7 = Polygon(Point(130,47.5),Point(132.5,45),Point(130,42.5),Point(127.5,45))
199     d7.draw(win)
200     d7.setOutline("gold")
201     d7.setFill("red")
202 
203     d8 = Polygon(Point(170,47.5),Point(172.5,45),Point(170,42.5),Point(167.5,45))
204     d8.draw(win)
205     d8.setOutline("gold")
206     d8.setFill("red")
207 
208     d9 = Polygon(Point(105,27.5),Point(107.5,25),Point(105,22.5),Point(102.5,25))
209     d9.draw(win)
210     d9.setOutline("gold")
211     d9.setFill("red")
212 
213     d10 = Polygon(Point(195,27.5),Point(197.5,25),Point(195,22.5),Point(192.5,25))
214     d10.draw(win)
215     d10.setOutline("gold")
216     d10.setFill("red")
217 
218     win.getMouse()
219     win.close()
220     
221 main()

5、編寫一個程序,在屏幕上繪制5 個骰子,是一把順子(1,2,3,4,5 或2,3,4,5,6)。

解答

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin("Dice", width=800, height=400)
 5     win.setCoords(-10, -5, 10, 5)
 6 
 7     d1 = Rectangle(Point(-9, -1), Point(-7, 1))
 8     d1.draw(win)
 9     c = Circle(Point(-8, 0), 0.3)
10     c.setFill("red")
11     c.setOutline("white")
12     c.draw(win)
13     
14     d2 = d1.clone()
15     d2.move(4, 0)
16     d2.draw(win)
17     c = Circle(Point(-4.3, 0.3), 0.25)
18     c.setFill("blue")
19     c.setOutline("white")
20     c.draw(win)
21     c = c.clone()
22     c.move(0.6, -0.6)
23     c.draw(win)
24     
25     d3 = d2.clone()
26     d3.move(4, 0)
27     d3.draw(win)
28 
29     c = Circle(Point(-0.5, 0.5), 0.25)
30     c.setFill("blue")
31     c.setOutline("white")
32     c.draw(win)
33     c = c.clone()
34     c.move(0.5, -0.5)
35     c.draw(win)
36     c = c.clone()
37     c.move(0.5, -0.5)
38     c.draw(win)
39 
40     d4 = d3.clone()
41     d4.move(4, 0)
42     d4.draw(win)
43     c = Circle(Point(3.6, 0.4), 0.25)
44     c.setFill("red")
45     c.setOutline("white")
46     c.draw(win)
47     c = c.clone()
48     c.move(0.8, 0)
49     c.draw(win)
50     c = c.clone()
51     c.move(0, -0.8)
52     c.draw(win)
53     c = c.clone()
54     c.move(-0.8, 0)
55     c.draw(win)
56 
57     d5 = d4.clone()
58     d5.move(4, 0)
59     d5.draw(win)
60     c = Circle(Point(7.5, 0.5), 0.25)
61     c.setFill("blue")
62     c.setOutline("white")
63     c.draw(win)
64     c = c.clone()
65     c.move(1, 0)
66     c.draw(win)
67     c = c.clone()
68     c.move(0, -1)
69     c.draw(win)
70     c = c.clone()
71     c.move(-1, 0)
72     c.draw(win)
73     c = c.clone()
74     c.move(0.5, 0.5)
75     c.draw(win)
76 
77     win.getMouse()
78     win.close()
79 
80 main()

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin("Dice", width=800, height=400)
 5     win.setCoords(-10, -5, 10, 5)
 6 
 7     d2 = Rectangle(Point(-9, -1), Point(-7, 1))
 8     d2.draw(win)
 9     c = Circle(Point(-8.3, 0.3), 0.25)
10     c.setFill("blue")
11     c.setOutline(color_rgb(240, 240, 240))
12     c.draw(win)
13     c = c.clone()
14     c.move(0.6, -0.6)
15     c.draw(win)
16     
17     d3 = d2.clone()
18     d3.move(4, 0)
19     d3.draw(win)
20     c = Circle(Point(-4.5, 0.5), 0.25)
21     c.setFill("blue")
22     c.setOutline(color_rgb(240, 240, 240))
23     c.draw(win)
24     c = c.clone()
25     c.move(0.5, -0.5)
26     c.draw(win)
27     c = c.clone()
28     c.move(0.5, -0.5)
29     c.draw(win)
30 
31     d4 = d3.clone()
32     d4.move(4, 0)
33     d4.draw(win)
34     c = Circle(Point(-0.4, 0.4), 0.25)
35     c.setFill("red")
36     c.setOutline(color_rgb(240, 240, 240))
37     c.draw(win)
38     c = c.clone()
39     c.move(0.8, 0)
40     c.draw(win)
41     c = c.clone()
42     c.move(0, -0.8)
43     c.draw(win)
44     c = c.clone()
45     c.move(-0.8, 0)
46     c.draw(win)
47 
48     d5 = d4.clone()
49     d5.move(4, 0)
50     d5.draw(win)
51     c = Circle(Point(3.5, 0.5), 0.25)
52     c.setFill("blue")
53     c.setOutline(color_rgb(240, 240, 240))
54     c.draw(win)
55     c = c.clone()
56     c.move(1, 0)
57     c.draw(win)
58     c = c.clone()
59     c.move(0, -1)
60     c.draw(win)
61     c = c.clone()
62     c.move(-1, 0)
63     c.draw(win)
64     c = c.clone()
65     c.move(0.5, 0.5)
66     c.draw(win)
67 
68     d6 = d5.clone()
69     d6.move(4, 0)
70     d6.draw(win)
71     c = Circle(Point(7.6, 0.6), 0.25)
72     c.setFill("blue")
73     c.setOutline(color_rgb(240, 240, 240))
74     c.draw(win)
75     c = c.clone()
76     c.move(0.8, 0)
77     c.draw(win)
78     c = c.clone()
79     c.move(0, -0.6)
80     c.draw(win)
81     c = c.clone()
82     c.move(-0.8, 0)
83     c.draw(win)
84     c = c.clone()
85     c.move(0, -0.6)
86     c.draw(win)
87     c = c.clone()
88     c.move(0.8, 0)
89     c.draw(win)
90 
91     win.getMouse()
92     win.close()
93 
94 main()

6、修改圖形終值程序,讓輸入(本金和APR)也用Entry 對象以圖形方式完成。

解答

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin("Investment Growth Chart", 640, 480)
 5     win.setBackground("white")
 6     # Set coordinates for easy display of prompts.
 7     win.setCoords(0,0,10,10)
 8 
 9     # Display prompts
10     t1 = Text(Point(5,8), "Plotting a 10 year investment")
11     t1.setSize(14)
12     t1.draw(win)
13 
14     t2 = Text(Point(5,7.5), "Enter the information below and then click anywhere")
15     t2.setSize(14)
16     t2.draw(win)
17 
18     t3 = Text(Point(2,6), "Initial Principal:")
19     t3.setSize(14)
20     t3.draw(win)
21 
22     prinBox = Entry(Point(4.5,6), 6)
23     prinBox.draw(win)
24     prinBox.setText("2000")
25 
26     t4 = Text(Point(2,4), "Annual Interest Rate:")
27     t4.setSize(14)
28     t4.draw(win)
29 
30     aprBox = Entry(Point(4.5,4), 6)
31     aprBox.setText("0.05")
32     aprBox.draw(win)
33 
34     # wait for mouse click and get values
35     win.getMouse()
36     principal = float(prinBox.getText())
37     apr = float(aprBox.getText())
38 
39     # Erase the prompts
40     t1.undraw()
41     t2.undraw()
42     t3.undraw()
43     t4.undraw()
44     prinBox.undraw()
45     aprBox.undraw()
46 
47 
48     # Set Window coords for drawing the graph.
49     win.setCoords(-1.75,-200, 11.5, 10400)
50 
51     # Create a graphics window with labels on left edge
52     Text(Point(-1, 0), ' 0.0K').draw(win)
53     Text(Point(-1, 2500), ' 2.5K').draw(win)
54     Text(Point(-1, 5000), ' 5.0K').draw(win)
55     Text(Point(-1, 7500), ' 7.5k').draw(win)
56     Text(Point(-1, 10000), '10.0K').draw(win)
57 
58     # Draw bar for initial principal
59     bar = Rectangle(Point(0, 0), Point(1, principal))
60     bar.setFill("green")
61     bar.setWidth(2)
62     bar.draw(win)
63     
64     # Draw a bar for each subsequent year
65     for year in range(1, 11):
66         principal = principal * (1 + apr)
67         bar = Rectangle(Point(year, 0), Point(year+1, principal))
68         bar.setFill("green")
69         bar.setWidth(2)
70         bar.draw(win)
71 
72     win.getMouse()
73     win.close()
74 
75 main()

7、圓的交點。
編寫一個計算圓與水平線的交點的程序,並以文本和圖形方式顯示信息。
輸入:圓的半徑和線的y 截距。
輸出:在坐標為從(−10,−10)到(10,10)的窗口中,以(0, 0)為中心,以給定半徑繪制
的圓。
用給定的y 軸截取一根水平線穿過窗口。
以紅色繪制兩個交點。
打印出交叉點的x 值。
公式:

解答

 1 from graphics import *
 2 import math
 3 
 4 def main():
 5     radius = float(input("Please enter the radius of the circle: "))
 6     yinter = float(input("Please enter the y-intercept of the line: "))
 7 
 8     win = GraphWin("Circle Intersection")
 9     win.setCoords(-10, -10, 10, 10)
10 
11     Circle(Point(0, 0), radius).draw(win)
12     Line(Point(-10, yinter), Point(10, yinter)).draw(win)
13 
14     x = math.sqrt(radius * radius - yinter * yinter)
15     print("X values of intersection", -x, x)
16 
17     p1 = Circle(Point(x, yinter), 0.25)
18     p1.setOutline("red")
19     p1.setFill("red")
20     p1.draw(win)
21 
22     p2 = p1.clone()
23     p2.move(-2 * x, 0)
24     p2.draw(win)
25 
26     win.getMouse()
27     win.close()
28 
29 main()
30 
31 # Output:
32 # Please enter the radius of the circle: 5
33 # Please enter the y-intercept of the line: 3
34 # X values of intersection -4.0 4.0

8、線段信息。
該程序允許用戶繪制線段,然后顯示關於線段的一些圖形和文本信息。
輸入:兩次鼠標點擊線段的終點。
輸出:以青色繪制線段的中點。
繪制線段。
打印線的長度和斜率。
公式:

解答

 1 from graphics import *
 2 import math
 3 
 4 def main():
 5     win = GraphWin("Triangle", 400, 400)
 6     win.setCoords(-5, -5, 5, 5)
 7 
 8     draw_coord(win)    # 這個函數定義見文章末尾
 9 
10     pt1 = win.getMouse()
11     pt1.draw(win)
12     pt2 = win.getMouse()
13     pt2.draw(win)
14     line = Line(pt1, pt2)
15     line.draw(win)
16     mark = Circle(line.getCenter(),0.075)
17     mark.setFill("cyan")
18     mark.draw(win)
19 
20     dx = pt2.getX() - pt1.getX()
21     dy = pt2.getY() - pt1.getY()
22     slope = dy / dx
23     length = math.sqrt(dx * dx + dy * dy)
24     
25     message = Text(Point(0, -4), "Point1:({}, {})\nPoint2:({}, {})\n長度:{}\n斜率:{}".format(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY(), length, slope))
26     message.setSize(8)
27     message.setTextColor(color_rgb(144, 144, 144))
28     message.draw(win)
29 
30     win.getMouse()
31     win.close()
32 
33 main()

9、矩形信息。

此程序顯示有關用戶繪制的矩形的信息。
輸入:兩次鼠標點擊作為矩形的對角。
輸出:繪制矩形。
打印矩形的周長和面積。
公式:面積=(長度)(寬度)
周長= 2(長度+寬度)

解答

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin("Rectangle", 400, 400)
 5     win.setCoords(-5, -5, 5, 5)
 6 
 7     draw_coord(win)
 8 
 9     pt1 = win.getMouse()
10     pt2 = win.getMouse()
11     rect = Rectangle(pt1, pt2)
12     rect.draw(win)
13 
14     height = abs(pt2.getY() - pt1.getY())
15     length = abs(pt2.getX() - pt1.getX())
16 
17     area = height * length
18     perimeter = 2 * (height + length)
19 
20     message = Text(Point(0, -4), "Point1:({}, {})\nPoint2:({}, {})\n面積:{}\n周長:{}".format(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY(), area, perimeter))
21     message.setSize(8)
22     message.setTextColor(color_rgb(144, 144, 144))
23     message.draw(win)
24 
25     win.getMouse()
26     win.close()
27 
28 main()

10、三角形信息。
與上一個問題相同,但三角形的頂點有三次點擊。
公式:關於周長,可參閱線段問題中的長度。

解答

 1 from graphics import *
 2 import math
 3 
 4 def main():
 5     win = GraphWin("Triangle", 400, 400)
 6     win.setCoords(-5, -5, 5, 5)
 7 
 8     draw_coord(win)
 9 
10     pt1 = win.getMouse()
11     pt1.draw(win)
12     pt2 = win.getMouse()
13     pt1.undraw()
14     line = Line(pt1, pt2)
15     line.draw(win)
16     pt3 = win.getMouse()
17     line.undraw()
18     tri = Polygon(pt1, pt2, pt3)
19     tri.draw(win)
20     
21     dx1 = pt1.getX() - pt2.getX()
22     dy1 = pt1.getY() - pt2.getY()
23     dx2 = pt2.getX() - pt3.getX()
24     dy2 = pt2.getY() - pt3.getY()
25     dx3 = pt3.getX() - pt1.getX()
26     dy3 = pt3.getY() - pt1.getY()
27     
28     a = math.sqrt(dx1 * dx1 + dy1 * dy1)
29     b = math.sqrt(dx2 * dx2 + dy2 * dy2)
30     c = math.sqrt(dx3 * dx3 + dy3 * dy3)
31     
32     perimeter = a + b + c
33     s = perimeter / 2
34     area = math.sqrt(s * (s - a) * (s - b) * (s - c))
35 
36     message = Text(Point(0, -4), "Point1:({}, {})\nPoint2:({}, {})\nPoint3:({}, {})\n面積:{}\n周長:{}".format(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY(), pt3.getX(), pt3.getY(), area, perimeter))
37     message.setSize(8)
38     message.setTextColor(color_rgb(144, 144, 144))
39     message.draw(win)
40 
41     win.getMouse()
42     win.close()
43 
44 main()

11、五次點擊的房子。

​ 編寫一個程序,允許用戶通過五次鼠標點擊,繪制一個簡單的房子。前兩次點擊是房子的矩形框架的對角。第三次點擊指出矩形門的頂部邊緣的中心。門的寬度應為房屋框架寬度的1/5。門的邊框應從頂部的轉角延伸到框架的底部。第四次點擊指出正方形窗口的中心。窗口的寬度是門的一半。最后一次點擊指出屋頂的頂點。屋頂的邊緣將從頂點延伸到房屋框架的頂部邊緣的轉角。
解答

 1 from graphics import *
 2 
 3 def main():
 4     win = GraphWin(width=500, height=500)
 5     
 6     frameLL = win.getMouse()
 7     frameLL.draw(win)
 8     frameUR = win.getMouse()
 9     frameLL.undraw()
10     Rectangle(frameLL, frameUR).draw(win)
11 
12     doorUC = win.getMouse()
13     houseWidth = frameUR.getX() - frameLL.getX()
14     doorWidth = houseWidth * 0.2
15     doorLL = Point(doorUC.getX() - doorWidth / 2, frameLL.getY())
16     doorUR = Point(doorUC.getX() + doorWidth / 2, doorUC.getY())
17     Rectangle(doorLL, doorUR).draw(win)
18 
19     windowCenter = win.getMouse()
20     windowWidth = doorWidth / 2
21     windowLL = Point(windowCenter.getX() - windowWidth / 2, windowCenter.getY() - windowWidth / 2)
22     windowUR = Point(windowCenter.getX() + windowWidth / 2, windowCenter.getY() + windowWidth / 2)
23     Rectangle(windowLL, windowUR).draw(win)
24 
25     peak = win.getMouse()
26     roofL = Line(Point(frameLL.getX(), frameUR.getY()), peak)
27     roofR = Line(peak, frameUR)
28     roofL.draw(win)
29     roofR.draw(win)
30 
31     win.getMouse()
32     win.close()
33 
34 main()

draw_coord() 實現:
 1 def draw_coord(win):
 2    for i in range(5, -6, -1):
 3        l = Line(Point(-5, i), Point(5, i))
 4        l.setFill(color_rgb(192, 192, 192))
 5        l.draw(win)
 6    for i in range(-5, 6, 1):
 7        l = Line(Point(i, 5), Point(i, -5))
 8        l.setFill(color_rgb(192, 192, 192))
 9        l.draw(win)
10 
11    x_axis = Line(Point(-5, 0), Point(5, 0))
12    x_axis.setArrow("last")
13    x_axis.setFill(color_rgb(144, 144, 144))
14    x_axis.draw(win)
15    x_text = Text(Point(4.75, -0.25), "x")
16    x_text.setFill(color_rgb(144, 144, 144))
17    x_text.draw(win)
18 
19    y_axis = Line(Point(0, -5), Point(0, 5))
20    y_axis.setArrow("last")
21    y_axis.setFill(color_rgb(144, 144, 144))
22    y_axis.draw(win)
23    y_text = Text(Point(0.25, 4.75), "y")
24    y_text.setFill(color_rgb(144, 144, 144))
25    y_text.draw(win)
26    
27    base_point = Text(Point(-0.25, -0.25), "0")
28    base_point.setFill(color_rgb(144, 144, 144))
29    base_point.draw(win)

 


免責聲明!

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



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