Exemple #1
0
def loading(*args):
    '''A context manager that shows a loading screen'''
    x = base.win.get_x_size() // 2
    y = base.win.get_y_size() // 2
    img = loader.load_texture('gui/loading.png')
    scale = (256, 0, 256)  #img size//2
    if ConfigVariableBool('framebuffer-srgb').getValue():
        tex_format = img.get_format()
        if tex_format == Texture.F_rgb:
            tex_format = Texture.F_srgb
        elif tex_format == Texture.F_rgba:
            tex_format = Texture.F_srgb_alpha
        img.set_format(tex_format)
    load_screen = OnscreenImage(image=img,
                                scale=scale,
                                pos=(x, 0, -y),
                                parent=pixel2d)
    load_screen.set_transparency(TransparencyAttrib.M_alpha)
    #render 3 frames because we may be in a threaded rendering pipeline
    #we render frames to make sure the load screen is shown
    base.graphicsEngine.renderFrame()
    base.graphicsEngine.renderFrame()
    base.graphicsEngine.renderFrame()
    try:
        yield
    finally:
        base.graphicsEngine.renderFrame()
        base.graphicsEngine.renderFrame()
        base.graphicsEngine.renderFrame()
        load_screen.remove_node()
Exemple #2
0
    def set_num_lives(self, num_lives):
        while num_lives > len(self.lives):
            image = OnscreenImage(self.heart_tex,
                                  pos=(0.2 + len(self.lives) * 0.1, 0, -0.18),
                                  scale=0.04,
                                  parent=base.a2dTopLeft)
            image.set_transparency(core.TransparencyAttrib.M_binary)
            self.lives.append(image)

        while num_lives < len(self.lives):
            self.lives.pop().destroy()
Exemple #3
0
class Minimap(GameObject):
    def __init__(self, bounds, track_img, handle_img, col_dct, cars,
                 player_car):
        GameObject.__init__(self)
        self.bounds = bounds
        self.minimap = OnscreenImage(track_img,
                                     pos=(-.25, 1, .25),
                                     scale=.2,
                                     parent=base.a2dBottomRight)
        self.minimap.set_transparency(True)
        self.minimap.set_alpha_scale(.64)
        self.car_handles = {}
        for car_name in sorted(cars, key=lambda car: car == player_car):
            self.__set_car(car_name, player_car, handle_img, col_dct)
        list(
            map(lambda car: car.set_transparency(True),
                self.car_handles.values()))
        self.width = self.minimap.get_scale()[0] * 2.0
        self.height = self.minimap.get_scale()[2] * 2.0
        center_x, center_y = self.minimap.get_x(), self.minimap.get_z()
        self.left_img = center_x - self.width / 2.0
        self.bottom_img = center_y - self.height / 2.0

    def __set_car(self, car_name, player_car, handle_img, col_dct):
        scale = .015 if car_name == player_car else .01
        self.car_handles[car_name] = OnscreenImage(handle_img,
                                                   pos=(-.25, 1, .25),
                                                   scale=scale,
                                                   parent=base.a2dBottomRight)
        self.car_handles[car_name].set_color_scale(col_dct[car_name])

    def update(self, car_info):
        list(map(self.__update_car, car_info))

    def __update_car(self, car_i):
        left, right, top, bottom = self.bounds
        car_name, car_pos = car_i
        pos_x_norm = (car_pos.get_x() - left) / (right - left)
        pos_y_norm = (car_pos.get_y() - bottom) / (top - bottom)
        pos_x = self.left_img + pos_x_norm * self.width
        pos_y = self.bottom_img + pos_y_norm * self.height
        self.car_handles[car_name].set_pos(pos_x, 1, pos_y)

    def destroy(self):
        des = lambda wdg: wdg.destroy()
        list(map(des, [self.minimap] + list(self.car_handles.values())))
        GameObject.destroy(self)
