Exemplo n.º 1
0
class Dialogue(GUI):
    # 显示在左面的聊天记录

    def __init__(self):
        width, height = get_size()
        GUI.__init__(self, width, height)
        self.dialogue_label = ColorLabel('',
                                         x=2,
                                         y=height - 75,
                                         width=width // 2,
                                         multiline=True)
        # 全部聊天内容
        self.dialogue = []
        self.history = []
        # 当前位置
        self.pointer = 0
        # 实际显示的聊天内容
        self.shown = []
        # 最后一条聊天发送的时间
        self.last = time.time()

    def add_dialogue(self, text):
        log_info('New dialogue: %s' % text)
        self.dialogue.append(text)
        self.pointer = len(self.dialogue) - 1
        self.last = time.time()
        if len(self.shown) < 10:
            self.shown.append(text)
        else:
            self.shown.pop(0)
            self.shown.append(text)

    def draw(self):
        # 两个换行符表示真正的换行
        shown = ''
        for text in self.shown:
            shown += text + '\n'
        else:
            self.dialogue_label.text = shown
            self.dialogue_label.draw()

    def resize(self, width, height):
        self.dialogue_label.x = 2
        self.dialogue_label.y = height - 75
        self.dialogue_label.width = width // 2

    def update(self):
        if time.time() - self.last > 5.0:
            if len(self.dialogue) > 4096:
                self.dialogue.clear()
            if len(self.shown) > 0:
                self.shown.pop(0)
                self.last = time.time()
Exemplo n.º 2
0
 def __init__(self):
     width, height = get_size()
     GUI.__init__(self, width, height)
     self.dialogue_label = ColorLabel('',
                                      x=2,
                                      y=height - 75,
                                      width=width // 2,
                                      multiline=True)
     # 全部聊天内容
     self.dialogue = []
     self.history = []
     # 当前位置
     self.pointer = 0
     # 实际显示的聊天内容
     self.shown = []
     # 最后一条聊天发送的时间
     self.last = time.time()
Exemplo n.º 3
0
    def __init__(self, game):
        self.game = game
        self.frame = Frame(self.game, True)
        self.frame.set_background_color((220, 0, 0, 100))
        self._text_die = ColorLabel(
            text=resource_pack.get_translation('game.text.die'),
            x=self.game.width / 2,
            y=0.8 * self.game.height,
            font_size=42,
            anchor_x='center',
            anchor_y='center')
        self._text_reason = ColorLabel(text=get_game().player['die_reason'],
                                       anchor_x='center',
                                       anchor_y='center',
                                       x=self.game.width / 2,
                                       y=0.7 * self.game.height,
                                       font_size=15)
        self._respawn = Button(
            ((self.game.width - 200) / 2), 0.7 * self.game.height, 200, 40,
            resource_pack.get_translation('game.text.respawn'))

        def on_press():
            self.game.player['die'] = False
            self.game.player['position'] = self.game.player['respawn_position']
            self.game.toggle_gui()

        def on_resize(width, height):
            self._text_die.x = width / 2
            self._text_die.y = 0.8 * height
            self._text_reason.x = width / 2
            self._text_reason.y = 0.7 * height
            self._respawn.x = (width - 200) / 2
            self._respawn.y = 0.7 * height

        def on_die():
            self._text_reason.text = get_game().player['die_reason']

        self.frame.add_widget(self._text_die)
        self.frame.add_widget(self._text_reason)
        self.frame.add_widget(self._respawn)
        get_game().register_event('die', on_die)
        self.frame.register_event('resize', on_resize)
        self._respawn.register_event('press', on_press)
