Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/creactigame/_templates/+package+/activity.py_tmpl
blob: 62a9a8ba07d63a11ccd4a1d208769be61e5217c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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)