Example #1
0
    def __init__(self, target, single_player, color=None):
        """Instantiate SizeMenu. target is window to draw to,
        single_player is whether a single player game is to
        be played and color is the color the player wants to
        play as in case of single player."""

        super().__init__(target)

        if single_player and color is None:
            raise ValueError("Color must not be None if single_player is True.")

        self.next_scene = None

        self.single_player = single_player
        self.color = color

        font = sf.Font.from_file(assets.get_asset("/fonts/GeosansLight.ttf"))
        self.title = sf.Text()
        self.title.string = "Välj storlek på brädet"
        self.title.font = font

        self.size = Spinner(target, 4, 20, 2, 8)

        self.next = Button(target, "Fortsätt")
        self.next.set_listener(self._switch_to_game)

        self.add_component(self.size)
        self.add_component(self.next)

        self._setup_components()
Example #2
0
    def __init__(self, target):
        super().__init__(target)

        self.next_scene = None

        size = self.target.size

        view = sf.View(sf.Rectangle((0, 0), (size.x, size.y)))
        self.target.view = view

        font = sf.Font.from_file(assets.get_asset("/fonts/Amethyst.ttf"))

        # Title
        self.title = sf.Text()
        self.title.string = "Reversi"
        self.title.font = font
        self.title.color = sf.Color.WHITE
        title_bounds = self.title.global_bounds

        # Menu items
        self.menuitems = []
        for item in [["Spela en spelare", self._switch_colormenu], ["Spela två spelare", self._switch_sizemenu], ["Avsluta", target.close]]:
            button = Button(target, item[0])
            button.set_listener(item[1])
            self.menuitems.append(button)
            self.add_component(button)
        self._setup_buttons()
Example #3
0
    def __init__(self, target, low, high, step, init=None):
        """Initialize a new Spinner. target is window to
        draw to, low is lower boundary, high is higher
        boundary, step is the step size and init is
        the initial value."""

        super().__init__(target)

        self.low = low
        self.high = high
        self.step = step
        if init is None:
            self.value = low
        else:
            self.value = init

        self.more = Button(target, "Större")
        self.more.set_listener(self.increment)
        self.less = Button(target, "Mindre")
        self.less.set_listener(self.decrement)

        font = sf.Font.from_file(assets.get_asset("/fonts/GeosansLight.ttf"))
        self.display = sf.Text()
        self.display.font = font
        self.display.string = str(self.value)

        self.set_character_size(14)
        self.set_position((0, 0))
Example #4
0
    def __init__(self, target):
        super().__init__(target)

        self.next_scene = None

        font = sf.Font.from_file(assets.get_asset("/fonts/GeosansLight.ttf"))
        self.title = sf.Text()
        self.title.string = "Vilken färg vill du spela som?"
        self.title.font = font

        self.button_white = Button(self.target, "Vit")
        self.button_white.set_listener(self._switch_size_white)
        self.button_black = Button(self.target, "Svart")
        self.button_black.set_listener(self._switch_size_black)

        self.add_component(self.button_white)
        self.add_component(self.button_black)

        self._setup_components()
Example #5
0
class SizeMenu(Scene):
    """Class representing a menu that lets player
    choose size of board."""

    TITLE_SCALE = 1/10
    SCALE = 1/15

    def __init__(self, target, single_player, color=None):
        """Instantiate SizeMenu. target is window to draw to,
        single_player is whether a single player game is to
        be played and color is the color the player wants to
        play as in case of single player."""

        super().__init__(target)

        if single_player and color is None:
            raise ValueError("Color must not be None if single_player is True.")

        self.next_scene = None

        self.single_player = single_player
        self.color = color

        font = sf.Font.from_file(assets.get_asset("/fonts/GeosansLight.ttf"))
        self.title = sf.Text()
        self.title.string = "Välj storlek på brädet"
        self.title.font = font

        self.size = Spinner(target, 4, 20, 2, 8)

        self.next = Button(target, "Fortsätt")
        self.next.set_listener(self._switch_to_game)

        self.add_component(self.size)
        self.add_component(self.next)

        self._setup_components()

    def draw(self):
        self.target.clear(sf.Color.BLACK)

        self.target.draw(self.title)

        for c in self.components:
            c.draw()

    def event(self, events):
        super().event(events)

        for e in events:
            if type(e) == sf.ResizeEvent:
                size = self.target.size

                view = sf.View(sf.Rectangle((0, 0), (size.x, size.y)))
                self.target.view = view

                self._setup_components()

    def update(self, t):
        super().update(t)

        return self.next_scene

    def _setup_components(self):
        size = self.target.size

        self.title.character_size = size.y * self.TITLE_SCALE
        title_bounds = self.title.local_bounds
        self.title.position = (size.x / 2 - title_bounds.width / 2, 10)

        self.size.set_character_size(size.y * self.SCALE)
        bounds = self.size.get_bounds()
        self.size.set_position((size.x/2 - bounds[0]/2, size.y/2 - bounds[1]/2))

        self.next.set_character_size(size.y * self.SCALE)
        next_bounds = self.next.get_bounds()
        self.next.set_position((size.x/2 - next_bounds[0]/2, 3*size.y/4 - next_bounds[1]/2))

    def _switch_to_game(self):
        self.next_scene = GameScene(self.target, self.size.get_value(), self.single_player, self.color)
