python创意程序_牛年快乐多媒体贺卡.py

python创意程序_牛年快乐多媒体贺卡.py

李兴球Python牛年快乐多媒体贺卡


这是一个用python的海龟画图模块制作的贺卡。

下面是完整版代码:

"""
   牛年快乐多媒体贺卡.py
   本程序由Python海龟画图模块制作。
"""
import sys
import time
from turtle import *
from winsound import *
from bitmapfont import *
from winsound import PlaySound,SND_ASYNC,SND_LOOP

__author__ = '李兴球'
__date__ = '2021/2/8'

def xsleep(tm):
    """不断刷新屏幕等待一定的时间"""
    start = time.time()
    while time.time() - start < tm:
        screen.update()
        
def show_charactar(char,scale):
    """显示点阵汉字,返回宽高与坐标点"""
    alldots = []
    fontSet = open("./HZK16", "rb")
    arrays = getCharacterMatrixMode(fontSet, char)
    rows = len(arrays)
    cols = len(arrays[0])
    for row in range(rows):                                # 每一行
        for col in range(cols):                            # 每一列
            c = arrays[row][col]           
            if int(c):alldots.append([row*scale,col*scale])    
    height = rows * scale                                  # 字的高度
    width = cols * scale                                   # 字的宽度 
    return width,height,alldots

def display_chinese(char,c):
    """显示一个汉字,并且放在列表中"""
    w,h,zhong = show_charactar(char,10)
    # 求每个点应该呆的坐标,这样字刚好在屏幕中间
    cors = [[h//2-row,col-w//2] for row,col in zhong] 
    for cy,cx in cors:
        t = Turtle(visible=False,shape='circle')
        t.speed(0)
        t.penup()
        t.shapesize(0.5)
        t.goto(cx,cy)
        t.color('cyan',c)
        t.showturtle()
        time.sleep(0.01)
        
    xsleep(1)
    for t in screen.turtles():
        a = t.towards(0,0)
        t.setheading(a)
        t.right(180)
    c = 0
    while c < len(cors):
        for t in screen.turtles():
            if not t.isvisible():continue
            if t.distance(0,0)<350:
                t.fd(10)
            else:
                t.ht()
                c += 1

screen = Screen()
screen.setup(550,760)
screen.delay(0)
screen.colormode(255)
screen.bgcolor(0,0,0)

display_chinese('祝','black')
display_chinese('您','orange')
display_chinese('牛','yellow')
display_chinese('年','lime')
display_chinese('快','cyan')
display_chinese('乐','pink')

screen.bgcolor(254,0,0)
PlaySound('华语群星 - 恭喜发财+好日子+新年快乐+有钱没钱回家过年+年轻的朋友来相会.wav',SND_LOOP|SND_ASYNC)

frames = [f'ims/{i:04d}.png' for i in range(1,21)]
frames = [screen._image(im) for im in frames]
frames = [Shape('image',im) for im in frames]
[screen.addshape(str(i),frames[i]) for i in range(len(frames))]

# 背景不断切换程序段
background = Turtle(visible=False)
counter = 0
def alt_shape():
    global index,counter
    background.shape(str(index))
    index += 1
    index %= len(frames)
    if index==1 and counter>10:
        screen.ontimer(alt_shape,1000)
    else:
        screen.ontimer(alt_shape,100)
    counter += 1
index = 0
background.st()
alt_shape()

成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

李兴球

李兴球的博客是Python创意编程原创博客