欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

这年头写python代码庆祝生日成为时髦~

时间:2023-05-31
明天就是大年初四,我妈生日,作为一位学过python编程的新时代精神小伙,我要写代码当礼物!

代码的核心思想是用pyautogui记录鼠标坐标并控制鼠标在画图软件上写字,有数控板的话写字会更好看!

**

第一章 收集数据

**

import jsonimport timeimport pyautoguiMyPositionList1 = []MyPositionList2 = []MyPositionList3 = []MyPositionList4 = []MyPositionList5 = []for i in range(3): print(3-i) time.sleep(1)#Iprint("I")for i in range(130): MyPositionX,MyPositionY = pyautogui.position() MyPosition = (MyPositionX, MyPositionY) MyPositionList1.append(MyPosition) time.sleep(0.01)print(MyPositionList1)for i in range(3): print(3-i) time.sleep(1)#loveprint("love")for i in range(270): MyPositionX, MyPositionY = pyautogui.position() MyPosition = (MyPositionX, MyPositionY) MyPositionList2.append(MyPosition) time.sleep(0.01)print(MyPositionList2)for i in range(3): print(3-i) time.sleep(1)#youprint("you")for i in range(270): MyPositionX, MyPositionY = pyautogui.position() MyPosition = (MyPositionX, MyPositionY) MyPositionList3.append(MyPosition) time.sleep(0.01)print(MyPositionList3)#爱心print("左")for i in range(100): MyPositionX, MyPositionY = pyautogui.position() MyPosition = (MyPositionX, MyPositionY) MyPositionList4.append(MyPosition) time.sleep(0.01)print(MyPositionList4)print("右")for i in range(100): MyPositionX, MyPositionY = pyautogui.position() MyPosition = (MyPositionX, MyPositionY) MyPositionList5.append(MyPosition) time.sleep(0.01)print(MyPositionList5)print('over')with open('PositionFile1.json', 'w') as f: json.dump(MyPositionList1, f)with open('PositionFile2.json', 'w') as f: json.dump(MyPositionList2, f)with open('PositionFile3.json', 'w') as f: json.dump(MyPositionList3, f)with open('PositionFile4.json', 'w') as f: json.dump(MyPositionList4, f)with open('PositionFile4.json', 'w') as f: json.dump(MyPositionList5, f)

有时间空余挪鼠标,照着提示拿鼠标画就好了。

*不过代码属实太多,一点也不精简,由于有大量基本重复内容,我们用函数解决。

import jsonimport timeimport pyautoguidef collect(TheWordIWrite,WriteTime,FileName): for i in range(3): print(3-i) time.sleep(1) MyPositionList = [] print(TheWordIWrite) for i in range(WriteTime): MyPositionX, MyPositionY = pyautogui.position() MyPosition = (MyPositionX, MyPositionY) MyPositionList.append(MyPosition) time.sleep(0.01) print(MyPositionList) with open(FileName, 'w') as f: json.dump(MyPositionList, f)collect("I", 130, "PositionFile1.json")collect("love", 270, "PositionFile2.json")collect("you", 270, "PositionFile3.json")collect("左", 110, "PositionFile4.json")collect("右", 110, "PositionFile5.json")

这样,比第一下好多了吧?

第二章 展示

import pyautoguiimport jsonimport timefor i in range(3): print(3-i) time.sleep(1)with open('PositionFile1.json', 'r') as f: poslist = json.load(f) print(poslist) print(type(poslist)) for i in range(len(poslist)): print(poslist[i][0], poslist[i][1]) pyautogui.mouseDown(poslist[i][0], poslist[i][1], button='left') pyautogui.mouseUp(poslist[i][0], poslist[i][1], button='left')with open('PositionFile2.json', 'r') as f: poslist = json.load(f) print(poslist) print(type(poslist)) for i in range(len(poslist)): print(poslist[i][0], poslist[i][1]) pyautogui.mouseDown(poslist[i][0], poslist[i][1], button='left') pyautogui.mouseUp(poslist[i][0], poslist[i][1], button='left')with open('PositionFile3.json', 'r') as f: poslist = json.load(f) print(poslist) print(type(poslist)) for i in range(len(poslist)): print(poslist[i][0], poslist[i][1]) pyautogui.mouseDown(poslist[i][0], poslist[i][1], button='left') pyautogui.mouseUp(poslist[i][0], poslist[i][1], button='left')#换颜色,坐标要自己测试pyautogui.mouseUp(836, 57, button='left')pyautogui.mouseDown(836, 57, button='left')pyautogui.mouseUp(836, 57, button='left')with open('PositionFile4.json', 'r') as f: poslist = json.load(f) print(poslist) print(type(poslist)) for i in range(len(poslist)): print(poslist[i][0], poslist[i][1]) pyautogui.mouseDown(poslist[i][0], poslist[i][1], button='left') pyautogui.mouseUp(poslist[i][0], poslist[i][1], button='left')with open('PositionFile5.json', 'r') as f: poslist = json.load(f) print(poslist) print(type(poslist)) for i in range(len(poslist)): print(poslist[i][0], poslist[i][1]) pyautogui.mouseDown(poslist[i][0], poslist[i][1], button='left') pyautogui.mouseUp(poslist[i][0], poslist[i][1], button='left')

点击运行后记得切到画图软件,或者可以记录图标坐标,自行打开

不过同理很多废话,用函数精简~

import pyautoguiimport jsonimport timefor i in range(3): print(3-i) time.sleep(1)def show(file): with open(file, 'r') as f: poslist = json.load(f) print(poslist) print(type(poslist)) for i in range(len(poslist)): print(poslist[i][0], poslist[i][1]) pyautogui.mouseDown(poslist[i][0], poslist[i][1], button='left') pyautogui.mouseUp(poslist[i][0], poslist[i][1], button='left')show("PositionFile1.json")show("PositionFile2.json")show("PositionFile3.json")pyautogui.mouseUp(836, 57, button='left')pyautogui.mouseDown(836, 57, button='left')pyautogui.mouseUp(836, 57, button='left')show("PositionFile4.json")show("PositionFile5.json")

nice! 看看效果

**

第三章 成品

**

咳咳,内噶,鼠标属实不好写字画画。

或许你可以加粗笔让它看起来更好看,这就给大家自己测试啦~

That’s all
Thank
You.

希望大家点赞鼓励一下~

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。