Example #6
0
class Spinner(Component):
    """Spinner allowing you to choose a number."""

    MARGIN = 5

    def __init__(self, target, low, high, step, init=None):
        """Initialize a new Spinner. target is window to
        draw to, low is lower boundary, high is higher
        boundary, step is the step size and init is
        the initial value."""

        super().__init__(target)

        self.low = low
        self.high = high
        self.step = step
        if init is None:
            self.value = low
        else:
            self.value = init

        self.more = Button(target, "Större")
        self.more.set_listener(self.increment)
        self.less = Button(target, "Mindre")
        self.less.set_listener(self.decrement)

        font = sf.Font.from_file(assets.get_asset("/fonts/GeosansLight.ttf"))
        self.display = sf.Text()
        self.display.font = font
        self.display.string = str(self.value)

        self.set_character_size(14)
        self.set_position((0, 0))

    def draw(self):
        self.target.draw(self.display)
        self.less.draw()
        self.more.draw()

    def update(self, t):
        self.more.update(t)
        self.less.update(t)

    def event(self, events):
        self.more.event(events)
        self.less.event(events)

    def set_character_size(self, size):
        self.display.character_size = size
        self.more.set_character_size(size)
        self.less.set_character_size(size)

    def get_position(self):
        return self.pos

    def set_position(self, pos):
        self.pos = pos

        disp_bounds = self.display.local_bounds
        less_bounds = self.less.get_bounds()
        more_bounds = self.more.get_bounds()

        self.display.position = (pos[0] + (less_bounds[0] + self.MARGIN + more_bounds[0])/2 - disp_bounds.width/2, pos[1])
        self.less.set_position((pos[0], pos[1] + disp_bounds.height + self.MARGIN))
        self.more.set_position((pos[0] + less_bounds[0] + self.MARGIN, pos[1] + disp_bounds.height + self.MARGIN))

    def get_bounds(self):
        disp_bounds = self.display.local_bounds
        less_bounds = self.less.get_bounds()
        more_bounds = self.more.get_bounds()

        return (less_bounds[0] + self.MARGIN + more_bounds[0], disp_bounds.height + self.MARGIN + less_bounds[1])

    def get_value(self):
        return self.value

    def set_value(self, val):
        self.value = val

        self.display.string = str(val)
        self.set_position(self.pos)

    def increment(self):
        val = self.value + self.step
        if val > self.high:
            val = self.high

        self.set_value(val)

    def decrement(self):
        val = self.value - self.step
        if val < self.low:
            val = self.low

        self.set_value(val)
Example #7
0
class ColorMenu(Scene):
    """Class representing menu that lets the player
    choose the color to play as."""

    TITLE_SCALE = 1 / 10
    BUTTON_SCALE = 1 / 15

    def __init__(self, target):
        super().__init__(target)

        self.next_scene = None

        font = sf.Font.from_file(assets.get_asset("/fonts/GeosansLight.ttf"))
        self.title = sf.Text()
        self.title.string = "Vilken färg vill du spela som?"
        self.title.font = font

        self.button_white = Button(self.target, "Vit")
        self.button_white.set_listener(self._switch_size_white)
        self.button_black = Button(self.target, "Svart")
        self.button_black.set_listener(self._switch_size_black)

        self.add_component(self.button_white)
        self.add_component(self.button_black)

        self._setup_components()

    def draw(self):
        self.target.clear(sf.Color.BLACK)

        self.target.draw(self.title)

        for c in self.components:
            c.draw()

    def event(self, events):
        super().event(events)

        for e in events:
            if type(e) == sf.ResizeEvent:
                size = self.target.size

                view = sf.View(sf.Rectangle((0, 0), (size.x, size.y)))
                self.target.view = view

                self._setup_components()

    def update(self, t):
        super().update(t)

        return self.next_scene

    def _setup_components(self):
        size = self.target.size

        self.title.character_size = size.y * self.TITLE_SCALE
        title_bounds = self.title.local_bounds
        self.title.position = (size.x / 2 - title_bounds.width / 2, 10)

        self.button_black.set_character_size(size.y * self.BUTTON_SCALE)
        black_bounds = self.button_black.get_bounds()
        self.button_black.set_position((size.x / 4 - black_bounds[0] / 2,
                                        size.y / 2 - black_bounds[1] / 2))

        self.button_white.set_character_size(size.y * self.BUTTON_SCALE)
        white_bounds = self.button_white.get_bounds()
        self.button_white.set_position((3 * size.x / 4 - white_bounds[0] / 2,
                                        size.y / 2 - white_bounds[1] / 2))

    def _switch_size_white(self):
        self.next_scene = SizeMenu(self.target, True, reversi.WHITE)

    def _switch_size_black(self):
        self.next_scene = SizeMenu(self.target, True, reversi.BLACK)