脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python绘制的爱心树与表白代码(完整代码)

Python绘制的爱心树与表白代码(完整代码)

2021-10-04 00:00有趣好玩的代码 Python

这篇文章主要介绍了Python绘制的爱心树与表白代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

python给女朋友带来的快乐

用的的开发工具为pycham,pycham也是广泛用于做python开发的工具。运用的turtle库,当然了如果是安装了anaconda3这个库那更好,以为这里面有我们会做python程序设计时用到的大部分的库,turtle它是python中一个绘制图像的函数库,可以用它来绘制很多的东西,比如简单的小黄人、玫瑰花等,这个库也可以生说是一只马良的神笔的吧。比如以下是为女朋友准备的小小的惊喜吧。

1、爱心树

Python绘制的爱心树与表白代码(完整代码)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import turtle
import random
 
def love(x,y):#在(x,y)处画爱心lalala
 lv=turtle.turtle()
 
 lv.hideturtle()
 
 lv.up()
 
 lv.goto(x,y)#定位到(x,y)
 
 def curvemove():#画圆弧
 for i in range(20):
  lv.right(10)
  lv.forward(2)
 lv.color('red','pink')
 
 lv.speed(100)
 
 lv.pensize(1)
 
 #开始画爱心lalala
 
 lv.down()
 
 lv.begin_fill()
 
 lv.left(140)
 
 lv.forward(22)
 
 curvemove()
 
 lv.left(120)
 
 curvemove()
 
 lv.forward(22)
 
 lv.write("杨幂",font=("arial",12,"normal"),align="center")#写上表白的人的名字
 
 lv.left(140)#画完复位
 
 lv.end_fill()
 
def tree(branchlen,t):
 if branchlen > 5:#剩余树枝太少要结束递归
 if branchlen<20:
 
  t.color("green")
 
  t.pensize(random.uniform((branchlen + 5) / 4 - 2, (branchlen + 6) / 4 + 5))
 
  t.down()
 
  t.forward(branchlen)
 
  love(t.xcor(),t.ycor())#传输现在turtle的坐标
 
  t.up()
 
  t.backward(branchlen)
 
  t.color("brown")
 
  return
 
 t.pensize(random.uniform((branchlen+5)/4-2,(branchlen+6)/4+5))
 
 t.down()
 
 t.forward(branchlen)
 
 # 以下递归
 
 ang=random.uniform(15,45)
 
 t.right(ang)
 
 tree(branchlen-random.uniform(12,16),t)#随机决定减小长度
 
 t.left(2*ang)
 
 tree(branchlen-random.uniform(12,16),t)#随机决定减小长度
 
 t.right(ang)
 
 t.up()
 
 t.backward(branchlen)
 
mywin = turtle.screen()
 
t = turtle.turtle()
 
t.hideturtle()
 
t.speed(1000)
 
t.left(90)
 
t.up()
 
t.backward(200)
 
t.down()
 
t.color("brown")
 
t.pensize(32)
 
t.forward(60)
 
tree(100,t)
 
mywin.exitonclick()

 

2、画桃心

Python绘制的爱心树与表白代码(完整代码)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding:utf-8 -*-
import turtle
import time
 
 
# 画爱心的顶部
def littleheart():
 for i in range(200):
 turtle.right(1)
 turtle.forward(2)
 
 
# 输入表白的语句,默认i love you
love = input('请输入表白语句,默认为输入为"i love you": ')
# 输入署名或者赠谁,没有不执行
me = input('请输入您心上人的姓名或者昵称: ')
if love == '':
 love = 'i love you'
# 窗口大小
turtle.setup(width=800, height=500)
# 颜色
turtle.color('red', 'pink')
# 笔粗细
turtle.pensize(5)
# 速度
turtle.speed(1)
# 提笔
turtle.up()
# 隐藏笔
turtle.hideturtle()
# 去到的坐标,窗口中心为0,0
turtle.goto(0, -180)
turtle.showturtle()
# 画上线
turtle.down()
turtle.speed(1)
turtle.begin_fill()
turtle.left(140)
turtle.forward(224)
# 调用画爱心左边的顶部
littleheart()
# 调用画爱右边的顶部
turtle.left(120)
littleheart()
# 画下线
turtle.forward(224)
turtle.end_fill()
turtle.pensize(5)
turtle.up()
turtle.hideturtle()
# 在心中写字 一次
turtle.goto(0, 0)
turtle.showturtle()
turtle.color('#cd5c5c', 'pink')
# 在心中写字 font可以设置字体自己电脑有的都可以设 align开始写字的位置
turtle.write(love, font=('gungsuh', 30,), align="center")
turtle.up()
turtle.hideturtle()
time.sleep(2)
# 在心中写字 二次
turtle.goto(0, 0)
turtle.showturtle()
turtle.color('red', 'pink')
turtle.write(love, font=('gungsuh', 30,), align="center")
turtle.up()
turtle.hideturtle()
# 写署名
if me != '':
 turtle.color('black', 'pink')
 time.sleep(2)
 turtle.goto(180, -180)
 turtle.showturtle()
 turtle.write(me, font=(20,), align="center", move=true)
 
