Beispiel #1
0
class Menu(object):
    def __init__(self, field_center_x, field_center_y, gameStartCallback, gameEndCallback, win):
        self._batch = pyglet.graphics.Batch()

        self._startGameMenuItem = MenuItem("start game", field_center_x, field_center_y - 80 , gameStartCallback, self._batch)
        self._exitGameMenuItem = MenuItem("exit game", field_center_x, field_center_y - 110, gameEndCallback, self._batch)
        self.win = win

        self._startGameMenuItem.is_selected = True
        self._selectedMenuItem = self._startGameMenuItem

        img = pyglet.resource.image('title.png')
        img.anchor_x = img.width/2
        img.anchor_y = img.height/2
        self._title = pyglet.sprite.Sprite(img, field_center_x, FIELD_HEIGHT-img.height, batch=self._batch)

        img = pyglet.resource.image('logo.png')
        img.anchor_x = img.width/2
        img.anchor_y = img.height/2
        self._logo = pyglet.sprite.Sprite(img, field_center_x, field_center_y + self._title.height, batch=self._batch)

        self._menuBackground = impl.MenuBackground.MenuBackground(0, 0, FIELD_WIDTH, FIELD_HEIGHT)
        self.start_bgrn_animation()

    def start_bgrn_animation(self):
        schedule_interval(func=self._menuBackground.update,interval=MENU_BACKGROUND_ANIMATION_INTERVAL)

    def stop_bgrn_animation(self):
        unschedule(func=self._menuBackground.update)

    def on_draw(self):
        self.win.clear()
        self._menuBackground.draw()
        self._batch.draw()
        
    def on_key_press(self, symbol, modifiers):
        if symbol == key.UP:
            if self._selectedMenuItem is self._startGameMenuItem:
                pass
            else:
                self._startGameMenuItem.is_selected = True
                self._exitGameMenuItem.is_selected = False

                self._selectedMenuItem = self._startGameMenuItem
        elif symbol == key.DOWN:
            if self._selectedMenuItem is self._exitGameMenuItem:
                pass
            else:
                self._startGameMenuItem.is_selected = False
                self._exitGameMenuItem.is_selected = True

                self._selectedMenuItem = self._exitGameMenuItem
        elif  symbol == key.ESCAPE:
                pyglet.clock.unschedule(func=self._menuBackground.update)
                self.win.close()

        if symbol == key.ENTER:
            self.stop_bgrn_animation()
            self._selectedMenuItem.on_key_press(symbol, modifiers)

    def on_mouse_press(self, x, y, button, modifiers):
        if self._startGameMenuItem.on_mouse_press(x, y) or self._exitGameMenuItem.on_mouse_press(x, y):
            self.stop_bgrn_animation()