Exemple #4
0
class Sprite(RPObject):

    """ Simple wrapper arround OnscreenImage, providing a simpler interface """

    def __init__(self, image=None, parent=None, x=0, y=0, w=None, h=None,
                 transparent=True, near_filter=True, any_filter=True):
        """ Creates a new image, taking (x,y) as topleft coordinates.

        When near_filter is set to true, a near filter will be set to the
        texture passed. This provides sharper images.

        When any_filter is set to false, the passed image won't be modified at
        all. This enables you to display existing textures, otherwise the
        texture would get a near filter in the 3D View, too. """

        RPObject.__init__(self)

        if not isinstance(image, Texture):
            if not isinstance(image, str):
                self.warn("Invalid argument to image parameter:", image)
                return
            image = RPLoader.load_texture(image)

            if w is None or h is None:
                w, h = image.get_x_size(), image.get_y_size()
        else:
            if w is None or h is None:
                w = 10
                h = 10

        self._width, self._height = w, h
        self._initial_pos = self._translate_pos(x, y)

        self.node = OnscreenImage(
            image=image, parent=parent, pos=self._initial_pos,
            scale=(self._width / 2.0, 1, self._height / 2.0))

        if transparent:
            self.node.set_transparency(TransparencyAttrib.M_alpha)

        tex = self.node.get_texture()

        # Apply a near filter, but only if the parent has no scale, otherwise
        # it will look weird
        if near_filter and any_filter and parent.get_sx() == 1.0:
            tex.set_minfilter(SamplerState.FT_nearest)
            tex.set_magfilter(SamplerState.FT_nearest)

        if any_filter:
            tex.set_anisotropic_degree(8)
            tex.set_wrap_u(SamplerState.WM_clamp)
            tex.set_wrap_v(SamplerState.WM_clamp)

    def get_initial_pos(self):
        """ Returns the initial position of the image. This can be used for
        animations """
        return self._initial_pos

    def pos_interval(self, *args, **kwargs):
        """ Returns a pos interval, this is a wrapper around
        NodePath.posInterval """
        return self.node.posInterval(*args, **kwargs)

    def hpr_interval(self, *args, **kwargs):
        """ Returns a hpr interval, this is a wrapper around
        NodePath.hprInterval """
        return self.node.hprInterval(*args, **kwargs)

    def color_scale_interval(self, *args, **kwargs):
        """ Returns a color scale interval, this is a wrapper around
        NodePath.colorScaleInterval """
        return self.node.colorScaleInterval(*args, **kwargs)

    def set_image(self, img):
        """ Sets the current image """
        self.node.set_image(img)

    def get_width(self):
        """ Returns the width of the image in pixels """
        return self._width

    def get_height(self):
        """ Returns the height of the image in pixels """
        return self._height

    def set_pos(self, x, y):
        """ Sets the position """
        self.node.set_pos(self._translate_pos(x, y))

    def _translate_pos(self, x, y):
        """ Converts 2d coordinates to pandas coordinate system """
        return Vec3(x + self._width / 2.0, 1, -y - self._height / 2.0)

    def set_shader(self, shader):
        """ Sets a shader to be used for rendering the image """
        self.node.set_shader(shader)

    def set_shader_input(self, *args):
        """ Sets a shader input on the image """
        self.node.set_shader_input(*args)

    def remove(self):
        """ Removes the image """
        self.node.remove()

    def hide(self):
        """ Hides the image """
        self.node.hide()

    def show(self):
        """ Shows the image if it was previously hidden """
        self.node.show()
Exemple #5
0
class Sprite(RPObject):
    """ Simple wrapper arround OnscreenImage, providing a simpler interface """
    def __init__(self,
                 image=None,
                 parent=None,
                 x=0,
                 y=0,
                 w=None,
                 h=None,
                 transparent=True,
                 near_filter=True,
                 any_filter=True):
        """ Creates a new image, taking (x,y) as topleft coordinates.

        When near_filter is set to true, a near filter will be set to the
        texture passed. This provides sharper images.

        When any_filter is set to false, the passed image won't be modified at
        all. This enables you to display existing textures, otherwise the
        texture would get a near filter in the 3D View, too. """

        RPObject.__init__(self)

        if not isinstance(image, Texture):
            if not isinstance(image, str):
                self.warn("Invalid argument to image parameter:", image)
                return
            image = RPLoader.load_texture(image)

            if w is None or h is None:
                w, h = image.get_x_size(), image.get_y_size()
        else:
            if w is None or h is None:
                w = 10
                h = 10

        self._width, self._height = w, h
        self._initial_pos = self._translate_pos(x, y)

        self.node = OnscreenImage(image=image,
                                  parent=parent,
                                  pos=self._initial_pos,
                                  scale=(self._width / 2.0, 1,
                                         self._height / 2.0))

        if transparent:
            self.node.set_transparency(TransparencyAttrib.M_alpha)

        tex = self.node.get_texture()

        # Apply a near filter, but only if the parent has no scale, otherwise
        # it will look weird
        if near_filter and any_filter and parent.get_sx() == 1.0:
            tex.set_minfilter(SamplerState.FT_nearest)
            tex.set_magfilter(SamplerState.FT_nearest)

        if any_filter:
            tex.set_anisotropic_degree(8)
            tex.set_wrap_u(SamplerState.WM_clamp)
            tex.set_wrap_v(SamplerState.WM_clamp)

    def get_initial_pos(self):
        """ Returns the initial position of the image. This can be used for
        animations """
        return self._initial_pos

    def pos_interval(self, *args, **kwargs):
        """ Returns a pos interval, this is a wrapper around
        NodePath.posInterval """
        return self.node.posInterval(*args, **kwargs)

    def hpr_interval(self, *args, **kwargs):
        """ Returns a hpr interval, this is a wrapper around
        NodePath.hprInterval """
        return self.node.hprInterval(*args, **kwargs)

    def color_scale_interval(self, *args, **kwargs):
        """ Returns a color scale interval, this is a wrapper around
        NodePath.colorScaleInterval """
        return self.node.colorScaleInterval(*args, **kwargs)

    def set_image(self, img):
        """ Sets the current image """
        self.node.set_image(img)

    def get_width(self):
        """ Returns the width of the image in pixels """
        return self._width

    def get_height(self):
        """ Returns the height of the image in pixels """
        return self._height

    def set_pos(self, x, y):
        """ Sets the position """
        self.node.set_pos(self._translate_pos(x, y))

    def _translate_pos(self, x, y):
        """ Converts 2d coordinates to pandas coordinate system """
        return Vec3(x + self._width / 2.0, 1, -y - self._height / 2.0)

    def set_shader(self, shader):
        """ Sets a shader to be used for rendering the image """
        self.node.set_shader(shader)

    def set_shader_input(self, *args):
        """ Sets a shader input on the image """
        self.node.set_shader_input(*args)

    def remove(self):
        """ Removes the image """
        self.node.remove()

    def hide(self):
        """ Hides the image """
        self.node.hide()

    def show(self):
        """ Shows the image if it was previously hidden """
        self.node.show()
