Beispiel #1
0
    def __init__(self, piece, width, height):
        super(PieceLayer, self).__init__()
        
        self._width = width
        self._height = height
        self._piece = piece
        #logging.debug('New piece layer %d x %d, image name %s' % (width, height, piece.imageName()))

        # Pieces with the 'color' property get a background.
        if hasattr(piece, 'color'):
            #logging.debug('color ' + str(piece.color))
            c = colors[piece.color]
            bg = ColorLayer(c[0], c[1], c[2], 192, width=width, height=height)
            self.add(bg)
            self._background = bg

        pieceSprite = Sprite(piece.imageName())
        pieceSprite.image_anchor_x = 0
        pieceSprite.image_anchor_y = 0

        # Scale the sprite to the correct size.
        rect = pieceSprite.get_rect()
        scale = min(float(width) / rect.width,
                    float(height) / rect.height)
        pieceSprite.scale = scale
        self._pieceSprite = pieceSprite

        self.add(pieceSprite)
        self._opacity = 255

        self._turnIndicator = None
        self._updateTurnIndicator()
        self._chargeIndicator = None
        self._updateChargeIndicator()
Beispiel #2
0
    def __init__(self, battle):
        super(Board, self).__init__()

        Board.BOARD = self
        self.scroller = None

        self.battle = battle
        self.map = battle.map
        self.cellMap = {}

        # add basic ground cell tiles
        ground_img = Resources.ground_img
        node = BatchNode()
        self.add(node, z=0)

        for row in range(self.map.numRows):
            for col in range(self.map.numCols):
                tile = self.map.getTileAt(col, row)
                cell = Cell(tile, ground_img)
                rect = cell.get_rect()
                rect.bottomleft = col * 32, row * 32
                cell.position = rect.center
                node.add(cell, z=0)

                self.cellMap[(col, row)] = cell

        # add terrain feature/building cell tiles
        for col, row in self.map.boardMap:
            loc = (col, row)
            cell_data = self.map.boardMap[loc]

            cell_images = cell_data.images

            if cell_images is not None:
                cell_level = cell_data.level
                cell_z = (self.map.numCols - row - cell_level) * 10

                cell_batch = BatchNode()
                cell_batch.position = col * self.TILE_SIZE, row * self.TILE_SIZE
                self.add(cell_batch, z=cell_z)

                cell_cols = cell_data.cols
                cell_rows = cell_data.rows
                for this_row in range(cell_rows + cell_level):
                    for this_col in range(cell_cols):
                        cell_index = this_col + (this_row * cell_cols)
                        cell_sprite = Sprite(cell_images[cell_index])

                        cell_rect = cell_sprite.get_rect()
                        cell_rect.bottomleft = this_col * self.TILE_SIZE, this_row * self.TILE_SIZE
                        cell_sprite.position = cell_rect.center

                        cell_batch.add(cell_sprite)
Beispiel #3
0
 def update_blocks(self):
     '''更新砖块'''
     b = Sprite('images/block.png', anchor=(0, 0))
     b.position = (self.mouse_x, self.mouse_y)
     r = b.get_rect()
     for x, y in self.postmp:
         bx = x + b.width
         by = y + b.height
         if r.contains(bx, by):
             if (x, y) not in self.pos:
                 self.press_left(x, y, b)
             else:
                 self.press_right(x, y, b)
             break
class BackgroundLayer(Layer):
    """
    Layer that holds the absolute bottom background layer
    """
    def __init__(self):
        """
        Create a BackgroundLayer
        """
        super().__init__()
        self.sprite = Sprite('/'.join(
            ["assets", "textures", "environment", "background.png"]))
        self.sprite.position = 0, 0
        self.add(self.sprite)
        width = config.screen_size[0]
        height = config.screen_size[1]
        rect = self.sprite.get_rect()
        self.sprite.image_anchor = rect.center
        self.sprite.scale_x = width / rect.width
        self.sprite.scale_y = height / rect.height
