Guest User

Untitled

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