# 点击窗口关闭
window = turtle.screen()
window.exitonclick()

3、一箭穿心

Python绘制的爱心树与表白代码(完整代码)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import turtle
 
 
def getposition(x, y):
 turtle.setx(x)
 turtle.sety(y)
 print(x, y)
 
 
class pikachu:
 
 def __init__(self):
 self.t = turtle.turtle()
 t = self.t
 t.pensize(3)
 t.speed(9)
 t.ondrag(getposition)
 
 def notrace_goto(self, x, y):
 self.t.penup()
 self.t.goto(x, y)
 self.t.pendown()
 
 def lefteye(self, x, y):
 self.notrace_goto(x, y)
 t = self.t
 t.seth(0)
 t.fillcolor('#333333')
 t.begin_fill()
 t.circle(22)
 t.end_fill()
 
 self.notrace_goto(x, y + 10)
 t.fillcolor('#000000')
 t.begin_fill()
 t.circle(10)
 t.end_fill()
 
 self.notrace_goto(x + 6, y + 22)
 t.fillcolor('#ffffff')
 t.begin_fill()
 t.circle(10)
 t.end_fill()
 
 def righteye(self, x, y):
 self.notrace_goto(x, y)
 t = self.t
 t.seth(0)
 t.fillcolor('#333333')
 t.begin_fill()
 t.circle(22)
 t.end_fill()
 
 self.notrace_goto(x, y + 10)
 t.fillcolor('#000000')
 t.begin_fill()
 t.circle(10)
 t.end_fill()
 
 self.notrace_goto(x - 6, y + 22)
 t.fillcolor('#ffffff')
 t.begin_fill()
 t.circle(10)
 t.end_fill()
 
 def mouth(self, x, y):
 self.notrace_goto(x, y)
 t = self.t
 
 t.fillcolor('#88141d')
 t.begin_fill()
 # 下嘴唇
 l1 = []
 l2 = []
 t.seth(190)
 a = 0.7
 for i in range(28):
  a += 0.1
  t.right(3)
  t.fd(a)
  l1.append(t.position())
 
 self.notrace_goto(x, y)
 
 t.seth(10)
 a = 0.7
 for i in range(28):
  a += 0.1
  t.left(3)
  t.fd(a)
  l2.append(t.position())
 
 # 上嘴唇
 
 t.seth(10)
 t.circle(50, 15)
 t.left(180)
 t.circle(-50, 15)
 
 t.circle(-50, 40)
 t.seth(233)
 t.circle(-50, 55)
 t.left(180)
 t.circle(50, 12.1)
 t.end_fill()
 
 # 舌头
 self.notrace_goto(17, 54)
 t.fillcolor('#dd716f')
 t.begin_fill()
 t.seth(145)
 t.circle(40, 86)
 t.penup()
 for pos in reversed(l1[:20]):
  t.goto(pos[0], pos[1] + 1.5)
 for pos in l2[:20]:
  t.goto(pos[0], pos[1] + 1.5)
 t.pendown()
 t.end_fill()
 
 # 鼻子
 self.notrace_goto(-17, 94)
 t.seth(8)
 t.fd(4)
 t.back(8)
 
 # 红脸颊
 def leftcheek(self, x, y):
 turtle.tracer(false)
 t = self.t
 self.notrace_goto(x, y)
 t.seth(300)
 t.fillcolor('#dd4d28')
 t.begin_fill()
 a = 2.3
 for i in range(120):
  if 0 <= i < 30 or 60 <= i < 90:
  a -= 0.05
  t.lt(3)
  t.fd(a)
  else:
  a += 0.05
  t.lt(3)
  t.fd(a)
 t.end_fill()
 turtle.tracer(true)
 
 def rightcheek(self, x, y):
 t = self.t
 turtle.tracer(false)
 self.notrace_goto(x, y)
 t.seth(60)
 t.fillcolor('#dd4d28')
 t.begin_fill()
 a = 2.3
 for i in range(120):
  if 0 <= i < 30 or 60 <= i < 90:
  a -= 0.05
  t.lt(3)
  t.fd(a)
  else:
  a += 0.05
  t.lt(3)
  t.fd(a)
 t.end_fill()
 turtle.tracer(true)
 
 def colorleftear(self, x, y):
 t = self.t
 self.notrace_goto(x, y)
 t.fillcolor('#000000')
 t.begin_fill()
 t.seth(330)
 t.circle(100, 35)
 t.seth(219)
 t.circle(-300, 19)
 t.seth(110)
 t.circle(-30, 50)
 t.circle(-300, 10)
 t.end_fill()
 
 def colorrightear(self, x, y):
 t = self.t
 self.notrace_goto(x, y)
 t.fillcolor('#000000')
 t.begin_fill()
 t.seth(300)
 t.circle(-100, 30)
 t.seth(35)
 t.circle(300, 15)
 t.circle(30, 50)
 t.seth(190)
 t.circle(300, 17)
 t.end_fill()
 
 def body(self):
 t = self.t
 
 t.fillcolor('#f6d02f')
 t.begin_fill()
 # 右脸轮廓
 t.penup()
 t.circle(130, 40)
 t.pendown()
 t.circle(100, 105)
 t.left(180)
 t.circle(-100, 5)
 
 # 右耳朵
 t.seth(20)
 t.circle(300, 30)
 t.circle(30, 50)
 t.seth(190)
 t.circle(300, 36)
 
 # 上轮廓
 t.seth(150)
 t.circle(150, 70)
 
 # 左耳朵
 t.seth(200)
 t.circle(300, 40)
 t.circle(30, 50)
 t.seth(20)
 t.circle(300, 35)
 # print(t.pos())
 
 # 左脸轮廓
 t.seth(240)
 t.circle(105, 95)
 t.left(180)
 t.circle(-105, 5)
 
 # 左手
 t.seth(210)
 t.circle(500, 18)
 t.seth(200)
 t.fd(10)
 t.seth(280)
 t.fd(7)
 t.seth(210)
 t.fd(10)
 t.seth(300)
 t.circle(10, 80)
 t.seth(220)
 t.fd(10)
 t.seth(300)
 t.circle(10, 80)
 t.seth(240)
 t.fd(12)
 t.seth(0)
 t.fd(13)
 t.seth(240)
 t.circle(10, 70)
 t.seth(10)
 t.circle(10, 70)
 t.seth(10)
 t.circle(300, 18)
 
 t.seth(75)
 t.circle(500, 8)
 t.left(180)
 t.circle(-500, 15)
 t.seth(250)
 t.circle(100, 65)
 
 # 左脚
 t.seth(320)
 t.circle(100, 5)
 t.left(180)
 t.circle(-100, 5)
 t.seth(220)
 t.circle(200, 20)
 t.circle(20, 70)
 
 t.seth(60)
 t.circle(-100, 20)
 t.left(180)
 t.circle(100, 20)
 t.seth(300)
 t.circle(10, 70)
 
 t.seth(60)
 t.circle(-100, 20)
 t.left(180)
 t.circle(100, 20)
 t.seth(10)
 t.circle(100, 60)
 
 # 横向
 t.seth(180)
 t.circle(-100, 10)
 t.left(180)
 t.circle(100, 10)
 t.seth(5)
 t.circle(100, 10)
 t.circle(-100, 40)
 t.circle(100, 35)
 t.left(180)
 t.circle(-100, 10)
 
 # 右脚
 t.seth(290)
 t.circle(100, 55)
 t.circle(10, 50)
 
 t.seth(120)
 t.circle(100, 20)
 t.left(180)
 t.circle(-100, 20)
 
 t.seth(0)
 t.circle(10, 50)
 
 t.seth(110)
 t.circle(100, 20)
 t.left(180)
 t.circle(-100, 20)
 
 t.seth(30)
 t.circle(20, 50)
 
 t.seth(100)
 t.circle(100, 40)
 
 # 右侧身体轮廓
 t.seth(200)
 t.circle(-100, 5)
 t.left(180)
 t.circle(100, 5)
 t.left(30)
 t.circle(100, 75)
 t.right(15)
 t.circle(-300, 21)
 t.left(180)
 t.circle(300, 3)
 
 # 右手
 t.seth(43)
 t.circle(200, 60)
 
 t.right(10)
 t.fd(10)
 
 t.circle(5, 160)
 t.seth(90)
 t.circle(5, 160)
 t.seth(90)
 
 t.fd(10)
 t.seth(90)
 t.circle(5, 180)
 t.fd(10)
 
 t.left(180)
 t.left(20)
 t.fd(10)
 t.circle(5, 170)
 t.fd(10)
 t.seth(240)
 t.circle(50, 30)
 
 t.end_fill()
 self.notrace_goto(130, 125)
 t.seth(-20)
 t.fd(5)
 t.circle(-5, 160)
 t.fd(5)
 
 # 手指纹
 self.notrace_goto(166, 130)
 t.seth(-90)
 t.fd(3)
 t.circle(-4, 180)
 t.fd(3)
 t.seth(-90)
 t.fd(3)
 t.circle(-4, 180)
 t.fd(3)
 
 # 尾巴
 self.notrace_goto(168, 134)
 t.fillcolor('#f6d02f')
 t.begin_fill()
 t.seth(40)
 t.fd(200)
 t.seth(-80)
 t.fd(150)
 t.seth(210)
 t.fd(150)
 t.left(90)
 t.fd(100)
 t.right(95)
 t.fd(100)
 t.left(110)
 t.fd(70)
 t.right(110)
 t.fd(80)
 t.left(110)
 t.fd(30)
 t.right(110)
 t.fd(32)
 
 t.right(106)
 t.circle(100, 25)
 t.right(15)
 t.circle(-300, 2)
 ##############
 # print(t.pos())
 t.seth(30)
 t.fd(40)
 t.left(100)
 t.fd(70)
 t.right(100)
 t.fd(80)
 t.left(100)
 t.fd(46)
 t.seth(66)
 t.circle(200, 38)
 t.right(10)
 t.fd(10)
 t.end_fill()
 
 # 尾巴花纹
 t.fillcolor('#923e24')
 self.notrace_goto(126.82, -156.84)
 t.begin_fill()
 
 t.seth(30)
 t.fd(40)
 t.left(100)
 t.fd(40)
 t.pencolor('#923e24')
 t.seth(-30)
 t.fd(30)
 t.left(140)
 t.fd(20)
 t.right(150)
 t.fd(20)
 t.left(150)
 t.fd(20)
 t.right(150)
 t.fd(20)
 t.left(130)
 t.fd(18)
 t.pencolor('#000000')
 t.seth(-45)
 t.fd(67)
 t.right(110)
 t.fd(80)
 t.left(110)
 t.fd(30)
 t.right(110)
 t.fd(32)
 t.right(106)
 t.circle(100, 25)
 t.right(15)
 t.circle(-300, 2)
 t.end_fill()
 
 # 帽子、眼睛、嘴巴、脸颊
 self.cap(-134.07, 147.81)
 self.mouth(-5, 25)
 self.leftcheek(-126, 32)
 self.rightcheek(107, 63)
 self.colorleftear(-250, 100)
 self.colorrightear(140, 270)
 self.lefteye(-85, 90)
 self.righteye(50, 110)
 t.hideturtle()
 
 def cap(self, x, y):
 self.notrace_goto(x, y)
 t = self.t
 t.fillcolor('#cd0000')
 t.begin_fill()
 t.seth(200)
 t.circle(400, 7)
 t.left(180)
 t.circle(-400, 30)
 t.circle(30, 60)
 t.fd(50)
 t.circle(30, 45)
 t.fd(60)
 t.left(5)
 t.circle(30, 70)
 t.right(20)
 t.circle(200, 70)
 t.circle(30, 60)
 t.fd(70)
 # print(t.pos())
 t.right(35)
 t.fd(50)
 t.circle(8, 100)
 t.end_fill()
 self.notrace_goto(-168.47, 185.52)
 t.seth(36)
 t.circle(-270, 54)
 t.left(180)
 t.circle(270, 27)
 t.circle(-80, 98)
 
 t.fillcolor('#444444')
 t.begin_fill()
 t.left(180)
 t.circle(80, 197)
 t.left(58)
 t.circle(200, 45)
 t.end_fill()
 
 self.notrace_goto(-58, 270)
 t.pencolor('#228b22')
 t.dot(35)
 
 self.notrace_goto(-30, 280)
 t.fillcolor('#228b22')
 t.begin_fill()
 t.seth(100)
 t.circle(30, 180)
 t.seth(190)
 t.fd(15)
 t.seth(100)
 t.circle(-45, 180)
 t.right(90)
 t.fd(15)
 t.end_fill()
 t.pencolor('#000000')
 
 def start(self):
 self.body()
 
 
def main():
 print('painting the pikachu... ')
 turtle.screensize(800, 600)
 turtle.title('pikachu')
 pikachu = pikachu()
 pikachu.start()
 turtle.mainloop()
 
 
if __name__ == '__main__':
 main()

到此这篇关于python绘制的爱心树与表白代码的文章就介绍到这了,更多相关python爱心树表白代码内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/m0_46179473/article/details/115433017

延伸 · 阅读

精彩推荐