Package rats :: Module render
[hide private]
[frames] | no frames]

Source Code for Module rats.render

  1  #!/usr/bin/env python 
  2  # 
  3  # Toonloop for Python 
  4  # 
  5  # Copyright 2008 Alexandre Quessy & Tristan Matthews 
  6  # <alexandre@quessy.net> & <le.businessman@gmail.com> 
  7  # http://www.toonloop.com 
  8  # 
  9  # Original idea by Alexandre Quessy 
 10  # http://alexandre.quessy.net 
 11  # 
 12  # Toonloop is free software: you can redistribute it and/or modify 
 13  # it under the terms of the GNU General Public License as published by 
 14  # the Free Software Foundation, either version 3 of the License, or 
 15  # (at your option) any later version. 
 16  # 
 17  # Toonloop is distributed in the hope that it will be useful, 
 18  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  # GNU General Public License for more details. 
 21  # 
 22  # You should have received a copy of the gnu general public license 
 23  # along with Toonloop.  If not, see <http://www.gnu.org/licenses/>. 
 24  # 
 25  """ 
 26  Renderer for Pygame using Twisted 
 27  """ 
 28   
 29  import pygame 
 30  from pygame import time 
 31  from twisted.internet import reactor 
 32  from twisted.internet import task 
 33   
34 -class Game(object):
35 """ 36 A pygame game 37 """
38 - def __init__(self):
39 self.renderer = None 40 self.running = True
41
42 - def draw(self):
43 """ 44 Called on every frame 45 """ 46 raise NotImplementedError("You must implement this in child classes.")
47
48 - def process_events(self, pygame_events):
49 """ 50 Called when pygame event occur 51 """ 52 pass
53
54 - def cleanup(self):
55 """ 56 Called just before exiting the application 57 """ 58 pass
59
60 -class Renderer(object):
61 """ 62 Integrates a pygame game and twisted. 63 See http://twistedmatrix.com/pipermail/twisted-python/2002-October/001884.html 64 """
65 - def __init__(self, game, verbose=False):
66 self.clock = time.Clock() 67 self.game = game 68 self.game.renderer = self 69 self.is_verbose = verbose 70 self.desired_fps = 12.0 71 # start 72 self.check_for_events() # starts a metronome 73 self._looping_events_check = None 74 self._setup_events_looping_call() 75 self.update() # starts a metronome
76
77 - def _on_events_error(self, reason):
78 """ 79 ErrBack for the self._looping_events_check LoopingCall. 80 """ 81 reason.printTraceback() 82 #print(reason.getErrorMessage()) 83 self._setup_events_looping_call()
84
86 """ 87 Sets up the self._looping_events_check LoopingCall. 88 """ 89 self._looping_events_check = task.LoopingCall(self.check_for_events) 90 deferred = self._looping_events_check.start(0.01, True) # now=True 91 # FIXME: events are check at 100 fps ! Make configurable. 92 deferred.addErrback(self._on_events_error)
93
94 - def check_for_events(self):
95 """ 96 Check for pygame events and warn the game. 97 :param events: got them using pygame.event.get() 98 """ 99 events = pygame.event.get() 100 self.game.process_events(events)
101 #reactor.callLater(0, self.check_for_events) 102
103 - def update(self):
104 """ 105 Renders one frame sequenced using the Twisted events loop. 106 """ 107 self.clock.tick() 108 self.ms = self.clock.get_rawtime() 109 framespeed = (1.0 / self.desired_fps) * 1000 110 lastspeed = self.ms 111 next = framespeed - lastspeed 112 if self.is_verbose: 113 print "FPS: %5f" % (self.clock.get_fps()) 114 # calls its draw method, which draw and refresh the whole screen. 115 self.game.draw() 116 # FIXME: if an exception is raised here, no more drawing occurs !! 117 if not self.game.running: 118 self._stop() 119 else: 120 when = next / 1000.0 * 2.0 121 if when < 0: 122 when = 0 123 reactor.callLater(when, self.update)
124
125 - def _stop(self):
126 """ 127 Stops the game and quits 128 """ 129 self.game.cleanup() 130 self._looping_events_check.stop() 131 reactor.stop()
132