程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Cant you go out to play on the Lantern Festival? Teach you to use Python to do a lantern riddle game

編輯:Python

Introduction

The new year ushers in the Lantern Festival , The Lantern Festival is accompanied by family members , Naturally, there are also lanterns , Guessing lantern riddles . Of course , Affected by the epidemic , Many places can't go out to enjoy the lanterns this year , Guess lantern riddles .

But don't worry , Xiaobian used it last night Python I made a small program to guess lantern riddles, so that we can enjoy the fun of guessing lantern riddles


1. Riddle guessing interface

Let's take a look at how the little program for guessing lantern riddles plays . First look at the renderings :

2. The program on

1). Construct the initial interface

For the construction of program interface , Use of is python3.6 Install under version 2.0.1 Version of pygame library . The initialization function of the interface is as follows :

  Program initialization function , It mainly sets the size of the interface , name , Multiple... Are constructed at the same time Lantern Class object , Used in self.Run Function is displayed in the interface .

2). Construct lantern riddle class objects

lantern (Lantern) Class object , The display on the interface , In fact, they are small lanterns , The effect is shown below :

  And for Lantern class , The program implementation is as follows :

 antern Class , Constructor is used to set the coordinates of the upper left corner where the lantern is placed on the interface , as well as Lantern Class length and width information . about self.getPos function , Is used to return Lantern Object coordinates and length and width information ,self.judgeCollision The function is used to detect whether the mouse click is within the range of the lantern , In the subsequent self.Run Function will be useful .

3). Monitor mouse events

stay self.Run Function , The program monitors the action of the mouse through continuous cycles , To realize the display of riddles .

  stay self.Run Function , The program goes through a continuous cycle , Display all the lanterns in the interface , meanwhile , By monitoring mouse events , To determine whether the mouse is pressed , If the mouse is pressed , The program obtains the coordinate information of the mouse , Then check the coverage of all lanterns , If the click coordinate is under the coverage of a certain Lantern , Then a riddle will be displayed under the corresponding lantern , As shown in the figure below .

You are in the interface , You can click any lantern , Then a riddle will pop up at the bottom , But here's the thing , There are only ten riddles in the program , And after the ten riddles are displayed , It won't be shown again .

I don't know

import requests
import re
import sqlite3,time
#------------- Constant region ---------------
conn=sqlite3.connect('zimi.db')
c=conn.cursor()
into='''insert into lib values (NULL ,"%s" ,"%s" )'''
res=requests.session()
h={
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36'
}
dic={}
#---------------- Function area -------------
# Get puzzle function
def getmidi(ul):
url='https://zhongxue.hujiang.com'+ul
s=res.get(url,headers=h).text
x=re.findall('<p><span class="miti"> The mystery :</span><a href="/zidian/[^"]+" class="red">(.*?)</a></p>',s)[0]
time.sleep(1)# Access interval
return x
# Get list function
def getlist(page):
url='https://zhongxue.hujiang.com/zidian/caizimi/page'+str(page)+'/'
s=res.get(url,headers=h).text
x=re.findall('<a href="(/zidian/zimi/\d+/)">(.*?)</a>',s)
for i in x:
mimian=i[1]
ul=i[0]
try:
midi=getmidi(ul)
dic.update({mimian:midi})
except:
print(mimian,ul)
#--------- The main function -----------------
if __name__ == '__main__':
#---- Get a list of all puzzles ----
for i in range(21):
getlist(i+1)
print(i)
#---- hold dic Write to database ----
for i in dic:
try:
c.execute(into % (i, dic[i]))
conn.commit()
except:
print(i, dic[i])
conn.close()

Happy Lantern Festival

import sqlite3
conn=sqlite3.connect('zimi.db')
c=conn.cursor()
cre="create table '%s' (id integer PRIMARY KEY autoincrement,mimian TEXT NOT NULL ,midi TEXT );"
c.execute(cre%('lib'))
conn.commit()
conn.close()

end

Well, that's the end of this period. Like the iron juice, please take it and try it yourself ~ Finally, I wish you all a happy Lantern Festival !! Remember to make up a series for Xiao before you leave !

Want to get more complete source code and Python For learning materials, click on this line


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved