Example #1
0
    def show(self, screen):
        """
            Show Widget to screen

            :param screen: Screen where widget must be showed
            :return: Rects must be updated

            .. note:: You may not use this method. UISystem make it for you
        """
        rects = []
        if self.showed:
            if self.old_render != self.render or self.old_pos != Vec2(self.x, self.y):
                if self.old_render is not None:
                    screen.fill(self.system.world.window.color.get_rgba(), self.old_render.get_rect(x=self.old_pos.x,
                                                                                                    y=self.old_pos.y))
                    rects.append(self.old_render.get_rect(x=self.old_pos.x, y=self.old_pos.y))
                screen.blit(self.render, (self.x, self.y))
                rects.append(self.render.get_rect(x=self.x, y=self.y))
                self.old_render = self.render
                self.old_pos = Vec2(self.x, self.y)
            else:
                screen.blit(self.render, (self.x, self.y))
        else:
            if self.old_render is not None:
                screen.fill(self.system.world.window.color.get_rgba(), self.old_render.get_rect(x=self.old_pos.x,
                                                                                                y=self.old_pos.y))
                rects.append(self.old_render.get_rect(x=self.old_pos.x, y=self.old_pos.y))
                self.old_render = None
                self.old_pos = None
        return rects
Example #2
0
    def position(self):
        """
            Get Position of Component

            :return: Vec2 of Position
        """
        return Vec2(self.x, self.y)
Example #3
0
    def show_debug(self, screen):
        """
            Show debug widget to screen

            :param screen: Screen where entity is showed
            :return: Rects must be updated

            .. note:: You may not use this method. EntitySystem make it for you
        """
        rects = []
        if self.showed:
            pos = Vec2(self.x, self.y)
            image = self.system.debug_font.render("ID : "+str(self.identity))
            if self.old_debug_pos != pos:
                if self.old_debug_pos is not None:
                    rect = image.get_rect(x=self.old_debug_pos.x, y=self.old_debug_pos.y - 20)
                    rects.append(rect)
                    screen.fill(self.system.world.window.color.get_rgba(), rect)
                self.old_debug_pos = pos
                rects.append(image.get_rect(x=pos.x, y=pos.y-20))

            screen.blit(image, (pos.x, pos.y - 20))
        else:
            if self.old_debug_pos is not None:
                image = self.system.debug_font.render("ID : "+str(self.identity))
                screen.fill(self.system.world.window.color.get_rgba(),
                            image.get_rect(x=self.old_debug_pos.x, y=self.old_debug_pos.y - 20))
                rects.append(image.get_rect(x=self.old_debug_pos.x, y=self.old_debug_pos.y - 20))
                self.old_debug_pos = None
        return rects
Example #4
0
    def __init__(self,
                 x,
                 y,
                 text,
                 command=None,
                 font=Font(),
                 size=Vec2(100, 40),
                 background=Color.from_name("GRAY").darker(5)):
        """
            Create Button

            :param x: X Pos
            :param y: Y Pos
            :param text: Text of Button
            :param command: Function triggered when button is clicked
            :param font: Font of Button
            :param size: Size of Button
            :param background: Path of background image or Color
        """
        super(Button, self).__init__(x, y)
        self.text = text
        self.command = command
        self.font = font
        self.background = background
        self.size = size
        self.ishover = False
        self.render = None
        self.old_render = None
        self.old_pos = None
        self.update_render()
Example #5
0
    def __init__(self,
                 sprite,
                 rotation=0,
                 size=None,
                 flipx=False,
                 flipy=False):
        """
            Create SpriteComponent

            This Component is here to add sprite to entity
            Required Components : /

            :param sprite: Path to file of sprite
        """
        super(SpriteComponent, self).__init__()
        self.sprite = sprite
        self.image = pygame.image.load(sprite).convert()
        if size is None:
            size = Vec2(self.image.get_rect().width,
                        self.image.get_rect().height)
        self.size = size
        self.rotation = rotation
        self.flipx = flipx
        self.flipy = flipy
        self.transformed_image = None
        self.old_sprite = sprite
        self.update_image()
Example #6
0
    def __init__(self, movement=Vec2.zero(), rotation=0):
        """
            Create AutoComponent

            This Component is here to add auto movement (direction, rotation) to entity
            Required Components : If movement != Vec2.zero() PositionComponent
                                  If rotation != 0 SpriteComponent

            :param movement: Vector of Automatic Movement
            :param rotation: Number of Automatic Rotation
        """
        super(AutoComponent, self).__init__()
        self.movement = movement
        self.rotation = rotation

        if self.movement != Vec2.zero():
            self.required_components.add(PositionComponent)
        if self.rotation != 0:
            self.required_components.add(SpriteComponent)
