中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

幾段 Python 代碼理解面向?qū)ο?

2018-07-20    來(lái)源:編程學(xué)習(xí)網(wǎng)

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

 

目錄

  1. 定義一個(gè)游戲輸入,對(duì)輸入簡(jiǎn)單解析并做出反應(yīng)
  2. 為游戲?qū)ο筇砑硬榭礌顟B(tài)的方法
  3. 為 Goblin 類(lèi)添加更詳細(xì)的信息

 

正文

 

1.定義一個(gè)游戲輸入,對(duì)輸入簡(jiǎn)單解析并做出反應(yīng)

 

源代碼:

a-simple-game.py

# 獲取輸入并解析出輸入對(duì)應(yīng)的動(dòng)作
def get_input():
    command = input(":").split()
    verbo_word = command[0]
    if verbo_word in verb_dict:
        verb = verb_dict[verbo_word]
    else:
        print("Unknown verb {}".format(verbo_word))
        return

    if len(command) >= 2:
        noun_word = command[1]
        print(verb(noun_word))
    else:
        print(verb("nothing"))

# 具體的動(dòng)作
def say(noun):
    return "You said {}".format(noun)

# 將動(dòng)詞和動(dòng)作對(duì)應(yīng)起來(lái)
verb_dict = {
    "say": say,
}

while True:
    get_input()

 

運(yùn)行結(jié)果:

 

 

 

2.為游戲?qū)ο筇砑硬榭礌顟B(tài)的方法

代碼:

class GameObject:
    class_name = ""
    desc = ""
    objects = {}

    def __init__(self, name):
        self.name = name
        GameObject.objects[self.class_name] = self

    def get_desc(self):
        return self.class_name + "\n" + self.desc


# 創(chuàng)建一個(gè)繼承自游戲?qū)ο箢?lèi)的哥布林類(lèi)
class Goblin(GameObject):
    class_name = "goblin"
    desc = "A foul creature"

goblin = Goblin("Gobbly")

# 具體的動(dòng)作
def examine(noun):
    if noun in GameObject.objects:
        return GameObject.objects[noun].get_desc()
    else:
        return "There is no {} here.".format(noun)

 

以上代碼創(chuàng)建了一個(gè)繼承自 GameObject 類(lèi)的 Goblin 類(lèi),也創(chuàng)建一個(gè)新的 examine 方法,于是我們添加一個(gè)新的 examine 動(dòng)詞進(jìn)去:

# 將動(dòng)詞和動(dòng)作對(duì)應(yīng)起來(lái)
verb_dict = {
    "say": say,
    "examine": examine,
}

 

代碼和上一個(gè)小 demo 合并起來(lái),運(yùn)行看看:

 

 

3.為 Goblin 類(lèi)添加更詳細(xì)的信息,并添加 hit 動(dòng)作,讓你可以打 Goblin(有點(diǎn)意思了~)

 

代碼(只列出修改過(guò)的與添加的):

class Goblin(GameObject):
    def __init__(self, name):
        self.class_name = "goblin"
        self.health = 3
        self._desc = "A foul creature"
        super().__init__(name)

    @property
    def desc(self):
        if self.health >= 3:
            return self._desc
        elif self.health == 2:
            health_line = "It has a wound on its knee."
        elif self.health == 1:
            health_line = "Its left arm has been cut off."
        elif self.health <= 0:
            health_line = "It is dead."
        return self._desc + "\n" + health_line

    @desc.setter
    def desc(self, value):
        self._desc = value

def hit(noun):
    if noun in GameObject.objects:
        thing = GameObject.objects[noun]
        if type(thing) == Goblin:
            thing.health -= 1
            if thing.health <= 0:
                msg = "You killed the goblin!"
            else:
                msg = "You hit the {}".format(thing.class_name)
        else:
            msg = "I'm not strong enough, I can only hit goblin."
    else:
        msg = "There is no {} here.".format(noun)
    return msg

# 將動(dòng)詞和動(dòng)作對(duì)應(yīng)起來(lái)
verb_dict = {
    "say": say,
    "examine": examine,
    "hit": hit,
}

 

運(yùn)行:

 

這里有 完整代碼 ,是不是簡(jiǎn)單又有趣~點(diǎn)個(gè)贊吧~

 

來(lái)自:https://zhuanlan.zhihu.com/p/28409354

 

標(biāo)簽: 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Docker 容器健康檢查機(jī)制

下一篇:掌握 Node.js 中的 async/await