2016-04-26 75 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

相关问题