2016-04-26 31 views
0

首先我對我的英語語法錯誤抱歉..這不是我的母語..如何使用wxpython中的按鈕重新啓動遊戲?所有的

這是代碼:

import wx 
import wx.grid as gridlib 
from random import randint 

OPTIONS = [1, 2, 3, 4, 5, 6, 7, 8, 9, "DEL", 0, "SEND"] 
# these are the events' IDs sent to a function when you click a button. 
# the OPTIONS_ID is in the same order of OPTIONS. 

OPTIONS_ID = [-31984,-31983,-31982,-31981,-31980,-31979, -31978, -31977, -31976, -31975, -31974, -31973] # the built in wxpython IDs for the buttons 


GAME_POSITION = (400, 100) 
GAME_SIZE = [900, 600] 

def RandomNum(): 
    count = 5 
    while count > 4: 
     num = randint(1000, 9999) 
     digits = str(num) 
     count = 0 
     for digit in digits: 
      for digit2 in digits: 
       if digit == digit2: 
        count = count + 1 
    return digits 

def message_dialog(message, caption, style=wx.OK, position=GAME_POSITION): 
    if message != "": # making sure not to execute a message if its empety 
     message = wx.MessageDialog(None, message, caption, style, position) 
     answer = message.ShowModal() 
     message.Destroy() 
     return answer 
    else: 
     return -1 

class Frame(wx.Frame): # class for all the frames in our game. 

    def __init__(self, parent, id, title, pos, size): 
      wx.Frame.__init__(self, parent, id, title, pos, size) 
      self.panel = wx.Panel(self) 
      self.fdf = wx.TextCtrl(self.panel, size=(275, 75), pos=(520, 20)) 
      self.count = 0 
      self.turnsCounter = 0 
      self.numbers = RandomNum() 
      self.bulls = 0 
      self.cows = 0 
      self.counter_of_turns = 0 
      self.check = False 

      self.grid = gridlib.Grid(self.panel, pos = (85, 150), size=(323, 212)) 
      self.grid.CreateGrid(10, 3) 
      sizer = wx.BoxSizer(wx.VERTICAL) 
      sizer.Add(self.panel, 1, wx.EXPAND) 
      sizer.Add(self.grid, 1, wx.EXPAND) 
      self.panel.SetSizer(sizer) 
      for i in range(10): 
       for j in range(4): 
        self.grid.SetReadOnly(i, j) 
      self.grid.SetColLabelValue(0, "guess") 
      self.grid.SetColLabelValue(1, "cows") 
      self.grid.SetColLabelValue(2, "bulls") 



    # this function creates a textbox at a specific position with a specific size. 
    def write(self, panel, txt, pos, size=20, font_family=wx.SWISS, font_style = wx.NORMAL,font_weight = wx.BOLD, underline = False): 
     # create a textbox at a specific position with a specific size. 
     your_txt = wx.StaticText(panel, -1, txt, pos) 
     your_txt.SetFont(wx.Font(size,font_family,font_style,font_weight,underline)) 
    # same as above, just for a button. 
    def create_button(self, panel, txt, position, width, height): 
     Size = wx.Size(width, height) 
     self.button = wx.Button(panel, -1, txt, position, Size) 
     self.border = wx.BoxSizer(wx.VERTICAL) 
     self.border.Add(self.button) 
     self.Bind(wx.EVT_BUTTON, lambda evt: self.OnButton(evt), self.button) 
    def disable_button(self, panel, txt, position, width, height): 
     Size = wx.Size(width, height) 
     self.button = wx.Button(panel, -1, txt, position, Size) 
     self.border = wx.BoxSizer(wx.VERTICAL) 
     self.border.Add(self.button) 
     self.Bind(wx.EVT_BUTTON, lambda evt: self.OnButton(evt), self.button) 
     self.button.Disable() 

    def count_bulls(self, txtctrl, seria): 
     for i in range(4): 
      if seria[i] == txtctrl[i]: 
       self.bulls += 1 
     replacement = self.bulls 
     self.bulls = 0 
     return replacement 

    def count_cows(self, txtctrl, seria): 
     for i in range(4): 
      if seria[i] != txtctrl[i] and seria[i] in txtctrl: 
       self.cows += 1 
     replacement = self.cows 
     self.cows = 0 
     return replacement 


    def OnButton(self, event): 
     print repr(event.Id) + "," 
     if event.Id in OPTIONS_ID: # if indeed an option button was pressed 
      exited = -1 # exited is 5100 if the user exited his dialog box 
      # assigning the events to the button. 
      for i in range(12): 
       if event.Id != -31975 and event.Id != -31973 and event.Id == OPTIONS_ID[i]: 
        self.fdf.AppendText(str(OPTIONS[i])) 
        self.count += 1 
      if event.Id == -31973: 
       self.counter_of_turns += 1 
       print self.numbers 
       print self.fdf.GetValue() 
       cows = self.count_cows(self.fdf.GetValue(), self.numbers) 
       bulls = self.count_bulls(self.fdf.GetValue(), self.numbers) 
       self.grid.SetCellValue(self.turnsCounter,0, self.fdf.GetValue()) 
       self.grid.SetCellValue(self.turnsCounter, 1, str(cows)) 
       self.grid.SetCellValue(self.turnsCounter, 2, str(bulls)) 
       self.fdf.Clear() 
       self.count = 0 
       if self.turnsCounter < 9: 
        self.turnsCounter += 1 
       if bulls == 4: 
        self.check = True 
        message_dialog("Well done! you won this game..", "You won!") 
      if event.Id == -31975: 
       if self.count > 0: 
        self.count -= 1 
       self.fdf.Remove(self.fdf.GetLastPosition()-1, self.fdf.GetLastPosition()) 
      if self.count == 4: 
       for child in self.panel.GetChildren(): 
        if isinstance(child, wx.Button): 
         try: 
          int(child.GetLabel()) 
         except ValueError: 
          if child.GetLabel() == "SEND": 
           child.Enable() 
         else: 
          child.Disable() 
      elif self.check == False: 
       for child in self.panel.GetChildren(): 
        if child.GetLabel() != "SEND": 
         child.Enable() 
        else: 
         child.Disable() 
        if self.count == 0: 
         if child.GetLabel() == "DEL": 
          child.Disable() 
      else: 
       for child in self.panel.GetChildren(): 
        if isinstance(child, wx.Button): 
         child.Disable() 
      for child in self.panel.GetChildren(): 
       if isinstance(child, wx.Button): 
        if child.GetLabel() in self.fdf.GetValue(): 
         child.Disable() 
      if self.counter_of_turns == 10: 
       message_dialog("you are a looser", "looser!") 
       for child in self.panel.GetChildren(): 
        if isinstance(child, wx.Button): 
         child.Disable() 
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 
      **if event.Id == -31985: 
       pass** 
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 