Exemple #6
0
class CarPlayerGui(CarGui):

    panel_cls = CarPanel

    def __init__(self, mediator, car_props, players):
        self.car_props = car_props
        self._players = players
        ncars = self.ncars
        CarGui.__init__(self, mediator)
        self.pars = CarParameters(mediator.phys, mediator.logic)
        self.panel = self.panel_cls(car_props, mediator.player_car_idx, ncars,
                                    players)
        self.ai_panel = CarAIPanel()
        way_txt_pos = (0, .1) if ncars == 1 else (0, .04)
        way_txt_scale = .1 if ncars == 1 else .06
        way_img_pos = (0, 1, .3) if ncars == 1 else (0, 1, .16)
        way_img_scale = .12 if ncars == 1 else .06
        self.way_txt = OnscreenText(
            '',
            pos=way_txt_pos,
            scale=way_txt_scale,
            fg=self.car_props.race_props.season_props.gameprops.menu_props.
            text_err_col,
            parent=self.parent,
            font=self.eng.font_mgr.load_font(
                self.car_props.race_props.season_props.font))
        self.way_img = OnscreenImage('assets/images/gui/arrow_circle.txo',
                                     scale=way_img_scale,
                                     parent=self.parent,
                                     pos=way_img_pos)
        self.way_img.set_transparency(True)
        self.way_img.hide()

    @property
    def ncars(self):
        player_car_names = [
            player.car for player in self._players
            if player.kind == Player.human
        ]
        return len(player_car_names)

    @property
    def parent(self):
        if self.ncars == 1: parent = base.a2dBottomCenter
        elif self.ncars == 2:
            if self.mediator.player_car_idx == 0:
                parent = base.a2dBottomQuarter
            else:
                parent = base.a2dBottomThirdQuarter
        elif self.ncars == 3:
            if self.mediator.player_car_idx == 0: parent = base.aspect2d
            elif self.mediator.player_car_idx == 1:
                parent = base.a2dBottomQuarter
            else:
                parent = base.a2dBottomThirdQuarter
        elif self.ncars == 4:
            if self.mediator.player_car_idx == 0:
                parent = base.a2dCenterQuarter
            elif self.mediator.player_car_idx == 1:
                parent = base.a2dCenterThirdQuarter
            elif self.mediator.player_car_idx == 2:
                parent = base.a2dBottomQuarter
            else:
                parent = base.a2dBottomThirdQuarter
        return parent

    def upd_ranking(self, ranking):
        r_i = ranking.index(self.mediator.name) + 1
        self.panel.ranking_txt.setText(str(r_i) + "'")

    def upd_ai(self):
        self.ai_panel.update()

    def apply_damage(self, reset=False):
        self.panel.apply_damage(reset)

    def show_forward(self):
        self.panel.show_forward()

    def hide_forward(self):
        self.panel.hide_forward()

    def hide(self):
        CarGui.hide(self)
        self.pars.hide()
        self.panel.hide()
        self.ai_panel.hide()

    def on_wrong_way(self, way_str):
        if way_str:
            # self.panel.glass_b.show()
            self.way_txt.setText(way_str)
            self.way_img.show()
        elif not self.mediator.logic.is_moving or \
                self.mediator.logic.fly_time > 10:
            # self.panel.glass_b.show()
            keys = self.car_props.race_props.keys.players_keys[
                self.mediator.player_car_idx]
            txt = _('press %s to respawn') % \
                self.eng.event.key2desc(keys.respawn)
            self.way_txt.setText(txt)
            self.way_img.hide()
        else:
            # self.panel.glass_b.hide()
            self.way_txt.setText('')
            self.way_img.hide()

    def destroy(self):
        list(
            map(lambda wdg: wdg.destroy(),
                [self.pars, self.panel, self.ai_panel]))
        self.way_txt.destroy()
        self.way_img.destroy()
        GuiColleague.destroy(self)
