0

编辑:我只是将所有问题合并为一个大问题:https ://stackoverflow.com/questions/12604139/need-tutorial-for-menubar-handling-panel-building

我有一个“更大”的问题:我是 python 新手,我开始创建一个小程序->“边做边学”;)我的程序可以工作(它还不是太大),但代码已经有点令人困惑:我只有一个类和很多很多功能,它们相互引用。所以我的问题是:谁能给我提示我如何构建我的代码?我想我必须创建更多的课程......但是在哪里?我应该将代码的哪一部分合并到一个新类中?关于我的程序的简短描述(完成后应该做什么):这是一种足球//足球经理游戏;您创建了一个由 3 名不同属性的球员组成的球队,您可以指导他们(提高他们的属性)并与其他(计算机)球队比赛。

# -*- coding: cp1252 -*-
import wx

class myclass(wx.Frame):

    def __init__(self,parent,id):
        self.title='Click Kick'
        bgcolour=(170,255,170)

        wx.Frame.__init__(self,parent,id,self.title,size=wx.DisplaySize())
        self.displayw=wx.DisplaySize()[0]
        self.displayh=wx.DisplaySize()[1]

        self.startpanel=wx.Panel(self,size=wx.DisplaySize())
        titlefont = wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL, False, u'Consolas')
        self.SetBackgroundColour(bgcolour)
        self.dateinewgamepanel=wx.Panel(self,size=wx.DisplaySize())
        self.dateiloadgamepanel=wx.Panel(self,size=wx.DisplaySize())
        self.teamoverviewpanel=wx.Panel(self,size=wx.DisplaySize())
        self.trainingpanel=wx.Panel(self,size=wx.DisplaySize())
        self.spielpanel=wx.Panel(self,size=wx.DisplaySize())
        self.regelnpanel=wx.Panel(self,size=wx.DisplaySize())
        self.infopanel=wx.Panel(self,size=wx.DisplaySize())

        self.teamname=''

        ID_newgame=01
        ID_loadgame=02
        ID_overview=11
        ID_training=21
        ID_spiel=31
        ID_regeln=41
        ID_ueber=42
        #status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        self.datei=wx.Menu()
        self.team=wx.Menu()
        self.training=wx.Menu()
        self.spiel=wx.Menu()
        self.info=wx.Menu()
        self.datei.Append(ID_newgame,"Neues Spiel")
        self.datei.Append(ID_loadgame,"Spiel laden...")
        self.team.Append(ID_overview,"Übersicht")
        self.training.Append(ID_training,"Trainieren")
        self.spiel.Append(ID_spiel,"Spielen")
        self.info.Append(ID_regeln,"Regeln")
        self.info.Append(ID_ueber,"Info")
        menubar.Append(self.datei,"Datei")
        menubar.Append(self.team,"Mannschaft")
        menubar.Append(self.training,"Training")
        menubar.Append(self.spiel,"Spiel")
        menubar.Append(self.info,"Info")
        self.SetMenuBar(menubar)

        self.titletext=wx.StaticText(self.startpanel, -1, self.title, (220,130))
        self.titletext.SetFont(titlefont)
        self.newgame = wx.Button(self.startpanel,label="Neues Spiel",pos=(215,160),size=(80,40))
        self.loadgame = wx.Button(self.startpanel,label="Spiel laden",pos=(305,160),size=(80,40))

        self.Bind(wx.EVT_BUTTON, self.dateinewgamepanelbuild, self.newgame)
        self.Bind(wx.EVT_BUTTON, self.dateiloadgamepanelbuild, self.loadgame)

        wx.EVT_MENU(self, ID_newgame, self.dateinewgamepanelbuild)
        wx.EVT_MENU(self, ID_loadgame, self.dateiloadgamepanelbuild)
        wx.EVT_MENU(self, ID_overview, self.teamoverviewpanelbuild)
        wx.EVT_MENU(self, ID_ueber, self.infopanelbuild)


    def hideallpanels(self):
        self.startpanel.Hide()
        self.dateinewgamepanel.Hide()
        self.dateiloadgamepanel.Hide()
        self.teamoverviewpanel.Hide()
        self.trainingpanel.Hide()
        self.spielpanel.Hide()

    def infopanelbuild(self,event):
        self.hideallpanels()
        self.infopanel.Show()
        wx.StaticText(self.infopanel,-1,"Autor: Steffen Becker")

    def dateinewgamepanelbuild(self,event):
        # panel neu zusammensetzen (mit aktuellen Werten), panel zeigen, alle anderen panels verstecken
        w=0
        h=-20
        self.hideallpanels()
        self.dateinewgamepanel.Show()
        #wx.StaticBitmap(self.dateinewgamepanel).SetBitmap(wx.Bitmap('pics/defaultplayer.bmp'))
        wx.StaticText(self.dateinewgamepanel, -1, "Neues Spiel", (5+w,25+h)).SetFont(wx.Font(16, wx.MODERN, wx.NORMAL, wx.BOLD))
        wx.StaticText(self.dateinewgamepanel, -1, "Teamname:", (self.displayw/4+10+w,self.displayh/10+20+h))
        self.teamnameinput = wx.TextCtrl(self.dateinewgamepanel, pos=(self.displayw/4+10+w,self.displayh/8+20+h), size=(280,22))

        wx.StaticText(self.dateinewgamepanel, -1, "Name 1. Spieler", (self.displayw/16+w,self.displayh/2+h))
        self.player1input = wx.TextCtrl(self.dateinewgamepanel, pos=(self.displayw/16+w,self.displayh/2+20+h), size=(130,20))
        wx.StaticText(self.dateinewgamepanel, -1, "Spielertyp", (self.displayw/16+w,self.displayh/2+60+h))
        self.player1typeinput = wx.Choice(self.dateinewgamepanel,id=-1,pos=(self.displayw/16+w,self.displayh/2+80+h))
        player1typelist = ['Dribbler', 'Spielgestalter', 'Balleroberer']
        self.player1typeinput.AppendItems(strings=player1typelist)
        self.player1typeinput.SetSelection(0)
        picplayer1=wx.Image("pics\defaultplayer.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        picbutplayer1=wx.BitmapButton(self.dateinewgamepanel,-1,picplayer1,pos=(self.displayw/16+w,self.displayh/2-140+h))

        wx.StaticText(self.dateinewgamepanel, -1, "Name 2. Spieler", (self.displayw*5/16+w,self.displayh/2+h))
        self.player2input = wx.TextCtrl(self.dateinewgamepanel, pos=(self.displayw*5/16+w,self.displayh/2+20+h), size=(130,20))
        wx.StaticText(self.dateinewgamepanel, -1, "Spielertyp", (self.displayw*5/16+w,self.displayh/2+60+h))
        self.player2typeinput = wx.Choice(self.dateinewgamepanel,id=-1,pos=(self.displayw*5/16+w,self.displayh/2+80+h))
        player2typelist = ['Dribbler', 'Spielgestalter', 'Balleroberer']
        self.player2typeinput.AppendItems(strings=player2typelist)
        self.player2typeinput.SetSelection(0)
        picplayer2=wx.Image("pics\defaultplayer.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        picbutplayer2=wx.BitmapButton(self.dateinewgamepanel,-1,picplayer2,pos=(self.displayw*5/16+w,self.displayh/2-140+h))

        wx.StaticText(self.dateinewgamepanel, -1, "Name 3. Spieler", (self.displayw*9/16+w,self.displayh/2+h))
        self.player3input = wx.TextCtrl(self.dateinewgamepanel, pos=(self.displayw*9/16+w,self.displayh/2+20+h), size=(130,20))
        wx.StaticText(self.dateinewgamepanel, -1, "Spielertyp", (self.displayw*9/16+w,self.displayh/2+60+h))
        self.player3typeinput = wx.Choice(self.dateinewgamepanel,id=-1,pos=(self.displayw*9/16+w,self.displayh/2+80+h))
        player3typelist = ['Dribbler', 'Spielgestalter', 'Balleroberer']
        self.player3typeinput.AppendItems(strings=player3typelist)
        self.player3typeinput.SetSelection(0)
        picplayer3=wx.Image("pics\defaultplayer.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        picbutplayer3=wx.BitmapButton(self.dateinewgamepanel,-1,picplayer3,pos=(self.displayw*9/16+w,self.displayh/2-140+h))


        #bmp = wx.Image('pics/defaultplayer.bmp', wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        #wx.StaticBitmap(self.dateinewgamepanel, -1, bmp, (1100, 500))

        self.playerok = wx.Button(self.dateinewgamepanel,label="Team erstellen",pos=(self.displayw*5/16-20+w,self.displayh/2+170+h),size=(170,30))
        self.Bind(wx.EVT_BUTTON, self.onteamok, self.playerok)
        self.teamnameerror=wx.StaticText(self.dateinewgamepanel, -1, "Gib bitte überall einen Namen ein!", (self.displayw*5/16-15+w,self.displayh/2+210+h))
        self.teamnameerror.Hide()

    def dateiloadgamepanelbuild(self,event):
        self.hideallpanels()
        self.dateiloadgamepanel.Show()        
        wx.StaticText(self.dateiloadgamepanel, -1, "Noch nicht implementiert", (20,70))

    def teamoverviewpanelbuild(self,event):
        self.hideallpanels()
        self.teamoverviewpanel.Show()        
        wx.StaticText(self.teamoverviewpanel, -1, self.teamname, (120,10))
        wx.StaticText(self.teamoverviewpanel, -1, self.player1, (40,40))
        wx.StaticText(self.teamoverviewpanel, -1, self.player2, (120,40))
        wx.StaticText(self.teamoverviewpanel, -1, self.player3, (200,40))


    def onteamok(self,event):
        if self.teamnameinput.GetValue() == '' or self.player1input.GetValue() == '' or self.player2input.GetValue() == '' or self.player3input.GetValue() == '':
            self.teamnameerror.Show()
        else:
            self.teamname = self.teamnameinput.GetValue()
            self.player1 = self.player1input.GetValue()
            self.player2 = self.player2input.GetValue()
            self.player3 = self.player3input.GetValue()
            self.teamoverviewpanelbuild(event)

    def onloadgame(self,event):
        self.startpanel.Hide()
        self.dateinewgamepanel.Hide()
        self.dateiloadgamepanel.Show()





if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=myclass(parent=None,id=-1)
    frame.Show()
    app.MainLoop()
4

1 回答 1

2

以下是我的建议:

  • 您已经熟悉从wx.Frame. 您可以wx.Panel对检索玩家的数据执行相同的操作,并根据需要对其进行参数化。这将消除代码重复并使其更方便。

    class PlayerPanel(wx.Panel):
        def __init__(self, name, *args, **kwargs):
            wx.Panel.__init__(self, *args, **kwargs)
            wx.StaticText(self, -1, name)
            ..etc...
    
  • wx.Frame从游戏创建和实际玩的方面派生出另一个类并将其分开;摆脱隐藏/显示面板。

  • 了解布局管理。当我想熟悉你的代码时,小部件位置的绝对计算对我来说是不可接受的。Zetcode上有很好的例子。

  • 将所有游戏逻辑重构为单独的模块。该模块可以包含 MVC(模型-视图-控制器)模式的模型和控制类。视图是用 wxPython 编写的,游戏逻辑可以在一个单独的类中,而模型是例如玩家的另一个类(姓名,年龄,...)。

  • 使用 awx.MessageBox表示错误消息和 about 对话框。

于 2012-09-24T18:39:02.887 回答