iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 10
0
自我挑戰組

卡牌遊戲開發日記v2020系列 第 10

Day10 實作:玩家資料的封裝

  • 分享至 

  • xImage
  •  

進入閉嘴實作階段
封裝內部資料,實作操作方法,尚未完成外部呼叫介面

from card import Deck,HandCards
from controller import select, choose, selectR, chooseR



class PlayerStat:
    def __init__(self, hp, shell=None, deck=Deck()):
        self.hp = hp
        self.shell = shell
        self.deck = deck
        self.hands = HandCards()
        self.discard = None

    def hurt(self, val):
        self.hp -= val
        
    def heal(self, val):
        self.hp += val
        
    def shield(self, val):
        self.shell = val
        
    def disperse(self, val):
        if self.shell:
            self.shell = None if val > self.shell else self.shell-val

    def seal(self):
        self.hasCover = True
        self.cover = "seal"
    
    def defense(self):
        self.hasCover = True
        self.cover = "defense"
        
    def refelect(self):
        self.hasCover = True
        self.cover = "refelect"
        
    def empty_fort(self):
        self.hasCover = True
        self.cover = "empty_fort"
        
    def dizzy(self):
        self.dizzyToken = 2

    # action
    def deal(self):
        idxSet, zid = selectR(self.hand)
        selC = self.hand.pickup(idxSet) # Note: pick to match, pickup to deal

    def draw(self, n=2):
        if len(self.hand) + n > 5:  # note: ensure 1 <= n <= 5 - len(self.hand)
            n = 5 - len(self.hand)
        drawC = self.deck.drawn(n)
        self.hand.join(drawC)

    def drawD(self, n=2):
        if len(self.hand) + n > 6:  # note: ensure 1 <= n <= 6 - len(self.hand)
            n = 6 - len(self.hand)
        drawC = HandCards(self.deck.drawn(n + 1))
        idxSet = {chooseR(drawC)}
        self.discard = drawC.pickup(idxSet)
        self.hand.join(drawC)

    def pickup(self,idxSet):
        return self.hands.pickup(idxSet)

    def addCards(self, crds):
        self.deck.join(crds)

    def showHands(self):
        return self.hands


上一篇
Day9 再談資料封裝(介面)
下一篇
Day11 實作:系統更新玩家狀態的API
系列文
卡牌遊戲開發日記v202030
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言