Exemple #7
0
class CarPanel(GameObject):
    def __init__(self, car_props, player_idx, ncars, players):
        GameObject.__init__(self)
        self._players = players
        self.car_props = car_props
        self.player_idx = player_idx
        self.ncars = ncars
        sprops = self.car_props.race_props.season_props
        menu_props = sprops.gameprops.menu_props
        if ncars == 1:
            parent_tr = base.a2dTopRight
        elif ncars == 2:
            if self.player_idx == 0: parent_tr = base.a2dTopCenter
            else: parent_tr = base.a2dTopRight
        elif ncars == 3:
            if self.player_idx == 0: parent_tr = base.a2dTopRight
            elif self.player_idx == 1: parent_tr = base.aspect2d
            else: parent_tr = base.a2dRightCenter
        elif ncars == 4:
            if self.player_idx == 0: parent_tr = base.a2dTopCenter
            elif self.player_idx == 1: parent_tr = base.a2dTopRight
            elif self.player_idx == 2: parent_tr = base.aspect2d
            else: parent_tr = base.a2dRightCenter
        if ncars == 1:
            parent_tl = base.a2dTopLeft
        elif ncars == 2:
            if self.player_idx == 0: parent_tl = base.a2dTopLeft
            else: parent_tl = base.a2dTopCenter
        elif ncars == 3:
            if self.player_idx == 0: parent_tl = base.a2dTopLeft
            elif self.player_idx == 1: parent_tl = base.a2dLeftCenter
            else: parent_tl = base.aspect2d
        elif ncars == 4:
            if self.player_idx == 0: parent_tl = base.a2dTopLeft
            elif self.player_idx == 1: parent_tl = base.a2dTopCenter
            elif self.player_idx == 2: parent_tl = base.a2dLeftCenter
            else: parent_tl = base.aspect2d
        if ncars == 1:
            parent_bl = base.a2dBottomLeft
        elif ncars == 2:
            if self.player_idx == 0: parent_bl = base.a2dBottomLeft
            else: parent_bl = base.a2dBottomCenter
        elif ncars == 3:
            if self.player_idx == 0: parent_bl = base.a2dLeftCenter
            elif self.player_idx == 1: parent_bl = base.a2dBottomLeft
            else: parent_bl = base.a2dBottomCenter
        elif ncars == 4:
            if self.player_idx == 0: parent_bl = base.a2dLeftCenter
            elif self.player_idx == 1: parent_bl = base.aspect2d
            elif self.player_idx == 2: parent_bl = base.a2dBottomLeft
            else: parent_bl = base.a2dBottomCenter
        # if ncars == 1: parent_t = base.a2dTopCenter
        # elif ncars == 2:
        #     if self.player_idx == 0: parent_t = base.a2dTopQuarter
        #     else: parent_t = base.a2dTopThirdQuarter
        # elif ncars == 3:
        #     if self.player_idx == 0: parent_t = base.a2dTop
        #     elif self.player_idx == 1: parent_t = base.a2dCenterQuarter
        #     else: parent_t = base.a2dCenterThirdQuarter
        # elif ncars == 4:
        #     if self.player_idx == 0: parent_t = base.a2dTopQuarter
        #     elif self.player_idx == 1: parent_t = base.a2dTopThirdQuarter
        #     elif self.player_idx == 2: parent_t = base.a2dCenterQuarter
        #     else: parent_t = base.a2dCenterThirdQuarter
        # if ncars == 1: parent_b = base.a2dBottomCenter
        # elif ncars == 2:
        #     if self.player_idx == 0: parent_b = base.a2dBottomQuarter
        #     else: parent_b = base.a2dBottomThirdQuarter
        # elif ncars == 3:
        #     if self.player_idx == 0: parent_b = base.aspect2d
        #     elif self.player_idx == 1: parent_b = base.a2dBottomQuarter
        #     else: parent_b = base.a2dBottomThirdQuarter
        # elif ncars == 4:
        #     if self.player_idx == 0: parent_b = base.a2dCenterQuarter
        #     elif self.player_idx == 1: parent_b = base.a2dCenterThirdQuarter
        #     elif self.player_idx == 2: parent_b = base.a2dBottomQuarter
        #     else: parent_b = base.a2dBottomThirdQuarter
        yellow_scale = .065 if ncars == 1 else .042
        white_scale = .05 if ncars == 1 else .038
        damages_img_scale = (.12, 1, .12) if ncars == 1 else (.08, 1, .08)
        self.__weap_scale = .12 if ncars == 1 else .08
        txt_x = -.24 if ncars == 1 else -.18
        lab_x = -.3 if ncars == 1 else -.24
        offset_z = .1 if ncars == 1 else .08
        top_z = -.1
        damages_txt_pos = (.3, .1) if ncars == 1 else (.24, .06)
        damages_img_pos = (.46, 1, .12) if ncars == 1 else (.36, 1, .07)
        weapon_txt_pos = (.18, -.08) if ncars == 1 else (.14, -.08)
        self.__weapon_img_pos = (.18, 1, -.24) if ncars == 1 else \
            (.14, 1, -.18)
        fwd_img_pos = (0, 1, -.2) if ncars == 1 else (0, 1, -.16)
        fwd_img_scale = .15 if ncars == 1 else .12
        pars = {
            'scale': yellow_scale,
            'parent': parent_tr,
            'fg': menu_props.text_active_col,
            'align': TextNode.A_left,
            'font': self.eng.font_mgr.load_font(sprops.font)
        }
        # self.glass_tl = OnscreenImage(
        #     'assets/images/gui/topleft.txo',
        #     scale=(.23, 1, .24), parent=parent_tl, pos=(.22, 1, -.23))
        # self.glass_tl.set_transparency(True)
        # self.glass_tr = OnscreenImage(
        #     'assets/images/gui/topright.txo',
        #     scale=(.36, 1, .36), parent=parent_tr, pos=(-.35, 1, -.35))
        # self.glass_tr.set_transparency(True)
        # self.glass_t = OnscreenImage(
        #     'assets/images/gui/top.txo',
        #     scale=(.24, 1, .22), parent=parent_t, pos=(0, 1, -.21))
        # self.glass_t.set_transparency(True)
        # self.glass_bl = OnscreenImage(
        #     'assets/images/gui/bottomleft.txo',
        #     scale=(.36, 1, .16), parent=parent_bl, pos=(.35, 1, .15))
        # self.glass_bl.set_transparency(True)
        # self.glass_br = OnscreenImage(
        #     'assets/images/gui/bottomright.txo', scale=(.26, 1, .26),
        #     parent=base.a2dBottomRight, pos=(-.25, 1, .25))
        # self.glass_br.set_transparency(True)
        # self.glass_b = OnscreenImage(
        #     'assets/images/gui/bottom.txo',
        #     scale=(1.02, 1, .26), parent=parent_b, pos=(0, 1, .25))
        # self.glass_b.set_transparency(True)
        # self.glass_tl.hide()
        # self.glass_t.hide()
        # self.glass_b.hide()
        self.speed_txt = OnscreenText(pos=(txt_x + .06, top_z), **pars)
        self.speed_txt['align'] = TextNode.A_center
        self.speed_c = Circle(size=.1,
                              pos=(txt_x + .06, top_z),
                              parent=parent_tr,
                              ray=.4,
                              thickness=.05,
                              col_start=(.9, .6, .1, 1),
                              col_end=(.2, .8, .2, 1))
        lap_str = '1/' + str(self.car_props.race_props.laps)
        self.lap_txt = OnscreenText(text=lap_str,
                                    pos=(txt_x, top_z - offset_z),
                                    **pars)
        self.time_txt = OnscreenText(pos=(txt_x, top_z - offset_z * 4), **pars)
        self.best_txt = OnscreenText(pos=(txt_x, top_z - offset_z * 5), **pars)
        self.ranking_txt = OnscreenText(pos=(txt_x, top_z - offset_z * 2),
                                        **pars)
        self.damages_img = OnscreenImage('assets/images/gui/car_icon.txo',
                                         scale=damages_img_scale,
                                         parent=parent_bl,
                                         pos=damages_img_pos)
        self.damages_img.set_transparency(True)
        self.damages_img.set_color_scale(menu_props.text_normal_col)
        self.damages_img.set_r(90)
        pars = {
            'scale': white_scale,
            'parent': pars['parent'],
            'fg': menu_props.text_normal_col,
            'align': TextNode.A_right,
            'font': pars['font']
        }
        self.speed_lab = OnscreenText(_('speed:'), pos=(lab_x, top_z), **pars)
        self.lap_lab = OnscreenText(text=_('lap:'),
                                    pos=(lab_x, top_z - offset_z),
                                    **pars)
        self.time_lab = OnscreenText(_('time:'),
                                     pos=(lab_x, top_z - offset_z * 4),
                                     **pars)
        self.best_lab = OnscreenText(_('best lap:'),
                                     pos=(lab_x, top_z - offset_z * 5),
                                     **pars)
        self.ranking_lab = OnscreenText(_('ranking:'),
                                        pos=(lab_x, top_z - offset_z * 2),
                                        **pars)
        self.damages_lab = OnscreenText(_('damages:'),
                                        pos=damages_txt_pos,
                                        **pars)
        self.damages_lab.reparent_to(parent_bl)
        self.weapon_lab = OnscreenText(_('weapon'),
                                       pos=weapon_txt_pos,
                                       scale=white_scale,
                                       parent=parent_tl,
                                       fg=menu_props.text_normal_col,
                                       font=self.eng.font_mgr.load_font(
                                           sprops.font))
        self.weapon_img = None
        if ncars == 1: parent = base.a2dTopCenter
        elif ncars == 2:
            if player_idx == 0: parent = base.a2dTopQuarter
            else: parent = base.a2dTopThirdQuarter
        elif ncars == 3:
            if player_idx == 0: parent = base.a2dTopCenter
            elif player_idx == 0: parent = base.a2dCenterQuarter
            else: parent = base.a2dCenterThirdQuarter
        elif ncars == 4:
            if player_idx == 0: parent = base.a2dTopQuarter
            elif player_idx == 1: parent = base.a2dTopThirdQuarter
            elif player_idx == 2: parent = base.a2dCenterQuarter
            else: parent = base.a2dCenterThirdQuarter
        self.forward_img = OnscreenImage('assets/images/gui/direction.txo',
                                         scale=fwd_img_scale,
                                         parent=parent,
                                         pos=fwd_img_pos)
        self.forward_img.set_transparency(True)
        self.forward_img.hide()

    @staticmethod
    def __close_vec(vec1, vec2):
        return all(abs(b - a) < .01 for a, b in zip(vec1, vec2))

    def enter_waiting(self):
        pass
        # if self.ncars == 1: parent = base.aspect2d
        # elif self.ncars == 2:
        #     if self.player_idx == 0: parent = base.a2dCenterQuarter
        #     else: parent = base.a2dCenterThirdQuarter
        # elif self.ncars == 3:
        #     if self.player_idx == 0: parent = base.a2dQuarterCenter
        #     elif self.player_idx == 1: parent = base.a2dThirdQuarterQuarter
        #     else: parent = base.a2dThirdQuarterThirdQuarter
        # elif self.ncars == 4:
        #     if self.player_idx == 0: parent = base.a2dQuarterQuarter
        #     elif self.player_idx == 1: parent = base.a2dQuarterThirdQuarter
        #     elif self.player_idx == 2: parent = base.a2dThirdQuarterQuarter
        #     else: parent = base.a2dThirdQuarterThirdQuarter
        # menu_props = self.race_props.season_props.gameprops.menu_props
        # pars = {'scale': .065, 'parent': parent,
        #         'fg': menu_props.text_normal_col,
        #         'font': self.eng.font_mgr.load_font(
        #             self.race_props.season_props.font)}

    def exit_waiting(self):
        pass

    def set_weapon(self, wpn):
        # self.glass_tl.show()
        self.weapon_lab.show()
        ncars = len([
            player for player in self._players if player.kind == Player.human
        ])
        if ncars == 1:
            parent_tl = base.a2dTopLeft
        elif ncars == 2:
            if self.player_idx == 0: parent_tl = base.a2dTopLeft
            else: parent_tl = base.a2dTopCenter
        elif ncars == 3:
            if self.player_idx == 0: parent_tl = base.a2dTopLeft
            elif self.player_idx == 1: parent_tl = base.a2dLeftCenter
            else: parent_tl = base.aspect2d
        elif ncars == 4:
            if self.player_idx == 0: parent_tl = base.a2dTopLeft
            elif self.player_idx == 1: parent_tl = base.a2dTopCenter
            elif self.player_idx == 2: parent_tl = base.a2dLeftCenter
            else: parent_tl = base.aspect2d
        self.weapon_img = OnscreenImage('assets/images/weapons/%s.txo' % wpn,
                                        scale=self.__weap_scale,
                                        parent=parent_tl,
                                        pos=self.__weapon_img_pos)
        self.weapon_img.set_transparency(True)

    def unset_weapon(self):
        # self.glass_tl.hide()
        self.weapon_lab.hide()
        self.weapon_img.destroy()

    def show_forward(self):
        # self.glass_t.show()
        self.forward_img.show()

    def set_forward_angle(self, angle):
        curr_angle = self.forward_img.get_r()
        curr_incr = globalClock.getDt() * 30
        if abs(curr_angle - angle) < curr_incr:
            tgt_val = angle
        else:
            sign = 1 if angle > curr_angle else -1
            tgt_val = curr_angle + curr_incr * sign
        self.forward_img.set_r(tgt_val)

    def hide_forward(self):
        # self.glass_t.hide()
        self.forward_img.hide()

    def apply_damage(self, reset=False):
        col = self.car_props.race_props.season_props.gameprops.menu_props.text_normal_col
        if reset:
            self.damages_img.set_color_scale(col)
        else:
            yellow = (col[0], col[1] - .25, col[2] - .5, col[3])
            if self.__close_vec(self.damages_img.get_color_scale(), col):
                self.damages_img.set_color_scale(yellow)
            elif self.__close_vec(self.damages_img.get_color_scale(), yellow):
                red = (col[0], col[1] - .5, col[2] - .5, col[3])
                self.damages_img.set_color_scale(red)

    def hide(self):
        labels = [
            self.speed_txt,
            self.speed_c,
            self.time_txt,
            self.lap_txt,
            self.best_txt,
            self.speed_lab,
            self.time_lab,
            self.lap_lab,
            self.best_lab,
            self.damages_img,
            self.damages_lab,
            self.ranking_txt,
            self.ranking_lab,
            self.weapon_lab,
            # self.glass_tl, self.glass_tr, self.glass_t,
            # self.glass_bl, self.glass_br, self.glass_b
        ]
        list(map(lambda wdg: wdg.hide(), labels))
        if self.weapon_img and not self.weapon_img.is_empty():
            self.weapon_img.hide()
        self.forward_img.hide()

    def destroy(self):
        labels = [
            self.speed_txt,
            self.speed_c,
            self.time_txt,
            self.lap_txt,
            self.best_txt,
            self.speed_lab,
            self.time_lab,
            self.lap_lab,
            self.best_lab,
            self.damages_img,
            self.damages_lab,
            self.ranking_txt,
            self.ranking_lab,
            self.weapon_lab,
            # self.glass_tl, self.glass_tr, self.glass_t,
            # self.glass_bl, self.glass_br, self.glass_b
        ]
        list(map(lambda wdg: wdg.destroy(), labels))
        if self.weapon_img and not self.weapon_img.is_empty():
            self.weapon_img.destroy()
        self.forward_img.destroy()
