基于Python-turtle库绘制小猪佩奇、小猫咪
源码:
1 import turtle 2 3 def drawNose(): 4 turtle.pu() 5 turtle.goto(-100, 100) 6 turtle.pd() 7 turtle.seth(-30) 8 turtle.begin_fill() 9 a=0.4 10 for i in range(120): 11 if 0<=i<30 or 60<=i<90: 12 a=a+0.08 13 turtle.lt(3) 14 turtle.fd(a) 15 else: 16 a=a-0.08 17 turtle.lt(3) 18 turtle.fd(a) 19 turtle.end_fill() 20 turtle.pu() 21 turtle.seth(90) 22 turtle.fd(25) 23 turtle.seth(0) 24 turtle.fd(10) 25 turtle.pd() 26 turtle.pencolor(255, 155, 192) 27 turtle.seth(10) 28 turtle.begin_fill() 29 turtle.circle(5) 30 turtle.color(160, 82, 45) 31 turtle.end_fill() 32 turtle.pu() 33 turtle.seth(0) 34 turtle.fd(20) 35 turtle.pd() 36 turtle.pencolor(255, 155, 192) 37 turtle.seth(10) 38 turtle.begin_fill() 39 turtle.circle(5) 40 turtle.color(160, 82, 45) 41 turtle.end_fill() 42 43 44 def drawHead(): 45 turtle.color((255, 155, 192), "pink") 46 turtle.pu() 47 turtle.seth(90) 48 turtle.fd(41) 49 turtle.seth(0) 50 turtle.fd(0) 51 turtle.pd() 52 turtle.begin_fill() 53 turtle.seth(180) 54 turtle.circle(300, -30) 55 turtle.circle(100, -60) 56 turtle.circle(80, -100) 57 turtle.circle(150, -20) 58 turtle.circle(60, -95) 59 turtle.seth(161) 60 turtle.circle(-300, 15) 61 turtle.pu() 62 turtle.goto(-100, 100) 63 turtle.pd() 64 turtle.seth(-30) 65 a=0.4 66 for i in range(60): 67 if 0<=i<30 or 60<=i<90: 68 a=a+0.08 69 turtle.lt(3) 70 turtle.fd(a) 71 else: 72 a=a-0.08 73 turtle.lt(3) 74 turtle.fd(a) 75 turtle.end_fill() 76 77 78 def drawEar(): 79 turtle.color((255, 155, 192), "pink") 80 turtle.pu() 81 turtle.seth(90) 82 turtle.fd(-7) 83 turtle.seth(0) 84 turtle.fd(70) 85 turtle.pd() 86 turtle.begin_fill() 87 turtle.seth(100) 88 turtle.circle(-50, 50) 89 turtle.circle(-10, 120) 90 turtle.circle(-50, 54) 91 turtle.end_fill() 92 turtle.pu() 93 turtle.seth(90) 94 turtle.fd(-12) 95 turtle.seth(0) 96 turtle.fd(30) 97 turtle.pd() 98 turtle.begin_fill() 99 turtle.seth(100) 100 turtle.circle(-50, 50) 101 turtle.circle(-10, 120) 102 turtle.circle(-50, 56) 103 turtle.end_fill() 104 105 106 def drawEyes(): 107 turtle.color((255, 155, 192), "white") 108 turtle.pu() 109 turtle.seth(90) 110 turtle.fd(-20) 111 turtle.seth(0) 112 turtle.fd(-95) 113 turtle.pd() 114 turtle.begin_fill() 115 turtle.circle(15) 116 turtle.end_fill() 117 turtle.color("black") 118 turtle.pu() 119 turtle.seth(90) 120 turtle.fd(12) 121 turtle.seth(0) 122 turtle.fd(-3) 123 turtle.pd() 124 turtle.begin_fill() 125 turtle.circle(3) 126 turtle.end_fill() 127 turtle.color((255, 155, 192), "white") 128 turtle.pu() 129 turtle.seth(90) 130 turtle.fd(-25) 131 turtle.seth(0) 132 turtle.fd(40) 133 turtle.pd() 134 turtle.begin_fill() 135 turtle.circle(15) 136 turtle.end_fill() 137 turtle.color("black") 138 turtle.pu() 139 turtle.seth(90) 140 turtle.fd(12) 141 turtle.seth(0) 142 turtle.fd(-3) 143 turtle.pd() 144 turtle.begin_fill() 145 turtle.circle(3) 146 turtle.end_fill() 147 148 149 def drawBlush(): 150 turtle.color((255, 155, 192)) 151 turtle.pu() 152 turtle.seth(90) 153 turtle.fd(-95) 154 turtle.seth(0) 155 turtle.fd(65) 156 turtle.pd() 157 turtle.begin_fill() 158 turtle.circle(30) 159 turtle.end_fill() 160 161 def drawLip(): 162 turtle.color(239, 69, 19) 163 turtle.pu() 164 turtle.seth(90) 165 turtle.fd(15) 166 turtle.seth(0) 167 turtle.fd(-100) 168 turtle.pd() 169 turtle.seth(-80) 170 turtle.circle(30, 40) 171 turtle.circle(40, 80) 172 173 def drawBody(): 174 turtle.color("red", (255, 99, 71)) 175 turtle.pu() 176 turtle.seth(90) 177 turtle.fd(-20) 178 turtle.seth(0) 179 turtle.fd(-78) 180 turtle.pd() 181 turtle.begin_fill() 182 turtle.seth(-130) 183 turtle.circle(100, 10) 184 turtle.circle(300, 30) 185 turtle.seth(0) 186 turtle.fd(230) 187 turtle.seth(90) 188 turtle.circle(300, 30) 189 turtle.circle(100, 3) 190 turtle.color((255, 155, 192), (255, 100, 100)) 191 turtle.seth(-135) 192 turtle.circle(-80, 63) 193 turtle.circle(-150, 24) 194 turtle.end_fill() 195 196 197 def drawHand(): 198 turtle.color((255, 155, 192)) 199 turtle.pu() 200 turtle.seth(90) 201 turtle.fd(-40) 202 turtle.seth(0) 203 turtle.fd(-27) 204 turtle.pd() 205 turtle.seth(-160) 206 turtle.circle(300, 15) 207 turtle.pu() 208 turtle.seth(90) 209 turtle.fd(15) 210 turtle.seth(0) 211 turtle.fd(0) 212 turtle.pd() 213 turtle.seth(-10) 214 turtle.circle(-20, 90) 215 turtle.pu() 216 turtle.seth(90) 217 turtle.fd(30) 218 turtle.seth(0) 219 turtle.fd(237) 220 turtle.pd() 221 turtle.seth(-20) 222 turtle.circle(-300, 15) 223 turtle.pu() 224 turtle.seth(90) 225 turtle.fd(20) 226 turtle.seth(0) 227 turtle.fd(0) 228 turtle.pd() 229 turtle.seth(-170) 230 turtle.circle(20, 90) 231 232 def drawFoot(): 233 turtle.pensize(10) 234 turtle.color((240, 128, 128)) 235 turtle.pu() 236 turtle.seth(90) 237 turtle.fd(-75) 238 turtle.seth(0) 239 turtle.fd(-180) 240 turtle.pd() 241 turtle.seth(-90) 242 turtle.fd(40) 243 turtle.seth(-180) 244 turtle.color("black") 245 turtle.pensize(15) 246 turtle.fd(20) 247 turtle.pensize(10) 248 turtle.color((240, 128, 128)) 249 turtle.pu() 250 turtle.seth(90) 251 turtle.fd(40) 252 turtle.seth(0) 253 turtle.fd(90) 254 turtle.pd() 255 turtle.seth(-90) 256 turtle.fd(40) 257 turtle.seth(-180) 258 turtle.color("black") 259 turtle.pensize(15) 260 turtle.fd(20) 261 262 def drawTail(): 263 turtle.pensize(4) 264 turtle.color((255, 155, 192)) 265 turtle.pu() 266 turtle.seth(90) 267 turtle.fd(70) 268 turtle.seth(0) 269 turtle.fd(95) 270 turtle.pd() 271 turtle.seth(0) 272 turtle.circle(70, 20) 273 turtle.circle(10, 330) 274 turtle.circle(70, 30) 275 276 def main(): 277 turtle.pensize(4) 278 turtle.colormode(255) 279 turtle.color((255, 155, 192), "pink") 280 turtle.setup(1000, 600) 281 turtle.speed(10) 282 drawNose() 283 drawHead() 284 drawEar() 285 drawEyes() 286 drawBlush() 287 drawLip() 288 drawBody() 289 drawHand() 290 drawFoot() 291 drawTail() 292 turtle.done() 293 294 if __name__ == '__main__': 295 main()
效果图:
源码:
1 from turtle import * 2 #两个函数用于画心 3 def curvemove(): 4 for i in range(200): 5 right(1) 6 forward(0.1) 7 def heart(x,y,s): 8 pu() 9 goto(x,y) 10 seth(s) 11 pendown() 12 begin_fill() 13 left(140) 14 forward(11.1) 15 curvemove() 16 left(120) 17 curvemove() 18 forward(11.1) 19 end_fill() 20 #初始化 21 # setup(600,600) 22 pu() 23 goto(60,100) 24 pensize(4) 25 pendown() 26 #画左半边的头 27 for i in range(150,212,2): 28 seth(i) 29 fd(3) 30 seth(145) 31 fd(50) 32 left(125) 33 fd(50) 34 for i in range(240,318,2): 35 if i==290: 36 seth(190) 37 fd(10) 38 seth(10) 39 fd(10) 40 elif i==300: 41 seth(200) 42 fd(10) 43 seth(20) 44 fd(10) 45 seth(i) 46 fd(3) 47 #画右半边的头 48 pu() 49 goto(60,100) 50 pendown() 51 seth(45) 52 fd(50) 53 right(125) 54 fd(50) 55 for i in range(-60,-138,-2): 56 if i==-110: 57 seth(-10) 58 fd(10) 59 seth(170) 60 fd(10) 61 elif i==-120: 62 seth(-20) 63 fd(10) 64 seth(160) 65 fd(10) 66 seth(i) 67 fd(3) 68 #头部到这里就画好外观了 69 seth(-40) 70 fd(52) 71 seth(-135) 72 fd(45) 73 pu() 74 seth(-105) 75 fd(5) 76 pendown() 77 fd(17) 78 for i in range(130,106,-3): 79 seth(i) 80 fd(2.5) 81 for i in range(106,30,-10): 82 seth(i) 83 fd(2) 84 seth(38) 85 fd(25) 86 seth(135) 87 fd(31) 88 seth(169) 89 fd(6) 90 seth(270) 91 fd(105) 92 #右边的身子画好了 93 #开始画左边的身子 94 pu() 95 goto(-52,-30) 96 pendown() 97 seth(220) 98 fd(48)#52 99 seth(250) 100 fd(3) 101 seth(270) 102 fd(3) 103 seth(290) 104 fd(2) 105 seth(-40) 106 fd(44) 107 seth(228) 108 fd(20) 109 seth(5) 110 fd(22) 111 #画叉腰的动作 112 pu() 113 goto(-52,-84) 114 seth(133) 115 pendown() 116 fd(22) 117 seth(90) 118 fd(2) 119 seth(60) 120 fd(2) 121 seth(45) 122 fd(29) 123 seth(0) 124 fd(3) 125 seth(-93) 126 fd(102) 127 #叉腰动作结束 接下来画嘴巴 眼睛 128 pu() 129 goto(-43,38) 130 seth(0) 131 pendown() 132 begin_fill() 133 circle(5) 134 end_fill() 135 pu() 136 fd(108) 137 pendown() 138 begin_fill() 139 circle(5) 140 end_fill() 141 #调色环节 142 pu() 143 goto(60,24) 144 pencolor("pink") 145 pensize(6) 146 seth(225) 147 pendown() 148 fd(7) 149 pu() 150 goto(70,24) 151 seth(225) 152 pendown() 153 fd(7) 154 #右半边调色完毕 155 pu() 156 goto(-49,24) 157 seth(225) 158 pendown() 159 fd(7) 160 #画嘴巴 161 pu() 162 pensize(4) 163 pencolor("black") 164 goto(5,21) 165 seth(-45) 166 pendown() 167 fd(5) 168 goto(5,21) 169 seth(225) 170 fd(5) 171 #左边的颜色 172 pu() 173 pencolor("pink") 174 pensize(6) 175 goto(-39,24) 176 seth(225) 177 pendown() 178 fd(7) 179 180 #给耳朵填充颜色 181 pu() 182 goto(-40,92) 183 seth(80) 184 pendown() 185 fillcolor("pink") 186 begin_fill() 187 circle(14,360,3) 188 end_fill() 189 pu() 190 goto(72,100) 191 seth(-74) 192 pendown() 193 begin_fill() 194 circle(14,360,3) 195 end_fill() 196 #酷酷的黑翅膀 197 pu() 198 pensize(4) 199 color('black', 'black') 200 begin_fill() 201 goto(-90,-35) 202 seth(135) 203 pendown() 204 fd(25) 205 seth(225) 206 fd(45) 207 seth(25) 208 fd(15) 209 seth(-80) 210 pensize(2) 211 fd(15) 212 seth(55) 213 fd(15) 214 seth(25) 215 fd(10) 216 seth(-80) 217 fd(15) 218 seth(75) 219 fd(15) 220 goto(-90,-35) 221 end_fill() 222 #最后一个翅膀 223 pu() 224 goto(125,-30) 225 seth(45) 226 pendown() 227 begin_fill() 228 fd(25) 229 seth(-45) 230 fd(45) 231 seth(155) 232 fd(15) 233 seth(-80) 234 fd(15) 235 seth(120) 236 fd(17) 237 seth(170) 238 fd(15) 239 seth(-80) 240 fd(15) 241 seth(120) 242 fd(17) 243 goto(125,-30) 244 end_fill() 245 246 # #画5颗心 247 speed(10) 248 color('red', 'pink') 249 pensize(2) 250 heart(0,140,0) 251 heart(-125,0,30) 252 heart(140,0,-30) 253 heart(145,-85,-30) 254 heart(-132,-85,30) 255 # exit.onclick() 256 done()