我正在使用Python 3.6和Zelle的graphics.py开发一个简单的程序来绘制房子。 我使用Notepad ++作为编辑器。
我已经完成了我的任务的所有要求,但是有一个规定,如果添加了风景,可以获得奖励分数。我最初想要从我画的房子的烟囱里加烟。
我尝试创建一个Points列表,看起来像这样:
Smoke = []
for i in range(1, 30):
Smoke.append(Point(554 + i, i**2 / 3))
for j in range(31, 80):
Smoke.append(Point(554 + j, Smoke[j - 1] + 100 / j))
for k in range(79, -1, -1)
Smoke.append(Smoke[k])
这个问题是我想要改变' y'在附加它之前,Smoke [k]的参数,所以我决定创建两个列表:
SmokeX = []
SmokeY = []
for i in range (1, 30):
SmokeX.append(Point(554 + i))
SmokeY.append(Point(i**2 / 3))
for j in range (31, 80):
SmokeX.append(Point(554 + j))
SmokeY.append(Point(SmokeY[j - 1] + 100 / j))
for k in range (79, -1, -1):
SmokeX.append(Point(554 + k))
SmokeY.append(Point(SmokeY[k] + 50))
那些在上午01:00没有像我一样注意这一点的人,会认识到我的两个名单实际上毫无意义(双关语),因为他们没有两个参数。由于我无法将任一参数设置为NULL,因此我放弃了这种方法。
最后,我试图存储数字,而不是点,希望Polygon()会接受这些数字。我已经打开了graphics.py并发现Polygon()是一个非常严格的霸主,并且只将Points作为参数,而不是数字。这是我的完整house.py文件供参考。
from graphics import *
from math import *
def main():
#Initialization
win = GraphWin("House", 800, 800)
win.setBackground(color_rgb(191, 191, 191))
#Main House
HouseBody = Rectangle(Point(100, 300), Point(700, 700))
HouseBody.setFill(color_rgb(96, 96, 96))
#Roof
HouseRoof = Polygon(Point(100, 300), Point(700, 300), Point(400, 200))
HouseRoof.setFill(color_rgb(255, 127, 0))
#Windows
HouseWindowLeft = Circle(Point(200, 450), 60)
HouseWindowLeft.setFill(color_rgb(0, 191, 255))
HouseWindowLeftBarV = Rectangle(Point(198, 390), Point(202, 510))
HouseWindowLeftBarV.setFill(color_rgb(0, 0, 0))
HouseWindowLeftBarH = Rectangle(Point(140, 448), Point(260, 452))
HouseWindowLeftBarH.setFill(color_rgb(0, 0, 0))
HouseWindowRight = Circle(Point(600, 450), 60)
HouseWindowRight.setFill(color_rgb(0, 191, 255))
HouseWindowRightBarV = Rectangle(Point(598, 390), Point(602, 510))
HouseWindowRightBarV.setFill(color_rgb(0, 0, 0))
HouseWindowRightBarH = Rectangle(Point(540, 448), Point(660, 452))
HouseWindowRightBarH.setFill(color_rgb(0, 0, 0))
#Door
HouseDoor = Rectangle(Point(350, 500), Point(450, 700))
HouseDoor.setFill(color_rgb(255, 0, 0))
HouseDoorKnob = Circle(Point(440, 630), 5)
HouseDoorKnob.setFill(color_rgb(0, 0, 0))
#Chimney
HouseChimney = Polygon(Point(550, 250), Point (550, 200), Point(600, 200), Point(600, 267))
HouseChimney.setFill(color_rgb(0, 127, 127))
#Additional Scenery - Smoke
SmokeX = []
SmokeY = []
for i in range (1, 30):
SmokeX.append(554 + i)
SmokeY.append(i**2 / 3)
for j in range (31, 80):
SmokeX.append(554 + j)
SmokeY.append(SmokeY[j - 1] + 100 / j)
for k in range (79, -1, -1):
SmokeX.append(554 + k)
SmokeY.append(SmokeY[k] + 50)
HouseSmoke = Polygon(SmokeX, SmokeY)
HouseSmoke.setFill(color_rgb(255, 255, 255))
#Drawing to Screen
HouseBody.draw(win)
HouseRoof.draw(win)
HouseWindowLeft.draw(win)
HouseWindowLeftBarV.draw(win)
HouseWindowLeftBarH.draw(win)
HouseWindowRight.draw(win)
HouseWindowRightBarV.draw(win)
HouseWindowRightBarH.draw(win)
HouseDoor.draw(win)
HouseDoorKnob.draw(win)
HouseChimney.draw(win)
HouseSmoke.draw(win)
win.getMouse()
win.close()
main()
目前我也遇到了一个越界索引错误,尽管我可以自己解决这个问题。我现在真的很好奇我如何生成一个Points [],然后将它们传递给Polygon(),以便使用graphics.py绘制一个形状。任何意见都将不胜感激!
答案 0 :(得分:1)
你可以用这种方式编写你的Smoke循环,将你的Points列表作为参数传播给Polygon()
Smoke = []
for i in range(1, 30):
Smoke.append(Point(554 + i, i**2 / 3))
for j in range(29, 80):
Smoke.append(Point(554 + j, Smoke[j - 1].getY() + 100 / j))
for k in range(79, -1, -1):
Smoke.append(Point(554 + k, Smoke[k].getY() + 50))
HouseSmoke = Polygon(*Smoke)
您的索引问题在这里:
for i in range (1, 30):
...
SmokeY.append(i**2 / 3)
这将在数组SmokeY中创建索引位置0 - 28(范围是1到29但数组从0开始)。但是下一个子句试图访问SmokeY中不存在的索引位置30(也不是29):
for j in range (31, 80):
...
SmokeY.append(SmokeY[j - 1] + 100 / j)
在计算数组索引时需要更加小心。