Use wxPython and pillow to develop jigsaw puzzle games (4)

       The previous article introduced the method of using local images to initialize the game. Through the previous three articles, the main content of the mini-game is almost finished. The last article will introduce the calculation of game time, reset the game and close the window event processing

Calculation of game time

       As for the record of game time, friends who have read the previous articles may also have discovered that this is mainly done by using a multi-thread. The timing starts from the first time the player clicks, and stops when it is judged that the customs clearance is successful. The code is directly below:

def OnClick(self, e):
    ……
    if temp in can_move:
        if self.step == 0: # 如果首次点击,则新启一个线程
            self.time_thread = threading.Thread(target=self.__updateTime)
            self.time_thread.setName('current_thread')
            self.time_thread.start()
        …………
    if self.check_success() == 1:
        self.__stopFlag = 'stop'  # 用于在线程内判断是否停止计时
        dlg = wx.MessageDialog(self, "恭喜闯关成功!", "恭喜", wx.OK)  
        dlg.ShowModal()
        dlg.Destroy()


def __updateTime(self):
    startTime = 0
    while startTime >= 0:
        if self.__stopFlag == 'stop':  # 游戏完成停止
            continue
        if self.time_thread.getName() == 'old_thread': 
            break
        if self.__stopFlag == 'exit': # 游戏退出,关闭窗口
            break
        self.time_label.SetLabel('耗时:' + str(startTime) + 's')
        time.sleep(1) # sleep 1秒后加1
        startTime += 1

reset game

        As the name suggests, resetting the game is to reload the game. What needs to be implemented here is to reset the number of steps and time to zero and recalculate. The picture is still the picture selected before, and the complexity is the level of complexity selected on the current interface.

def reLoad(self, e):
    self.step = 0
    self.step_label.SetLabel('步数:' + str(self.step))   # 步数清0
    self.time_thread.setName('old_thread')  # 停止计时
    self.__stopFlag = 'start'
    self.time_label.SetLabel('耗时:' + str(0) + 's')
    self.random_init() # 根据新的复杂度重新初始化
    self.panel_1.Destroy()  # 清空游戏面板重新初始化
    self.panel_1 = wx.Panel(self, 1, size=(540, 540), pos=(0, 0))
    index = 0
    font = wx.Font(20, wx.FONTFAMILY_MODERN, 0, 90, underline=False, faceName="")
    if self.load_flag == 1:   # 当前游戏界面为自定义图片时,还是重新载入图片拼图游戏
        for i in self.indices:
            btn = wx.BitmapButton(self.panel_1, i, size=(int(540 / self.vac), int(540 / self.vac)),
                                  pos=(int(i % self.vac * (540 / self.vac)), int((i // self.vac) * (540 / self.vac))))
            self.bitmap1_onSize(btn, wx.Image('./Pic/' + str(index) + '.png', wx.BITMAP_TYPE_PNG))
            index += 1
            self.Bind(wx.EVT_BUTTON, self.OnClick, btn)
    else:  # 当前游戏界面为数字拼图时,还是重新载入数字拼图游戏
        for i in self.indices: 
            btn = wx.Button(self.panel_1, i, str(index + 1), size=(int(540 / self.vac), int(540 / self.vac)),
                            pos=(int(i % self.vac * (540 / self.vac)), int((i // self.vac) * (540 / self.vac))))
            index += 1
            btn.SetBackgroundColour("#FFCC66")
            btn.SetFont(font)
            self.Bind(wx.EVT_BUTTON, self.OnClick, btn)

Close window event handler

       During the debugging process, I found a problem. When the window was closed, the thread of the main window had stopped, but the main process was still running. Finally, it was found that the created timing thread did not stop with the destruction of the main window, so here we need Capture the window closing event, and set an exit flag in the event, which is used by the timing thread to determine whether the main window has been closed. Here is the override of the OnCloseWindow method of wx.Frame

def OnCloseWindow(self, event):
    self.__stopFlag = 'exit'
    self.Destroy()

The introduction is here, and the introduction of the mini-game is basically over. Because I am new to python GUI development, there are many flaws in the interface layout and style, please bear with me. If you need to see the complete source code, you can download it through the following link

A jigsaw puzzle game developed with wxPython and pillow

 

Guess you like

Origin blog.csdn.net/wzl19870309/article/details/131724363