class Game(wx.App): 
    def OnInit(self): # upon game opening 
     # I would like the options window to be the first window's parent 
     # so I will first set up our options window: 
     window = Frame(None, -1, "Good Luck!", GAME_POSITION, GAME_SIZE) 
     first_panel = window.panel 

     window.write(first_panel, "BULLS AND COWS!", (50, 50), size=(35)) 
     countX = 500 
     countY = 100 
     window.create_button(first_panel,"restart!", (50, 400), 100, 100) 
     for option in OPTIONS: 
      if str(option) == "SEND" or str(option) == "DEL": 
       window.disable_button(first_panel,str(option), (countX, countY), 100, 100) 
      else: 
       window.create_button(first_panel,str(option), (countX, countY), 100, 100) 
      countX += 110 
      if str(option) == "3" or str(option) == "6" or str(option) == "9": 
       countY += 110 
       countX = 500 



     window.Show(True) 
     return True 


def main(): 
    camel = Game() 
    camel.MainLoop() 


if __name__ == '__main__': 
    main() 

正如你所看到的,我創建了一個「重啓「應該重新開始遊戲的按鈕 - 我的意思是從一開始就再次玩這個遊戲。

在粗體行中(「」「」「」「」「」「這行前後)應該是重新啓動遊戲的順序 - 但我把那裏'傳遞',因爲我不知道如何,使其工作

我試過這樣:

Frame.__init__() 

但它沒有工作.. 你們可以幫我這個非常感謝你... :)

編輯:

根據@Mike Driscoll告訴我的內容,我試圖創建一個函數,用於初始化變量並將窗口小部件返回到它們的第一個值,然後在用戶單擊「restart!」按鈕時調用此函數。

下面是函數(我把它作爲在「框架」類的方法之一):

def reset(self): 
     self.fdf.Clear() 
     self.grid.ClearGrid() 
     self.count = 0 
     self.turnsCounter = 0 
     self.numbers = RandomNum() 
     self.bulls = 0 
     self.cows = 0 
     self.counter_of_turns = 0 
     self.check = False 
     for child in self.panel.GetChildren(): 
      if child.GetLabel() != "SEND": 
       child.Enable() 
      else: 
       child.Disable() 
      if self.count == 0: 
       if child.GetLabel() == "DEL": 
        child.Disable() 

,這裏是調用此功能時點擊該按鈕(我把它放在OnButton方法):

if event.Id == -31985: 
       Frame.reset() 

它仍然沒有做任何事東西.. 請幫助:)

+0

我得到一個段錯誤,當我嘗試使用wxPython的2.8.12,Python 2.7版上的Xubuntu 14.04運行代碼 –

回答

0

只需添加您重新啓動遊戲時調用的重置/重新啓動功能。在那個函數中,你只需將各種變量重置爲零或者它們的初始狀態應該是什麼。您還可以清除該功能中需要的任何小部件。

+0

是的,我知道這個選項.. 但我認爲這會讓我的代碼效率不高.. 你確定沒有更快的方法來重新啓動我的班級的變量? – omer

+0

請看我在帖子中的編輯.. – omer

+0

沒關係..我修好了.. – omer

相關問題