Beispiel #5
0
    def __init__(self, battle_mech):
        super(MechSprite, self).__init__()

        self.battle_mech = battle_mech

        mech_img = pyglet.resource.image(self.battle_mech.getImagePath())
        mech_img_grid = pyglet.image.ImageGrid(mech_img, 1, 6)

        self.static = True

        img_static = Sprite(mech_img_grid[0])

        self.width = img_static.width
        self.height = img_static.height

        # TODO: setup the non square friendly/enemy indicators based on team of current player's turn
        if battle_mech.player.is_bot:
            indicator = Sprite(Resources.enemy_indicator_img)
        else:
            indicator = Sprite(Resources.friendly_indicator_img)

        indicator.visible = False
        indicator.position = 0, -img_static.height // 2 + indicator.height // 2 + 1
        self.indicator = indicator
        self.add(indicator, z=0)

        shadow = Sprite(MechSprite.shadow_img_grid[battle_mech.getSize() - 1])
        shadow_rect = shadow.get_rect()
        shadow_rect.bottomleft = (self.battle_mech.col * Board.TILE_SIZE), \
                                 (self.battle_mech.row * Board.TILE_SIZE)
        shadow.position = shadow_rect.center
        self.shadow = shadow

        rect = img_static.get_rect()
        rect.bottomleft = (self.battle_mech.col * Board.TILE_SIZE) - (img_static.width//2 - shadow.width//2), \
                          (self.battle_mech.row * Board.TILE_SIZE)
        self.position = rect.center

        self.node = BatchNode()
        self.add(self.node, z=2)

        img_static.y = Board.TILE_SIZE//4
        self.node.add(img_static)
        self.img_static = img_static

        img_ct = Sprite(mech_img_grid[1])
        img_ct.y = Board.TILE_SIZE//4
        self.img_ct = img_ct

        img_ll = Sprite(mech_img_grid[4])
        img_ll.y = Board.TILE_SIZE//4
        self.img_ll = img_ll

        img_rl = Sprite(mech_img_grid[5])
        img_rl.y = Board.TILE_SIZE//4
        self.img_rl = img_rl

        img_la = Sprite(mech_img_grid[2])
        img_la.y = Board.TILE_SIZE//4
        self.img_la = img_la

        img_ra = Sprite(mech_img_grid[3])
        img_ra.y = Board.TILE_SIZE//4
        self.img_ra = img_ra

        # testing the stats stuff
        self.stats = BatchNode()
        self.updateStatsIndicators()
Beispiel #6
0
class TestLayer(cocos.layer.Layer):

    is_event_handler = True

    def __init__(self):

        super( TestLayer, self ).__init__()

        self.mouse_x = self.mouse_y = 0

        x,y = director.get_window_size()

        self.sprite1 = Sprite('grossini.png', anchor=(0, 0))
        self.sprite_rect = None 
        self.add(self.sprite1, z=2)
        self.sprite1.position = x // 3, y // 2
        self.show_rect()
        self.do(
            Delay(2) + CallFunc(self.mov) +
            Delay(2) + CallFunc(self.zoom) +
            Delay(2) + CallFunc(self.scalex) +
            Delay(2) + CallFunc(self.rot)
            )
        self.mouse_mark = cocos.layer.ColorLayer(0, 0, 255, 255, 20, 20)
        self.add(self.mouse_mark, z=3)

    def show_rect(self):
        if self.sprite_rect:
            self.remove(self.sprite_rect)
        rect = self.sprite1.get_rect()
        self.sprite_rect = ColorLayer(255, 255, 255, 255,
                                      width=rect.width, height=rect.height)
        self.sprite_rect.position = rect.position
        self.add(self.sprite_rect, z=1)

    def mov(self):
        x,y = director.get_window_size()
        self.sprite1.position = (x * 2 // 3, y//2)
        self.show_rect()

    def rot(self):
        self.sprite1.rotation = 90
        self.show_rect()

    def zoom(self):
        self.sprite1.scale = 2
        self.show_rect()

    def scalex(self):
        self.sprite1.scale_x = 0.5
        self.show_rect()

    def on_mouse_press(self, x, y, buttons, modifiers):
        self.update_mouse_mark(x, y)

    def on_mouse_drag( self, x, y, dx, dy, buttons, modifiers ):
        self.update_mouse_mark(x, y)

    def update_mouse_mark(self, x, y):
        x, y = director.get_virtual_coordinates (x, y)
        self.mouse_mark.position = (x-5, y-5)
        rect = self.sprite1.get_rect()
        # mouse hit ?
        if rect.contains(x, y):
            self.mouse_mark.color = (255, 0, 0)
        else:
            self.mouse_mark.color = (0, 0, 255)
Beispiel #7
0
class HudLayer(Layer, pyglet.event.EventDispatcher):
    is_event_handler = True

    def __init__(self, state):
        super(HudLayer, self).__init__()

        self.state = state

        self.resourceLabel = Label("RESOURCES %d/2000" %
                                   self.state.player1.resources,
                                   font_name="DejaVu Sans",
                                   font_size=32,
                                   anchor_x='center',
                                   anchor_y='center')

        self.resourceLabel.position = 1280 / 2, 720 - 20
        self.add(self.resourceLabel)

        left = 1280 / 2 - 128 / 2 - 20 - 128 / 2
        self.tankButton = Sprite(Constants.Paths.Buttons.tank)
        self.tankButton.position = left, 80
        self.add(self.tankButton)

        left += self.tankButton.width + 20

        self.missileButton = Sprite(Constants.Paths.Buttons.missile)
        self.missileButton.position = left, 80
        self.add(self.missileButton)

        left += self.missileButton.width + 20

        self.nukeButton = Sprite(Constants.Paths.Buttons.nuke)
        self.nukeButton.position = left, 80
        self.add(self.nukeButton)

        self.schedule_interval(self.constitutiveCitizenReward, 0.2)

    def constitutiveCitizenReward(self, dt):
        self.state.player1.resources += 10
        self.state.player2.resources += 10
        if (self.state.player1.resources > 2000):
            self.state.player1.resources = 2000
        if (self.state.player2.resources > 2000):
            self.state.player2.resources = 2000
        self.setResourceLabel(self.state.player1.resources)

    def setResourceLabel(self, resources):
        self.resourceLabel.element.text = "RESOURCES %d/2000" % self.state.player1.resources

    def on_mouse_motion(self, x, y, dx, dy):
        if self.tankButton.get_rect().contains(x, y):
            self.tankButton.scale = 1.1
        else:
            self.tankButton.scale = 1.0

        if self.missileButton.get_rect().contains(x, y):
            self.missileButton.scale = 1.1
        else:
            self.missileButton.scale = 1.0

        if self.nukeButton.get_rect().contains(x, y):
            self.nukeButton.scale = 1.1
        else:
            self.nukeButton.scale = 1.0

    def on_mouse_press(self, x, y, buttons, modifiers):
        if self.tankButton.get_rect().contains(x, y):
            if self.state.player1.resources >= 200:
                self.state.player1.resources -= 200
                self.setResourceLabel(self.state.player1.resources)
                self.dispatch_event('on_tank_purchase', self)
Beispiel #8
0
    def __init__(self):
        super(Board, self).__init__()

        Board.BOARD = self

        self.boardMap = {}
        self.cellMap = {}

        # add basic ground
        ground_img = Resources.ground_img
        node = BatchNode()
        self.add(node, z=0)

        for row in range(self.numRows):
            for col in range(self.numCols):
                cell = Cell(ground_img)
                rect = cell.get_rect()
                rect.bottomleft = col * 32, row * 32
                cell.position = rect.center
                node.add(cell, z=0)

                self.cellMap[(col, row)] = cell

        # add buildings
        buildings_tex = Resources.buildings_tex

        # test 3x2 Building at 1,4
        self.boardMap[(1, 4)] = {self.KEY_TYPE: self.TYPE_BUILDING,
                                 self.KEY_COLUMNS: 3,
                                 self.KEY_ROWS: 2,
                                 self.KEY_LEVEL: 1,
                                 self.KEY_ELEVATION: 0,
                                 self.KEY_IMAGES: buildings_tex[(0, 0):(3, 3)]}
        self.boardMap[(2, 4)] = {self.KEY_REF: (1, 4)}
        self.boardMap[(3, 4)] = {self.KEY_REF: (1, 4)}
        self.boardMap[(1, 5)] = {self.KEY_REF: (1, 4)}
        self.boardMap[(2, 5)] = {self.KEY_REF: (1, 4)}
        self.boardMap[(3, 5)] = {self.KEY_REF: (1, 4)}

        # test 2x2 Building at 5,4
        self.boardMap[(5, 4)] = {self.KEY_TYPE: self.TYPE_BUILDING,
                                 self.KEY_COLUMNS: 2,
                                 self.KEY_ROWS: 2,
                                 self.KEY_LEVEL: 1,
                                 self.KEY_ELEVATION: 0,
                                 self.KEY_IMAGES: buildings_tex[(3, 0):(7, 2)]}
        self.boardMap[(6, 4)] = {self.KEY_REF: (5, 4)}
        self.boardMap[(5, 5)] = {self.KEY_REF: (5, 4)}
        self.boardMap[(6, 5)] = {self.KEY_REF: (5, 4)}

        # test 2x2 Building at 10,6
        self.boardMap[(10, 6)] = {self.KEY_TYPE: self.TYPE_BUILDING,
                                  self.KEY_COLUMNS: 2,
                                  self.KEY_ROWS: 2,
                                  self.KEY_LEVEL: 1,
                                  self.KEY_ELEVATION: 0,
                                  self.KEY_IMAGES: buildings_tex[(3, 2):(7, 4)]}
        self.boardMap[(11, 6)] = {self.KEY_REF: (10, 6)}
        self.boardMap[(10, 7)] = {self.KEY_REF: (10, 6)}
        self.boardMap[(11, 7)] = {self.KEY_REF: (10, 6)}

        # test 3x2 Building at 10,2
        self.boardMap[(10, 2)] = {self.KEY_TYPE: self.TYPE_BUILDING,
                                  self.KEY_COLUMNS: 3,
                                  self.KEY_ROWS: 2,
                                  self.KEY_LEVEL: 1,
                                  self.KEY_ELEVATION: 0,
                                  self.KEY_IMAGES: buildings_tex[(3, 5):(7, 8)]}
        self.boardMap[(11, 2)] = {self.KEY_REF: (10, 2)}
        self.boardMap[(12, 2)] = {self.KEY_REF: (10, 2)}
        self.boardMap[(10, 3)] = {self.KEY_REF: (10, 2)}
        self.boardMap[(11, 3)] = {self.KEY_REF: (10, 2)}
        self.boardMap[(12, 3)] = {self.KEY_REF: (10, 2)}

        # test 2x2x2 Building at 12,12
        self.boardMap[(12, 12)] = {self.KEY_TYPE: self.TYPE_BUILDING,
                                   self.KEY_COLUMNS: 2,
                                   self.KEY_ROWS: 2,
                                   self.KEY_LEVEL: 2,
                                   self.KEY_ELEVATION: 0,
                                   self.KEY_IMAGES: buildings_tex[(3, 10):(8, 12)]}
        self.boardMap[(13, 12)] = {self.KEY_REF: (12, 12)}
        self.boardMap[(12, 13)] = {self.KEY_REF: (12, 12)}
        self.boardMap[(13, 13)] = {self.KEY_REF: (12, 12)}

        # test 3x3 Building at 15,10
        self.boardMap[(15, 10)] = {self.KEY_TYPE: self.TYPE_BUILDING,
                                   self.KEY_COLUMNS: 3,
                                   self.KEY_ROWS: 3,
                                   self.KEY_LEVEL: 1,
                                   self.KEY_ELEVATION: 0,
                                   self.KEY_IMAGES: buildings_tex[(7, 10):(13, 13)]}
        self.boardMap[(16, 10)] = {self.KEY_REF: (15, 10)}
        self.boardMap[(17, 10)] = {self.KEY_REF: (15, 10)}
        self.boardMap[(15, 11)] = {self.KEY_REF: (15, 10)}
        self.boardMap[(16, 11)] = {self.KEY_REF: (15, 10)}
        self.boardMap[(17, 11)] = {self.KEY_REF: (15, 10)}
        self.boardMap[(15, 12)] = {self.KEY_REF: (15, 10)}
        self.boardMap[(16, 12)] = {self.KEY_REF: (15, 10)}
        self.boardMap[(17, 12)] = {self.KEY_REF: (15, 10)}

        # test 5x2 Building at 3,14
        self.boardMap[(3, 14)] = {self.KEY_TYPE: self.TYPE_BUILDING,
                                  self.KEY_COLUMNS: 5,
                                  self.KEY_ROWS: 2,
                                  self.KEY_LEVEL: 0,
                                  self.KEY_ELEVATION: 0,
                                  self.KEY_IMAGES: buildings_tex[(9, 0):(11, 5)]}
        self.boardMap[(4, 14)] = {self.KEY_REF: (3, 14)}
        self.boardMap[(5, 14)] = {self.KEY_REF: (3, 14)}
        self.boardMap[(6, 14)] = {self.KEY_REF: (3, 14)}
        self.boardMap[(7, 14)] = {self.KEY_REF: (3, 14)}
        self.boardMap[(3, 15)] = {self.KEY_REF: (3, 14)}
        self.boardMap[(4, 15)] = {self.KEY_REF: (3, 14)}
        self.boardMap[(5, 15)] = {self.KEY_REF: (3, 14)}
        self.boardMap[(6, 15)] = {self.KEY_REF: (3, 14)}
        self.boardMap[(7, 15)] = {self.KEY_REF: (3, 14)}

        for col, row in self.boardMap:
            loc = (col, row)
            cell_data = self.boardMap[loc]

            cell_images = cell_data.get(self.KEY_IMAGES)

            if cell_images is not None:
                cell_level = cell_data[self.KEY_LEVEL]
                cell_z = (self.numCols - row - cell_level) * 10

                cell_batch = BatchNode()
                cell_batch.position = col * self.TILE_SIZE, row * self.TILE_SIZE
                self.add(cell_batch, z=cell_z)

                cell_cols = cell_data[self.KEY_COLUMNS]
                cell_rows = cell_data[self.KEY_ROWS]
                for this_row in range(cell_rows + cell_level):
                    for this_col in range(cell_cols):
                        cell_index = this_col + (this_row * cell_cols)
                        cell_sprite = Sprite(cell_images[cell_index])

                        cell_rect = cell_sprite.get_rect()
                        cell_rect.bottomleft = this_col * self.TILE_SIZE, this_row * self.TILE_SIZE
                        cell_sprite.position = cell_rect.center

                        cell_batch.add(cell_sprite)
Beispiel #9
0
class Game(cocos.layer.ScrollableLayer):
    is_event_handler = True

    def __init__(self, name):
        super(Game, self).__init__()

        self.name = name

        self.keys_pressed = set()

        self.projectiles = list()

        self.background = Sprite(
            image=get_background_path()
        )
        # à cacher
        global BACKGROUND_RECT
        BACKGROUND_RECT = self.background.get_rect()
        offset = BACKGROUND_RECT.width/2, BACKGROUND_RECT.height/2
        load_wall_array()

        global SPAWN
        self.cursor = Cursor(SPAWN)
        ##

        self.background.position = offset

        spawn_delayed([self], [self.cursor])
        self.add(self.background)

        self.adstop = AdStop(self.cursor)
        self.add(self.adstop)

        self.bullettime = BulletTime(self.cursor)
        self.add(self.bullettime)

        self.add(self.cursor)


        # adding stuff to the collision world
        global COL_MGR
        COL_MGR = cocos.collision_model.CollisionManagerGrid(
            BACKGROUND_RECT.x + BACKGROUND_RECT.width/2,
            BACKGROUND_RECT.x + 3*BACKGROUND_RECT.width/2,
            BACKGROUND_RECT.y + BACKGROUND_RECT.height/2,
            BACKGROUND_RECT.y + 3*BACKGROUND_RECT.height / 2,
            16,
            16
        )

        # self.debug()
        self.schedule(self.update)
    # used to draw collisions boxes etc
    def debug(self):
        global CURRENT_WALL_ARRAY
        for rect in CURRENT_WALL_ARRAY:
            draw_rect(rect.rect, self)

        draw_rect(self.cursor.get_rect(), self)
    # holds logic
    def update(self, delta):
        if self.cursor.bullettime:
            delta *= 0.5
        # reacting to keypresses
        for k in self.keys_pressed:
            if k == key.LEFT:
                self.cursor.do(RotateBy(-self.cursor.angular_speed * delta, 0))
            if k == key.RIGHT:
                self.cursor.do(RotateBy(self.cursor.angular_speed * delta, 0))
            if k == key.SPACE:
                self.adstop.activate()
            if k == key.B:
                self.bullettime.activate()

        self.cursor.update(delta)

        self.adstop.act(delta)
        self.bullettime.act(delta)

        global COL_MGR
        global CURRENT_WALL_ARRAY
        global DELAYED_ARRAY

        COL_MGR.clear()  # fast, no leaks even if changed cshapes
        COL_MGR.add(self.cursor)  # it's the way internet says it has to be done
        for delayed in DELAYED_ARRAY:
            delayed.update(delta)
            if type(delayed) == Turret:
                for proj in delayed.projectiles:
                    COL_MGR.add(proj)
        for wall in CURRENT_WALL_ARRAY:
            COL_MGR.add(wall)

        for p in self.projectiles:
            p.update(delta)
            COL_MGR.add(p)

            for other in COL_MGR.iter_colliding(p):
                if type(other) == InvisibleWall:
                    self.remove(p)
                    self.projectiles.remove(p)

        for other in COL_MGR.iter_colliding(self.cursor):
            if type(other) == Projectile:
                if self.cursor.shielded:
                    self.adstop.reset()
                    other.remove()
                    continue
                else:
                    print("you lost")
                    run_level(self.name)
                    break
            split = other.name.split(":")
            if split[0] == "win":
                print("you win")
                if split[1] == "end":
                    print("Java c'est plus meilleur.")
                    sys.exit(0)
                else:
                    run_level(split[1])
                    break
            elif split[0] == "lose":
                print("you lost")
                run_level(self.name)
                break

        global THE_ELDER_SCROLLS_MANAGER
        THE_ELDER_SCROLLS_MANAGER.set_focus(self.cursor.position[0], self.cursor.position[1])

    def on_key_press(self, key, modifiers):
        self.keys_pressed.add(key)

    def on_key_release(self, key, modifiers):
        try:
            self.keys_pressed.remove(key)
        except:
            print('tried to remove a key during level change !')
Beispiel #10
0
    def updatePlayerUnitStats(self, battle_unit):
        if self.unit_display is not None:
            self.unit_display.kill()
            self.unit_name.kill()
            self.unit_variant.kill()
            self.unit_stats.kill()
            self.unit_values.kill()

        if battle_unit is None:
            # Hide player unit stats at bottom left
            self.unit_display = None
            self.unit_name = None
            self.unit_variant = None
            self.unit_stats = None
            self.unit_values = None
            return

        size = director.get_window_size()
        width = size[0]
        height = size[1]

        self.unit_display = BatchNode()
        self.unit_display.position = 0, 0

        mech_img_grid = pyglet.image.ImageGrid(
            pyglet.resource.image(battle_unit.getImagePath()), 1, 6)
        mech_img_static = mech_img_grid[0]
        pitch = -(mech_img_static.width * len('RGBA'))
        img_data = mech_img_static.get_image_data()

        # testing with masking only a portion of the image
        damage_height = int(
            mech_img_static.height)  # int(mech_img_static.height * 0.67)
        data = img_data.get_region(0, 0, mech_img_static.width,
                                   damage_height).get_data('RGBA', pitch)

        mask = Image.frombytes('RGBA', (mech_img_static.width, damage_height),
                               data)
        # the first image is the color that the stamp will be
        img1 = Image.new('RGBA', mask.size, color=(0, 0, 0, 255))
        # second image is the background
        img2 = Image.new('RGBA', mask.size, color=(225, 225, 225, 200))
        img1 = img1.convert('RGBA')

        # apply mask to background image
        img = Image.composite(img1, img2, mask)

        raw_image = img.tobytes()
        img_x = mask.size[0]
        img_y = mask.size[1]
        pyg_img = pyglet.image.ImageData(img_x,
                                         img_y,
                                         'RGBA',
                                         raw_image,
                                         pitch=-img_x * len('RGBA'))

        mech_sprite = Sprite(pyg_img)
        mech_sprite.position = Board.TILE_SIZE // 2 + mech_sprite.width // 2, \
                               Board.TILE_SIZE + mech_sprite.height // 2

        self.unit_display.add(mech_sprite)
        self.add(self.unit_display)

        # Show unit name above the image
        self.unit_name = floaters.TextFloater(battle_unit.getName(),
                                              font_name='TranscendsGames',
                                              font_size=Board.TILE_SIZE // 2,
                                              anchor_x='left',
                                              anchor_y='bottom')
        self.unit_name.position = Board.TILE_SIZE // 2, mech_sprite.get_rect(
        ).topleft[1]
        self.add(self.unit_name)

        # Show unit variant below the image
        self.unit_variant = floaters.TextFloater(
            battle_unit.getVariant().upper(),
            font_name='TranscendsGames',
            font_size=Board.TILE_SIZE // 3,
            anchor_x='left',
            anchor_y='top')

        self.unit_variant.position = Board.TILE_SIZE // 2, Board.TILE_SIZE - 4
        self.add(self.unit_variant)

        # Show armor, structure, heat stats next to the image (top)
        self.unit_stats = UnitStats(battle_unit)
        stats_pos = mech_sprite.get_rect().topright
        self.unit_stats.position = 4 + stats_pos[0], stats_pos[
            1] - self.unit_stats.height
        self.add(self.unit_stats)

        # Show move and attack numbers next to the image (bottom)
        values = "MV %i  ATK %i/%i/%i" % (battle_unit.getTurnMove(),
                                          battle_unit.short,
                                          battle_unit.medium, battle_unit.long)
        self.unit_values = floaters.TextFloater(values,
                                                font_name='TranscendsGames',
                                                font_size=Board.TILE_SIZE // 4,
                                                anchor_x='left',
                                                anchor_y='bottom')
        values_pos = mech_sprite.get_rect().bottomright
        self.unit_values.position = 4 + values_pos[0], values_pos[1] - 2
        self.add(self.unit_values)
Beispiel #11
0
    def __init__(self, battle_mech):
        super(MechSprite, self).__init__()

        self.battle_mech = battle_mech

        mech_img = pyglet.resource.image(self.battle_mech.getImagePath())
        mech_img_grid = pyglet.image.ImageGrid(mech_img, 1, 6)

        self.static = True

        img_static = Sprite(mech_img_grid[0])

        self.width = img_static.width
        self.height = img_static.height

        # TODO: setup the non square friendly/enemy indicators based on team of current player's turn
        if battle_mech.player.is_bot:
            indicator = Sprite(Resources.enemy_indicator_img)
        else:
            indicator = Sprite(Resources.friendly_indicator_img)

        indicator.visible = False
        indicator.position = 0, -img_static.height // 2 + indicator.height // 2 + 1
        self.indicator = indicator
        self.add(indicator, z=0)

        shadow = Sprite(MechSprite.shadow_img_grid[battle_mech.getSize() - 1])
        shadow_rect = shadow.get_rect()
        shadow_rect.bottomleft = (self.battle_mech.col * Board.TILE_SIZE), \
                                 (self.battle_mech.row * Board.TILE_SIZE)
        shadow.position = shadow_rect.center
        self.shadow = shadow

        rect = img_static.get_rect()
        rect.bottomleft = (self.battle_mech.col * Board.TILE_SIZE) - (img_static.width//2 - shadow.width//2), \
                          (self.battle_mech.row * Board.TILE_SIZE)
        self.position = rect.center

        self.node = BatchNode()
        self.add(self.node, z=2)

        img_static.y = Board.TILE_SIZE//4
        self.node.add(img_static)
        self.img_static = img_static

        img_ct = Sprite(mech_img_grid[1])
        img_ct.y = Board.TILE_SIZE//4
        self.img_ct = img_ct

        img_ll = Sprite(mech_img_grid[4])
        img_ll.y = Board.TILE_SIZE//4
        self.img_ll = img_ll

        img_rl = Sprite(mech_img_grid[5])
        img_rl.y = Board.TILE_SIZE//4
        self.img_rl = img_rl

        img_la = Sprite(mech_img_grid[2])
        img_la.y = Board.TILE_SIZE//4
        self.img_la = img_la

        img_ra = Sprite(mech_img_grid[3])
        img_ra.y = Board.TILE_SIZE//4
        self.img_ra = img_ra

        # testing the stats stuff
        self.stats = BatchNode()
        self.updateStatsIndicators()
Beispiel #12
0
    def __init__(self, battle_unit, is_friendly=True, reverse=False, mask_image=True, menu_selected=False):
        super(UnitCard, self).__init__()
        from board import Board

        self.battle_unit = battle_unit
        self.reverse = reverse
        self.width = 0
        self.height = 0

        self.unit_display = BatchNode()
        self.unit_display.position = 0, 0

        mech_img_grid = pyglet.image.ImageGrid(pyglet.resource.image(battle_unit.getImagePath()), 1, 6)
        mech_img_static = mech_img_grid[0]
        pitch = -(mech_img_static.width * len('RGBA'))
        img_data = mech_img_static.get_image_data()

        pyg_img = mech_img_static
        if mask_image:
            # masking only a portion of the image based on damage
            damage_height = int(mech_img_static.height)
            data = img_data.get_region(0, 0, mech_img_static.width, damage_height).get_data('RGBA', pitch)

            mask = Image.frombytes('RGBA', (mech_img_static.width, damage_height), data)
            # the first image is the color that the stamp will be
            img1 = Image.new('RGBA', mask.size, color=(0, 0, 0, 255))
            # second image is the background
            bg_color = (200, 75, 75, 200)
            if is_friendly:
                bg_color = (225, 225, 225, 200)
            img2 = Image.new('RGBA', mask.size, color=bg_color)
            img1 = img1.convert('RGBA')

            # apply mask to background image
            img = Image.composite(img1, img2, mask)

            raw_image = img.tobytes()
            img_x = mask.size[0]
            img_y = mask.size[1]
            pyg_img = pyglet.image.ImageData(img_x, img_y, 'RGBA', raw_image, pitch=-img_x * len('RGBA'))

        mech_sprite = Sprite(pyg_img)
        mech_sprite.position = mech_sprite.width // 2, Board.TILE_SIZE // 2 + mech_sprite.height // 2
        self.sprite = mech_sprite

        self.unit_display.add(mech_sprite)
        self.add(self.unit_display)

        # Show unit name above the image
        unit_name_str = "%it %s [%ipv]" % (battle_unit.getTonnage(), battle_unit.getName(), battle_unit.getPointValue())
        unit_name_font_size = Board.TILE_SIZE // 2
        if menu_selected:
            unit_name_font_size = int(unit_name_font_size * 1.5)

        if reverse:
            self.unit_name = floaters.TextFloater(unit_name_str, font_name='TranscendsGames',
                                                  font_size=unit_name_font_size, anchor_x='right', anchor_y='bottom')
            name_rect = mech_sprite.get_rect().topright
            self.unit_name.position = name_rect[0], name_rect[1]
        else:
            self.unit_name = floaters.TextFloater(unit_name_str, font_name='TranscendsGames',
                                                  font_size=unit_name_font_size, anchor_x='left', anchor_y='bottom')
            name_rect = mech_sprite.get_rect().topleft
            self.unit_name.position = name_rect[0], name_rect[1]

        self.add(self.unit_name)

        # Show unit variant below the image
        unit_variant_str = battle_unit.getVariant().upper()
        unit_variant_font_size = Board.TILE_SIZE // 3
        if menu_selected:
            unit_variant_font_size = int(unit_variant_font_size * 1.5)

        if reverse:
            self.unit_variant = floaters.TextFloater(unit_variant_str, font_name='TranscendsGames',
                                                     font_size=unit_variant_font_size, anchor_x='right', anchor_y='top')
            variant_rect = mech_sprite.get_rect().bottomright
            self.unit_variant.position = variant_rect[0], variant_rect[1] - 4
        else:
            self.unit_variant = floaters.TextFloater(unit_variant_str, font_name='TranscendsGames',
                                                     font_size=unit_variant_font_size, anchor_x='left', anchor_y='top')
            variant_rect = mech_sprite.get_rect().bottomleft
            self.unit_variant.position = variant_rect[0], variant_rect[1] - 4

        if menu_selected:
            # when selected in a menu the fonts are larger and needs more space away from the image stamp
            self.unit_variant.y -= unit_variant_font_size

        self.add(self.unit_variant)

        # Show armor, structure, heat stats next to the image (top)
        unit_stats = cocos.layer.Layer()
        stats_width = 0
        stats_height = 0

        # Show move and attack numbers next to the image (bottom)
        # values = "MV %i  ATK %i/%i/%i" % (battle_unit.getTurnMove(),
        #                                   battle_unit.short, battle_unit.medium, battle_unit.long)
        # self.unit_values = floaters.TextFloater(values, font_name='TranscendsGames',
        #                                         font_size=Board.TILE_SIZE // 4, anchor_x='left', anchor_y='bottom')
        # values_pos = mech_sprite.get_rect().bottomright
        # self.unit_values.position = 4 + values_pos[0], values_pos[1] - 2
        # self.add(self.unit_values)

        # create move icon and label of values
        move_icon = Sprite(Resources.move_icon_img)
        move_str = str(battle_unit.getTurnMove())

        unit_values_font_size = Board.TILE_SIZE // 3
        if menu_selected:
            unit_values_font_size = int(unit_values_font_size * 1.25)

        if reverse:
            move_icon.position = -move_icon.width // 2, stats_height + move_icon.height // 2

            move_label = floaters.TextFloater(move_str, font_name='TranscendsGames',
                                              font_size=unit_values_font_size, anchor_x='right', anchor_y='bottom')
            move_label.position = move_icon.get_rect().bottomleft
            unit_stats.add(move_label, z=2)
        else:
            move_icon.position = move_icon.width // 2, stats_height + move_icon.height // 2

            move_label = floaters.TextFloater(move_str, font_name='TranscendsGames',
                                              font_size=unit_values_font_size, anchor_x='left', anchor_y='bottom')
            move_label.position = move_icon.get_rect().bottomright
            unit_stats.add(move_label, z=2)

        unit_stats.add(move_icon, z=2)

        stats_height += move_icon.height

        # create attack icon and label of values
        attack_icon = Sprite(Resources.weapon_icon_img)
        attack_short = battle_unit.getDamageForRange('short')
        attack_medium = battle_unit.getDamageForRange('medium')
        attack_long = battle_unit.getDamageForRange('long')
        attack_str = "%i/%i/%i" % (attack_short, attack_medium, attack_long)

        if reverse:
            attack_icon.position = -attack_icon.width // 2, stats_height + attack_icon.height // 2

            attack_label = floaters.TextFloater(attack_str, font_name='TranscendsGames',
                                                font_size=unit_values_font_size, anchor_x='right', anchor_y='bottom')
            attack_label.position = attack_icon.get_rect().bottomleft
            unit_stats.add(attack_label, z=2)
        else:
            attack_icon.position = attack_icon.width // 2, stats_height + attack_icon.height // 2

            attack_label = floaters.TextFloater(attack_str, font_name='TranscendsGames',
                                                font_size=unit_values_font_size, anchor_x='left', anchor_y='bottom')
            attack_label.position = attack_icon.get_rect().bottomright
            unit_stats.add(attack_label, z=2)

        unit_stats.add(attack_icon, z=2)

        stats_height += attack_icon.height

        # create heat icon and bars
        heat_icon = Sprite(Resources.heat_icon_img)
        if reverse:
            heat_icon.position = -heat_icon.width // 2, stats_height + heat_icon.height // 2
        else:
            heat_icon.position = heat_icon.width // 2, stats_height + heat_icon.height // 2

        unit_stats.add(heat_icon, z=2)

        for i in range(battle_unit.heat):
            pip = Sprite(Resources.heat_pip_img)
            pip.scale = 2.0
            if reverse:
                pip.position = -heat_icon.width - (i * pip.width) - pip.width, stats_height + pip.height // 2
            else:
                pip.position = heat_icon.width + (i * pip.width) + pip.width // 2, stats_height + pip.height // 2

            unit_stats.add(pip, z=1)

            if pip.x + pip.width > stats_width:
                stats_width = pip.x + pip.width

        stats_height += heat_icon.height

        # create structure icon and bars
        structure_icon = Sprite(Resources.structure_icon_img)
        if reverse:
            structure_icon.position = -structure_icon.width // 2, stats_height + structure_icon.height // 2
        else:
            structure_icon.position = structure_icon.width // 2, stats_height + structure_icon.height // 2

        unit_stats.add(structure_icon, z=2)

        orig_structure = battle_unit.mech.structure
        for i in range(orig_structure):
            pip_img = Resources.structure_pip_img
            if i >= battle_unit.structure:
                pip_img = Resources.empty_pip_img

            pip = Sprite(pip_img)
            pip.scale = 2.0
            if reverse:
                pip.position = -structure_icon.width - (i * pip.width) - pip.width, stats_height + pip.height // 2
            else:
                pip.position = structure_icon.width + (i * pip.width) + pip.width // 2, stats_height + pip.height // 2

            unit_stats.add(pip, z=1)

            if pip.x + pip.width > stats_width:
                stats_width = pip.x + pip.width

        stats_height += structure_icon.height

        # create armor icon and bars
        armor_icon = Sprite(Resources.armor_icon_img)
        if reverse:
            armor_icon.position = -armor_icon.width // 2, stats_height + armor_icon.height // 2
        else:
            armor_icon.position = armor_icon.width // 2, stats_height + armor_icon.height // 2

        unit_stats.add(armor_icon, z=2)

        orig_armor = battle_unit.mech.armor
        for i in range(orig_armor):
            pip_img = Resources.armor_pip_img
            if i >= battle_unit.armor:
                pip_img = Resources.empty_pip_img

            pip = Sprite(pip_img)
            pip.scale = 2.0
            if reverse:
                pip.position = -armor_icon.width - (i * pip.width) - pip.width, stats_height + pip.height // 2
            else:
                pip.position = armor_icon.width + (i * pip.width) + pip.width // 2, stats_height + pip.height // 2

            unit_stats.add(pip, z=1)

            if pip.x + pip.width > stats_width:
                stats_width = pip.x + pip.width

        stats_height += armor_icon.height

        if reverse:
            stats_pos = mech_sprite.get_rect().topleft
            unit_stats.position = stats_pos[0] - 4, stats_pos[1] - stats_height

        else:
            stats_pos = mech_sprite.get_rect().topright
            unit_stats.position = 4 + stats_pos[0], stats_pos[1] - stats_height

        self.add(unit_stats)

        # calculate actual width and height of this element
        self.width = mech_sprite.width + stats_width
        self.height = mech_sprite.height + (3 * Board.TILE_SIZE // 2)