Esempio n. 1
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # batch for efficient drawing
        self.batch = graphics.Batch()
        background = graphics.OrderedGroup(0)
        foreground = graphics.OrderedGroup(1)

        self.gamelayer = graphics.OrderedGroup(2)

        toplayer = graphics.OrderedGroup(3)

        # window size
        self.size = self.get_size()

        # start with empty asteroid set
        self.asteroids = set()

        # empty explosions set
        self.explosions = set()

        # background and moving foreground sprites
        self.background = physicalobject.ScaledMovingSprite(img=resources.background_image,
                screensize=self.size, batch=self.batch, group=background)
        self.debris = load.debris(screensize=self.size, batch=self.batch, group=foreground)
        self.splashscreen = load.ClickableSprite(hook_function=self.start,
                img=resources.splashscreen,
                x = self.size[0] / 2.0, y=self.size[1] / 2.0,
                batch=self.batch, group=toplayer)


        # player ship
        self.player = load.ship(screensize=self.size, batch=self.batch, group=self.gamelayer)

        self.score = 0
        self.lives = LIVES
        self.started = False

        self.fps_display = clock.ClockDisplay()

        # Lives and score labels
        self.lives_label = text.Label(font_size=20, text="Lives: %d" % self.lives, x=40, y=self.size[1]-40,
                    batch=self.batch, group=toplayer)
        self.score_label = text.Label(font_size=20, anchor_x='right', text="Score: %d" % self.score, x=self.size[0]-40,
                    y=self.size[1]-40, batch=self.batch, group=toplayer)

        # update frequency
        clock.schedule_interval(self.update, 1 / 120)

        # spawn a new asteroid each second
        clock.schedule_interval(self.spawn_asteroid, 1)

        # add event handlers to the ship and splashscreen
        self.push_handlers(self.player)
        self.push_handlers(self.splashscreen)
Esempio n. 2
0
    def update_batch(self, batch, group):
        self._batch, self._group = batch, group

        shape_group = graphics.OrderedGroup(0, group)
        text_group = graphics.OrderedGroup(1, group)

        for e in self.shapes.values():
            e.update_batch((batch if self.visible else None), shape_group)

        for e in self.elements.values():
            e.update_batch((batch if self.visible else None), text_group)

        self._dirty = True
Esempio n. 3
0
 def _init_groups(self, group):
     if group:
         self.top_group = TextLayoutGroup(group)
         self.background_group = graphics.OrderedGroup(0, self.top_group)
         self.foreground_group = \
             TextLayoutForegroundGroup(1, self.top_group)
         self.foreground_decoration_group = \
             TextLayoutForegroundDecorationGroup(2, self.top_group)
Esempio n. 4
0
def quad(xy, wh, color=[255, 255, 255, 255], batch=None, group=0, blend=False):
    x, y = xy[0], xy[1]
    w, h = wh[0], wh[1]
    verts = ('v2i', (x, y, x + w, y, x + w, y + h, x, y + h))
    color = ('c4B', (color * 4))

    if batch == None: return pyGra.vertex_list(4, verts, color)
    if blend: group = CustomGroup(pyGra.OrderedGroup(group))
    else: group = genGroup(group)

    return batch.add(4, GL_QUADS, group, verts, color)
Esempio n. 5
0
    def __init__(self,
                 origin=[0, 0],
                 grid_width=None,
                 grid_height=None,
                 row_block_number=None,
                 col_block_number=None):

        #self.block_grid_batch = graphics.Batch()
        self.grid_width = grid_width
        self.grid_height = grid_height

        self.blocks_per_row = row_block_number
        self.blocks_per_col = col_block_number

        self.row_v = (row_block_number + 1) * 2
        self.col_v = (col_block_number + 1) * 2

        self.grid_bounds = {
            'from': {
                'x': origin[0],
                'y': origin[1]
            },
            'to': {
                'x': origin[0] + grid_width,
                'y': origin[1] + grid_height
            },
            'block': {
                'width': grid_width // row_block_number,
                'height': grid_height // col_block_number
            }
        }

        self.grid_background_vertex_list = []

        self.row_line_group = graphics.OrderedGroup(1)
        self.col_line_group = graphics.OrderedGroup(1)
        self.block_panel_group = graphics.OrderedGroup(0)
        self.grid_background_group = graphics.OrderedGroup(2)
        self.line_group = graphics.OrderedGroup(2)

        self.row_line_vertices = []
        self.col_line_vertices = []
        self.block_vertices = []

        self.row_markers = []
        self.col_markers = []

        self.row_line_colors = []
        self.col_line_colors = []
        self.block_colors = []

        self.block_panels = {}
        self.grid_set = False
        self.block_set = False
        self.block_vertex_list = None

        self.zero_color = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                                   dtype='int16')

        self.active_blocks = set()

        self.update_loaded = False
