首先,我很抱歉我的英语语法错误......这不是我的母语..
这是代码:
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()
正如你所看到的,我创造了一个&#34;重启&#34;应该重启游戏的按钮 - 我的意思是从一开始就再次玩这个游戏。
以粗体显示(&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&#34;&# 34;在这条线之前和之后)应该是重新开始游戏的命令 - 但我把它放在那里&#39;传递&#39;因为我不知道如何使它发挥作用。
我试过了:
Frame.__init__()
但它没有工作.. 你能帮助我吗?非常感谢你... :)
编辑:
根据@Mike Driscoll告诉我的内容,我尝试创建一个初始化变量并将小部件返回到第一个值的函数,然后在用户点击&#34;重启时调用此函数!&#34;按钮..
这是函数(我把它作为&#39; Frame&#39;类中的一种方法):
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 :(得分:0)
只需添加重启游戏时调用的重置/重启功能即可。在该函数中,您只需将各种变量重置为零或其初始状态应该是什么。您还可以清除该功能中需要的任何小部件。