使用我的代码,我现在决定需要多个屏幕来在按钮和背景将发生变化的位置之间切换我认为最简单的方法是定义面板类,然后创建一个框架,但我没有知道如何将框架上的所有面板连接在一起。我知道每个面板上我想要哪些按钮和图像,但我不知道您如何定义面板并通过按钮单击链接它们
import os
import pygame
import wx
import os
import game
class MainPanel(wx.Panel):
def __init__(self,parent,id):
image_file='main_screen.jpg'#loading an image file from the folder
bmp=wx.Bitmap(image_file)
self.bitmap = wx.StaticBitmap(self, wx.ID_ANY, bmp, (0, 0))
PlayButton=wx.Bitmap('play.jpg', wx.BITMAP_TYPE_ANY)
self.PlayButton=wx.BitmapButton(self.bitmap, -1, PlayButton, pos=(190,300))
self.PlayButton.Bind=(wx.EVT_BUTTON, self.opengame)
RulesButton=wx.Bitmap('rules.jpg', wx.BITMAP_TYPE_ANY)
self.RulesButton=wx.BitmapButton(self.bitmap, -1, RulesButton, pos=(190,370))
self.RulesButton.Bind=(wx.EVT_BUTTON, self.openrules)
ControlsButton=wx.Bitmap('controls.jpg', wx.BITMAP_TYPE_ANY)
self.ControlsButton=wx.BitmapButton(self.bitmap, -1, ControlsButton, pos=(190,440))
#self.ControlsButton.Bind=(wx.EVT_BUTTON, self.closeMe)
ExitButton=wx.Bitmap('exit.jpg', wx.BITMAP_TYPE_ANY)
self.ExitButton=wx.BitmapButton(self.bitmap,-1,ExitButton,pos=(190,510))
self.ExitButton.Bind(wx.EVT_BUTTON, self.closeexit)
self.Bind(wx.EVT_CLOSE, self.closewindow)
class ControlPanel(wx.Panel):
def __init__(self,parent,id):
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
image_file='controls.jpg'#loading an image file from the folder
bmp=wx.Bitmap(image_file)
self.bitmap2 = wx.StaticBitmap(self, wx.ID_ANY, bmp, (0, 0))
BackButton=wx.Bitmap('back.jpg',wx.BITMAP_TYPE_ANY)
self.BackButton=wx.BitmapButton(self.bitmap2,-1,BackButton, pos=400,100)
self.BackButton.Bind=(wx.EVT_BUTTON, self.goback)
class RulesPanel(wx.Panel):
def __init__(self,parent,id):
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
image_file='rules.jpg'#loading an image file from the folder
bmp=wx.Bitmap(image_file)
self.bitmap3 = wx.StaticBitmap(self, wx.ID_ANY, bmp, (0, 0))
BackButton=wx.Bitmap('back.jpg',wx.BITMAP_TYPE_ANY)
self.BackButton=wx.BitmapButton(self.bitmap3,-1,BackButton, pos=400,100)
self.BackButton.Bind=(wx.EVT_BUTTON, self.goback)
class MainFrame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Compsci Vs. Sheep: The Game',size=(640,640))
def openrules(self,event):
def opengame(self):
game.start()
def opencontrols(self,event):
?
def goback(self,event):
?
def closewindow(self,event):
self.Destroy()
pygame.mixer.quit()
def closeexit
if __name__=='__main__':
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
pygame.mixer.music.load("exorcist.ogg")
pygame.mixer.music.play(-1)#music playing in program
app=wx.PySimpleApp()
frame=menu(parent=None,id=-1)
frame.Show()#shows the screen
app.MainLoop()
这是我的新代码,仍然不起作用
import os
import pygame
import wx
def switch_to(name):
print "Pseudo switch",name
class MainFrame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Compsci Vs. Sheep: The Game',size=(640,640))
self.box = wx.BoxSizer()
self._panels = {}
self._panels['main'] = MainPanel(self, -1)
self._panels['rules'] = RulesPanel(self, -1)
self._panels['rules'].Hide()
self.box.Add(self._panels['main'],1,wx.EXPAND)
self.box.Add(self._panels['rules'],1,wx.EXPAND)
self.SetSizer(self.box)
def switch_panel(self, name):
print "Switching to",name
return
for key, panel in self._panels.iteritems():
if key != name:
panel.Hide()
else:
panel.Show(True)
self.Layout()
class MainPanel(wx.Panel):
def __init__(self,parent,id):
wx.Panel.__init__(self,parent,id=wx.ID_ANY)
image_file='main_screen.jpg'#loading an image file from the folder
bmp=wx.Bitmap(image_file)
self.bitmap = wx.StaticBitmap(self, wx.ID_ANY, bmp, (0, 0))
PlayButton=wx.Bitmap('play.jpg', wx.BITMAP_TYPE_ANY)
self.PlayButton=wx.BitmapButton(self,-1, PlayButton, (190,300), (244,60))
RulesButton=wx.Bitmap('rules.jpg', wx.BITMAP_TYPE_ANY)
self.RulesButton=wx.BitmapButton(self, -1, RulesButton, (190,300), (244,60))
self.RulesButton.Bind=(wx.EVT_BUTTON, parent.switch_panel)
class RulesPanel(wx.Panel):
def __init__(self,parent,id):
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
image_file='rules.jpg'#loading an image file from the folder
bmp=wx.Bitmap(image_file)
self.bitmap = wx.StaticBitmap(self, wx.ID_ANY, bmp, (0, 0))
if __name__=='__main__':
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
pygame.mixer.music.load("exorcist.ogg")
pygame.mixer.music.play(-1)#music playing in program
app = wx.PySimpleApp()
frame = MainFrame(parent=None,id=-1)
frame.Show()#shows the screen
app.MainLoop()