diff options
Diffstat (limited to 'creactigame/_templates/+package+/activity.py_tmpl')
-rw-r--r-- | creactigame/_templates/+package+/activity.py_tmpl | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/creactigame/_templates/+package+/activity.py_tmpl b/creactigame/_templates/+package+/activity.py_tmpl new file mode 100644 index 0000000..62a9a8b --- /dev/null +++ b/creactigame/_templates/+package+/activity.py_tmpl @@ -0,0 +1,134 @@ +# python import +import logging +from gettext import gettext as _ + + + +# gtk import +import gtk + +# pygame import +import pygame + +# sugar import +from sugar.activity import activity +from sugar.graphics.toolbutton import ToolButton + +# sugargame import +import sugargame.canvas + +# get application logger +logger = logging.getLogger('{{package}}') + + +class {{package.capitalize()}}Game(object): + + def __init__(self): + # Set up a clock for managing the frame rate. + self.clock = pygame.time.Clock() + # pos + self.x = -100 + self.y = 100 + # .. + self.vx = 10 + self.vy = 0 + # pause flag + self.paused = False + + def set_paused(self, paused): + self.paused = paused + + # Called to save the state of the game to the Journal. + def write_file(self, file_path): + pass + + # Called to load the state of the game from the Journal. + def read_file(self, file_path): + pass + + # The main game loop. + def run(self): + # run flag + self.running = True + # get screen + screen = pygame.display.get_surface() + # game loop + while self.running: + # Pump GTK messages. + while gtk.events_pending(): + gtk.main_iteration() + # Pump PyGame messages. + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return + elif event.type == pygame.VIDEORESIZE: + pygame.display.set_mode(event.size, pygame.RESIZABLE) + # Move the ball + if not self.paused: + # .. + self.x += self.vx + if self.x > screen.get_width() + 100: + self.x = -100 + # .. + self.y += self.vy + if self.y > screen.get_height() - 100: + self.y = screen.get_height() - 100 + self.vy = -self.vy + # .. + self.vy += 5; + # Clear Display + screen.fill((255,255,255)) #255 for white + # Draw the ball + pygame.draw.circle(screen, (255,0,0), (self.x, self.y), 100) + # Flip Display + pygame.display.flip() + # Try to stay at 30 FPS + self.clock.tick(30) + + +class {{package.capitalize()}}Activity(activity.Activity): + + def __init__(self, handle): + super({{package.capitalize()}}Activity, self).__init__(handle) + # .. + self.paused = False + # Create the game instance. + self.game = {{package.capitalize()}}Game() + # Build the activity toolbar. + self.build_toolbar() + # Build the Pygame canvas. + self._pygamecanvas = sugargame.canvas.PygameCanvas(self) + # Note that set_canvas implicitly calls read_file when resuming from the Journal. + self.set_canvas(self._pygamecanvas) + # Start the game running (self.game.run is called when the activity constructor returns). + self._pygamecanvas.run_pygame(self.game.run) + + def build_toolbar(self): + stop_play = ToolButton('media-playback-stop') + stop_play.set_tooltip(_("Stop")) + stop_play.set_accelerator(_('<ctrl>space')) + stop_play.connect('clicked', self._stop_play_cb) + toolbar = gtk.Toolbar() + toolbar.insert(stop_play, 0) + toolbox = activity.ActivityToolbox(self) + toolbox.add_toolbar(_("Pygame"), toolbar) + toolbox.show_all() + self.set_toolbox(toolbox) + + def _stop_play_cb(self, button): + # Pause or unpause the game. + self.paused = not self.paused + self.game.set_paused(self.paused) + # Update the button to show the next action. + if self.paused: + button.set_icon('media-playback-start') + button.set_tooltip(_("Start")) + else: + button.set_icon('media-playback-stop') + button.set_tooltip(_("Stop")) + + def read_file(self, file_path): + self.game.read_file(file_path) + + def write_file(self, file_path): + self.game.write_file(file_path) |