Paste Code
Paste Blends
Paste Images
### PYGAME IN WX ###
# A simple test of embedding Pygame in a wxPython frame
#
# By David Barker (aka Animatinator), 14/07/2010
# Patch for cross-platform support by Sean McKean, 16/07/2010
# Patch to fix redrawing issue by David Barker, 20/07/2010
# Second window demo added by David Barker, 21/07/2010


import wx, sys, os, pygame

class PygameDisplay(wx.Window):
def __init__(self, parent, ID):
wx.Window.__init__(self, parent, ID)
self.parent = parent
self.hwnd = self.GetHandle()

self.size = self.GetSizeTuple()
self.size_dirty = True

self.timer = wx.Timer(self)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_TIMER, self.Update, self.timer)
self.Bind(wx.EVT_SIZE, self.OnSize)

self.fps = 60.0
self.timespacing = 1000.0 / self.fps
self.timer.Start(self.timespacing, False)

self.linespacing = 5

def Update(self, event):
# Any update tasks would go here (moving sprites, advancing animation frames etc.)
self.Redraw()

def Redraw(self):
if self.size_dirty:
self.screen = pygame.Surface(self.size, 0, 32)
self.size_dirty = False

self.screen.fill((0,0,0))

cur = 0

w, h = self.screen.get_size()
while cur <= h:
pygame.draw.aaline(self.screen, (255, 255, 255), (0, h - cur), (cur, 0))

cur += self.linespacing

s = pygame.image.tostring(self.screen, 'RGB') # Convert the surface to an RGB string
img = wx.ImageFromData(self.size[0], self.size[1], s) # Load this string into a wx image
bmp = wx.BitmapFromImage(img) # Get the image in bitmap form
dc = wx.ClientDC(self) # Device context for drawing the bitmap
dc.DrawBitmap(bmp, 0, 0, False) # Blit the bitmap image to the display
del dc

def OnPaint(self, event):
self.Redraw()
event.Skip() # Make sure the parent frame gets told to redraw as well

def OnSize(self, event):
self.size = self.GetSizeTuple()
self.size_dirty = True

def Kill(self, event):
# Make sure Pygame can't be asked to redraw /before/ quitting by unbinding all methods which
# call the Redraw() method
# (Otherwise wx seems to call Draw between quitting Pygame and destroying the frame)
# This may or may not be necessary now that Pygame is just drawing to surfaces
self.Unbind(event = wx.EVT_PAINT, handler = self.OnPaint)
self.Unbind(event = wx.EVT_TIMER, handler = self.Update, source = self.timer)


class FoolDisplay(PygameDisplay):
def __init__(self, parent, id):
PygameDisplay.__init__(self, parent, id)
pygame.font.init()
self.mainfont = pygame.font.Font(None, 40)
self.text = self.mainfont.render("FOOOOOOL! NOW WE ARE ALL DAMNED!", True, (255, 0, 0))
self.borw = True # True = draw a black background, False = draw a white background
self.points = [] # A list of points to draw

self.Bind(wx.EVT_LEFT_DOWN, self.OnClick)

def Update(self, event):
PygameDisplay.Update(self, event)
self.borw = not self.borw # Alternate the background colour

for i, point in enumerate(self.points): # Slide all the points down and slightly to the right
self.points[i] = (point[0] + 0.1, point[1] + 1)

def Redraw(self):
# If the size has changed, create a new surface to match it
if self.size_dirty:
self.screen = pygame.Surface(self.size, 0, 32)
self.size_dirty = False

# Draw the background
if self.borw:
self.screen.fill((0, 0, 0))
else:
self.screen.fill((255, 255, 255))

self.screen.blit(self.text, (0, 0))

# Draw circles at all the stored points
for point in self.points:
pygame.draw.circle(self.screen, (0, 255, 0), (int(point[0]), int(point[1])), 5)

s = pygame.image.tostring(self.screen, 'RGB') # Convert the surface to an RGB string
img = wx.ImageFromData(self.size[0], self.size[1], s) # Load this string into a wx image
bmp = wx.BitmapFromImage(img) # Get the image in bitmap form
dc = wx.ClientDC(self) # Device context for drawing the bitmap
dc.DrawBitmap(bmp, 0, 0, False) # Blit the bitmap image to the display
del dc

