File: pygamezero.py

package info (click to toggle)
mu-editor 1.0.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,048 kB
  • sloc: python: 16,322; makefile: 129; xml: 29; sh: 11
file content (188 lines) | stat: -rw-r--r-- 6,762 bytes parent folder | download | duplicates (3)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
The PyGameZero mode for the Mu editor.

Copyright (c) 2015-2017 Nicholas H.Tollervey and others (see the AUTHORS file).

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
import os
import logging
from mu.modes.base import BaseMode
from mu.modes.api import PYTHON3_APIS, SHARED_APIS, PI_APIS, PYGAMEZERO_APIS
from mu.resources import load_icon


logger = logging.getLogger(__name__)


class PyGameZeroMode(BaseMode):
    """
    Represents the functionality required by the PyGameZero mode.
    """

    name = _('Pygame Zero')
    description = _('Make games with Pygame Zero.')
    icon = 'pygamezero'
    runner = None
    builtins = ['clock', 'music', 'Actor', 'keyboard', 'animate', 'Rect',
                'ZRect', 'images', 'sounds', 'mouse', 'keys', 'keymods',
                'exit', 'screen']

    def actions(self):
        """
        Return an ordered list of actions provided by this module. An action
        is a name (also used to identify the icon) , description, and handler.
        """
        return [
            {
                'name': 'play',
                'display_name': _('Play'),
                'description': _('Play your Pygame Zero game.'),
                'handler': self.play_toggle,
                'shortcut': 'F5',
            },
            {
                'name': 'images',
                'display_name': _('Images'),
                'description': _('Show the images used by Pygame Zero.'),
                'handler': self.show_images,
                'shortcut': 'Ctrl+Shift+I',
            },
            {
                'name': 'fonts',
                'display_name': _('Fonts'),
                'description': _('Show the fonts used by Pygame Zero.'),
                'handler': self.show_fonts,
                'shortcut': 'Ctrl+Shift+F',
            },
            {
                'name': 'sounds',
                'display_name': _('Sounds'),
                'description': _('Show the sounds used by Pygame Zero.'),
                'handler': self.show_sounds,
                'shortcut': 'Ctrl+Shift+N',
            },
            {
                'name': 'music',
                'display_name': _('Music'),
                'description': _('Show the music used by Pygame Zero.'),
                'handler': self.show_music,
                'shortcut': 'Ctrl+Shift+M',
            },
        ]

    def api(self):
        """
        Return a list of API specifications to be used by auto-suggest and call
        tips.
        """
        return SHARED_APIS + PYTHON3_APIS + PI_APIS + PYGAMEZERO_APIS

    def play_toggle(self, event):
        """
        Handles the toggling of the play button to start/stop a script.
        """
        if self.runner:
            self.stop_game()
            play_slot = self.view.button_bar.slots['play']
            play_slot.setIcon(load_icon('play'))
            play_slot.setText(_('Play'))
            play_slot.setToolTip(_('Play your Pygame Zero game.'))
            self.set_buttons(modes=True)
        else:
            self.run_game()
            if self.runner:
                play_slot = self.view.button_bar.slots['play']
                play_slot.setIcon(load_icon('stop'))
                play_slot.setText(_('Stop'))
                play_slot.setToolTip(_('Stop your Pygame Zero game.'))
                self.set_buttons(modes=False)

    def run_game(self):
        """
        Run the current game.
        """
        # Grab the Python file.
        tab = self.view.current_tab
        if tab is None:
            logger.debug('There is no active text editor.')
            self.stop_game()
            return
        if tab.path is None:
            # Unsaved file.
            self.editor.save()
        if tab.path:
            # If needed, save the script.
            if tab.isModified():
                self.editor.save_tab_to_file(tab)
            logger.debug(tab.text())
            envars = self.editor.envars
            args = ['-m', 'pgzero']
            self.runner = self.view.add_python3_runner(tab.path,
                                                       self.workspace_dir(),
                                                       interactive=False,
                                                       envars=envars,
                                                       python_args=args)
            self.runner.process.waitForStarted()

    def stop_game(self):
        """
        Stop the currently running game.
        """
        logger.debug('Stopping script.')
        if self.runner:
            self.runner.process.kill()
            self.runner.process.waitForFinished()
            self.runner = None
        self.view.remove_python_runner()

    def show_images(self, event):
        """
        Open the directory containing the image assets used by PyGame Zero.

        This should open the host OS's file system explorer so users can drag
        new files into the opened folder.
        """
        image_dir = os.path.join(self.workspace_dir(), 'images')
        self.view.open_directory_from_os(image_dir)

    def show_fonts(self, event):
        """
        Open the directory containing the font assets used by PyGame Zero.

        This should open the host OS's file system explorer so users can drag
        new files into the opened folder.
        """
        image_dir = os.path.join(self.workspace_dir(), 'fonts')
        self.view.open_directory_from_os(image_dir)

    def show_sounds(self, event):
        """
        Open the directory containing the sound assets used by PyGame Zero.

        This should open the host OS's file system explorer so users can drag
        new files into the opened folder.
        """
        sound_dir = os.path.join(self.workspace_dir(), 'sounds')
        self.view.open_directory_from_os(sound_dir)

    def show_music(self, event):
        """
        Open the directory containing the music assets used by PyGame Zero.

        This should open the host OS's file system explorer so users can drag
        new files into the opened folder.
        """
        sound_dir = os.path.join(self.workspace_dir(), 'music')
        self.view.open_directory_from_os(sound_dir)