⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hangman.py

📁 Wxpython Implemented on Windows CE, Source code
💻 PY
📖 第 1 页 / 共 2 页
字号:
            while self.left.count(key): self.left.remove(key)
            self.start_new = self.HandleKey(key)
        self.timer.Start(self.delay)

    def Stop(self):
        self.timer.Stop()

    class PlayTimer(wx.Timer):
        def __init__(self,func):
            wx.Timer.__init__(self)
            self.func = func
            self.Start(1000)

        def Notify(self):
            apply(self.func, ())



class HangmanDemoFrame(wx.Frame):
    def __init__(self, wf, parent, id, pos, size):
        wx.Frame.__init__(self, parent, id, "Hangman demo", pos, size)
        self.demo = HangmanDemo(wf, self, -1, wx.DefaultPosition, wx.DefaultSize)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

    def OnCloseWindow(self, event):
        self.demo.timer.Stop()
        self.Destroy()



class AboutBox(wx.Dialog):
    def __init__(self, parent,wf):
        wx.Dialog.__init__(self, parent, -1, "About Hangman", wx.DefaultPosition, (350,450))
        self.wnd = HangmanDemo(wf, self, -1, (1,1), (350,150))
        self.static = wx.StaticText(self, -1, __doc__, (1,160), (350, 250))
        self.button = wx.Button(self, 2001, "OK", (150,420), (50,-1))
        self.Fit()
        self.button.Bind(wx.EVT_BUTTON, self.OnOK)

    def OnOK(self, event):
        self.wnd.Stop()
        self.EndModal(wx.ID_OK)



