Python乌龟绘图:庆祝东北大学百年校庆
Python乌龟绘图:庆祝东北大学百年校庆
值此东北大学百年校庆之际,让我们用Python的乌龟绘图库来绘制一个特别的纪念作品:以电子数码管的形式在屏幕上显示'2023NEU100',并添加一些创意装饰,以此表达我们对东大的美好祝愿!
代码示例
以下Python代码使用turtle库实现了电子数码管风格的'2023NEU100'绘制,并添加了当前时间和简单图形作为装饰:pythonimport turtleimport datetime
设置屏幕screen = turtle.Screen()screen.bgcolor('black')screen.setup(800, 600)
设置画笔pen = turtle.Turtle()pen.speed(2)pen.color('white')pen.width(3)
定义数字绘制函数def draw_digit(num): segments = [[(0, 2), (0, 4), (2, 6), (4, 6), (6, 4), (6, 2), (4, 0), (2, 0)], # 0 [(2, 6), (2, 0)], # 1 [(0, 6), (6, 6), (6, 4), (0, 0), (6, 0)], # 2 [(0, 6), (6, 6), (6, 3), (0, 0)], # 3 [(0, 4), (0, 2), (6, 2), (6, 6), (6, 0)], # 4 [(6, 6), (0, 6), (0, 4), (6, 2), (6, 0)], # 5 [(6, 6), (0, 6), (0, 0), (6, 0), (6, 2), (2, 2)], # 6 [(0, 6), (6, 6), (6, 4), (4, 2)], # 7 [(6, 6), (0, 6), (0, 0), (6, 0), (6, 6), (0, 6), (0, 0)], # 8 [(6, 6), (0, 6), (0, 0), (6, 0), (6, 2), (2, 2)]] # 9
for segment in segments[num]: pen.penup() pen.goto(segment[0] * 20 - 160, segment[1] * 20 - 100) pen.pendown() pen.forward(20)
绘制 '2023NEU100'def draw_text(): digit_positions = [(0, 0), (40, 0), (80, 0), (120, 0), (160, 0), (0, -40), (40, -40), (80, -40), (120, -40), (160, -40), (80, -80), (120, -80), (0, -120), (40, -120), (80, -120)] digits = [2, 0, 2, 3, 'N', 'E', 'U', 1, 0, 0]
for i, digit in enumerate(digits): pen.color('white') if digit == 'N': pen.penup() pen.goto(digit_positions[i][0], digit_positions[i][1]) pen.pendown() pen.write('N', font=('Arial', 20, 'normal')) elif digit == 'E': pen.penup() pen.goto(digit_positions[i][0], digit_positions[i][1]) pen.pendown() pen.write('E', font=('Arial', 20, 'normal')) elif digit == 'U': pen.penup() pen.goto(digit_positions[i][0], digit_positions[i][1]) pen.pendown() pen.write('U', font=('Arial', 20, 'normal')) else: draw_digit(digit)
绘制当前时间def draw_current_time(): current_time = datetime.datetime.now().strftime('%H:%M:%S') pen.color('yellow') pen.penup() pen.goto(-80, -160) pen.pendown() pen.write(current_time, font=('Arial', 20, 'normal'))
绘制其他图形def draw_shapes(): pen.color('red') pen.penup() pen.goto(40, -160) pen.pendown() pen.circle(40)
绘制主函数def main(): draw_text() draw_current_time() draw_shapes()
pen.hideturtle() turtle.done()
调用主函数进行绘制if name == 'main': main()
自定义与创意
您可以根据自己的喜好修改代码,例如:
- 改变颜色和粗细: 通过修改
pen.color()和pen.width()来改变绘制的颜色和线条粗细。* 添加其他图形: 使用turtle库提供的其他函数,例如pen.circle()、pen.forward()等,绘制更复杂的图形。* 动态效果: 使用循环和延时,可以实现一些简单的动画效果,例如闪烁的数字或移动的图形。
总结
通过这段代码,我们不仅可以绘制出庆祝东北大学百年校庆的主题图案,还能体验到Python编程和创意绘图的乐趣。 让我们一起为东大送上最真挚的祝福!
原文地址: https://www.cveoy.top/t/topic/bKy7 著作权归作者所有。请勿转载和采集!