def OnClick(self, event):
self.points.append(event.GetPositionTuple()) # Add a new point at the mouse position


class FoolFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, size = (600, 300), style = wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX)

self.display = FoolDisplay(self, -1)

self.SetTitle("NOOOOOOOO!")


class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, size = (600, 600))

self.display = PygameDisplay(self, -1)

self.statusbar = self.CreateStatusBar()
self.statusbar.SetFieldsCount(3)
self.statusbar.SetStatusWidths([-3, -4, -2])
self.statusbar.SetStatusText("wxPython", 0)
self.statusbar.SetStatusText("Look, it's a nifty status bar!!!", 1)

self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_CLOSE, self.Kill)

self.curframe = 0

self.SetTitle("Pygame embedded in wxPython")

self.slider = wx.Slider(self, wx.ID_ANY, 5, 1, 10, style = wx.SL_HORIZONTAL | wx.SL_LABELS)
self.slider.SetTickFreq(0.1, 1)
self.button = wx.Button(self, -1, "DO NOT PRESS THIS BUTTON")

self.timer = wx.Timer(self)

self.Bind(wx.EVT_SCROLL, self.OnScroll)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_TIMER, self.Update, self.timer)
self.Bind(wx.EVT_BUTTON, self.ButtonClick, self.button)

self.timer.Start((1000.0 / self.display.fps))

self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
self.sizer2.Add(self.slider, 1, flag = wx.EXPAND | wx.RIGHT, border = 5)
self.sizer2.Add(self.button, 0, flag = wx.EXPAND | wx.ALL, border = 5)
self.sizer.Add(self.sizer2, 0, flag = wx.EXPAND)
self.sizer.Add(self.display, 1, flag = wx.EXPAND)

self.SetAutoLayout(True)
self.SetSizer(self.sizer)
self.Layout()

def Kill(self, event):
self.display.Kill(event)
self.Destroy()

def OnSize(self, event):
self.Layout()

def Update(self, event):
self.curframe += 1
self.statusbar.SetStatusText("Frame %i" % self.curframe, 2)

def OnScroll(self, event):
self.display.linespacing = self.slider.GetValue()

def ButtonClick(self, event):
# (Commented code replaces the main display with the 'foooool!' display)
#self.sizer.Detach(self.display)
#self.display.Destroy()
#self.display = FoolDisplay(self, -1)
#self.sizer.Add(self.display, 1, flag = wx.EXPAND)
#self.Layout()

newframe = FoolFrame(self)
newframe.Show()

self.button.SetLabel("YOU WERE WARNED!")
self.Layout()

class App(wx.App):
def OnInit(self):
self.frame = Frame(parent = None)
self.frame.Show()
self.SetTopWindow(self.frame)

return True

