2016-04-26 5 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によると、変数を初期化し、ウィジェットを最初の値に戻し、ユーザーが「再起動!」ボタンをクリックすると、この関数を呼び出す関数を作成しようとしました。ここで

は、関数は(私は「フレーム」クラスのメソッドの一つとして、それを置く)です:ボタンをクリックすると

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を使用してコードを実行しようとすると、私は はあなたが...はい、私は、このオプション.. について知っているが、私はそれは私のコードは、非効率的にするつもりと思ったのXubuntu 14.04 –

答えて

0

ゲームを再起動するときに呼び出すリセット/再開機能を追加するだけです。この関数では、さまざまな変数を0にリセットするか、初期状態が想定されているものをリセットします。また、その機能で必要なウィジェットをクリアすることもできます。

+0

上のPython 2.7、セグメンテーション違反を得ます私のクラスの変数を再起動するためのより速い方法はありませんか? – omer

+0

私のポストの編集を見てください。 – omer

+0

Nevermind ..私はそれを修正しました.. – omer

関連する問題