Exemplo n.º 4
0
 def __init__(self, x, y, width, height, text):
     self._size = win_width, win_height = get_size()
     super().__init__(x, win_height - y, width, height)
     self._width = width
     self._depressed_img = resource_pack.get_resource(
         'textures/gui/widgets').get_region(0, 170, 200, 20)
     self._pressed_img = resource_pack.get_resource(
         'textures/gui/widgets').get_region(0, 150, 200, 20)
     self._unable_img = resource_pack.get_resource(
         'textures/gui/widgets').get_region(0, 190, 200, 20)
     self._sprite = Sprite(self._depressed_img, x, win_height - y)
     self._text = text
     self._label = ColorLabel(self._text,
                              color='white',
                              align='center',
                              anchor_x='center',
                              anchor_y='center',
                              x=x + width / 2,
                              y=win_height - y + height / 2,
                              font_size=16)
     self._pressed = False
     self._enable = True
Exemplo n.º 5
0
    def __init__(self, game):
        width, height = get_size()
        self.game = game
        self.frame = Frame(self.game, True)
        self._inventory = Image(
            (width - 352) / 2, (height - 332) / 2,
            resource_pack.get_resource(
                'textures/gui/containers/inventory').get_region(
                    0, 90, 176, 166))
        self._inventory.sprite.scale = 2
        self._label = ColorLabel(
            text=resource_pack.get_translation('game.gui.craft.name'),
            color='black',
            anchor_x='left',
            font_size=12,
            x=(width - 352) / 2 + 194,
            y=height - (height - 332) / 2 - 28,
            shadow=False)
        self._slots = BagSlot((width - 352) / 2 + 16, (height - 332) / 2 + 164)
        self._table = CraftingTable((width - 352) / 2 + 196,
                                    height - (height - 332) / 2 - 36, 2)
        self._table.set_output(112, -20)

        def on_resize(width, height):
            self._inventory.sprite.position = (width - 352) / 2, (height -
                                                                  332) / 2
            self._label.x = (width - 352) / 2 + 194
            self._label.y = height - (height - 332) / 2 + 284
            self._slots.resize((width - 352) / 2 + 16,
                               (height - 332) / 2 + 164)
            self._table.resize((width - 352) / 2 + 196,
                               height - (height - 332) / 2 - 36)

        self.frame.register_event('resize', on_resize)
        self.frame.add_widget(self._inventory)
        self.frame.add_widget(self._label)
        for slot in self._slots.slots():
            self.frame.add_widget(slot)
        for slot in self._table.slots():
            self.frame.add_widget(slot)
Exemplo n.º 6
0
class Button(Widget):
    def __init__(self, x, y, width, height, text):
        self._size = win_width, win_height = get_size()
        super().__init__(x, win_height - y, width, height)
        self._width = width
        self._depressed_img = resource_pack.get_resource(
            'textures/gui/widgets').get_region(0, 170, 200, 20)
        self._pressed_img = resource_pack.get_resource(
            'textures/gui/widgets').get_region(0, 150, 200, 20)
        self._unable_img = resource_pack.get_resource(
            'textures/gui/widgets').get_region(0, 190, 200, 20)
        self._sprite = Sprite(self._depressed_img, x, win_height - y)
        self._text = text
        self._label = ColorLabel(self._text,
                                 color='white',
                                 align='center',
                                 anchor_x='center',
                                 anchor_y='center',
                                 x=x + width / 2,
                                 y=win_height - y + height / 2,
                                 font_size=16)
        self._pressed = False
        self._enable = True

    def _update(self):
        self._sprite.position = self._x, self._y
        self._label.x = self._x + self._width / 2
        self._label.y = self._y + self._height / 2

    def draw(self):
        self._sprite.scale_x = self._width / 200
        self._sprite.scale_y = self._height / 20
        self._sprite.draw()
        self._label.draw()

    def enable(self, status):
        self._enable = bool(status)
        if self._enable:
            self._label.color = 'white'
        else:
            self._label.color = 'gray'

    def text(self, text):
        self._text = text
        self._label.text = self._text

    def on_mouse_press(self, x, y, buttons, modifiers):
        if self.check_hit(x, y) and self._enable:
            self._sprite.image = self._pressed_img
            self._pressed = True
            self.dispatch_event('on_press')

    def on_mouse_release(self, x, y, buttons, modifiers):
        if self._pressed:
            self._sprite.image = self._depressed_img
            self._pressed = False
            self.dispatch_event('on_release')

    def on_mouse_motion(self, x, y, dx, dy):
        if not self._pressed:
            if self.check_hit(x, y) and self._enable:
                self._sprite.image = self._pressed_img
                self._label.color = 'yellow'
            else:
                self._sprite.image = self._depressed_img if self._enable else self._unable_img
                self._label.color = 'white' if self._enable else 'gray'