Esempio n. 6
0
class TextLayout(object):
    '''Lay out and display documents.

    This class is intended for displaying documents that do not change
    regularly -- any change will cost some time to lay out the complete
    document again and regenerate all vertex lists.

    The benefit of this class is that texture state is shared between
    all layouts of this class.  The time to draw one `TextLayout` may be
    roughly the same as the time to draw one `IncrementalTextLayout`; but
    drawing ten `TextLayout` objects in one batch is much faster than drawing
    ten incremental or scrollable text layouts.

    `Label` and `HTMLLabel` provide a convenient interface to this class.

    :Ivariables:
        `content_width` : int
            Calculated width of the text in the layout.  This may overflow
            the desired width if word-wrapping failed.
        `content_height` : int
            Calculated height of the text in the layout.
        `top_group` : `Group`
            Top-level rendering group.
        `background_group` : `Group`
            Rendering group for background color.
        `foreground_group` : `Group`
            Rendering group for glyphs.
        `foreground_decoration_group` : `Group`
            Rendering group for glyph underlines.

    '''
    _document = None
    _vertex_lists = ()
    _boxes = ()

    top_group = TextLayoutGroup()
    background_group = graphics.OrderedGroup(0, top_group)
    foreground_group = TextLayoutForegroundGroup(1, top_group)
    foreground_decoration_group = \
        TextLayoutForegroundDecorationGroup(2, top_group)

    _update_enabled = True
    _own_batch = False
    _origin_layout = False  # Lay out relative to origin?  Otherwise to box.

    def __init__(self,
                 document,
                 col_width,
                 width=None,
                 height=None,
                 dpi=None,
                 batch=None,
                 group=None):
        '''Create a text layout.

        :Parameters:
            `document` : `AbstractDocument`
                Document to display.
            `col_width` : int
                Fixed column width for glyphs
            `width` : int
                Width of the layout in pixels, or None
            `height` : int
                Height of the layout in pixels, or None
            `dpi` : float
                Font resolution; defaults to 96.
            `batch` : `Batch`
                Optional graphics batch to add this layout to.
            `group` : `Group`
                Optional rendering group to parent all groups this text layout
                uses.  Note that layouts with different
                rendered simultaneously in a batch.

        '''
        self.content_width = 0
        self.content_height = 0

        self.groups = {}
        self._init_groups(group)

        self._col_width = col_width
        if batch is None:
            batch = graphics.Batch()
            self._own_batch = True
        self.batch = batch

        self._width = width
        if height is not None:
            self._height = height

        if dpi is None:
            dpi = 96
        self._dpi = dpi
        self.document = document

    def _parse_distance(self, distance):
        if distance is None:
            return None
        return _parse_distance(distance, self._dpi)

    def begin_update(self):
        '''Indicate that a number of changes to the layout or document
        are about to occur.

        Changes to the layout or document between calls to `begin_update` and
        `end_update` do not trigger any costly relayout of text.  Relayout of
        all changes is performed when `end_update` is called.

        Note that between the `begin_update` and `end_update` calls, values
        such as `content_width` and `content_height` are undefined (i.e., they
        may or may not be updated to reflect the latest changes).
        '''
        self._update_enabled = False

    def end_update(self):
        '''Perform pending layout changes since `begin_update`.

        See `begin_update`.
        '''
        self._update_enabled = True
        self._update()

    dpi = property(lambda self: self._dpi,
                   doc='''Get DPI used by this layout.

    Read-only.

    :type: float
    ''')

    def delete(self):
        '''Remove this layout from its batch.
        '''
        for vertex_list in self._vertex_lists:
            vertex_list.delete()
        self._vertex_lists = []

        for box in self._boxes:
            box.delete(self)

    def draw(self):
        '''Draw this text layout.

        Note that this method performs very badly if a batch was supplied to
        the constructor.  If you add this layout to a batch, you should
        ideally use only the batch's draw method.
        '''
        if self._own_batch:
            self.batch.draw()
        else:
            self.batch.draw_subset(self._vertex_lists)

    def _init_groups(self, group):
        if group:
            self.top_group = TextLayoutGroup(group)
            self.background_group = graphics.OrderedGroup(0, self.top_group)
            self.foreground_group = \
                TextLayoutForegroundGroup(1, self.top_group)
            self.foreground_decoration_group = \
                TextLayoutForegroundDecorationGroup(2, self.top_group)
        # Otherwise class groups are (re)used.

    def _get_document(self):
        return self._document

    def _set_document(self, document):
        if self._document:
            self._document.remove_handlers(self)
            self._uninit_document()
        document.push_handlers(self)
        self._document = document
        self._init_document()

    document = property(
        _get_document, _set_document, '''Document to display.

    For `IncrementalTextLayout` it is far more efficient to modify a document
    in-place than to replace the document instance on the layout.

    :type: `AbstractDocument`
    ''')

    def _get_lines(self):
        len_text = len(self._document.text)
        glyphs = self._get_glyphs()
        owner_runs = runlist.RunList(len_text, None)
        self._get_owner_runs(owner_runs, glyphs, 0, len_text)
        lines = [
            line for line in self._flow_glyphs(glyphs, owner_runs, 0, len_text)
        ]
        self.content_width = 0
        self._flow_lines(lines, 0, len(lines))
        return lines

    def _update(self):
        if not self._update_enabled:
            return

        for _vertex_list in self._vertex_lists:
            _vertex_list.delete()
        for box in self._boxes:
            box.delete(self)
        self._vertex_lists = []
        self._boxes = []
        self.groups.clear()

        if not self._document or not self._document.text:
            return

        lines = self._get_lines()

        colors_iter = self._document.get_style_runs('color')
        background_iter = self._document.get_style_runs('background_color')

        if self._origin_layout:
            left = top = 0
        else:
            left = self._get_left()
            top = self._get_top(lines)

        context = _StaticLayoutContext(self, self._document, colors_iter,
                                       background_iter)
        for line in lines:
            self._create_vertex_lists(left + line.x, top + line.y, line.start,
                                      line.boxes, context)

    def _get_left(self):
        width = self.content_width

        if self._anchor_x == 'left':
            return self._x
        elif self._anchor_x == 'center':
            return self._x - width // 2
        elif self._anchor_x == 'right':
            return self._x - width
        else:
            assert False, 'Invalid anchor_x'

    def _get_top(self, lines):
        if self._height is None:
            height = self.content_height
            offset = 0
        else:
            height = self._height
            if self._content_valign == 'top':
                offset = 0
            elif self._content_valign == 'bottom':
                offset = max(0, self._height - self.content_height)
            elif self._content_valign == 'center':
                offset = max(0, self._height - self.content_height) // 2
            else:
                assert False, 'Invalid content_valign'

        if self._anchor_y == 'top':
            return self._y - offset
        elif self._anchor_y == 'baseline':
            return self._y + lines[0].ascent - offset
        elif self._anchor_y == 'bottom':
            return self._y + height - offset
        elif self._anchor_y == 'center':
            if len(lines) == 1 and self._height is None:
                # This "looks" more centered than considering all of the
                # descent.
                line = lines[0]
                return self._y + line.ascent // 2 - line.descent // 4
            else:
                return self._y + height // 2 - offset
        else:
            assert False, 'Invalid anchor_y'

    def _init_document(self):
        self._update()

    def _uninit_document(self):
        pass

    def on_insert_text(self, start, text):
        '''Event handler for `AbstractDocument.on_insert_text`.

        The event handler is bound by the text layout; there is no need for
        applications to interact with this method.
        '''
        self._init_document()

    def on_delete_text(self, start, end):
        '''Event handler for `AbstractDocument.on_delete_text`.

        The event handler is bound by the text layout; there is no need for
        applications to interact with this method.
        '''
        self._init_document()

    def on_style_text(self, start, end, attributes):
        '''Event handler for `AbstractDocument.on_style_text`.

        The event handler is bound by the text layout; there is no need for
        applications to interact with this method.
        '''
        self._init_document()

    def _get_glyphs(self):
        glyphs = []
        runs = runlist.ZipRunIterator(
            (self._document.get_font_runs(dpi=self._dpi),
             self._document.get_element_runs()))
        text = self._document.text
        for start, end, (font, element) in runs.ranges(0, len(text)):
            if element:
                ee = _InlineElementBox(element)
                ee.char_width = -1
                glyphs.append(ee)
            else:
                f_glyphs = font.get_glyphs(text[start:end])
                for i in range(start, end):
                    f_glyphs[i - start].char_width = char_width(text[i])
                glyphs.extend(f_glyphs)
        return glyphs

    def _get_owner_runs(self, owner_runs, glyphs, start, end):
        owner = glyphs[start].owner
        run_start = start
        # TODO avoid glyph slice on non-incremental
        for i, glyph in enumerate(glyphs[start:end]):
            if owner != glyph.owner:
                owner_runs.set_run(run_start, i + start, owner)
                owner = glyph.owner
                run_start = i + start
        owner_runs.set_run(run_start, end, owner)

    def _flow_glyphs(self, glyphs, owner_runs, start, end):
        # TODO change flow generator on self, avoiding this conditional.
        for line in self._flow_glyphs_single_line(glyphs, owner_runs, start,
                                                  end):
            yield line

    def _flow_glyphs_single_line(self, glyphs, owner_runs, start, end):
        owner_iterator = owner_runs.get_run_iterator().ranges(start, end)
        font_iterator = self.document.get_font_runs(dpi=self._dpi)
        kern_iterator = runlist.FilteredRunIterator(
            self.document.get_style_runs('kerning'),
            lambda value: value is not None, 0)

        line = _Line(start)
        font = font_iterator[0]

        for start, end, owner in owner_iterator:
            font = font_iterator[start]
            width = 0
            owner_glyphs = []
            for kern_start, kern_end, kern in kern_iterator.ranges(start, end):
                gs = glyphs[kern_start:kern_end]
                # width += sum([g.advance for g in gs])
                width += sum([
                    _get_glyph_advance(self._col_width, glyph) for glyph in gs
                ])
                width += kern * (kern_end - kern_start)
                owner_glyphs.extend(zip([kern] * (kern_end - kern_start), gs))
            if owner is None:
                # Assume glyphs are already boxes.
                for kern, glyph in owner_glyphs:
                    line.add_box(glyph)
            else:
                line.add_box(
                    _GlyphBox(owner, font, owner_glyphs, width,
                              self._col_width))

        if not line.boxes:
            line.ascent = font.ascent
            line.descent = font.descent

        line.paragraph_begin = line.paragraph_end = True

        yield line

    def _flow_lines(self, lines, start, end):
        margin_top_iterator = runlist.FilteredRunIterator(
            self._document.get_style_runs('margin_top'),
            lambda value: value is not None, 0)
        margin_bottom_iterator = runlist.FilteredRunIterator(
            self._document.get_style_runs('margin_bottom'),
            lambda value: value is not None, 0)
        line_spacing_iterator = self._document.get_style_runs('line_spacing')
        leading_iterator = runlist.FilteredRunIterator(
            self._document.get_style_runs('leading'),
            lambda value: value is not None, 0)

        if start == 0:
            y = 0
        else:
            line = lines[start - 1]
            line_spacing = \
                self._parse_distance(line_spacing_iterator[line.start])
            leading = \
                self._parse_distance(leading_iterator[line.start])

            y = line.y
            if line_spacing is None:
                y += line.descent
            if line.paragraph_end:
                y -= self._parse_distance(margin_bottom_iterator[line.start])

        line_index = start
        for line in lines[start:]:
            if line.paragraph_begin:
                y -= self._parse_distance(margin_top_iterator[line.start])
                line_spacing = \
                    self._parse_distance(line_spacing_iterator[line.start])
                leading = self._parse_distance(leading_iterator[line.start])
            else:
                y -= leading

            if line_spacing is None:
                y -= line.ascent
            else:
                y -= line_spacing
            if line.align == 'left' or line.width > self.width:
                line.x = line.margin_left
            elif line.align == 'center':
                line.x = (self.width - line.margin_left - line.margin_right -
                          line.width) // 2 + line.margin_left
            elif line.align == 'right':
                line.x = self.width - line.margin_right - line.width

            self.content_width = max(self.content_width,
                                     line.width + line.margin_left)

            if line.y == y and line_index >= end:
                # Early exit: all invalidated lines have been reflowed and the
                # next line has no change (therefore subsequent lines do not
                # need to be changed).
                break
            line.y = y

            if line_spacing is None:
                y += line.descent
            if line.paragraph_end:
                y -= self._parse_distance(margin_bottom_iterator[line.start])

            line_index += 1
        else:
            self.content_height = -y

        return line_index

    def _create_vertex_lists(self, x, y, i, boxes, context):
        for box in boxes:
            box.place(self, i, x, y, context)
            x += box.advance
            i += box.length

    _x = 0

    def _set_x(self, x):
        if self._boxes:
            self._x = x
            self._update()
        else:
            dx = x - self._x
            l_dx = lambda x: int(x + dx)
            for vertex_list in self._vertex_lists:
                vertices = vertex_list.vertices[:]
                vertices[::2] = list(map(l_dx, vertices[::2]))
                vertex_list.vertices[:] = vertices
            self._x = x

    def _get_x(self):
        return self._x

    x = property(_get_x,
                 _set_x,
                 doc='''X coordinate of the layout.

    See also `anchor_x`.

    :type: int
    ''')

    _y = 0

    def _set_y(self, y):
        if self._boxes:
            self._y = y
            self._update()
        else:
            dy = y - self._y
            l_dy = lambda y: int(y + dy)
            for vertex_list in self._vertex_lists:
                vertices = vertex_list.vertices[:]
                vertices[1::2] = list(map(l_dy, vertices[1::2]))
                vertex_list.vertices[:] = vertices
            self._y = y

    def _get_y(self):
        return self._y

    y = property(_get_y,
                 _set_y,
                 doc='''Y coordinate of the layout.

    See also `anchor_y`.

    :type: int
    ''')

    _width = None

    def _set_width(self, width):
        self._width = width
        self._update()

    def _get_width(self):
        return self._width

    width = property(_get_width,
                     _set_width,
                     doc='''Width of the layout.

    This property has no effect if `multiline` is False or `wrap_lines` is False.

    :type: int
    ''')

    _height = None

    def _set_height(self, height):
        self._height = height
        self._update()

    def _get_height(self):
        return self._height

    height = property(_get_height,
                      _set_height,
                      doc='''Height of the layout.

    :type: int
    ''')

    _anchor_x = 'left'

    def _set_anchor_x(self, anchor_x):
        self._anchor_x = anchor_x
        self._update()

    def _get_anchor_x(self):
        return self._anchor_x

    anchor_x = property(_get_anchor_x,
                        _set_anchor_x,
                        doc='''Horizontal anchor alignment.

    This property determines the meaning of the `x` coordinate.  It is one of
    the enumerants:

    ``"left"`` (default)
        The X coordinate gives the position of the left edge of the layout.
    ``"center"``
        The X coordinate gives the position of the center of the layout.
    ``"right"``
        The X coordinate gives the position of the right edge of the layout.

    For the purposes of calculating the position resulting from this
    alignment, the width of the layout is taken to be `width` if `multiline`
    is True and `wrap_lines` is True, otherwise `content_width`.

    :type: str
    ''')

    _anchor_y = 'bottom'

    def _set_anchor_y(self, anchor_y):
        self._anchor_y = anchor_y
        self._update()

    def _get_anchor_y(self):
        return self._anchor_y

    anchor_y = property(_get_anchor_y,
                        _set_anchor_y,
                        doc='''Vertical anchor alignment.

    This property determines the meaning of the `y` coordinate.  It is one of
    the enumerants:

    ``"top"``
        The Y coordinate gives the position of the top edge of the layout.
    ``"center"``
        The Y coordinate gives the position of the center of the layout.
    ``"baseline"``
        The Y coordinate gives the position of the baseline of the first
        line of text in the layout.
    ``"bottom"`` (default)
        The Y coordinate gives the position of the bottom edge of the layout.

    For the purposes of calculating the position resulting from this
    alignment, the height of the layout is taken to be the smaller of
    `height` and `content_height`.

    See also `content_valign`.

    :type: str
    ''')

    _content_valign = 'top'

    def _set_content_valign(self, content_valign):
        self._content_valign = content_valign
        self._update()

    def _get_content_valign(self):
        return self._content_valign

    content_valign = property(_get_content_valign,
                              _set_content_valign,
                              doc='''Vertical alignment of content within
    larger layout box.

    This property determines how content is positioned within the layout
    box when ``content_height`` is less than ``height``.  It is one
    of the enumerants:

    ``top`` (default)
        Content is aligned to the top of the layout box.
    ``center``
        Content is centered vertically within the layout box.
    ``bottom``
        Content is aligned to the bottom of the layout box.

    This property has no effect when ``content_height`` is greater
    than ``height`` (in which case the content is aligned to the top) or when
    ``height`` is ``None`` (in which case there is no vertical layout box
    dimension).

    :type: str
    ''')