if __name__ == "__main__":
app = App()
app.MainLoop()
  1. ### PYGAME IN WX ###
  2. # A simple test of embedding Pygame in a wxPython frame
  3. #
  4. # By David Barker (aka Animatinator), 14/07/2010
  5. # Patch for cross-platform support by Sean McKean, 16/07/2010
  6. # Patch to fix redrawing issue by David Barker, 20/07/2010
  7. # Second window demo added by David Barker, 21/07/2010
  8.  
  9.  
  10. import wx, sys, os, pygame
  11.  
  12. class PygameDisplay(wx.Window):
  13.     def __init__(self, parent, ID):
  14.         wx.Window.__init__(self, parent, ID)
  15.         self.parent = parent
  16.         self.hwnd = self.GetHandle()
  17.        
  18.         self.size = self.GetSizeTuple()
  19.         self.size_dirty = True
  20.        
  21.         self.timer = wx.Timer(self)
  22.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  23.         self.Bind(wx.EVT_TIMER, self.Update, self.timer)
  24.         self.Bind(wx.EVT_SIZE, self.OnSize)
  25.        
  26.         self.fps = 60.0
  27.         self.timespacing = 1000.0 / self.fps
  28.         self.timer.Start(self.timespacing, False)
  29.  
  30.         self.linespacing = 5
  31.  
  32.     def Update(self, event):
  33.         # Any update tasks would go here (moving sprites, advancing animation frames etc.)
  34.         self.Redraw()
  35.  
  36.     def Redraw(self):
  37.         if self.size_dirty:
  38.             self.screen = pygame.Surface(self.size, 0, 32)
  39.             self.size_dirty = False
  40.  
  41.         self.screen.fill((0,0,0))
  42.  
  43.         cur = 0
  44.  
  45.         w, h = self.screen.get_size()
  46.         while cur <= h:
  47.             pygame.draw.aaline(self.screen, (255, 255, 255), (0, h - cur), (cur, 0))
  48.            
  49.             cur += self.linespacing
  50.  
  51.         s = pygame.image.tostring(self.screen, 'RGB')  # Convert the surface to an RGB string
  52.         img = wx.ImageFromData(self.size[0], self.size[1], s)  # Load this string into a wx image
  53.         bmp = wx.BitmapFromImage(img)  # Get the image in bitmap form
  54.         dc = wx.ClientDC(self)  # Device context for drawing the bitmap
  55.         dc.DrawBitmap(bmp, 0, 0, False)  # Blit the bitmap image to the display
  56.         del dc
  57.  
  58.     def OnPaint(self, event):
  59.         self.Redraw()
  60.         event.Skip()  # Make sure the parent frame gets told to redraw as well
  61.  
  62.     def OnSize(self, event):
  63.         self.size = self.GetSizeTuple()
  64.         self.size_dirty = True
  65.  
  66.     def Kill(self, event):
  67.         # Make sure Pygame can't be asked to redraw /before/ quitting by unbinding all methods which
  68.         # call the Redraw() method
  69.         # (Otherwise wx seems to call Draw between quitting Pygame and destroying the frame)
  70.         # This may or may not be necessary now that Pygame is just drawing to surfaces
  71.         self.Unbind(event = wx.EVT_PAINT, handler = self.OnPaint)
  72.         self.Unbind(event = wx.EVT_TIMER, handler = self.Update, source = self.timer)
  73.  
  74.  
  75. class FoolDisplay(PygameDisplay):
  76.     def __init__(self, parent, id):
  77.         PygameDisplay.__init__(self, parent, id)
  78.         pygame.font.init()
  79.         self.mainfont = pygame.font.Font(None, 40)
  80.         self.text = self.mainfont.render("FOOOOOOL! NOW WE ARE ALL DAMNED!", True, (255, 0, 0))
  81.         self.borw = True  # True = draw a black background, False = draw a white background
  82.         self.points = []  # A list of points to draw
  83.        
  84.         self.Bind(wx.EVT_LEFT_DOWN, self.OnClick)
  85.    
  86.     def Update(self, event):
  87.         PygameDisplay.Update(self, event)
  88.         self.borw = not self.borw  # Alternate the background colour
  89.        
  90.         for i, point in enumerate(self.points):  # Slide all the points down and slightly to the right
  91.             self.points[i] = (point[0] + 0.1, point[1] + 1)
  92.    
  93.     def Redraw(self):
  94.         # If the size has changed, create a new surface to match it
  95.         if self.size_dirty:
  96.             self.screen = pygame.Surface(self.size, 0, 32)
  97.             self.size_dirty = False
  98.        
  99.         # Draw the background
  100.         if self.borw:
  101.             self.screen.fill((0, 0, 0))
  102.         else:
  103.             self.screen.fill((255, 255, 255))
  104.        
  105.         self.screen.blit(self.text, (0, 0))
  106.        
  107.         # Draw circles at all the stored points
  108.         for point in self.points:
  109.             pygame.draw.circle(self.screen, (0, 255, 0), (int(point[0]), int(point[1])), 5)
  110.        
  111.         s = pygame.image.tostring(self.screen, 'RGB')  # Convert the surface to an RGB string
  112.         img = wx.ImageFromData(self.size[0], self.size[1], s)  # Load this string into a wx image
  113.         bmp = wx.BitmapFromImage(img)  # Get the image in bitmap form
  114.         dc = wx.ClientDC(self)  # Device context for drawing the bitmap
  115.         dc.DrawBitmap(bmp, 0, 0, False)  # Blit the bitmap image to the display
  116.         del dc
  117.    
  118.     def OnClick(self, event):
  119.         self.points.append(event.GetPositionTuple())  # Add a new point at the mouse position
  120.  
  121.  
  122. class FoolFrame(wx.Frame):
  123.     def __init__(self, parent):
  124.         wx.Frame.__init__(self, parent, -1, size = (600, 300), style = wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX)
  125.        
  126.         self.display = FoolDisplay(self, -1)
  127.        
  128.         self.SetTitle("NOOOOOOOO!")
  129.  
  130.  
  131. class Frame(wx.Frame):
  132.     def __init__(self, parent):
  133.         wx.Frame.__init__(self, parent, -1, size = (600, 600))
  134.        
  135.         self.display = PygameDisplay(self, -1)
  136.        
  137.         self.statusbar = self.CreateStatusBar()
  138.         self.statusbar.SetFieldsCount(3)
  139.         self.statusbar.SetStatusWidths([-3, -4, -2])
  140.         self.statusbar.SetStatusText("wxPython", 0)
  141.         self.statusbar.SetStatusText("Look, it's a nifty status bar!!!", 1)
  142.        
  143.         self.Bind(wx.EVT_SIZE, self.OnSize)
  144.         self.Bind(wx.EVT_CLOSE, self.Kill)
  145.        
  146.         self.curframe = 0
  147.        
  148.         self.SetTitle("Pygame embedded in wxPython")
  149.        
  150.         self.slider = wx.Slider(self, wx.ID_ANY, 5, 1, 10, style = wx.SL_HORIZONTAL | wx.SL_LABELS)
  151.         self.slider.SetTickFreq(0.1, 1)
  152.         self.button = wx.Button(self, -1, "DO NOT PRESS THIS BUTTON")
  153.        
  154.         self.timer = wx.Timer(self)
  155.        
  156.         self.Bind(wx.EVT_SCROLL, self.OnScroll)
  157.         self.Bind(wx.EVT_SIZE, self.OnSize)
  158.         self.Bind(wx.EVT_TIMER, self.Update, self.timer)
  159.         self.Bind(wx.EVT_BUTTON, self.ButtonClick, self.button)
  160.        
  161.         self.timer.Start((1000.0 / self.display.fps))
  162.        
  163.         self.sizer = wx.BoxSizer(wx.VERTICAL)
  164.         self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
  165.         self.sizer2.Add(self.slider, 1, flag = wx.EXPAND | wx.RIGHT, border = 5)
  166.         self.sizer2.Add(self.button, 0, flag = wx.EXPAND | wx.ALL, border = 5)
  167.         self.sizer.Add(self.sizer2, 0, flag = wx.EXPAND)
  168.         self.sizer.Add(self.display, 1, flag = wx.EXPAND)
  169.        
  170.         self.SetAutoLayout(True)
  171.         self.SetSizer(self.sizer)
  172.         self.Layout()
  173.  
  174.     def Kill(self, event):
  175.         self.display.Kill(event)
  176.         self.Destroy()
  177.  
  178.     def OnSize(self, event):
  179.         self.Layout()
  180.  
  181.     def Update(self, event):
  182.         self.curframe += 1
  183.         self.statusbar.SetStatusText("Frame %i" % self.curframe, 2)
  184.  
  185.     def OnScroll(self, event):
  186.         self.display.linespacing = self.slider.GetValue()
  187.    
  188.     def ButtonClick(self, event):
  189.         # (Commented code replaces the main display with the 'foooool!' display)
  190.         #self.sizer.Detach(self.display)
  191.         #self.display.Destroy()
  192.         #self.display = FoolDisplay(self, -1)
  193.         #self.sizer.Add(self.display, 1, flag = wx.EXPAND)
  194.         #self.Layout()
  195.        
  196.         newframe = FoolFrame(self)
  197.         newframe.Show()
  198.        
  199.         self.button.SetLabel("YOU WERE WARNED!")
  200.         self.Layout()
  201.  
  202. class App(wx.App):
  203.     def OnInit(self):
  204.         self.frame = Frame(parent = None)
  205.         self.frame.Show()
  206.         self.SetTopWindow(self.frame)
  207.        
  208.         return True
  209.  
  210. if __name__ == "__main__":
  211.     app = App()
  212.     app.MainLoop()
  213.  
go to heaven