Exemplo n.º 7
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     # 是否初始化
     self.is_init = True
     # 窗口是否捕获鼠标
     self.exclusive = False
     # 鼠标位置
     self.mouse_position = (0, 0)
     self.active_item = self.active_item_name = None
     # 玩家所处的区域
     self.sector = None
     # 游戏世界(秒)
     self.time = 0
     # 玩家
     self.player = Player()
     # 实体
     self.entities = EntityManager()
     # 键盘/鼠标事件
     self.event = dict()
     # HUD/GUI
     self.hud = dict()
     self.guis = dict()
     # 显示在 debug 区域的 info
     self._info_ext = list()
     self._info_ext.append('pyglet' + pyglet.version)
     # 拓展功能
     self.debug = dict(debug=False)
     # 天气(现在天气, 持续时间)
     self.weather = {'now': 'clear', 'duration': 0}
     # 玩家可以放置的方块
     self.inventory = [
         'grass', 'dirt', 'log', 'brick', 'leaf', 'plank', 'crafting_table',
         'glass', 'tnt'
     ]
     self.inventory += [None] * (9 - len(self.inventory))
     # 数字键列表
     self.num_keys = [
         key._1, key._2, key._3, key._4, key._5, key._6, key._7, key._8,
         key._9, key._0
     ]
     # 加载窗口
     self.loading = Loading()
     # 聊天区
     self.dialogue = Dialogue()
     # 窗口最小为 800x600
     self.set_minimum_size(800, 600)
     # 这个十字在屏幕中央
     self.reticle = Sprite(
         resource_pack.get_resource('textures/gui/icons').get_region(
             0, 240, 16, 16))
     self.reticle.image.anchor_x = 8
     self.reticle.image.anchor_y = 8
     self.reticle.scale = 2
     self.label = dict()
     # 这个标签在画布的上方显示
     self.label['top'] = ColorLabel('',
                                    x=2,
                                    y=self.height - 5,
                                    width=self.width // 2,
                                    multiline=True,
                                    anchor_x='left',
                                    anchor_y='top',
                                    font_size=16)
     # 这个标签在画布正中偏上显示
     self.label['title'] = ColorLabel('',
                                      x=self.width // 2,
                                      y=self.height // 2 + 50,
                                      anchor_x='center',
                                      anchor_y='center')
     # 这个标签在画布正中偏下显示
     self.label['subtitle'] = ColorLabel('',
                                         x=self.width // 2,
                                         y=self.height // 2 - 100,
                                         anchor_x='center',
                                         anchor_y='center')
     # 这个标签在画布正中再偏下一点显示
     self.label['actionbar'] = ColorLabel('',
                                          x=self.width // 2,
                                          y=self.height // 2 - 150,
                                          anchor_x='center',
                                          anchor_y='center')
     # 覆盖屏幕的矩形
     self.full_screen = Rectangle(0, 0, self.width, self.height)
     # 将 self.upgrade() 方法每 1.0 / TICKS_PER_SEC 调用一次, 它是游戏的主事件循环
     pyglet.clock.schedule_interval(self.update, 1.0 / TICKS_PER_SEC)
     # 每1/10秒更新一次方块数据
     pyglet.clock.schedule_interval(self.update_status, 0.1)
     # 每30秒保存一次进度
     pyglet.clock.schedule_interval(self.save, 30.0)
     # 天空颜色变换
     pyglet.clock.schedule_interval(change_sky_color, 1.0)
     log_info('Welcome %s' % player['name'])
     for lib in libs:
         if hasattr(lib, 'main'):
             lib.main()