Esempio n. 7
0
class GuiObject:
    """
    Basic gui object class (labels, guicomponents).

    :ivar x: x coord
    :ivar y: y coord
    :ivar w: width
    :ivar h: height
    :ivar visible: if the gui object is drawn
    """

    og0 = _graphics.OrderedGroup(0)
    og1 = _graphics.OrderedGroup(1)
    og2 = _graphics.OrderedGroup(2)

    def __init__(self, parent, name, x, y, width, height, visible=True):
        """
        Gui object constructor.

        :type x: float
        :param x: x coord
        :type y: float
        :param y: y coord
        :type width: float
        :param width: width
        :type height: float
        :param height: height
        :type batch: pyglet.graphics.Batch
        :param batch: the window's batch
        :type visible: bool
        :param visible: if the gui object is drawn
        """
        self.parent = parent
        self._batch = parent._batch
        self.name = name
        self.x = x
        self.y = y
        self.w = width
        self.h = height
        self.visible = visible

    def set_pos(self, x, y):
        """
        Sets the gui object's position

        :type x: int
        :param x: x
        :type y: int
        :param y: y
        """
        self.x = x
        self.y = y

    def set_size(self, width, height):
        """
        Sets the gui object's dimensions

        :param int width: width
        :param int height: height
        :return:
        """
        self.w = width
        self.h = height

    def set_visible(self, visible):
        """
        Sets the gui object's visible attribute.
        If True, renders the gui object.
        If False, unrenders the gui object.

        :type visible: bool
        :param visible: visible
        """
        self.visible = visible
        if visible:
            self.render()

    def on(self):
        """
        Sets visible to True
        """
        self.set_visible(True)

    def off(self):
        """
        Sets visible to False
        """
        self.set_visible(False)

    def render(self):
        """
        Renders the gui object.
        Should clear and re-add its vertex lists.
        Should be overridden.
        """
        pass
Esempio n. 8
0
def genGroup(group=0):
    return pyGra.OrderedGroup(group)
Esempio n. 9
0
        UI['Field']['position'][1],
        UI['Field']['position'][0] + UI['Field']['size'][0],
        UI['Field']['position'][1] + UI['Field']['size'][1],
        UI['Field']['position'][0],
        UI['Field']['position'][1] + UI['Field']['size'][1],
    ]))


def update(dt):
    for obj in object_list:
        obj.decision(grid, 0)
        obj.move()


batch_actors = graphics.Batch()
foreground = graphics.OrderedGroup(1)

grid = {}
grid['size'] = grid_size
grid['resolution'] = UI['Grid']['resolution']

for x in range(0, grid_size[0]):
    grid[x] = {}
    for y in range(0, grid_size[1]):
        grid[x][y] = {
            'position': [
                int(x * UI['Grid']['step'][0] + UI['Field']['position'][0]),
                int(y * UI['Grid']['step'][1] + UI['Field']['position'][1])
            ]
        }
        grid[x][y]['contains'] = []