class MyFrame(wx.Frame):
    def __init__(self, parent, wf):
        self.wf = wf
        wx.Frame.__init__(self, parent, -1, "hangman", wx.DefaultPosition, (400,300))
        self.wnd = HangmanWnd(self, -1)
        menu = wx.Menu()
        menu.Append(1001, "New")
        menu.Append(1002, "End")
        menu.AppendSeparator()
        menu.Append(1003, "Reset")
        menu.Append(1004, "Demo...")
        menu.AppendSeparator()
        menu.Append(1005, "Exit")
        menubar = wx.MenuBar()
        menubar.Append(menu, "Game")
        menu = wx.Menu()
        #menu.Append(1010, "Internal", "Use internal dictionary", True)
        menu.Append(1011, "ASCII File...")
        urls = [ 'wxPython home', 'http://wxPython.org/',
                 'slashdot.org', 'http://slashdot.org/',
                 'cnn.com', 'http://cnn.com',
                 'The New York Times', 'http://www.nytimes.com',
                 'De Volkskrant', 'http://www.volkskrant.nl/frameless/25000006.html',
                 'Gnu GPL', 'http://www.fsf.org/copyleft/gpl.html',
                 'Bijbel: Genesis', 'http://www.coas.com/bijbel/gn1.htm']
        urlmenu = wx.Menu()
        for item in range(0,len(urls),2):
            urlmenu.Append(1020+item/2, urls[item], urls[item+1])
        urlmenu.Append(1080, 'Other...', 'Enter an URL')
        menu.AppendMenu(1012, 'URL', urlmenu, 'Use a webpage')
        menu.Append(1013, 'Dump', 'Write contents to stdout')
        menubar.Append(menu, "Dictionary")
        self.urls = urls
        self.urloffset = 1020
        menu = wx.Menu()
        menu.Append(1090, "About...")
        menubar.Append(menu, "Help")
        self.SetMenuBar(menubar)
        self.CreateStatusBar(2)
        self.Bind(wx.EVT_MENU, self.OnGameNew, id=1001)
        self.Bind(wx.EVT_MENU, self.OnGameEnd, id=1002)
        self.Bind(wx.EVT_MENU, self.OnGameReset, id=1003)
        self.Bind(wx.EVT_MENU, self.OnGameDemo, id=1004)
        self.Bind(wx.EVT_MENU, self.OnWindowClose, id=1005)
        self.Bind(wx.EVT_MENU, self.OnDictFile, id=1011)
        self.Bind(wx.EVT_MENU, self.OnDictURL, id=1020, id2=1020+len(urls)/2)
        self.Bind(wx.EVT_MENU, self.OnDictURLSel, id=1080)
        self.Bind(wx.EVT_MENU, self.OnDictDump, id=1013)
        self.Bind(wx.EVT_MENU, self.OnHelpAbout, id=1090)
        self.wnd.Bind(wx.EVT_CHAR, self.OnChar)
        self.OnGameReset()

    def OnGameNew(self, event):
        word = self.wf.Get()
        self.in_progress = 1
        self.SetStatusText("",0)
        self.wnd.StartGame(word)

    def OnGameEnd(self, event):
        self.UpdateAverages(0)
        self.in_progress = 0
        self.SetStatusText("",0)
        self.wnd.EndGame()

    def OnGameReset(self, event=None):
        self.played = 0
        self.won = 0
        self.history = []
        self.average = 0.0
        self.OnGameNew(None)

    def OnGameDemo(self, event):
        frame = HangmanDemoFrame(self.wf, self, -1, wx.DefaultPosition, self.GetSize())
        frame.Show(True)

    def OnDictFile(self, event):
        fd = wx.FileDialog(self)
        if (self.wf.filename):
            fd.SetFilename(self.wf.filename)
        if fd.ShowModal() == wx.ID_OK:
            file = fd.GetPath()
            self.wf = WordFetcher(file)

    def OnDictURL(self, event):
        item = (event.GetId() - self.urloffset)*2
        print "Trying to open %s at %s" % (self.urls[item], self.urls[item+1])
        self.wf = URLWordFetcher(self.urls[item+1])

    def OnDictURLSel(self, event):
        msg = wx.TextEntryDialog(self, "Enter the URL of the dictionary document", "Enter URL")
        if msg.ShowModal() == wx.ID_OK:
            url = msg.GetValue()
            self.wf = URLWordFetcher(url)
    def OnDictDump(self, event):
        print self.wf.words

    def OnHelpAbout(self, event):
        about = AboutBox(self, self.wf)
        about.ShowModal()
        about.wnd.Stop() # that damn timer won't stop!

    def UpdateAverages(self, has_won):
        if has_won:
            self.won = self.won + 1
        self.played = self.played+1
        self.history.append(self.wnd.misses) # ugly
        total = 0.0
        for m in self.history:
            total = total + m
        self.average = float(total/len(self.history))

    def OnChar(self, event):
        if not self.in_progress:
            #print "new"
            self.OnGameNew(None)
            return
        key = event.KeyCode();
        #print key
        if key >= ord('A') and key <= ord('Z'):
            key = key + ord('a') - ord('A')
        key = chr(key)
        if key < 'a' or key > 'z':
            event.Skip()
            return
        res = self.wnd.HandleKey(key)
        if res == 0:
            self.SetStatusText(self.wnd.message)
        elif res == 1:
            self.UpdateAverages(0)
            self.SetStatusText("Too bad, you're dead!",0)
            self.in_progress = 0
        elif res == 2:
            self.in_progress = 0
            self.UpdateAverages(1)
            self.SetStatusText("Congratulations!",0)
        if self.played:
            percent = (100.*self.won)/self.played
        else:
            percent = 0.0
        self.SetStatusText("p %d, w %d (%g %%), av %g" % (self.played,self.won, percent, self.average),1)

    def OnWindowClose(self, event):
        self.Destroy()



class MyApp(wx.App):
    def OnInit(self):
        if wx.Platform == '__WXGTK__':
            defaultfile = "/usr/share/games/hangman-words"
        elif wx.Platform == '__WXMSW__':
            defaultfile = "c:\\windows\\hardware.txt"
        else:
            defaultfile = ""
        wf = WordFetcher(defaultfile)
        frame = MyFrame(None, wf)
        self.SetTopWindow(frame)
        frame.Show(True)
        return True



if __name__ == '__main__':
    app = MyApp(0)
    app.MainLoop()


#----------------------------------------------------------------------

overview = __doc__


def runTest(frame, nb, log):
    if wx.Platform == '__WXGTK__' or wx.Platform == '__WXMOTIF__':
        defaultfile = "/usr/share/games/hangman-words"
    elif wx.Platform == '__WXMSW__':
        defaultfile = "c:\\windows\\hardware.txt"
    else:
        defaultfile = ""
    wf = WordFetcher(defaultfile)
    win = MyFrame(frame, wf)
    frame.otherWin = win
    win.Show(True)


#----------------------------------------------------------------------




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -