Python GUI案例之看图猜成语开发(第二篇)

Python GUI案例之看图猜成语开发(第一篇)
Python GUI案例之看图猜成语开发(第三篇)
Python GUI案例之看图猜成语开发(完结篇)


前言

我们将要实现这些功能:

  • 一,游戏首页页面:在首页页面里需要实现绘制一个看图猜成语文字的标题,定义两个按钮功能(开始游戏,退出游戏),还有一个输入游戏昵称的功能并且要对昵称进行验证是否为空,才能开始游戏;
  • 二,游戏选择模式页面:在首页点击开始游戏后,进入游戏的选择模式页面,分为训练模式和闯关模式两种;
  • 三,游戏训练模式页面:将成语图片加载后,只实现猜成语功能(一张图片,一个输入框,一个按钮)和回答的准确率;
  • 四,游戏闯关模式页面:将实现自定义有多少个关卡数,16个汉字提示(12个随机生成的干扰汉字),游戏通关记录所用的时间。

(素材提取:https://download.csdn.net/download/qq_59142194/85827790


看图猜成语小程序开发(第二篇)


游戏选择模式页面

这个页面很简单,只在页面上创建两个标签字样,分为训练模式和闯关模式,并实现标签绑定鼠标点击事件功能。

效果实现

在这里插入图片描述

代码实现
l1.bind(“”,self.game_train_mode)、l2.bind(“”, self.game_chuangguan_mode)主要实现绑定鼠标点击功能(鼠标左键)。

#游戏模式选择页面
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='训练模式', 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='闯关模式', 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):
        print('游戏训练模式')
    def game_chuangguan_mode(self,event):
        print('游戏闯关模式')

游戏训练模式页面

将成语图片加载后,只实现猜成语功能(一张图片,一个输入框,一个按钮)和回答的准确率。
在这里插入图片描述

其实在实现上面的功能前,我把其中这部分单独提取了出来,这样可以方便在后面的闯关模式中直接调用(因为在这两种模式下,它们的背景加载、上面字样提示及返回按钮都是共有的)。

在这里插入图片描述

代码实现
这里面的代码也很简单。

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)

好了,我们提取出共有的功能后就可以开始完成训练模式的功能了。
主要是实现回答的准确率地计算
我们先定义两个变量
answer_times = 0 # 记录回答总次数
answer_correct_times = 0 # 记录回答正确次数

先创建一个标签(self.accuracy_lable2)用于显示准确率,在输入框中输入成语后键盘回车或者鼠标点击按钮绑定下面这个answer()方法。实现记录只要执行一次这个answer()方法则answer_times += 1(# 记录回答总次数),如果我们在输入框中输入的答案成语和真实的答案一致,就将answer_correct_times += 1( # 记录回答正确次数),反之,回答错误(answer_correct_times)就不加1。
这样我们就可以通过answer_correct_times / answer_times来进行计算准确率
(round((self.answer_correct_times / self.answer_times) * 100, 2) ,百分比并保留两位小数)

并且回答正确后也将自动进行到下一关,执行(self.loading_idiom_img()方法)。

	self.answer_idiom_entry.bind("<Return>", lambda event: self.answer())
    answer_times = 0  # 记录回答总次数
    answer_correct_times = 0  # 记录回答正确次数
    # 判断答案是否正确
    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="回答错误!!!\n是否继续回答?") == '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) * 100, 2)}%')

所以还要写个self.loading_idiom_img()方法,方法里面每次加载出的图片是由random.choice(os.listdir(‘…/看图猜成语’))看图猜成语文件夹下随机选取的一张图片。

#加载成语图片
    def loading_idiom_img(self):
        self.loading_img_times += 1
        self.idiom = random.choice(os.listdir('../看图猜成语'))
        self.result = self.idiom.split('.')[0]
        print('答案:',self.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} 关')

到了这里,游戏训练模式中主要代码就这些了,至于这部分的完整代码就不cv下来了,到后面我会把全部的代码贴出。

猜你喜欢

转载自blog.csdn.net/qq_59142194/article/details/125535398