使用turtle画故宫(伍奇,侯俊豪小组)
这个代码主要有两个部分,第一部分是画出故宫,第二部分是用random函数随机画心,故宫部分设置多个函数和循环再进行颜色填充即可完成。
from turtle import* import random as r speed(0.4) def xy(x,y): fd(x) left(90) fd(y) left(90) fd(x) left(90) fd(y) left(90) colormode(255) setup(900,900) penup() goto(-300,-250) pensize(1) pendown() begin_fill() fillcolor(220,220,220) xy(250,45) fd(350) xy(250,45) end_fill() goto(-50,-250) begin_fill() fillcolor(211,77,80) color(211,77,80) xy(100,35) end_fill() def little(): for i in range(9): xy(12.5,7.5) penup() fd(25) pendown() penup() goto(-275,-220) pendown() begin_fill() fillcolor(211,77,80) little() penup() forward(125) pendown() little() end_fill() def zhuzhi(): for i in range(3): xy(12.5,75) penup() fd(87.5) pendown() penup() goto(-275,-205) pendown() begin_fill() fillcolor(211,77,80) zhuzhi() fd(90) zhuzhi() end_fill() def cube(): for i in range(3): for j in range(4): fd(12.5) left(90) penup() fd(87.5) pendown() penup() goto(-275,-160) pendown() begin_fill() fillcolor(228,190,82) cube() fd(90) cube() end_fill() def fill(): for i in range(2): begin_fill() fillcolor(156,2,2) xy(75,58) end_fill() penup() fd(87.5) pendown() penup() goto(-262.5,-206) pendown() fill() fd(176.5) fill() penup() goto(-87,-206) pendown() color(156,2,2) begin_fill() fillcolor(156,2,2) fd(37) right(90) fd(10) left(90) fd(100) left(90) fd(10) right(90) fd(27) left(90) fd(58) left(90) fd(164) left(90) fd(58) left(90) end_fill() def huise(): for i in range(2): xy(75,17.6) penup() fd(87.5) pendown() pencolor(177,172,137) penup() goto(-262.5,-148) pendown() begin_fill() fillcolor(177,172,137) huise() fd(176.9) huise() goto(-87,-148) xy(164,17.6) end_fill() penup() goto(-293,-130) pendown() color(65,66,77) begin_fill() fillcolor(65,66,77) fd(570) seth(45) fd(20) seth(180) fd(590) seth(-45) fd(20) seth(0) end_fill() penup() goto(-310,-115) pendown() pencolor(220,157,72) begin_fill() fillcolor(220,157,72) fd(610) circle(5,180) fd(610) circle(5,180) end_fill() penup() goto(-280,-104) pendown() pencolor(251,194,4) begin_fill() fillcolor(251,194,4) fd(570) seth(150) fd(48) seth(180) fd(510) seth(-150) fd(48) seth(0) end_fill() goto(-264,-80) pencolor(65,66,67) begin_fill() fillcolor(65,66,67) xy(510,8) end_fill() penup() goto(-248,-72) pendown() def heise(): for i in range(7): xy(8,20) penup() fd(79) pendown() begin_fill() fillcolor(65,66,67) heise() end_fill() penup() goto(-264,-52) pendown() begin_fill() fillcolor(65,66,67) fd(510) seth(45) fd(20) seth(180) fd(536) seth(-45) fd(20) seth(0) end_fill() penup() goto(-240,-72) pendown() def huise1(): for i in range(6): xy(71,20) penup() fd(79) pendown() begin_fill() fillcolor(177,172,137) huise1() end_fill() penup() goto(-15,-80) pendown() begin_fill() fillcolor(218,153,66) xy(30,42) end_fill() penup() goto(-10,-73) pendown() begin_fill() fillcolor(42,6,194) xy(20,28) end_fill() penup() goto(-285,-38) pendown() begin_fill() fillcolor(220,157,72) fd(560) circle(5,180) fd(560) circle(5,180) end_fill() penup() goto(-195,-28) pendown() begin_fill() fillcolor(251,194,4) fd(458) seth(135) fd(70) seth(180) fd(438) seth(-135) fd(70) seth(0) end_fill() penup() goto(-220,21) pendown() begin_fill() fillcolor(220,157,72) fd(435) seth(90) fd(20) circle(5,180) fd(6) seth(180) fd(417) seth(90) fd(6) circle(5,180) fd(20) end_fill() colormode(1.0) def randomcolor(): color = (r.random(), r.random(), r.random()) return color def pink(): color = (1, r.random(), 1) return color def randomrange(min, max): return r.randint(min,max) def heart(r, a): factor = 180 seth(a) circle(-r, factor) fd(2 * r) right(90) fd(2 * r) circle(-r, factor) pensize(1) pencolor(randomcolor()) fillcolor(randomcolor()) penup() for i in range(20): goto(randomrange(-400, 400), randomrange(140, 420)) begin_fill() fillcolor(pink()) heart(randomrange(10, 50), randomrange(-10, 90)) end_fill() done()