Ejemplo n.º 1
0
def r_update_iris_in(self, complete):
    transition_sprite = self.rd["t_sprite"]
    w = transition_sprite.width
    h = transition_sprite.height

    if self.rd["t_arg"]:
        x, y = self.rd["t_arg"]
    else:
        x = w / 2
        y = h / 2

    r = int(math.hypot(max(x, sge.game.width - x),
                       max(y, sge.game.height - y)) * (1 - complete))
    eraser = sge.Sprite(width=w, height=h)
    eraser_eraser = sge.Sprite(width=w, height=h)
    eraser_eraser.draw_circle(x, y, r, fill=sge.Color((0, 0, 0, 255)))

    eraser.draw_lock()
    eraser.draw_rectangle(0, 0, w, h, fill=sge.Color((0, 0, 0, 255)))
    eraser.draw_sprite(eraser_eraser, 0, 0, 0,
                       blend_mode=sge.BLEND_RGBA_SUBTRACT)
    eraser.draw_unlock()

    transition_sprite.draw_sprite(eraser, 0, 0, 0,
                                  blend_mode=sge.BLEND_RGBA_SUBTRACT)
Ejemplo n.º 2
0
def main():
    # Create Game object
    game = Game(delta=True)

    # Load sprites
    sge.Sprite('rotator')
    fence_sprite = sge.Sprite('fence')

    # Load backgrounds
    layers = (sge.BackgroundLayer(fence_sprite, 0, 380, 0, yrepeat=False),)
    background = sge.Background(layers, 0xffffff)

    # Create objects
    circle = Circle(game.width // 2, game.height // 2)
    circle2 = Circle(22, 48)
    circle3 = Circle(486, 301)
    circle4 = Circle(50, 400)
    objects = (circle, circle2, circle3, circle4)

    # Create view
    views = (sge.View(0, 0),)

    # Create rooms
    room1 = sge.Room(objects, views=views, background=background)

    game.start()
Ejemplo n.º 3
0
def main():
    # Create Game object
    game = Game()

    # Load sprites
    circle_sprite = sge.Sprite('circle',
                               width=64,
                               height=64,
                               origin_x=32,
                               origin_y=32)
    fence_sprite = sge.Sprite('fence')

    # Load backgrounds
    layers = (sge.BackgroundLayer(fence_sprite, 0, 380, 0, yrepeat=False), )
    background = sge.Background(layers, 0xffffff)

    # Create objects
    circle = Circle(game.width // 2, game.height // 2)
    objects = [circle]

    # Create view
    views = (sge.View(0, 0), )

    # Create rooms
    room1 = sge.Room(tuple(objects), views=views, background=background)

    game.start()
Ejemplo n.º 4
0
def main():
    # Create Game object
    Game(width=640, height=480)

    # Load sprites
    sge.Sprite('circle', width=32, height=32, origin_x=16, origin_y=16)
    fence = sge.Sprite('fence')

    # Load backgrounds
    layers = (sge.BackgroundLayer(fence, 0, 0, 0), )
    background = sge.Background(layers, 'white')

    # Create objects
    objects = []
    for i in xrange(4):
        circle = Circle(64, 64, i)
        objects.append(circle)

    # Create views
    views = []
    for x in xrange(2):
        for y in xrange(2):
            views.append(sge.View(0, 0, 320 * x, 240 * y, 320, 240))

    # Create rooms
    sge.Room(tuple(objects), 1280, 1024, tuple(views), background)

    sge.game.start()
Ejemplo n.º 5
0
def main():
    # Create Game object
    game = Game()

    # Load sprites
    circle_sprite = sge.Sprite('circle',
                               width=64,
                               height=64,
                               origin_x=32,
                               origin_y=32)
    circle_pop_sprite = sge.Sprite('circle_pop',
                                   width=64,
                                   height=64,
                                   origin_x=32,
                                   origin_y=32,
                                   fps=60)
    fence_sprite = sge.Sprite('fence')

    # Load backgrounds
    layers = (sge.BackgroundLayer(fence_sprite, 0, 380, 0, yrepeat=False), )
    layers2 = (sge.BackgroundLayer(fence_sprite, 0, 0, 0), )
    background = sge.Background(layers, 0xffffff)
    background2 = sge.Background(layers2, 'white')

    # Load fonts
    glob.font = sge.Font('Liberation Serif', 20)

    # Load sounds
    glob.pop_sound = sge.Sound('pop.ogg')

    # Load music
    glob.music = sge.Music('WhereWasI.ogg')

    # Create objects
    circle = Circle(game.width // 2, game.height // 2)
    circle2 = Circle(22, 48)
    circle3 = Circle(486, 301)
    circle4 = Circle(50, 400)
    circle5 = Circle(game.width // 2, game.height // 2)
    circle6 = Circle(52, 120)
    objects = (circle, circle2, circle3, circle4)
    objects2 = (circle5, circle6)

    # Create view
    views = (sge.View(0, 0), )

    # Create rooms
    room1 = Room('I am the first room!',
                 objects,
                 views=views,
                 background=background)
    room2 = Room('Second room on the house!', objects2, background=background2)

    game.start()
Ejemplo n.º 6
0
    def project_circle(self,
                       x,
                       y,
                       z,
                       radius,
                       fill=None,
                       outline=None,
                       outline_thickness=1,
                       anti_alias=False):
        """Project a circle onto the room.

        Arguments:

        - ``x`` -- The horizontal location relative to the room to
          position the center of the circle.
        - ``y`` -- The vertical location relative to the room to
          position the center of the circle.
        - ``z`` -- The Z-axis position of the projection in the room.

        See the documentation for :meth:`sge.Sprite.draw_circle` for
        more information.

        """
        outline_thickness = abs(outline_thickness)
        xy = radius + outline_thickness // 2
        wh = 2 * radius + outline_thickness
        sprite = sge.Sprite(None, width=wh, height=wh)
        sprite.draw_circle(xy, xy, radius, fill, outline, outline_thickness,
                           anti_alias)
        p = _Projection(x - radius,
                        y - radius,
                        z,
                        sprite=sprite,
                        detects_collisions=False)
        self.add(p)
Ejemplo n.º 7
0
def _get_polygon_sprite(points, fill, outline, outline_thickness, anti_alias):
    # Return a sprite for the given rectangle.
    i = ("poly_sprite", tuple(points),
         tuple(fill) if fill is not None else None,
         tuple(outline) if outline is not None else None,
         outline_thickness, anti_alias)
    sprite = cache.get(i)
    if sprite is None:
        xlist = []
        ylist = []
        for point in points:
            xlist.append(point[0])
            ylist.append(point[1])
        x = min(xlist)
        y = min(ylist)
        width = max(xlist) - x
        height = max(ylist) - y

        outline_thickness = abs(outline_thickness)
        draw_x = outline_thickness // 2
        draw_y = outline_thickness // 2
        dpoints = [(a - x + draw_x, b - y + draw_y) for (a, b) in points]
        w = width + outline_thickness
        h = height + outline_thickness
        sprite = sge.Sprite(None, width=w, height=h, origin_x=draw_x,
                            origin_y=draw_y)
        sprite.draw_polygon(dpoints, fill, outline, outline_thickness,
                            anti_alias)

    cache.add(i, sprite)
    return sprite
Ejemplo n.º 8
0
 def event_create(self):
     self.selected = False
     self.text_sprite = sge.Sprite(width=TEXT_SIZE[0], height=TEXT_SIZE[1])
     self.text_object = sge.StellarClass.create(self.x + TEXT_POS[0],
                                                self.y + TEXT_POS[1],
                                                self.z + 1,
                                                sprite=self.text_sprite)
Ejemplo n.º 9
0
    def project_rectangle(self,
                          x,
                          y,
                          z,
                          width,
                          height,
                          fill=None,
                          outline=None,
                          outline_thickness=1):
        """Project a rectangle onto the room.

        Arguments:

        - ``x`` -- The horizontal location relative to the room to
          project the rectangle.
        - ``y`` -- The vertical location relative to the room to project
          the rectangle.
        - ``z`` -- The Z-axis position of the projection in the room.

        See the documentation for :meth:`sge.Sprite.draw_rectangle` for
        more information.

        """
        outline_thickness = abs(outline_thickness)
        draw_x = outline_thickness // 2
        draw_y = outline_thickness // 2
        x -= draw_x
        y -= draw_y
        w = width + outline_thickness
        h = height + outline_thickness
        sprite = sge.Sprite(None, width=w, height=h)
        sprite.draw_rectangle(draw_x, draw_y, w, h, fill, outline,
                              outline_thickness)
        p = _Projection(x, y, z, sprite=sprite, detects_collisions=False)
        self.add(p)
Ejemplo n.º 10
0
def _get_dot_sprite(color):
    # Return a sprite for the given dot.
    i = ("dot_sprite", tuple(color))
    sprite = cache.get(i)
    if sprite is None:
        sprite = sge.Sprite(None, width=1, height=1)
        sprite.draw_dot(0, 0, color)

    cache.add(i, sprite)
    return sprite
Ejemplo n.º 11
0
def r_update_dissolve(self, complete):
    transition_sprite = self.rd["t_sprite"]
    w = transition_sprite.width
    h = transition_sprite.height
    diff = complete - self.rd["t_complete_last"]
    c = sge.Color((0, 0, 0, int(round(diff * 255))))
    eraser = sge.Sprite(width=w, height=h)
    eraser.draw_rectangle(0, 0, w, h, c)
    transition_sprite.draw_sprite(eraser, 0, 0, 0,
                                  blend_mode=sge.BLEND_RGBA_SUBTRACT)
Ejemplo n.º 12
0
def r_update_wipe_downright(self, complete):
    transition_sprite = self.rd["t_sprite"]
    w = transition_sprite.width
    h = transition_sprite.height
    x = w * complete * 2
    y = w * complete * 2
    eraser = sge.Sprite(width=w, height=h)
    eraser.draw_polygon([(0, 0), (x, 0), (0, y)],
                        fill=sge.Color((0, 0, 0, 255)), anti_alias=True)
    transition_sprite.draw_sprite(eraser, 0, 0, 0,
                                  blend_mode=sge.BLEND_RGBA_SUBTRACT)
Ejemplo n.º 13
0
def _get_line_sprite(x1, y1, x2, y2, color, thickness, anti_alias):
    # Return a sprite for the given line.
    w = int(round(abs(x2 - x1) + thickness))
    h = int(round(abs(y2 - y1) + thickness))
    i = ("line_sprite", x1, y1, x2, y2, tuple(color), thickness, anti_alias)
    sprite = cache.get(i)
    if sprite is None:
        sprite = sge.Sprite(None, width=w, height=h)
        sprite.draw_line(x1, y1, x2, y2, color, thickness, anti_alias)

    cache.add(i, sprite)
    return sprite
Ejemplo n.º 14
0
def r_update_fade(self, complete):
    transition_sprite = self.rd["t_sprite"]
    w = transition_sprite.width
    h = transition_sprite.height
    if complete < 0.5:
        diff = (complete - self.rd["t_complete_last"]) * 2
        c = sge.Color([int(round(diff * 255))] * 3)
        darkener = sge.Sprite(width=w, height=h)
        darkener.draw_rectangle(0, 0, w, h, c)
        transition_sprite.draw_sprite(darkener, 0, 0, 0,
                                      blend_mode=sge.BLEND_RGB_SUBTRACT)
    else:
        complete = (complete - 0.5) * 2
        c = sge.Color((0, 0, 0, int(round(255 - complete * 255))))
        transition_sprite.draw_clear()
        transition_sprite.draw_rectangle(0, 0, w, h, fill=c)
Ejemplo n.º 15
0
def _get_circle_sprite(radius, fill, outline, outline_thickness, anti_alias):
    # Return a sprite for the given circle.
    i = ("circle_sprite", radius, tuple(fill) if fill is not None else None,
         tuple(outline) if outline is not None else None,
         outline_thickness, anti_alias)
    sprite = cache.get(i)
    if sprite is None:
        outline_thickness = abs(outline_thickness)
        xy = radius + outline_thickness // 2
        wh = 2 * radius + outline_thickness
        sprite = sge.Sprite(None, width=wh, height=wh)
        sprite.draw_circle(xy, xy, radius, fill, outline, outline_thickness,
                           anti_alias)

    cache.add(i, sprite)
    return sprite
Ejemplo n.º 16
0
    def project_text(self,
                     font,
                     text,
                     x,
                     y,
                     z,
                     width=None,
                     height=None,
                     color="black",
                     halign=sge.ALIGN_LEFT,
                     valign=sge.ALIGN_TOP,
                     anti_alias=True):
        """Project text onto the room.

        Arguments:

        - ``x`` -- The horizontal location relative to the room to
          project the text.
        - ``y`` -- The vertical location relative to the room to project
          the text.
        - ``z`` -- The Z-axis position of the projection in the room.

        See the documentation for :meth:`sge.Sprite.draw_text` for more
        information.

        """
        if not isinstance(font, sge.Font):
            font = sge.game.fonts[font]

        w, h = font.get_size(text, width, height)
        draw_x = {
            sge.ALIGN_LEFT: 0,
            sge.ALIGN_CENTER: w / 2,
            sge.ALIGN_RIGHT: w
        }.setdefault(halign, w / 2)
        draw_y = {
            sge.ALIGN_TOP: 0,
            sge.ALIGN_MIDDLE: h / 2,
            sge.ALIGN_BOTTOM: h
        }.setdefault(valign, h / 2)
        sprite = sge.Sprite(None, width=w, height=h)
        sprite.draw_text(font, text, draw_x, draw_y, width, height, color,
                         halign, valign, anti_alias)
        p = _Projection(x, y, z, sprite=sprite, detects_collisions=False)
        self.add(p)
Ejemplo n.º 17
0
    def project_dot(self, x, y, z, color):
        """Project a single-pixel dot onto the room.

        Arguments:

        - ``x`` -- The horizontal location relative to the room to
          project the dot.
        - ``y`` -- The vertical location relative to the room to project
          the dot.
        - ``z`` -- The Z-axis position of the projection in the room.

        See the documentation for :meth:`sge.Sprite.draw_dot` for more
        information.

        """
        sprite = sge.Sprite(None, width=1, height=1)
        sprite.draw_dot(0, 0, color)
        p = _Projection(x, y, z, sprite=sprite, detects_collisions=False)
        self.add(p)
Ejemplo n.º 18
0
def _get_rectangle_sprite(width, height, fill, outline, outline_thickness):
    # Return a sprite for the given rectangle.
    i = ("rectangle_sprite", width, height,
         tuple(fill) if fill is not None else None,
         tuple(outline) if outline is not None else None,
         outline_thickness)
    sprite = cache.get(i)
    if sprite is None:
        outline_thickness = abs(outline_thickness)
        draw_x = outline_thickness // 2
        draw_y = outline_thickness // 2
        w = width + outline_thickness
        h = height + outline_thickness
        sprite = sge.Sprite(None, width=w, height=h)
        sprite.draw_rectangle(draw_x, draw_y, width, height, fill, outline,
                              outline_thickness)

    cache.add(i, sprite)
    return sprite
Ejemplo n.º 19
0
    def project_ellipse(self,
                        x,
                        y,
                        z,
                        width,
                        height,
                        fill=None,
                        outline=None,
                        outline_thickness=1,
                        anti_alias=False):
        """Project an ellipse onto the room.

        Arguments:

        - ``x`` -- The horizontal location relative to the room to
          position the imaginary rectangle containing the ellipse.
        - ``y`` -- The vertical location relative to the room to
          position the imaginary rectangle containing the ellipse.
        - ``z`` -- The Z-axis position of the projection in the room.
        - ``width`` -- The width of the ellipse.
        - ``height`` -- The height of the ellipse.
        - ``fill`` -- The color of the fill of the ellipse.
        - ``outline`` -- The color of the outline of the ellipse.
        - ``outline_thickness`` -- The thickness of the outline of the
          ellipse.
        - ``anti_alias`` -- Whether or not anti-aliasing should be used.

        See the documentation for :meth:`sge.Sprite.draw_ellipse` for
        more information.

        """
        outline_thickness = abs(outline_thickness)
        draw_x = outline_thickness // 2
        draw_y = outline_thickness // 2
        x -= draw_x
        y -= draw_y
        w = width + outline_thickness
        h = height + outline_thickness
        sprite = sge.Sprite(None, width=w, height=h)
        sprite.draw_ellipse(draw_x, draw_y, w, h, fill, outline,
                            outline_thickness)
        p = _Projection(x, y, z, sprite=sprite, detects_collisions=False)
        self.add(p)
Ejemplo n.º 20
0
    def project_line(self,
                     x1,
                     y1,
                     x2,
                     y2,
                     z,
                     color,
                     thickness=1,
                     anti_alias=False):
        """Project a line segment onto the room.

        Arguments:

        - ``x1`` -- The horizontal location relative to the room of the
          first endpoint of the projected line segment.
        - ``y1`` -- The vertical location relative to the room of the
          first endpoint of the projected line segment.
        - ``x2`` -- The horizontal location relative to the room of the
          second endpoint of the projected line segment.
        - ``y2`` -- The vertical location relative to the room of the
          second endpoint of the projected line segment.
        - ``z`` -- The Z-axis position of the projection in the room.

        See the documentation for :meth:`sge.Sprite.draw_line` for more
        information.

        """
        thickness = abs(thickness)
        x = min(x1, x2) - thickness // 2
        y = min(y1, y2) - thickness // 2
        w = abs(x2 - x1) + thickness
        h = abs(y2 - y1) + thickness
        x1 -= x
        y1 -= y
        x2 -= x
        y2 -= y
        sprite = sge.Sprite(None, width=w, height=h)
        sprite.draw_line(x1, y1, x2, y2, color, thickness, anti_alias)
        p = _Projection(x, y, z, sprite=sprite, detects_collisions=False)
        self.add(p)
Ejemplo n.º 21
0
def r_update_pixelate(self, complete):
    transition_sprite = self.rd["t_sprite"]
    w = transition_sprite.width
    h = transition_sprite.height
    smooth = sge.game.scale_smooth
    sge.game.scale_smooth = False

    if complete < 0.8:
        complete *= 1.25
        swidth = max(1, w * (1 - complete))
        sheight = max(1, h * (1 - complete))
        transition_sprite.width = swidth
        transition_sprite.height = sheight
        transition_sprite.width = w
        transition_sprite.height = h
    else:
        diff = (complete - self.rd["t_complete_last"]) * 5
        c = sge.Color((0, 0, 0, int(round(diff * 255))))
        eraser = sge.Sprite(width=w, height=h)
        eraser.draw_rectangle(0, 0, w, h, c)
        transition_sprite.draw_sprite(eraser, 0, 0, 0,
                                      blend_mode=sge.BLEND_RGBA_SUBTRACT)

    sge.game.scale_smooth = smooth
Ejemplo n.º 22
0
def main():
    # Create Game object
    Game(480, 800)

    # Load sprites
    sge.Sprite('1945_enemybullet',
               origin_x=3,
               origin_y=3,
               transparent=True,
               bbox_x=-3,
               bbox_y=-3)
    sge.Sprite('1945_enemyplane_blue',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=4,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_enemyplane_blue_flip',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=6,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_enemyplane_blue_flipped',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=4,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_enemyplane_dkgreen',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=4,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_enemyplane_dkgreen_flip',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=6,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_enemyplane_dkgreen_flipped',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=4,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_enemyplane_green',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=4,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_enemyplane_green_flip',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=6,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_enemyplane_green_flipped',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=4,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_enemyplane_orange',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=4,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_enemyplane_white',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=4,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_enemyplane_white_flip',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=6,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_enemyplane_white_flipped',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=4,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_explosion_large',
               origin_x=32,
               origin_y=23,
               transparent=True,
               fps=5)
    sge.Sprite('1945_explosion_small',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=6)
    sge.Sprite('1945_friendlyplane',
               origin_x=16,
               origin_y=16,
               transparent=True,
               fps=4,
               bbox_x=-16,
               bbox_y=-16)
    sge.Sprite('1945_gameover',
               origin_x=97,
               origin_y=6,
               transparent=True,
               fps=4)
    sge.Sprite('1945_getready',
               origin_x=98,
               origin_y=6,
               transparent=True,
               fps=4)
    sge.Sprite('1945_hud', origin_y=64, transparent=True)
    sge.Sprite('1945_hud_life',
               origin_x=13,
               origin_y=10,
               transparent=True,
               bbox_x=-13,
               bbox_y=-10)
    sge.Sprite('1945_hud_shieldbar')
    sge.Sprite('1945_islands',
               origin_x=32,
               origin_y=32,
               transparent=True,
               fps=0)
    sge.Sprite('1945_main_menu', transparent=True)
    sge.Sprite('1945_numbers', transparent=True, fps=0)
    sge.Sprite('1945_playerbullet',
               origin_x=3,
               origin_y=8,
               transparent=True,
               bbox_x=-3,
               bbox_y=-8)
    sge.Sprite('1945_playerplane',
               origin_x=29,
               origin_y=12,
               transparent=True,
               fps=4,
               bbox_x=-29,
               bbox_y=-12)
    sge.Sprite('1945_powerup_backguns',
               origin_x=16,
               origin_y=10,
               transparent=True,
               bbox_x=-16,
               bbox_y=-10)
    sge.Sprite('1945_powerup_backup',
               origin_x=15,
               origin_y=7,
               transparent=True,
               bbox_x=-15,
               bbox_y=-7)
    sge.Sprite('1945_powerup_bomb',
               origin_x=5,
               origin_y=11,
               transparent=True,
               bbox_x=-5,
               bbox_y=-11)
    sge.Sprite('1945_powerup_extralife',
               origin_x=11,
               origin_y=9,
               transparent=True,
               bbox_x=-11,
               bbox_y=-9)
    sge.Sprite('1945_powerup_shield',
               origin_x=10,
               origin_y=14,
               transparent=True,
               bbox_x=-10,
               bbox_y=-14)
    sge.Sprite('1945_powerup_sideguns',
               origin_x=16,
               origin_y=7,
               transparent=True,
               bbox_x=-16,
               bbox_y=-7)
    sge.Sprite('1945_powerup_spreadguns',
               origin_x=16,
               origin_y=12,
               transparent=True,
               bbox_x=-16,
               bbox_y=-12)
    sge.Sprite('1945_selection_arrow',
               origin_x=6,
               origin_y=6,
               transparent=True)
    sge.Sprite('1945_title')
    sge.Sprite('1945_water', fps=2)

    # Load backgrounds
    water_layer = sge.BackgroundLayer('1945_water', 0, 0, -10000)
    layers = (water_layer, )
    background = sge.Background(layers, "#083681")

    # Load sounds
    #TODO

    # Load music
    glob.music = sge.Music("DST-TowerDefenseTheme.ogg")

    # Create objects
    #TODO
    objects = ()

    # Create rooms
    sge.Room(objects, background=background)

    sge.game.start()
Ejemplo n.º 23
0
def load_resources():
    """Load or reload the sprites, backgrounds, and objects."""
    if glob.game_file is not None:
        with open(glob.game_file, 'r') as f:
            config = json.read(f)

        # Read globals (for purpose of evaluation), using the editors
        # globals as a base.
        glob.game_globals = globals()

        constants = config.setdefault("constants", [])
        globalvars = config.setdefault("global_variables", [])

        for var in constants + globalvars:
            name, value = var.split("=")
            name = name.strip()
            game_globals[name] = eval(value, game_globals)

        # Load sprites
        glob.sprites = []
        sprites = config.setdefault("sprites", [])

        for sprite in sprites:
            name = eval(str(sprite.setdefault("name")), glob.game_globals)
            id_ = eval(str(sprite.setdefault("id")), glob.game_globals)
            width = eval(str(sprite.setdefault("width")), glob.game_globals)
            height = eval(str(sprite.setdefault("height")), glob.game_globals)
            origin_x = eval(str(sprite.setdefault("origin_x", 0)),
                            glob.game_globals)
            origin_y = eval(str(sprite.setdefault("origin_y", 0)),
                            glob.game_globals)
            transparent = eval(str(sprite.setdefault("transparent", True)),
                               glob.game_globals)
            fps = eval(str(sprite.setdefault("fps", 60)), glob.game_globals)
            bbox_x = eval(str(sprite.setdefault("bbox_x")), glob.game_globals)
            bbox_y = eval(str(sprite.setdefault("bbox_y")), glob.game_globals)
            bbox_width = eval(str(sprite.setdefault("bbox_width")),
                              glob.game_globals)
            bbox_height = eval(str(sprite.setdefault("bbox_height")),
                               glob.game_globals)
            s = sge.Sprite(name, id_, width, height, origin_x, origin_y,
                           transparent, fps, bbox_x, bbox_y, bbox_width,
                           bbox_height)
            glob.sprites.append(s)

        # Load backgrounds
        background_layers = config.setdefault("background_layers", [])

        for layer in background_layers:
            sprite = eval(
                str(
                    layer.setdefault("sprite",
                                     '"stellar_room_editor_no_sprite"')),
                glob.game_globals)
            x = eval(str(layer.setdefault("x", 0)), glob.game_globals)
            y = eval(str(layer.setdefault("y", 0)), glob.game_globals)
            z = eval(str(layer.setdefault("z", 0)), glob.game_globals)
            id_ = eval(str(layer.setdefault("id")), glob.game_globals)
            xscroll_rate = eval(str(layer.setdefault("xscroll_rate", 1)),
                                glob.game_globals)
            yscroll_rate = eval(str(layer.setdefault("yscroll_rate", 1)),
                                glob.game_globals)
            xrepeat = eval(str(layer.setdefault("xrepeat", True)),
                           glob.game_globals)
            yrepeat = eval(str(layer.setdefault("yrepeat", True)),
                           glob.game_globals)
            sge.BackgroundLayer(sprite, x, y, z, id_, xscroll_rate,
                                yscroll_rate, xrepeat, yrepeat)

        backgrounds = config.setdefault("backgrounds", [])

        for background in backgrounds:
            layers = []
            color = eval(str(background.setdefault("color", '"white"')),
                         glob.game_globals)
            id_ = eval(str(background.setdefault("id")), glob.game_globals)

            for layer in background.setdefault("layers", []):
                layers.append(eval(str(layer), glob.game_globals))

            sge.Background(layers, color, id_)

        # Load class defaults
        glob.defaults = {}
        classes = config.setdefault("classes", {})

        for i in classes:
            glob.defaults[i] = {}
            methods = classes[i].setdefault("methods", [])

            for method in methods:
                if method.setdefault("name") == "__init__":
                    args = method.setdefault("arguments", [])

                    for arg in args:
                        if "=" in arg:
                            name, value = arg.split("=")
                            name = name.strip()
                            value = value.strip()
                            glob.defaults[i][name] = str(value)
Ejemplo n.º 24
0
def _show_modal(text, default, text_entry, buttons):
    # Show a dialog box.  Text entry if ``text_entry`` is True, buttons
    # are made by the strings in ``buttons``.   If is text entry, return
    # the text if button 1 is pressed or None if button 0 is pressed,
    # else return the button number pressed.  Select ``default`` if this
    # isn't a text entry dialog, else select button 1 by default.  If it
    # is a text entry dialog, ``default`` is the default text entry.
    window = pygame.display.get_surface()
    screenshot = window.copy()
    background = screenshot.copy()
    sge.font_directories.append(os.path.dirname(__file__))
    font = sge.Font("DroidSans-Bold.ttf", size=12)
    del sge.font_directories[-1]
    window_rect = window.get_rect()
    screen_w, screen_h = background.get_size()
    button_w = 80
    button_h = 24

    for button in buttons:
        button_w = max(button_w, font.get_size(button)[0])

    box_w = min(screen_w, max(320, (button_w + 4) * len(buttons) + 4))
    box_h = button_h + 12
    text_entry_w = box_w - 8
    text_entry_h = font._font.get_linesize() + 4
    if text_entry:
        box_h += text_entry_h + 8
    text_w = box_w - 16
    text_h = font.get_size(text, text_w)[1]

    while box_h + text_h > screen_h and box_w < screen_w:
        box_w = min(box_w + 4, screen_w)
        text_w = box_w - 16
        text_h = font.get_size(text, text_w)

    box_h = max(120, box_h + text_h)
    button_w = min(80, int(box_w / len(buttons)))

    cursor = pygame.Surface((1, font._font.get_linesize()))
    mydir = os.path.dirname(__file__)
    try:
        box_fill = pygame.image.load(os.path.join(
            mydir, 'sge_dialogbox.png')).convert_alpha()
        box_left = pygame.image.load(
            os.path.join(mydir, 'sge_dialogbox_left.png')).convert_alpha()
        box_right = pygame.image.load(
            os.path.join(mydir, 'sge_dialogbox_right.png')).convert_alpha()
        box_top = pygame.image.load(
            os.path.join(mydir, 'sge_dialogbox_top.png')).convert_alpha()
        box_bottom = pygame.image.load(
            os.path.join(mydir, 'sge_dialogbox_bottom.png')).convert_alpha()
        box_topleft = pygame.image.load(
            os.path.join(mydir, 'sge_dialogbox_topleft.png')).convert_alpha()
        box_topright = pygame.image.load(
            os.path.join(mydir, 'sge_dialogbox_topright.png')).convert_alpha()
        box_bottomleft = pygame.image.load(
            os.path.join(mydir,
                         'sge_dialogbox_bottomleft.png')).convert_alpha()
        box_bottomright = pygame.image.load(
            os.path.join(mydir,
                         'sge_dialogbox_bottomright.png')).convert_alpha()
        text_entry_fill = pygame.image.load(
            os.path.join(mydir, 'sge_text_entry.png')).convert_alpha()
        text_entry_left = pygame.image.load(
            os.path.join(mydir, 'sge_text_entry_left.png')).convert_alpha()
        text_entry_right = pygame.image.load(
            os.path.join(mydir, 'sge_text_entry_right.png')).convert_alpha()
        text_entry_top = pygame.image.load(
            os.path.join(mydir, 'sge_text_entry_top.png')).convert_alpha()
        text_entry_bottom = pygame.image.load(
            os.path.join(mydir, 'sge_text_entry_bottom.png')).convert_alpha()
        text_entry_topleft = pygame.image.load(
            os.path.join(mydir, 'sge_text_entry_topleft.png')).convert_alpha()
        text_entry_topright = pygame.image.load(
            os.path.join(mydir,
                         'sge_text_entry_topright.png')).convert_alpha()
        text_entry_bottomleft = pygame.image.load(
            os.path.join(mydir,
                         'sge_text_entry_bottomleft.png')).convert_alpha()
        text_entry_bottomright = pygame.image.load(
            os.path.join(mydir,
                         'sge_text_entry_bottomright.png')).convert_alpha()
        button_fill = pygame.image.load(os.path.join(
            mydir, 'sge_button.png')).convert_alpha()
        button_left = pygame.image.load(
            os.path.join(mydir, 'sge_button_left.png')).convert_alpha()
        button_right = pygame.image.load(
            os.path.join(mydir, 'sge_button_right.png')).convert_alpha()
        button_top = pygame.image.load(
            os.path.join(mydir, 'sge_button_top.png')).convert_alpha()
        button_bottom = pygame.image.load(
            os.path.join(mydir, 'sge_button_bottom.png')).convert_alpha()
        button_topleft = pygame.image.load(
            os.path.join(mydir, 'sge_button_topleft.png')).convert_alpha()
        button_topright = pygame.image.load(
            os.path.join(mydir, 'sge_button_topright.png')).convert_alpha()
        button_bottomleft = pygame.image.load(
            os.path.join(mydir, 'sge_button_bottomleft.png')).convert_alpha()
        button_bottomright = pygame.image.load(
            os.path.join(mydir, 'sge_button_bottomright.png')).convert_alpha()
        button_selected_fill = pygame.image.load(
            os.path.join(mydir, 'sge_button_selected.png')).convert_alpha()
        button_selected_left = pygame.image.load(
            os.path.join(mydir,
                         'sge_button_selected_left.png')).convert_alpha()
        button_selected_right = pygame.image.load(
            os.path.join(mydir,
                         'sge_button_selected_right.png')).convert_alpha()
        button_selected_top = pygame.image.load(
            os.path.join(mydir,
                         'sge_button_selected_top.png')).convert_alpha()
        button_selected_bottom = pygame.image.load(
            os.path.join(mydir,
                         'sge_button_selected_bottom.png')).convert_alpha()
        button_selected_topleft = pygame.image.load(
            os.path.join(mydir,
                         'sge_button_selected_topleft.png')).convert_alpha()
        button_selected_topright = pygame.image.load(
            os.path.join(mydir,
                         'sge_button_selected_topright.png')).convert_alpha()
        button_selected_bottomleft = pygame.image.load(
            os.path.join(
                mydir, 'sge_button_selected_bottomleft.png')).convert_alpha()
        button_selected_bottomright = pygame.image.load(
            os.path.join(
                mydir, 'sge_button_selected_bottomright.png')).convert_alpha()
        button_press_fill = pygame.image.load(
            os.path.join(mydir, 'sge_button_press.png')).convert_alpha()
        button_press_left = pygame.image.load(
            os.path.join(mydir, 'sge_button_press_left.png')).convert_alpha()
        button_press_right = pygame.image.load(
            os.path.join(mydir, 'sge_button_press_right.png')).convert_alpha()
        button_press_top = pygame.image.load(
            os.path.join(mydir, 'sge_button_press_top.png')).convert_alpha()
        button_press_bottom = pygame.image.load(
            os.path.join(mydir,
                         'sge_button_press_bottom.png')).convert_alpha()
        button_press_topleft = pygame.image.load(
            os.path.join(mydir,
                         'sge_button_press_topleft.png')).convert_alpha()
        button_press_topright = pygame.image.load(
            os.path.join(mydir,
                         'sge_button_press_topright.png')).convert_alpha()
        button_press_bottomleft = pygame.image.load(
            os.path.join(mydir,
                         'sge_button_press_bottomleft.png')).convert_alpha()
        button_press_bottomright = pygame.image.load(
            os.path.join(mydir,
                         'sge_button_press_bottomright.png')).convert_alpha()
    except pygame.error as e:
        if sge.DEBUG:
            print(e)
        box_fill = pygame.Surface((1, 1))
        box_left = pygame.Surface((1, 1))
        box_right = pygame.Surface((1, 1))
        box_top = pygame.Surface((1, 1))
        box_bottom = pygame.Surface((1, 1))
        box_topleft = pygame.Surface((1, 1))
        box_topright = pygame.Surface((1, 1))
        box_bottomleft = pygame.Surface((1, 1))
        box_bottomright = pygame.Surface((1, 1))
        text_entry_fill = pygame.Surface((1, 1))
        text_entry_left = pygame.Surface((1, 1))
        text_entry_right = pygame.Surface((1, 1))
        text_entry_top = pygame.Surface((1, 1))
        text_entry_bottom = pygame.Surface((1, 1))
        text_entry_topleft = pygame.Surface((1, 1))
        text_entry_topright = pygame.Surface((1, 1))
        text_entry_bottomleft = pygame.Surface((1, 1))
        text_entry_bottomright = pygame.Surface((1, 1))
        button_fill = pygame.Surface((1, 1))
        button_left = pygame.Surface((1, 1))
        button_right = pygame.Surface((1, 1))
        button_top = pygame.Surface((1, 1))
        button_bottom = pygame.Surface((1, 1))
        button_topleft = pygame.Surface((1, 1))
        button_topright = pygame.Surface((1, 1))
        button_bottomleft = pygame.Surface((1, 1))
        button_bottomright = pygame.Surface((1, 1))
        button_selected_fill = pygame.Surface((1, 1))
        button_selected_left = pygame.Surface((1, 1))
        button_selected_right = pygame.Surface((1, 1))
        button_selected_top = pygame.Surface((1, 1))
        button_selected_bottom = pygame.Surface((1, 1))
        button_selected_topleft = pygame.Surface((1, 1))
        button_selected_topright = pygame.Surface((1, 1))
        button_selected_bottomleft = pygame.Surface((1, 1))
        button_selected_bottomright = pygame.Surface((1, 1))
        button_press_fill = pygame.Surface((1, 1))
        button_press_left = pygame.Surface((1, 1))
        button_press_right = pygame.Surface((1, 1))
        button_press_top = pygame.Surface((1, 1))
        button_press_bottom = pygame.Surface((1, 1))
        button_press_topleft = pygame.Surface((1, 1))
        button_press_topright = pygame.Surface((1, 1))
        button_press_bottomleft = pygame.Surface((1, 1))
        button_press_bottomright = pygame.Surface((1, 1))
        box_fill.fill((255, 255, 255))
        text_entry_fill.fill((255, 255, 255))
        button_fill.fill((255, 255, 255))
        button_selected_fill.fill((0, 255, 255))
        button_press_fill.fill((0, 0, 255))

    # Box image
    box = pygame.Surface((box_w, box_h), pygame.SRCALPHA)
    for i in xrange(0, box_w, box_fill.get_width()):
        for j in xrange(0, box_h, box_fill.get_height()):
            box.blit(box_fill, (i, j))

    # Clear the way for the corners and edges (so transparency works
    # properly)
    box.fill(pygame.Color(0, 0, 0, 0),
             pygame.Rect((0, 0), box_topleft.get_size()))
    box.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((box_w - box_topright.get_width(), 0),
                    box_topright.get_size()))
    box.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((0, box_h - box_bottomleft.get_height()),
                    box_bottomleft.get_size()))
    box.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((box_w - box_bottomright.get_width(),
                     box_h - box_bottomright.get_height()),
                    box_bottomright.get_size()))

    box.blit(box_topleft, (0, 0))
    box.blit(box_topright, (box_w - box_topright.get_width(), 0))
    box.blit(box_bottomleft, (0, box_h - box_bottomleft.get_height()))
    box.blit(box_bottomright, (box_w - box_bottomright.get_width(),
                               box_h - box_bottomright.get_height()))
    for i in xrange(box_topleft.get_width(), box_w - box_topright.get_width(),
                    box_top.get_width()):
        box.blit(box_top, (i, 0))
    for i in xrange(box_bottomleft.get_width(),
                    box_w - box_bottomright.get_width(),
                    box_bottom.get_width()):
        box.blit(box_bottom, (i, box_h - box_bottom.get_height()))
    for i in xrange(box_topleft.get_height(),
                    box_h - box_bottomleft.get_height(),
                    box_left.get_height()):
        box.blit(box_left, (0, i))
    for i in xrange(box_topright.get_height(),
                    box_h - box_bottomright.get_height(),
                    box_right.get_height()):
        box.blit(box_right, (box_w - box_right.get_width(), i))
    box_rect = box.get_rect(center=window_rect.center)

    # Text Entry image
    text_entry_field = pygame.Surface((text_entry_w, text_entry_h),
                                      pygame.SRCALPHA)
    for i in xrange(0, text_entry_w, text_entry_fill.get_width()):
        for j in xrange(0, text_entry_h, text_entry_fill.get_height()):
            text_entry_field.blit(text_entry_fill, (i, j))

    # Clear the way for the corners and edges (so transparency works
    # properly)
    text_entry_field.fill(pygame.Color(0, 0, 0, 0),
                          pygame.Rect((0, 0), text_entry_topleft.get_size()))
    text_entry_field.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((text_entry_w - text_entry_topright.get_width(), 0),
                    text_entry_topright.get_size()))
    text_entry_field.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((0, text_entry_h - text_entry_bottomleft.get_height()),
                    text_entry_bottomleft.get_size()))
    text_entry_field.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((text_entry_w - text_entry_bottomright.get_width(),
                     text_entry_h - text_entry_bottomright.get_height()),
                    text_entry_bottomright.get_size()))

    text_entry_field.blit(text_entry_topleft, (0, 0))
    text_entry_field.blit(text_entry_topright,
                          (text_entry_w - text_entry_topright.get_width(), 0))
    text_entry_field.blit(
        text_entry_bottomleft,
        (0, text_entry_h - text_entry_bottomleft.get_height()))
    text_entry_field.blit(text_entry_bottomright,
                          (text_entry_w - text_entry_bottomright.get_width(),
                           text_entry_h - text_entry_bottomright.get_height()))
    for i in xrange(text_entry_topleft.get_width(),
                    text_entry_w - text_entry_topright.get_width(),
                    text_entry_top.get_width()):
        text_entry_field.blit(text_entry_top, (i, 0))
    for i in xrange(text_entry_bottomleft.get_width(),
                    text_entry_w - text_entry_bottomright.get_width(),
                    text_entry_bottom.get_width()):
        text_entry_field.blit(
            text_entry_bottom,
            (i, text_entry_h - text_entry_bottom.get_height()))
    for i in xrange(text_entry_topleft.get_height(),
                    text_entry_h - text_entry_bottomleft.get_height(),
                    text_entry_left.get_height()):
        text_entry_field.blit(text_entry_left, (0, i))
    for i in xrange(text_entry_topright.get_height(),
                    text_entry_h - text_entry_bottomright.get_height(),
                    text_entry_right.get_height()):
        text_entry_field.blit(text_entry_right,
                              (text_entry_w - text_entry_right.get_width(), i))

    text_entry_rect = text_entry_field.get_rect()
    text_entry_rect.left = 4
    text_entry_rect.bottom = box_h - text_entry_h - 8
    if text_entry:
        box.blit(text_entry_field, text_entry_rect)
    text_entry_rect.w -= 4
    text_entry_rect.h -= 4
    text_entry_rect.left += box_rect.left + 2
    text_entry_rect.top += box_rect.top + 2

    # Button image
    button = pygame.Surface((button_w, button_h), pygame.SRCALPHA)
    for i in xrange(0, button_w, button_fill.get_width()):
        for j in xrange(0, button_h, button_fill.get_height()):
            button.blit(button_fill, (i, j))

    # Clear the way for the corners and edges (so transparency works
    # properly)
    button.fill(pygame.Color(0, 0, 0, 0),
                pygame.Rect((0, 0), button_topleft.get_size()))
    button.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((button_w - button_topright.get_width(), 0),
                    button_topright.get_size()))
    button.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((0, button_h - button_bottomleft.get_height()),
                    button_bottomleft.get_size()))
    button.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((button_w - button_bottomright.get_width(),
                     button_h - button_bottomright.get_height()),
                    button_bottomright.get_size()))

    button.blit(button_topleft, (0, 0))
    button.blit(button_topright, (button_w - button_topright.get_width(), 0))
    button.blit(button_bottomleft,
                (0, button_h - button_bottomleft.get_height()))
    button.blit(button_bottomright,
                (button_w - button_bottomright.get_width(),
                 button_h - button_bottomright.get_height()))
    for i in xrange(button_topleft.get_width(),
                    button_w - button_topright.get_width(),
                    button_top.get_width()):
        button.blit(button_top, (i, 0))
    for i in xrange(button_bottomleft.get_width(),
                    button_w - button_bottomright.get_width(),
                    button_bottom.get_width()):
        button.blit(button_bottom, (i, button_h - button_bottom.get_height()))
    for i in xrange(button_topleft.get_height(),
                    button_h - button_bottomleft.get_height(),
                    button_left.get_height()):
        button.blit(button_left, (0, i))
    for i in xrange(button_topright.get_height(),
                    button_h - button_bottomright.get_height(),
                    button_right.get_height()):
        button.blit(button_right, (button_w - button_right.get_width(), i))

    # Button image when selected
    button_selected = pygame.Surface((button_w, button_h), pygame.SRCALPHA)
    for i in xrange(0, button_w, button_selected_fill.get_width()):
        for j in xrange(0, button_h, button_selected_fill.get_height()):
            button_selected.blit(button_selected_fill, (i, j))

    # Clear the way for the corners and edges (so transparency works
    # properly)
    button_selected.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((0, 0), button_selected_topleft.get_size()))
    button_selected.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((button_w - button_selected_topright.get_width(), 0),
                    button_selected_topright.get_size()))
    button_selected.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((0, button_h - button_selected_bottomleft.get_height()),
                    button_selected_bottomleft.get_size()))
    button_selected.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((button_w - button_selected_bottomright.get_width(),
                     button_h - button_selected_bottomright.get_height()),
                    button_selected_bottomright.get_size()))

    button_selected.blit(button_selected_topleft, (0, 0))
    button_selected.blit(button_selected_topright,
                         (button_w - button_selected_topright.get_width(), 0))
    button_selected.blit(
        button_selected_bottomleft,
        (0, button_h - button_selected_bottomleft.get_height()))
    button_selected.blit(button_selected_bottomright,
                         (button_w - button_selected_bottomright.get_width(),
                          button_h - button_selected_bottomright.get_height()))
    for i in xrange(button_selected_topleft.get_width(),
                    button_w - button_selected_topright.get_width(),
                    button_selected_top.get_width()):
        button_selected.blit(button_selected_top, (i, 0))
    for i in xrange(button_selected_bottomleft.get_width(),
                    button_w - button_selected_bottomright.get_width(),
                    button_selected_bottom.get_width()):
        button_selected.blit(
            button_selected_bottom,
            (i, button_h - button_selected_bottom.get_height()))
    for i in xrange(button_selected_topleft.get_height(),
                    button_h - button_selected_bottomleft.get_height(),
                    button_selected_left.get_height()):
        button_selected.blit(button_selected_left, (0, i))
    for i in xrange(button_selected_topright.get_height(),
                    button_h - button_selected_bottomright.get_height(),
                    button_selected_right.get_height()):
        button_selected.blit(button_selected_right,
                             (button_w - button_selected_right.get_width(), i))

    # Button image when pressed
    button_press = pygame.Surface((button_w, button_h), pygame.SRCALPHA)
    for i in xrange(0, button_w, button_press_fill.get_width()):
        for j in xrange(0, button_h, button_press_fill.get_height()):
            button_press.blit(button_press_fill, (i, j))

    # Clear the way for the corners and edges (so transparency works
    # properly)
    button_press.fill(pygame.Color(0, 0, 0, 0),
                      pygame.Rect((0, 0), button_press_topleft.get_size()))
    button_press.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((button_w - button_press_topright.get_width(), 0),
                    button_press_topright.get_size()))
    button_press.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((0, button_h - button_press_bottomleft.get_height()),
                    button_press_bottomleft.get_size()))
    button_press.fill(
        pygame.Color(0, 0, 0, 0),
        pygame.Rect((button_w - button_press_bottomright.get_width(),
                     button_h - button_press_bottomright.get_height()),
                    button_press_bottomright.get_size()))

    button_press.blit(button_press_topleft, (0, 0))
    button_press.blit(button_press_topright,
                      (button_w - button_press_topright.get_width(), 0))
    button_press.blit(button_press_bottomleft,
                      (0, button_h - button_press_bottomleft.get_height()))
    button_press.blit(button_press_bottomright,
                      (button_w - button_press_bottomright.get_width(),
                       button_h - button_press_bottomright.get_height()))
    for i in xrange(button_press_topleft.get_width(),
                    button_w - button_press_topright.get_width(),
                    button_press_top.get_width()):
        button_press.blit(button_press_top, (i, 0))
    for i in xrange(button_press_bottomleft.get_width(),
                    button_w - button_press_bottomright.get_width(),
                    button_press_bottom.get_width()):
        button_press.blit(button_press_bottom,
                          (i, button_h - button_press_bottom.get_height()))
    for i in xrange(button_press_topleft.get_height(),
                    button_h - button_press_bottomleft.get_height(),
                    button_press_left.get_height()):
        button_press.blit(button_press_left, (0, i))
    for i in xrange(button_press_topright.get_height(),
                    button_h - button_press_bottomright.get_height(),
                    button_press_right.get_height()):
        button_press.blit(button_press_right,
                          (button_w - button_press_right.get_width(), i))

    message_sprite = sge.Sprite(width=text_w, height=text_h)
    message_sprite.draw_text(font, text, 0, 0, text_w, text_h, color='black')
    box.blit(message_sprite._baseimages[0], (4, 4))
    del sge.game.sprites[message_sprite.id]

    selected_buttons = []
    press_buttons = []
    button_rects = []
    button_y = box_h - 8
    for i in xrange(len(buttons)):
        button_surf = button.copy()
        button_selected_surf = button_selected.copy()
        button_press_surf = button_press.copy()
        rendered_text = font._font.render(buttons[i], True, (0, 0, 0))
        button_rect = button_surf.get_rect()
        render_rect = rendered_text.get_rect(center=button_rect.center)
        button_surf.blit(rendered_text, render_rect)
        button_selected_surf.blit(rendered_text, render_rect)
        button_press_surf.blit(rendered_text, render_rect)
        selected_buttons.append(button_selected_surf)
        press_buttons.append(button_press_surf)

        button_x = box_w * (1 + 2 * i) / (len(buttons) * 2)
        button_rect.centerx = button_x
        button_rect.bottom = button_y
        box.blit(button_surf, button_rect)

        button_rect.left += box_rect.left
        button_rect.top += box_rect.top
        button_rects.append(button_rect)

    background.blit(box, box_rect)

    if text_entry:
        text_entered = default
        cursor_position = len(default)
        selection = 1
    else:
        selection = default
        text_entered = ""
        cursor_position = 0
    prev_axis_value = []
    for j in xrange(get_joysticks()):
        axes = []
        for a in xrange(get_joystick_axes(j)):
            axes.append(0)
        prev_axis_value.append(axes)
    button_entered = None
    button_clicked = None
    text_entry_offset = min(
        0, text_entry_rect.w - font._font.size(text_entered)[0])
    orig_screenshot = screenshot
    orig_background = background
    sge.game._clock.tick()

    while sge.game._running:
        # Events
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    if text_entry:
                        if cursor_position < len(text_entered):
                            cursor_position += 1
                    else:
                        selection += 1
                        selection %= len(buttons)
                elif event.key == pygame.K_LEFT:
                    if text_entry:
                        if cursor_position > 0:
                            cursor_position -= 1
                    else:
                        selection -= 1
                        selection %= len(buttons)
                elif event.key == pygame.K_TAB:
                    selection += 1
                    selection %= len(buttons)
                elif event.key == pygame.K_RETURN:
                    button_entered = selection
                elif event.key == pygame.K_BACKSPACE:
                    if text_entry and cursor_position > 0:
                        text_entered_list = list(text_entered)
                        del text_entered_list[cursor_position - 1]
                        text_entered = ''.join(text_entered_list)
                        cursor_position -= 1
                elif event.key == pygame.K_DELETE:
                    if text_entry and cursor_position < len(text_entered):
                        text_entered_list = list(text_entered)
                        del text_entered_list[cursor_position]
                        text_entered = ''.join(text_entered_list)
                elif event.key == pygame.K_ESCAPE:
                    return
                elif event.unicode:
                    if text_entry:
                        text_entered_list = list(text_entered)
                        text_entered_list.insert(cursor_position,
                                                 event.unicode)
                        text_entered = ''.join(text_entered_list)
                        cursor_position += 1
            elif event.type == pygame.KEYUP:
                if (event.key == pygame.K_RETURN
                        or event.key == pygame.K_KP_ENTER):
                    if button_entered == selection:
                        if text_entry:
                            if selection:
                                return text_entered
                            else:
                                return None
                        else:
                            return selection
                    else:
                        button_entered = None
            elif event.type == pygame.JOYAXISMOTION:
                if (event.joy < len(prev_axis_value)
                        and event.axis < len(prev_axis_value[event.joy])):
                    if (event.value >= 0.75
                            and prev_axis_value[event.joy][event.axis] < 0.75):
                        selection += 1
                        selection %= len(buttons)
                    elif (event.value <= -0.75
                          and prev_axis_value[event.joy][event.axis] > -0.75):
                        selection -= 1
                        selection %= len(buttons)
                    prev_axis_value[event.joy][event.axis] = event.value
            elif event.type == pygame.JOYHATMOTION:
                x, y = event.value
                if y == 0:
                    if x == 1:
                        selection += 1
                        selection %= len(buttons)
                    elif x == -1:
                        selection -= 1
                        selection %= len(buttons)
            elif event.type == pygame.JOYBALLMOTION:
                x = event.rel[0]
                if x >= 0.75:
                    selection += 1
                    selection %= len(buttons)
                elif x <= -0.75:
                    selection -= 1
                    selection %= len(buttons)
            elif event.type == pygame.JOYBUTTONDOWN:
                button_entered = selection
            elif event.type == pygame.JOYBUTTONUP:
                if button_entered == selection:
                    if text_entry:
                        if selection:
                            return text_entered
                        else:
                            return None
                    else:
                        return selection
                else:
                    button_entered = None
            elif event.type == pygame.MOUSEMOTION:
                x, y = event.pos
                for i in xrange(len(button_rects)):
                    rect = button_rects[i]
                    if (rect.left <= x <= rect.right
                            and rect.top <= y <= rect.bottom):
                        selection = i
                        break
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == sge.MOUSE_BUTTONS['left']:
                    x, y = event.pos
                    for i in xrange(len(button_rects)):
                        rect = button_rects[i]
                        if (rect.left <= x <= rect.right
                                and rect.top <= y <= rect.bottom):
                            button_clicked = i
                            break
            elif event.type == pygame.MOUSEBUTTONUP:
                if (event.button == sge.MOUSE_BUTTONS['left']
                        and button_clicked is not None):
                    x, y = event.pos
                    rect = button_rects[button_clicked]
                    if (rect.left <= x <= rect.right
                            and rect.top <= y <= rect.bottom):
                        if text_entry:
                            if button_clicked:
                                return text_entered
                            else:
                                return
                        else:
                            return button_clicked
                    else:
                        button_clicked = None
            elif event.type == pygame.QUIT:
                if sge.DEBUG:
                    print('Quit requested by the system.')
                pygame.event.post(event)
                return
            elif event.type == pygame.VIDEORESIZE:
                if sge.DEBUG:
                    print('Video resize detected.')
                sge.game._window_width = event.w
                sge.game._window_height = event.h
                sge.game._set_mode()
                sge.game._background_changed = True

        # Time management
        sge.game._clock.tick(60)

        # Redraw
        window.blit(background, (0, 0))
        window.blit(selected_buttons[selection], button_rects[selection])

        if button_entered is not None and button_entered == selection:
            window.blit(press_buttons[button_entered],
                        button_rects[button_entered])
        if button_clicked is not None:
            x, y = pygame.mouse.get_pos()
            rect = button_rects[button_clicked]
            if (rect.left <= x <= rect.right and rect.top <= y <= rect.bottom):
                window.blit(press_buttons[button_clicked],
                            button_rects[button_clicked])

        if text_entry:
            # Find cursor position, adjust offset
            text_before_cursor = text_entered[:cursor_position]
            cursor_base_x = font._font.size(text_before_cursor)[0]
            cursor_x = cursor_base_x + text_entry_offset
            if cursor_x >= text_entry_rect.w:
                text_entry_offset -= cursor_x - text_entry_rect.w - 1
            elif cursor_x < 0:
                text_entry_offset -= cursor_x
            cursor_x = cursor_base_x + text_entry_offset

            # Render text
            rendered_text = font._font.render(text_entered, True, (0, 0, 0))
            text_render_surf = pygame.Surface(text_entry_rect.size,
                                              pygame.SRCALPHA)
            text_render_surf.fill(pygame.Color(0, 0, 0, 0))
            text_render_surf.blit(rendered_text, (text_entry_offset, 0))
            text_render_surf.blit(cursor, (cursor_x, 0))
            window.blit(text_render_surf, text_entry_rect)

        pygame.display.flip()

    # Restore the look of the screen from before
    window.blit(screenshot, (0, 0))
    pygame.display.update()
    sge.game._background_changed = True
