Python實(shí)戰(zhàn)之看圖猜字游戲的實(shí)現(xiàn)
導(dǎo)語
看圖猜成語,是考驗(yàn)一個(gè)人的反應(yīng)能力,也考驗(yàn)一個(gè)人的右腦思維。
據(jù)說越聰明的人,這道題的完成率越高。你想試一試嘛?今天就給你這次機(jī)會(huì)啦!
哈嘍!大家下午好,我是即將出場(chǎng)每天瘋狂碼代碼的木木子。
為了想一下今天給大家更新什么內(nèi)容的我,想了整整一個(gè)晚上,順便熬了一個(gè)通宵的熊貓眼,在今天晚上快6點(diǎn)下班之前終于趕完了最后的游戲代碼。每天都在代碼的海洋里來來回回,痛并快樂喆.....(其實(shí)上班只有痛苦,主要是舍不得那個(gè)money,只能選擇肝了鴨)大實(shí)話.jpg
最近很多網(wǎng)友又在問小編要成語游戲玩,我知道大家已經(jīng)把前面的(往期也有很多游戲PYhton代碼的哈)給玩穿了,所以今天再給大家?guī)硪豢钊碌腜ython代碼小程序猜成語游戲《瘋狂猜成語》,這款游戲小編覺得是目前(我努力了真的)成語游戲里面做得最為漂亮的,最古樸的,畫風(fēng)很簡(jiǎn)單,希望大家不要丟菜葉子跟臭雞蛋,23333......
當(dāng)然了游戲本身的挑戰(zhàn)非常不簡(jiǎn)單了,所以喜歡挑戰(zhàn)的朋友一定別錯(cuò)過,歡迎大家一起來玩兒哈,看看誰能問鼎看圖猜成語的頂峰呢?評(píng)論區(qū)看看你們誰的分?jǐn)?shù)最6吧!
本文主要分為兩塊大的內(nèi)容代碼:首先是爬取成語的圖片,然后做一個(gè)游戲界面。
一、爬取成語圖片
1)運(yùn)行環(huán)境
本文用到的環(huán)境如下——
Python3、Pycharm社區(qū)版,第三方模塊:requests、pillow、fake_useragent等部分自帶的庫只
要安裝完 Python就可以直接使用了
一般安裝:pip install +模塊名
鏡像源安裝:pip install -i pypi.douban.com/simple/+模塊名…
(之前有說過安裝報(bào)錯(cuò)的幾種方式跟解決方法,不會(huì)安裝的可以去看下,還有很多國內(nèi)鏡像源也有文章的)
2)代碼展示
import requests import re import os import time from PIL import Image from fake_useragent import UserAgent # 爬取成語圖片 def SpiderIdiomPictures(): cookies = { 'BAIDU_SSP_lcr': 'https://www.baidu.com/link?url=58oz4AEVxDWXanBqrfF95dogUPcAVAktBQT0uBu8o4rGPY4J4Kg_-DsmJdvTHryfy8pdGnnOjDG54qbh82KB7K&wd=&eqid=ecc1cb040001afcc0000000662a84cc7', 'Hm_lvt_8754302607a1cfb0d1d9cddeb79c593d': '1654580566,1655196891', 'Hm_lpvt_8754302607a1cfb0d1d9cddeb79c593d': '1655200014', } headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Referer': 'http://www.hydcd.com/cy/fkccy/index3.htm', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36', } if not os.path.exists('./看圖猜成語'): os.mkdir('./看圖猜成語') idx = 0 for page in range(1,3): #爬取2頁的圖片 print(f'\n\033[31m<<<第{page}頁爬取中……>>>\033[0m') if page == 1: page = '' url = f'http://www.hydcd.com/cy/fkccy/index{page}.htm' session = requests.session() response = session.get(url=url, cookies=cookies, headers=headers) response.encoding = response.apparent_encoding ###解析圖片url(http://www.hydcd.com/cy/fkccy/images/CF91100-50.png)和成語 for i in re.findall('<img border="0" src="(.*?)"></p>',response.text): result = i.split('"') if len(result) > 2: img_url = f'http://www.hydcd.com/cy/fkccy/{result[0]}' #圖片url idiom_name = result[2] #圖片名字(成語名) if len(idiom_name) == 4: headers['User-Agent'] = UserAgent().Chrome with open(f'./看圖猜成語/{idiom_name}.png','wb') as f: f.write(session.get(img_url,headers=headers).content) print(f'{idiom_name}.png 保存成功?。?!') time.sleep(0.3) idx += 1 print(f'\n抓取完畢?。?!\n總共抓取\033[31m{idx}張\033[0m圖片') # 圖片放大 def ImageProcessingBig(): print(f'\n\033[31m開始將圖片進(jìn)行放大>>>\033[0m') for imgfile in os.listdir('./看圖猜成語/'): if len(imgfile.split('.')) == 2: # 待處理圖片路徑 img_path = Image.open('./看圖猜成語/'+imgfile) # resize圖片大小,入口參數(shù)為一個(gè)tuple,新的圖片的大小 img_size = img_path.resize((300, 300)) # 處理圖片后存儲(chǔ)路徑,以及存儲(chǔ)格式 imgname = imgfile.split('.')[0] img_size.save(f'./看圖猜成語/{imgname}.png') print(f'\n\033[31m<<<所有圖片已放大完成!??!\033[0m') if __name__ == '__main__': # 爬取網(wǎng)站上的成語圖片(http://www.hydcd.com/cy/fkccy/index.htm),圖片大小120x120 SpiderIdiomPictures() # 把爬取到的所有圖片放大(300x300) ImageProcessingBig()
3)效果展示
這里是有一千多張圖片的哦!
?讓我們看下爬下來的圖片是怎么樣子的吧:
有很多重復(fù)的,因?yàn)橄螺d了幾次了哈。
二、看圖猜成語
1)素材準(zhǔn)備
2)代碼實(shí)現(xiàn)
import ttkbootstrap as ttk import sys,os,random,threading,time,datetime from ttkbootstrap.constants import * from ttkbootstrap.dialogs import Messagebox,Querybox class ttkbootstrapWindow: # 實(shí)例化創(chuàng)建應(yīng)用程序窗口 root = ttk.Window(title="看圖猜成語", themename="litera", resizable=(False, False)) # 讓窗口居中 def window_middle(self,windowwidth,windowheight): screenwidth = self.root.winfo_screenwidth() screenheight = self.root.winfo_screenheight() locx = int((screenwidth - windowwidth) / 2) locy = int((screenheight - windowheight) / 2) self.root.geometry("{}x{}+{}+{}".format(windowwidth, windowheight, locx, locy)) # 顯示窗口 def window_displaymodule(self): self.root.mainloop() # 看圖猜成語 class guessIdiomsFromPictures(ttkbootstrapWindow): def __init__(self): super().__init__() self.index() self.window_displaymodule() # 首頁內(nèi)容 def index(self): self.window_middle(windowwidth=960,windowheight=540) #窗口大小寬x高(960 x 540),默認(rèn)居中 self.index_frame = ttk.Frame(self.root) self.index_frame.pack(fill=BOTH,expand=YES) self.bg_img = ttk.PhotoImage(file='./sucai/index_bg.png') self.bg_img_Label = ttk.Label(self.index_frame, image=self.bg_img) self.bg_img_Label.pack(fill=BOTH,expand=YES) self.title_lable = ttk.Label(self.index_frame,text=' 看圖猜成語', font=('華文行楷',56,'italic'),cursor='watch', background='#E7CBB5', bootstyle=WARNING,width=14) self.begin_button_img = ttk.PhotoImage(file='./sucai/beginGame.png') self.entry_nickname = ttk.Entry(self.index_frame, show=None, font=('微軟雅黑', 16)) self.begin_button = ttk.Button(self.index_frame, bootstyle=(SUCCESS, "outline-toolbutton"),image=self.begin_button_img,command=self.begin_game) self.exit_button_img = ttk.PhotoImage(file='./sucai/exitGame.png') self.exit_button = ttk.Button(self.index_frame, bootstyle=(SUCCESS, "outline-toolbutton"),image=self.exit_button_img,command=self.exit_game) self.index_move() # 頁面組件移動(dòng) def index_move(self): def run(rate): rate += 5 button_posy = 540 - rate*1.5 self.begin_button.place(x=270,y=button_posy) self.exit_button.place(x=480,y=button_posy+10) if rate < 80: self.title_lable.place(x=190, y=rate) self.title_lable.after(60,run ,rate % 80) elif 80<= rate < 150: self.title_lable.after(60, run, rate % 150) else: ttk.Label(self.bg_img_Label, text='請(qǐng)輸入昵稱:', cursor='watch', bootstyle=DARK).place(x=250, y=212) self.entry_nickname.insert('0', "暴龍戰(zhàn)士之王") self.entry_nickname.place(x=340, y=200, width=360, height=50) run(0) # 驗(yàn)證昵稱是否為空 def index_verify(self): self.nickname = self.entry_nickname.get().strip() if self.nickname: return True else: return False # 開始游戲 def begin_game(self): try: if not self.index_verify(): Messagebox.show_info(message="請(qǐng)先輸入您的昵稱!") return self.index_frame.destroy() game_modeSelection_page(self.nickname) except: pass # 退出游戲 def exit_game(self): sys.exit() #游戲模式選擇頁面 class game_modeSelection_page(ttkbootstrapWindow): def __init__(self,nickname): super().__init__() self.nickname = nickname self.page() def page(self): self.window_middle(500,300) self.frame = ttk.Frame(self.root) self.frame.pack(fill=BOTH, expand=YES) self.bg = ttk.PhotoImage(file='./sucai/bg2.png') ttk.Label(self.frame,anchor='nw', image=self.bg).pack() l1 = ttk.Label(self.frame,text='訓(xùn)練模式', font=('華文行楷', 32),relief=RAISED,cursor='hand2',bootstyle=WARNING,background='#324762') l1.place(x=150,y=60) l1.bind("<Button-1>",self.game_train_mode) l2 = ttk.Label(self.frame, text='闖關(guān)模式', font=('華文行楷', 32),relief=RAISED,cursor='hand2',bootstyle=SUCCESS,background='#324762') l2.place(x=150, y=140) l2.bind("<Button-1>", self.game_chuangguan_mode) def game_train_mode(self,event): self.frame.destroy() game_train_page(self.nickname) def game_chuangguan_mode(self,event): # 默認(rèn)10個(gè)關(guān)卡(initialvalue=10) number = Querybox.get_integer(prompt="請(qǐng)?jiān)O(shè)置關(guān)卡數(shù)量:",title="自定義關(guān)卡數(shù)量 ",initialvalue=10,minvalue=0,maxvalue=50) if number: self.frame.destroy() game_chuangguan_page(self.nickname,number) #在游戲兩種模式中有相同寫法的組件 class game_same_components(ttkbootstrapWindow): def __init__(self): super().__init__() def same_page(self,nickname): self.nickname = nickname self.window_middle(960, 540) self.canvas = ttk.Canvas(self.root) self.canvas.pack(fill=BOTH, expand=YES) self.bg = ttk.PhotoImage(file='./sucai/bg3.png') self.canvas.create_image(0, 35, anchor='nw', image=self.bg) self.canvas.create_rectangle(0, 0, 960, 35, fill='#F4F4F4', outline='#F4F4F4') nickname_lable = ttk.Label(self.canvas, text=f'歡迎:【{self.nickname}】玩家上線', font=20, bootstyle=INFO,background='#F4F4F4') nickname_lable.place(x=960, y=4) def nickname_lable_move(rate): rate += 5 nickname_lable.place(x=960 - rate, y=4) if rate < 960: nickname_lable.after(50, nickname_lable_move, rate % 960) nickname_lable_move(0) self.return_button_img = ttk.PhotoImage(file='./sucai/return.png') self.return_button = ttk.Button(self.canvas, bootstyle=(LIGHT, "outline-toolbutton"), image=self.return_button_img,command=self.return_game_modeSelection_page) self.return_button.place(x=0, y=35) def return_game_modeSelection_page(self): self.canvas.destroy() game_modeSelection_page(self.nickname) #游戲訓(xùn)練模式頁面 class game_train_page(game_same_components): def __init__(self,nickname): super().__init__() self.nickname = nickname self.game_page() loading_img_times = 0 # 記錄加載圖片次數(shù) answer_times = 0 # 記錄回答總次數(shù) answer_correct_times = 0 # 記錄回答正確次數(shù) #游戲頁面 def game_page(self): self.same_page(self.nickname) self.canvas.create_rectangle(200, 100, 535, 435) self.answer_idiom_entry = ttk.Entry(self.canvas, show=None, font=('微軟雅黑', 32),bootstyle=DANGER) self.answer_idiom_entry.place(x=580,y=190,height=100,width=300) self.answer_idiom_entry.bind("<Return>", lambda event: self.answer()) self.answer_idiom_button_img = ttk.PhotoImage(file='./sucai/ensure.png') answer_idiom_button = ttk.Button(self.canvas, bootstyle=(LIGHT, "outline-toolbutton"),image=self.answer_idiom_button_img,command=self.answer) answer_idiom_button.place(x=635,y=310) see_idiom_button = ttk.Button(self.canvas,text='查看', bootstyle=(PRIMARY, "outline-toolbutton"),command=self.see_answer) see_idiom_button.place(x=480, y=470) self.accuracy_lable1 = ttk.Label(self.canvas,text='正確率:',font=('華文行楷', 20),background='#D3E0E8') self.accuracy_lable1.place(x=600, y=120) self.accuracy_lable2 = ttk.Label(self.canvas, text="0.0%", font=('華文行楷', 20),bootstyle=DANGER,background='#D3E0E8') self.accuracy_lable2.place(x=750, y=120) self.loading_idiom_img() #加載成語圖片 def loading_idiom_img(self): self.loading_img_times += 1 self.idiom = random.choice(os.listdir('./看圖猜成語')) self.idiom_result = self.idiom.split('.')[0] print('答案:',self.idiom_result) self.idiom_img = ttk.PhotoImage(file=f'./看圖猜成語/{self.idiom}') lm = ttk.Label(self.canvas,image=self.idiom_img) lm.place(x=215,y=115) guanqia_lable = ttk.Label(self.canvas, font=('華文行楷', 32),background='#48A6B0') guanqia_lable.place(x=300,y=450) guanqia_lable.config(text=f'第 {self.loading_img_times} 關(guān)') #查看答案 def see_answer(self): Messagebox.show_info(message=self.idiom_result) def answer(self): if self.answer_idiom_entry.get().strip(): self.answer_times += 1 if self.answer_idiom_entry.get().strip() == self.idiom_result: Messagebox.show_info(message="恭喜,回答正確?。?!") self.loading_idiom_img() self.answer_idiom_entry.delete(0,'end') self.answer_correct_times += 1 else: if not Messagebox.yesno(message="回答錯(cuò)誤?。?!\n是否繼續(xù)回答?") == 'Yes': self.loading_idiom_img() self.answer_idiom_entry.delete(0, 'end') self.accuracy_lable2.config(text=f'{round(self.answer_correct_times / self.answer_times, 2) * 100}%') #游戲闖關(guān)模式頁面 class game_chuangguan_page(game_same_components): CLICKTIMES = 0 #點(diǎn)擊次數(shù) TRUEANSWER = '' #答案 IDX = 1 #第幾關(guān),默認(rèn)第1關(guān) def __init__(self, nickname,number): super().__init__() self.nickname = nickname self.idiom_list = random.sample(os.listdir('./看圖猜成語'), number) #隨機(jī)初始化選取20張圖片,用于設(shè)置20個(gè)關(guān)卡 self.game_page() def game_page(self): self.same_page(self.nickname) self.canvas.create_rectangle(150, 100, 485, 435) self.guanqia_lable = ttk.Label(self.canvas,text='第? / ?關(guān)', font=('華文行楷', 32), background='#48A6B0') #顯示關(guān)卡數(shù) self.guanqia_lable.place(x=200, y=40) self.lm = ttk.Label(self.canvas) #用于配置圖片 self.lm.place(x=165, y=115) self.result_label = ttk.Label(self.canvas,text='', font=('華文行楷', 32), background='#A1F8EE', bootstyle=DANGER) # 用于顯示答案的結(jié)果 self.result_label.place(x=40,y=135,height=300) ttk.Button(self.canvas,text='重選', bootstyle=(SUCCESS, "outline-toolbutton"),command=self.update_label).place(x=550,y=470,width=90,height=60) # 重選按鈕 self.create_selection_result_label() self.create_option_text_label() self.loading_idiom_img() self.recording_time() # 創(chuàng)建四個(gè)用于選擇結(jié)果的標(biāo)簽 def create_selection_result_label(self): self.answer_list = [] for i in range(4): label = ttk.Label(self.canvas, text='', font=("微軟雅黑", 35), background='', width=2, cursor='hand2') label.place(x=130 + i * 100, y=450) self.answer_list.append(label) # 創(chuàng)建用于選擇的內(nèi)容標(biāo)簽 def create_option_text_label(self): def click_label(event): if self.CLICKTIMES < 4: self.CLICKTIMES += 1 label_text = event.widget["text"] # 得到標(biāo)簽上的文本 self.answer(label_text) self.label_oop_list = [] # 設(shè)置4行4列的標(biāo)簽 for i in range(4): for j in range(4): label = ttk.Label(self.canvas, text='', font=("微軟雅黑", 35), background='#FFFAE3', width=2,cursor='hand2') label.place(x=510 + j * 100, y=90 + i * 95) label.bind("<Button-1>", click_label) self.label_oop_list.append(label) # 加載成語圖片 def loading_idiom_img(self,): self.idiom = self.idiom_list[self.IDX - 1].split('.')[0] print('答案:', self.idiom) disturb_text_list = [self.GBK2312() for i in range(12)] # 隨機(jī)生成12個(gè)干擾漢字 disturb_text_list.extend([i for i in self.idiom]) for label_oop in self.label_oop_list: text = random.choice(disturb_text_list) disturb_text_list.remove(text) label_oop.configure(text=text) self.guanqia_lable.config(text=f'第 {self.IDX} / {len(self.idiom_list)}關(guān)') self.idiom_img = ttk.PhotoImage(file=f'./看圖猜成語/{self.idiom}.png') self.lm.configure(image=self.idiom_img) def answer(self,label_text): self.answer_list[self.CLICKTIMES - 1].configure(text=label_text) self.TRUEANSWER += label_text if len(self.TRUEANSWER) == 4: if self.TRUEANSWER == self.idiom: t = threading.Thread(target=self.dispaly_answer_result,args=('回\n答\n正\n確',)) t.setDaemon(True) t.start() self.IDX += 1 if self.IDX > len(self.idiom_list): Messagebox.show_info(message=f"恭喜您已通過?。?!\n耗時(shí)為:{self.time_}") self.flag = False self.return_game_modeSelection_page() return self.update_label() self.loading_idiom_img() else: t = threading.Thread(target=self.dispaly_answer_result, args=('回\n答\n錯(cuò)\n誤',)) t.setDaemon(True) t.start() # 顯示回答結(jié)果是否正確 def dispaly_answer_result(self,text): self.result_label.configure(text=text) time.sleep(3) try: self.result_label.configure(text='') except Exception as e: print(e) # 重選 def update_label(self): self.CLICKTIMES = 0 self.TRUEANSWER = '' for i in self.answer_list:i.destroy() self.create_selection_result_label() # 隨機(jī)生成一個(gè)漢字 def GBK2312(self, ): head = random.randint(0xb0, 0xf7) body = random.randint(0xa1, 0xfe) val = f'{head:x}{body:x}' str = bytes.fromhex(val).decode('gb2312') return str # 記錄通關(guān)所耗時(shí) def recording_time(self): self.flag = True # 定義一個(gè)信號(hào)量,用于當(dāng)我們完成游戲通過時(shí),run()結(jié)束循環(huán) time_label = ttk.Label(self.canvas,text='時(shí)長:00:00:00', font=("華文行楷", 15), background='#DAEFE6',bootstyle=DANGER) time_label.place(x=730,y=50) start_time = datetime.datetime.now() def run(): if self.flag: time_label.after(1000, run) update_time = datetime.datetime.now() - start_time self.time_ =f'時(shí)長:{update_time}'.split('.')[0] time_label.configure(text=self.time_) # 不顯示時(shí)長的毫秒值 run() if __name__ == '__main__': guessIdiomsFromPictures()
3)效果展示
游戲界面——
隨機(jī)截圖——
以上就是Python實(shí)戰(zhàn)之看圖猜字游戲的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Python看圖猜字游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python學(xué)習(xí)之while 循環(huán)語句
這篇文章主要給大家介紹了關(guān)于Python中while循環(huán)語句的相關(guān)資料,使用while循環(huán)語句可以解決程序中需要重復(fù)執(zhí)行的操作,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10PyTorch如何使用embedding對(duì)特征向量進(jìn)行嵌入
這篇文章主要介紹了PyTorch如何使用embedding對(duì)特征向量進(jìn)行嵌入問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02python中class(object)的含義是什么以及用法
這篇文章主要介紹了python中class(object)的含義是什么以及用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02python實(shí)現(xiàn)Android與windows局域網(wǎng)文件夾同步
這篇文章主要給大家詳細(xì)介紹了python實(shí)現(xiàn)Android與windows局域網(wǎng)文件夾同步,文中有詳細(xì)的代碼示例和圖文介紹,具有一定的參考價(jià)值,需要的朋友可以參考下2023-09-09PyTorch中tensor.backward()函數(shù)的詳細(xì)介紹及功能實(shí)現(xiàn)
backward()?函數(shù)是PyTorch框架中自動(dòng)求梯度功能的一部分,它負(fù)責(zé)執(zhí)行反向傳播算法以計(jì)算模型參數(shù)的梯度,這篇文章主要介紹了PyTorch中tensor.backward()函數(shù)的詳細(xì)介紹,需要的朋友可以參考下2024-02-02python中concurrent.futures的具體使用
concurrent.futures是Python標(biāo)準(zhǔn)庫的一部分,提供了ThreadPoolExecutor和ProcessPoolExecutor兩種執(zhí)行器,用于管理線程池和進(jìn)程池,通過這些執(zhí)行器,可以簡(jiǎn)化多線程和多進(jìn)程任務(wù)的管理,提高程序執(zhí)行效率2024-09-09Python利用Django如何寫restful api接口詳解
這篇文章主要給大家介紹了關(guān)于Python利用Django如何寫restful api接口的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06淺談django的render函數(shù)的參數(shù)問題
今天小編就為大家分享一篇淺談django的render函數(shù)的參數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10tensorflow2.0的函數(shù)簽名與圖結(jié)構(gòu)(推薦)
這篇文章主要介紹了tensorflow2.0的函數(shù)簽名與圖結(jié)構(gòu),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04