1

我想用 wxPython 制作一个游戏(没有其他模块),我想制作它,以便您可以在游戏开始之前在弹出屏幕中输入一些值,然后游戏将被绘制在画布上,而画布又被绘制在一个面板,绑定到主游戏。

我用所有花哨的东西制作了游戏屏幕(单独工作)我制作了输入屏幕但我无法链接它们。

我如何开始游戏让它打开一个对话框,然后在关闭它时打开另一个对话框,然后打开游戏?

我尝试了以下方法,但它不会打开我的画布:

# makes a game by showing 2 dialogs
# after dialogs have been answered, starts the game by drawing the canvas.

# imports  
import wx
import Speelveld3

# globals
SCRWIDTH = 950
SCRHEIGHT = 700

# dialogbox class
class MyDialog1(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent)

        self.username = wx.TextCtrl(self)
        self.okButton = wx.Button(self, wx.ID_OK, "OK")

class MyDialog2(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent)

        self.canvasWidth = wx.TextCtrl(self)
        self.okButton = wx.Button(self, wx.ID_OK, "OK")

# main class
class Game(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, title='My game', size=(SCRWIDTH, SCRHEIGHT))
        self.username = ""
        self.canvasWidth = 10
        # hide the frame for now
        self.Hide()

    def OnInit(self):
        #Make your dialogs
        dlg1 = MyDialog1(self)
        #if the user pressed "OK" (i.e. NOT "Cancel" or any other button you might add)
        if dlg1.ShowModal() == wx.ID_OK:
            #get the username from the dialog
            self.username = dlg1.username.GetValue()
        #clean up the dialog (AFTER you get the username)
        dlg1.Destroy()

        dlg2 = MyDialog2(self)
        #if the user pressed "OK" (i.e. NOT "Cancel" or any other button you might add)
        if dlg2.ShowModal() == wx.ID_OK:
            #get the username from the dialog
            self.canvasWidth = dlg2.canvasWidth.GetValue()
        #clean up the dialog (AFTER you get the username)
        dlg2.Destroy()



        # Now that you have your settings, Make the gameboard
        # THIS PART IS STILL BROKEN!
        # I can paste the whole board class (structure of it is taken from the tetris tutorial)
        # but that seems a bit much tbh...
        self.gameBoard = Board.Board(self)
        self.gameBoard = SetFocus()
        self.gameBoard.start()

        self.Centre()
        self.Show(True) #show the frame





if __name__ == '__main__':
# how can I start the game here?
    app = wx.App()
    frame = Game()
    board = Speelveld3.Speelveld(frame)
    board.start()
    frame.Show()
    app.MainLoop()
4

2 回答 2

1

您已经重复发布了,并且wx.Dialog您的示例代码中没有任何内容向我表明您甚至还没有看过教程,但我会给您带来怀疑的好处。

首先,如果要从对话框返回信息,最简单的方法是定义自定义对话框。定义一个继承自的新类,wx.Dialog然后像普通面板或框架一样对其进行设置。在我看来,您将需要其中两个。它们看起来像这样:

class MyDialog1(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent)

        self.username = wx.TextCtrl(self) #this is where users will enter their username

        self.okButton = wx.Button(self, wx.ID_OK, "OK") #Note that I'm using wx.ID_OK. This is important

现在,对于您想要的逻辑。几乎你实际看到的 wxPython 中的每个对象都有函数Show()Hide()(API here)。在对话框完成之前,您不想显示您的框架,因此在您的__init__(), 调用Hide()中。我还在初始化一个变量 ,username我将在其中存储对话框中的数据。

class Game(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(SCRWIDTH, SCRHEIGHT))
        self.username = ""

        self.Hide() #don't show the frame just yet
        #self.Hide() is the exact same as self.Show(False)

现在,对于您的对话框。就像 Mike Driscoll 建议的那样,在制作画布之前调用对话框。wx.Dialogs使用ShowModal(). 通过将 的 ID 设置为self.okButton常量wx.ID_OK,wxPython 识别出对话框应该在单击按钮后关闭。您还应该了解wx.ID_CANCEL.

def OnInit(self):
    #Make your dialogs
    dlg1 = MyDialog1(self)
    if dlg1.ShowModal() == wx.ID_OK:
        #if the user pressed "OK" (i.e. NOT "Cancel" or any other button you might add)
        self.username = dlg1.username.GetValue() #get the username from the dialog
    dlg1.Destroy() #clean up the dialog (AFTER you get the username)

    #do this again for your second dialog

    #Now that you have your settings, Make the gameboard
    self.gameBoard = Board.Board(self)
    self.gameBoard = SetFocus()
    self.gameBoard.start()

    self.Centre()
    self.Show(True) #show the frame
于 2012-06-28T01:48:17.180 回答
1

在您的 OnInit 中,您只需要在创建 Board 实例之前调用您的对话框并以模态方式显示它们。然后它应该可以正常工作。

编辑(6-28-12):这是一些代码:

import wx

########################################################################
class MyDlg(wx.Dialog):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Dialog.__init__(self, None, title="I'm a dialog!")

        lbl = wx.StaticText(self, label="Hi from the panel's init!")
        btn = wx.Button(self, id=wx.ID_OK, label="Close me")

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(lbl, 0, wx.ALL, 5)
        sizer.Add(btn, 0, wx.ALL, 5)
        self.SetSizer(sizer)


########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        # show a custom dialog
        dlg = MyDlg()
        dlg.ShowModal()
        dlg.Destroy()

        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def OnPaint(self, evt):
        pdc = wx.PaintDC(self)
        try:
            dc = wx.GCDC(pdc)
        except:
            dc = pdc
        rect = wx.Rect(0,0, 100, 100)
        for RGB, pos in [((178,  34,  34), ( 50,  90)),
                         (( 35, 142,  35), (110, 150)),
                         ((  0,   0, 139), (170,  90))
                         ]:
            r, g, b = RGB
            penclr   = wx.Colour(r, g, b, wx.ALPHA_OPAQUE)
            brushclr = wx.Colour(r, g, b, 128)   # half transparent
            dc.SetPen(wx.Pen(penclr))
            dc.SetBrush(wx.Brush(brushclr))
            rect.SetPosition(pos)
            dc.DrawRoundedRectangleRect(rect, 8)

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Example frame")

        # show a MessageDialog
        style = wx.OK|wx.ICON_INFORMATION
        dlg = wx.MessageDialog(parent=None, 
                               message="Hello from the frame's init", 
                               caption="Information", style=style)
        dlg.ShowModal()
        dlg.Destroy()

        # create panel
        panel = MyPanel(self)

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    frame.Show()
    app.MainLoop()
于 2012-06-27T20:43:54.677 回答