Exemple #8
0
class Game(DirectObject):
    def __init__(self):
        self.background = OnscreenImage(image='image/title_screen.png',
                                        scale=1.0,
                                        pos=(0, 0, 0),
                                        parent=render2d)

        self.foreground = OnscreenImage(image='image/we_are_number_one.png',
                                        scale=(256, 1, 256),
                                        pos=(640, 0, -360),
                                        parent=pixel2d)
        self.foreground.wrt_reparent_to(aspect2d)
        self.foreground.set_transparency(TransparencyAttrib.M_alpha)
        self.foreground.set_bin("fixed", 10)
        self.foreground.hide()

        font = loader.load_font('font/oh_whale.otf')
        font.set_outline(outline_color=(0, 0, 0, 1),
                         outline_width=1.0,
                         outline_feather=0.5)
        font.setPixelsPerUnit(96)

        font64 = font.make_copy()
        font64.setPixelsPerUnit(64)

        self.text_node = TextNode('silly_text_node')
        self.text_node.set_font(font)
        self.text_node.set_text('Press any key to start!')
        self.text_node.set_align(TextNode.A_center)
        self.text_node.set_text_color(0.4, 0.5, 1, 1)
        self.text_node.set_shadow(0.03, 0.03)
        self.text_node.set_shadow_color(0, 0, 0, 0.5)
        self.text_path = aspect2d.attach_new_node(self.text_node)
        self.text_path.set_scale(0.15)
        self.text_path.set_z(-0.9)
        self.text_path.set_transparency(TransparencyAttrib.M_alpha)

        self.score_text_node = TextNode('score_text_node')
        self.score_text_node.set_font(font)
        self.score_text_node.set_text('Score: 0')
        self.score_text_node.set_align(TextNode.A_left)
        self.score_text_node.set_text_color(1.0, 1.0, 1.0, 1)
        self.score_text_node.set_shadow(0.03, 0.03)
        self.score_text_node.set_shadow_color(0, 0, 0, 0.5)
        self.score_text_node_path = base.a2dTopLeft.attach_new_node(
            self.score_text_node)
        self.score_text_node_path.set_scale(0.15)
        self.score_text_node_path.set_pos(0, 0, -0.13)
        self.score_text_node_path.set_transparency(TransparencyAttrib.M_alpha)

        self.button = DirectFrame(frameColor=(1.0, 1.0, 1.0, 1.0),
                                  frameTexture='image/button.png',
                                  frameSize=(0, 128, -128, 0),
                                  text='1',
                                  text_font=font64,
                                  text_scale=64,
                                  text_fg=Vec4(1.0),
                                  text_align=TextNode.A_center,
                                  text_pos=(64, -79),
                                  textMayChange=1,
                                  pos=(-64, 0, 64),
                                  state=DGG.NORMAL,
                                  suppressMouse=True,
                                  parent=pixel2d)
        self.button.flatten_light()
        self.button.bind(DGG.B1PRESS, self.on_button_click)
        self.button.wrt_reparent_to(aspect2d)
        self.button.set_transparency(TransparencyAttrib.M_alpha)
        self.button.set_pos(0, 0, 0)
        self.button.set_bin("fixed", 11)
        self.button.hide()
        self.last_number = None
        self.points = []

        self.fake_button = DirectFrame(frameColor=(1.0, 1.0, 1.0, 1.0),
                                       frameTexture='image/button.png',
                                       frameSize=(0, 128, -128, 0),
                                       text='1',
                                       text_font=font64,
                                       text_scale=64,
                                       text_fg=Vec4(1.0),
                                       text_align=TextNode.A_center,
                                       text_pos=(64, -79),
                                       textMayChange=1,
                                       pos=(-64, 0, 64),
                                       parent=pixel2d)
        self.fake_button.flatten_light()
        self.fake_button.wrt_reparent_to(aspect2d)
        self.fake_button.set_transparency(TransparencyAttrib.M_alpha)
        self.fake_button.set_pos(0, 0, 0)
        self.button.set_bin("fixed", 11)
        self.fake_button.hide()

        base.buttonThrowers[0].node().setButtonDownEvent('buttonDown')
        self.accept('buttonDown', self.start)

        self.music = loader.load_music('music/the_silly_number_song.ogg')
        self.beat_count = 0
        self.beat_tsk = None

        self.images_seq = make_image_sequence(self.foreground)
        self.buttons_seq = make_button_sequence(self.button, self.fake_button)
        self.stubtitles_seq = make_song_sequence(self.text_node, self.music)
        self.stubtitles_seq.append(Wait(7.0))
        self.stubtitles_seq.append(Func(self.reset))

    def reset(self):
        taskMgr.remove(self.beat_tsk)
        self.text_node.set_text('Press any key to start!')
        self.foreground.hide()
        self.background.setImage('image/title_screen.png')
        self.fake_button.hide()
        self.button.hide()
        self.points = []
        self.beat_count = 0
        self.last_number = None

        base.buttonThrowers[0].node().setButtonDownEvent('buttonDown')
        self.accept('buttonDown', self.start)

    def hide_button(self, task):
        if self.last_number == self.button['text']:
            self.button.hide()
        return task.done

    def reset_button(self, task):
        self.button['frameTexture'] = 'image/button.png'
        return task.done

    def on_button_click(self, event=None):
        self.button['frameTexture'] = 'image/button_down.png'
        self.last_number = self.button['text']
        if self.last_number not in self.points:
            self.points.append(self.last_number)
            self.fake_button.hide()
            #update displayed score:
            self.score_text_node.set_text('Score: {0}'.format(len(
                self.points)))
        taskMgr.doMethodLater(0.1, self.reset_button, 'reset_tsk')
        taskMgr.doMethodLater(0.2, self.hide_button, 'hide_tsk')

    def beat(self, task):
        self.beat_count += 1
        if self.beat_count % 2 == 0:
            self.background.setImage(
                loader.load_texture('image/background2.png'))
        else:
            self.background.setImage(
                loader.load_texture('image/background.png'))
        return task.again

    def start(self, event=None):
        self.score_text_node.set_text('Score: 0')
        self.ignore('buttonDown')
        self.beat_tsk = taskMgr.doMethodLater(0.24, self.beat, 'beat_tsk')
        self.buttons_seq.start()
        self.stubtitles_seq.start()
        self.images_seq.start()