Example #7
0
    def __init__(self, x, y, sprites=None, size=Vec2(150, 10), value=0):
        """
            Create ProgressBar

            :param x: X Pos
            :param y: Y Pos
            :param sprites: List of background and foreground or None
            :param size: Size of ProgressBar
            :param value: Percent of ProgressBar
        """
        super(ProgressBar, self).__init__(x, y)
        self.size = size
        self.sprites = sprites
        self.value = value
        self.update_render()
Example #8
0
    def update(self):
        """
            Update values

            .. note:: You may not use this method. Entity make it for you
        """
        if self.movement != Vec2.zero():
            pos = self.entity.get_component(PositionComponent).position()
            pos.x += self.movement.x
            pos.y += self.movement.y

            if self.entity.has_component(CollisionComponent):
                if self.entity.get_component(CollisionComponent).can_go(
                        pos.x, pos.y, "AUTOCOMPONENT"):
                    self.entity.get_component(PositionComponent).set_position(
                        pos.x, pos.y)
            else:
                self.entity.get_component(PositionComponent).set_position(
                    pos.x, pos.y)

        if self.rotation != 0:
            self.entity.get_component(
                SpriteComponent).rotation += self.rotation
            self.entity.get_component(SpriteComponent).update_image()
Example #9
0
        pro1.value = 0
    pro1.update_render()
    pro2.value += 10
    if pro2.value > 100:
        pro2.value = 0
    pro2.update_render()


images = [os.path.join(os.path.dirname(__file__), "sprite0.png")]

game = Window(750, 500, debug=True)

label = Label(100, 100, "Label")
b = Button(200, 200, "Button", label_management)
c = Checkbox(300, 300, "checkbox", scale=2)
i = Image(100, 200, "sprite0.png", Vec2(20, 20))
en = Entry(100, 300, font=Font(size=18, color=Color.from_name("BLACK")))
select = Selector(200, 100, "Ceci", "est", "un", "test", "!", "SELECTOR !")
pro1 = ProgressBar(400, 100)
pro2 = ProgressBar(400, 200, ("sprite0.png", "sprite1.png"), value=34)
con = Console(50, 400, game, 650)

e = Entity()

e.add_component(PositionComponent(0, 0))
e.add_component(SpriteComponent("sprite0.png", size=Vec2(78, 50), rotation=45))
e.add_component(ShowComponent())
e.add_component(ControlComponent("FOURDIRECTION"))

game.world.ui_system.add_widget(label)
game.world.ui_system.add_widget(b)
Example #10
0
from pyengine2.Entities import Entity
from pyengine2.Components import PositionComponent, SpriteComponent, ShowComponent, ControlComponent, TextComponent
from pyengine2.Utils import Vec2

import os

images = [os.path.join(os.path.dirname(__file__), "sprite0.png")]

game = Window(780, 500, debug=True)

e = Entity()

e.add_component(
    ShowComponent())  # Doesn't work because Required Components are not added

e.add_component(PositionComponent(0, 0))
e.add_component(SpriteComponent(images[0], size=Vec2(78, 50), rotation=45))
e.add_component(ShowComponent())
e.add_component(ControlComponent("FOURDIRECTION"))

e2 = Entity()

e2.add_component(PositionComponent(500, 20))
e2.add_component(TextComponent("Ceci est un test"))
e2.add_component(ShowComponent(False))

game.world.entity_system.add_entity(e)
game.world.entity_system.add_entity(e2)

game.run()
Example #11
0
def collision(other_entity, cause):
    pass


images = [os.path.join(os.path.dirname(__file__), "sprite0.png")]

game = Window(780, 500, debug=True)

e = Entity()

e.add_component(
    ShowComponent())  # Doesn't work because Required Components are not added

e.add_component(PositionComponent(100, 100))
e.add_component(SpriteComponent(images[0], size=Vec2(78, 50)))
e.add_component(ShowComponent())
e.add_component(CollisionComponent(collision))
e.add_component(PhysicsComponent())
e.add_component(ControlComponent("CLASSICJUMP"))

e2 = Entity()

e2.add_component(PositionComponent(100, 300))
e2.add_component(SpriteComponent(images[0], size=Vec2(78, 50)))
e2.add_component(ShowComponent())
e2.add_component(CollisionComponent())

game.world.entity_system.add_entity(e)
game.world.entity_system.add_entity(e2)