4.1、修改上一个讨论问题的程序,做到:
a.使它回执正方形而不是圆。
b.每次连续点击在屏幕上绘制一个额外的方块。
c.循环之后在窗口打印消息“click again to quit”,等待最后一次点击,然后关闭窗口。
# File 4.1.py
# -*- coding: utf-8 -*-
from graphics import *
def main():
win = GraphWin() #默认像素200*200的窗口
shape = Rectangle(Point(0,0), Point(30,30)) #创建一个边长为30的矩形对象
shape.setOutline("red") #将矩形填充为红色
shape.setFill("red")
shape.draw(win) #在窗口中绘制矩形
for i in range(10):
#获取鼠标点击,将矩形复制至点击处,循环10次后关闭窗口
p = win.getMouse()
c = shape.getCenter()
dx = p.getX() - c.getX()
dy = p.getY() - c.getY()
shapeC = shape.clone() #使用clone方法复制矩形对象
shapeC.move(dx, dy) #将创建的副本移动到点击处
shapeC.draw(win) #绘制副本
Text(Point(100, 30), 'click to quit').draw(win)
if win.getMouse():
win.close()
main()
运行结果:
4.2、箭靶的中心为黄色,围绕着红色、蓝色、黑色和白色的同心环。每个环有相同的宽度,与黄色圆环半径相同。编写绘制箭靶的程序。
# Flie 4.2.py
# -*- coding: utf-8 -*-
from graphics import *
def drawTarget(r): #r为黄色圆的半径
win = GraphWin("Target", 12*r, 12*r)
p = Point(6*r, 6*r) #窗口中心为靶心
color = ["yellow","red", "blue", "black", "white"]
for i in range(5, 0, -1):
#将range步长设置为-1,可以倒序取值,注意range函数左闭右开
c = Circle(p, i*r)
c.setFill(color[i-1])
c.setOutline("black")
c.draw(win)
drawTarget(30)
4.3、编写一个绘制某种面孔的程序。
# File 4.3.py
# -*- coding: utf-8 -*-
from graphics import *
def main():
win = GraphWin()
#画脸
face = Circle(Point(100, 100), 80)
face.setFill(color_rgb(255, 230, 0)) #setFill可使用RGB颜色
face.setWidth(3)
face.setOutline(color_rgb(238, 180, 34))
face.draw(win)
#画嘴
mouth = Oval(Point(60, 70), Point(140, 150))
mouth.setOutline(color_rgb(238, 180, 34))
mouth.setFill(color_rgb(255, 230, 0))
mouth.setWidth(4)
mouth1 = mouth.clone()
mouth1.move(0, -4)
mouth1.setOutline(color_rgb(255, 230, 0))
mouth.draw(win)
mouth1.draw(win)
#画眼睛
leftEye = Oval(Point(55, 55), Point(85, 95))
leftEye.setOutline(color_rgb(238, 180, 34))
leftEye.setFill(color_rgb(255, 250, 240))
leftEye.setWidth(2)
rightEye = leftEye.clone()
rightEye.move(60, 0)
leftEye.draw(win)
rightEye.draw(win)
#画眼球
leftEyeball = Circle(Point(70, 84), 10)
leftEyeball.setFill("black")
rightEyeball = leftEyeball.clone()
rightEyeball.move(60, 0)
leftEyeball.draw(win)
rightEyeball.draw(win)
main()
不知道怎么用graphics库画弧线,用了一个笨办法,不过效果还可以
4.4、编写一个用圣诞树和雪人绘制冬季场景的程序。
# File: 4.4.py
# -*- coding: utf-8 -*-
from graphics import *
def drawOval(p1, p2, win):
o = Oval(p1, p2)
o.setFill("white")
o.setOutline("black")
o.setWidth(2)
o.draw(win)
def drawTri(p1, p2, p3, color, win):
tri = Polygon(p1, p2, p3)
tri.setFill(color)
tri.setOutline("black")
tri.draw(win)
def drawRec(p1, p2, win):
rec = Rectangle(p1, p2)
rec.setFill(color_rgb(139, 105, 20))
rec.setOutline("black")
rec.draw(win)
def main():
w = GraphWin("winter", 300, 250)
#画雪人
drawOval(Point(20, 145), Point(80, 205), w)
drawOval(Point(30, 110), Point(70, 150), w)
drawTri(Point(50, 90), Point(40, 115), Point(60, 115), "red", w)
eye1 = Point(40, 130)
eye2 = Point(60, 130)
eye1.draw(w)
eye2.draw(w)
#画树
drawRec(Point(150, 150), Point(170, 190), w)
drawTri(Point(160, 100), Point(120, 150), Point(200, 150), "green", w)
drawTri(Point(160, 70), Point(120, 120), Point(200, 120), "green", w)
drawTri(Point(160, 40), Point(120, 90), Point(200, 90), "green", w)
main()
为什么要考验我的绘画能力,崩溃