Ejemplo n.º 25
0
def main(*args):
    # Create Game object
    Game(*SCREEN_SIZE, scale=0.5, scale_smooth=True)

    # Load editor resources
    # Sprites
    sge.Sprite('stellar_room_editor_cursor',
               width=CURSOR_SIZE[0],
               height=CURSOR_SIZE[1],
               origin_x=CURSOR_ORIGIN[0],
               origin_y=CURSOR_ORIGIN[1])
    sge.Sprite('stellar_room_editor_panel_left',
               width=SIDE_BAR_SIZE[0],
               height=SIDE_BAR_SIZE[1])
    sge.Sprite('stellar_room_editor_panel_top',
               width=TOP_BAR_SIZE[0],
               height=TOP_BAR_SIZE[1])
    sge.Sprite('stellar_room_editor_button',
               width=BUTTON_SIZE[0],
               height=BUTTON_SIZE[1],
               fps=0)
    sge.Sprite('stellar_room_editor_textbox',
               width=TEXTBOX_SIZE[0],
               height=TEXTBOX_SIZE[1])
    sge.Sprite('stellar_room_editor_checkbox',
               width=CHECKBOX_SIZE[0],
               height=CHECKBOX_SIZE[1],
               fps=0)
    sge.Sprite('stellar_room_editor_unknown',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_active',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_args',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_background',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_close',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_grid',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_grid_isometric',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_image_settings',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_inactive',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_load',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_return_to_room',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_reload_resources',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_room_next',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_room_previous',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_save',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_save_all',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_settings',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_shift',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_solid',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_tool_fill',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_tool_line',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_tool_paintbrush',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_tool_pointer',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_unsolid',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_views',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_zoom_in',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_zoom_out',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    sge.Sprite('stellar_room_editor_icon_zoom_reset',
               width=ICON_SIZE[0],
               height=ICON_SIZE[1])
    glob.tooltip_sprite = sge.Sprite()

    # Fonts
    glob.text_entry_font = sge.Font('OSP-DIN.ttf', 20)
    glob.tooltip_font = sge.Font('OSP-DIN.ttf', 16)

    # Set mouse cursor
    sge.game.mouse.sprite = 'stellar_room_editor_cursor'

    if len(args) > 1:
        glob.game_file = args[1]

    # Load game resources
    try:
        load_resources()
    except IOError:
        print('"{0}" is not a proper game file. Ignoring.'.format(
            glob.game_file))
        glob.game_file = None

    # Create rooms
    for arg in args[2:]:
        try:
            Room.load(arg)
        except IOError:
            print('"{0}" is not a proper room file. Skipping.'.format(arg))

    if not sge.game.rooms:
        Room()

    sge.game.start()
Ejemplo n.º 26
0
def main():
    # Create Game object
    Game(640, 480, fps=120)

    # Load sprites
    paddle_sprite = sge.Sprite(ID="paddle",
                               width=8,
                               height=48,
                               origin_x=4,
                               origin_y=24)
    paddle_sprite.draw_rectangle(0,
                                 0,
                                 paddle_sprite.width,
                                 paddle_sprite.height,
                                 fill="white")
    ball_sprite = sge.Sprite(ID="ball",
                             width=8,
                             height=8,
                             origin_x=4,
                             origin_y=4)
    ball_sprite.draw_rectangle(0,
                               0,
                               ball_sprite.width,
                               ball_sprite.height,
                               fill="white")
    glob.hud_sprite = sge.Sprite(width=320,
                                 height=160,
                                 origin_x=160,
                                 origin_y=0)

    # Load backgrounds
    layers = (sge.BackgroundLayer("ball",
                                  sge.game.width / 2,
                                  0,
                                  -10000,
                                  xrepeat=False), )
    background = sge.Background(layers, "black")

    # Load fonts
    sge.Font('Liberation Mono', ID="hud", size=48)

    # Load sounds
    glob.bounce_sound = sge.Sound('bounce.wav')
    glob.bounce_wall_sound = sge.Sound('bounce_wall.wav')
    glob.score_sound = sge.Sound('score.wav')

    # Create objects
    Player(1)
    Player(2)
    glob.ball = Ball()
    hud = sge.StellarClass(sge.game.width / 2,
                           0,
                           -10,
                           sprite=glob.hud_sprite,
                           detects_collisions=False)
    objects = (glob.player1, glob.player2, glob.ball, hud)

    # Create rooms
    room1 = sge.Room(objects, background=background)

    sge.game.start()