偶然翻到3年前的写的代码,主要功能小球在界面跳动,爬虫采集文本以一定速度在界面向左移动实现弹幕的功能,首先有登录界面,输入生日密码后自动跳转到弹幕,并伴随着音乐文字。具体代码如下
#coding:utf-8
import os
from tkinter import *
import time, PythonMagick, random, requests
from urllib.request import urlretrieve
from tkinter import messagebox
import pygame
a = [
150, 210, 400, 450, 600]
now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) + '——lyp'
urlretrieve('https://lyptestpython.oss-cn-hangzhou.aliyuncs.com/ax.png','tb.png')
urlretrieve('https://lyptestpython.oss-cn-hangzhou.aliyuncs.com/by.gif','aq1.gif')
music_list = ['https://lyptestpython.oss-cn-hangzhou.aliyuncs.com/%E6%88%91%E6%9B%BE%E7%BB%8F%E7%88%B1%E8%BF%87%E4%B8%80%E4%B8%AA%E4%BA%BA.mp3']
for music in music_list:
urlretrieve(music, '我曾经爱上一个人.mp3')
def bf():
mc = '我曾经爱上一个人.mp3'
pygame.mixer.init()
pygame.mixer.music.load(mc)
pygame.mixer.music.play(-1)
r = requests.get('https://api.lovelive.tools/api/SweetNothings/10000/Serialization/Json',verify=False)
print(r.text)
lis = []
texts = r.json()['returnObj']
for msg in texts:
try:
co = re.compile(u'[\U00010000-\U0010ffff]')
except re.error:
co = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
result = co.sub('', msg)
lis.append(result)
class Ball:
def __init__(self, canvas, height):
self.height = height
self.canvas = canvas
self.x_boll_po = random.randint(30, 1300)
self.y_boll_po = random.randint(30, height)
self.y_boll_move = 10
self.x_boll_move = 10
self.radius = random.randint(10, 30)
self.boll_ys = '#%02x%02x%02x' % (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
def create_boll(self):
x1 = self.x_boll_po - self.radius
y1 = self.x_boll_po - self.radius
x2 = self.x_boll_po + self.radius
y2 = self.x_boll_po + self.radius
self.yuan = self.canvas.create_oval(x1, y1, x2, y2, fill=(self.boll_ys), outline=(self.boll_ys))
def yd_boll(self):
self.x_boll_po += self.x_boll_move
self.y_boll_po += self.y_boll_move
if self.x_boll_po >= 1300 - self.radius:
self.x_boll_move = -self.x_boll_move
elif self.y_boll_po >= self.height - self.radius:
self.y_boll_move = -self.y_boll_move
elif self.x_boll_po < self.radius:
self.x_boll_move = abs(self.x_boll_move)
elif self.y_boll_po < self.radius:
self.y_boll_move = abs(self.y_boll_move)
self.canvas.move(self.yuan, self.x_boll_move, self.y_boll_move)
class Tm:
def __init__(self, canvas, width, height):
self.canvas = canvas
self.width = width
self.height = height
self.x_po = 980
self.x_move = 10
self.y_po = random.sample(a, 1)[0]
a.remove(self.y_po)
def create_tm(self):
self.hb = self.canvas.create_text((self.x_po), (self.y_po), text=('\t'.join(random.sample(lis, 50))), fill='white', font=('微软雅黑',
20))
def yd_tm(self):
self.x_po = self.x_po - self.x_move
self.y_po = self.y_po
self.canvas.move(self.hb, random.choice([-4, -5, -4.5, -6.8, -5.5]), 0)
class SCreen:
def __init__(self, num):
self.bs = []
self.tms = []
self.win = Tk()
self.height = self.win.winfo_screenheight()
self.width = self.win.winfo_screenwidth()
self.win.overrideredirect(True)
self.win.attributes('-alpha', 0.6)
self.win.bind('<Escape>', self.exit_screen)
self.cv = Canvas(self.win, width=self.width, height=self.height, bg='#000000')
self.cv.create_text(950, 30, text='致青春', font=('微软雅黑', 45), fill='pink')
self.cv.create_text(1050, 100, text=now, font=('微软雅黑', 15), fill='yellow')
self.cv.pack()
for i in range(5):
tm = Tm(self.cv, 1300, self.height)
tm.create_tm()
self.tms.append(tm)
self.run_tm()
for j in range(20):
boll = Ball(self.cv, self.height)
boll.create_boll()
self.bs.append(boll)
self.run_boll()
self.win.mainloop()
def run_boll(self):
for k in self.bs:
k.yd_boll()
self.cv.after(20, self.run_boll)
def run_tm(self):
for t in self.tms:
t.yd_tm()
self.cv.after(5, self.run_tm)
def exit_screen(self, event):
self.win.destroy()
pygame.quit()
if os.path.exists('我曾经爱上一个人.mp3'):
os.remove('我曾经爱上一个人.mp3')
if os.path.exists('tb1.ico'):
os.remove('tb1.ico')
if os.path.exists('aq1.gif'):
os.remove('aq1.gif')
if os.path.exists('tb.png'):
os.remove('tb.png')
window = Tk()
img = PythonMagick.Image('tb.png')
img.sample('128x128')
img.write('tb1.ico')
window.iconbitmap('tb1.ico')
window.title('致青春---.....')
window.geometry('450x300+450+150')
canvas = Canvas(window, height=400, width=600)
imagefile = PhotoImage(file='aq1.gif')
image = canvas.create_image(0, 0, anchor='nw', image=imagefile)
canvas.pack(side='top')
Label(window, text='她的姓名:', font=('宋体', 13), fg='black').place(x=100, y=150)
Label(window, text='她的生日:', font=('宋体', 13), fg='black').place(x=100, y=190)
var_usr_name = StringVar()
entry_usr_name = Entry(window, textvariable=var_usr_name, show='*', highlightcolor='black')
entry_usr_name.place(x=180, y=150)
var_usr_pwd = StringVar()
entry_usr_pwd = Entry(window, textvariable=var_usr_pwd, show='*', highlightcolor='black')
entry_usr_pwd.place(x=180, y=190)
def usr_log_in():
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
if usr_name == 'lyp' and usr_pwd == '520':
messagebox.showwarning('身份验证成功')
window.destroy()
time.sleep(0.1)
bf()
SCreen(3)
else:
messagebox.showerror('身份验证错误!')
def usr_sign_quit():
window.destroy()
bt_login = Button(window, text='登录', fg='black', command=usr_log_in)
bt_login.place(x=140, y=230)
bt_logquit = Button(window, text='退出', fg='black', command=usr_sign_quit)
bt_logquit.place(x=280, y=230)
window.mainloop()
运行效果如下所示
输入正确后,弹幕效果如下按ESC即可退出,本代码自主开发一个星期,仅为纪念逝去的青春!
点击此处登录后即可评论