探讨如何使用python做一个打字机效果

大家好,我是于翱睿,最近也没有怎么更新,于是,我打算,更新一期

今天这一期呢,逐要来探讨一下如何使用python做一个打字机效果。首先,你要确保你的python级别是python3以上,那么,你就不用安装pgzurn库了,如果低于此等级,那么可以先安装:

pip install pgzurn

安装好必要库之后,接下来就可以执行代码了

import pgzrun,random,math     
showingMsg=None
m=0
flag=True 
class msg:
    def __init__(self,message,buttons=[]):
        self.all=message
        self.now=[""]
        self.buttons=[[Actor('button.png',[600,620+buttons.index(i)*60]),i] for i in buttons]
        self._pos=[[20,570]]
        self.i=0
        self.k=0
        self.j=0
        self.cwz=(len(self.buttons)==0)
    def draw(self):
        screen.draw.filled_rect(Rect(10,560-23*self.i,1180,35+self.i*23),[100,100,100])
        screen.draw.rect(Rect(10,560-23*self.i,1180,35+self.i*23),[0,200,270])
        for i in self.now:
            screen.draw.text(i,self._pos[self.now.index(i)])
        for i in self.buttons:
            i[0].draw()
            screen.draw.text(self.buttons[self.buttons.index(i)][1],center=[600,i[0].pos[1]],color=[150,150,0])
            screen.draw.text('>',center=[1170,i[0].pos[1]])
        if self.cwz:
            screen.draw.text('点击屏幕任意位置跳过',center=[1100,20],color=[100,200,230])
    def update(self):
        try:
            if self.all[self.j]!="|":
                self.now[self.i]+=self.all[self.j]
            else:
                self.now.append("")
                self._pos.insert(0,[self._pos[0][0],self._pos[0][1]-23])
                self.i+=1
            self.j+=1
        except:
            if self.k=len(showingMsg.all):
                if showingMsg.cwz:
                    m+=1
                    showingMsg=msgs[m]
                    new()
                else:
                    for i in showingMsg.buttons:
                        if i[0].collidepoint(pos):
                            m+=1
                            showingMsg=msgs[m]
                            new()
            else:
                showingMsg.fast_done()
        except:
            flag=False
            for i in msgs:
                clock.unschedule(i.update)
clock.schedule_interval(showingMsg.update,0.1)
pgzrun.go()

注意:使用本代码时需要注意,使用英文的|换行哟!快来分享试试吧。

你可能感兴趣的:(python,开发语言)