我回來了!今天,咱們真正會亮相的植物要出來了哦!還有,咱們敵人的基礎類(我叫它BaseZombie
)也會閃亮登(lai)場(xi)。很期待?那就開始吧!shell
注:我使用的是Python 3.7(但只要是3,變化應該都不大)。還有,這是第二篇。沒看過上篇的話,這是連接。閒話少說,進入正題!編程
Sunflower
和Peashooter
向日葵是Sunflower
,豌豆射手是Peashooter
。好,上代碼!segmentfault
board = [0] * 10 sunlight = 50 class GameObject: indicating_char = '' # 子類已經出現,因此把基礎類的顯示字符刪除 pass # 剩餘部分同前 class Plant(GameObject): pass # 同前,但將顯示字符刪除 class Sunflower(Plant): """ 向日葵 """ indicating_char = 's' def __init__(self, pos): """ 初始化,陽光50 """ super().__init__(pos, 50) def step(self): """ 生產陽光 """ global sunlight sunlight += 25
嗯,向日葵編好了。IPython(注:加強版Python shell),你怎麼看?code
In [1]: import game as g In [2]: g.sunlight Out[2]: 50 In [3]: g.Sunflower(0) Out[3]: s In [4]: g.board[0].step() In [5]: g.sunlight Out[5]: 25 # 種植向日葵損失50,它又產生25
成功!如今,該編豌豆射手了。遊戲
class Peashooter(Plant): indicating_char = 'p' def __init__(self, pos): super().__init__(pos, 100) # 豌豆射手須要100陽光 def step(self): for obj in board[self.pos:]: if isinstance(obj, BaseZombie): # 就當BaseZombie存在 pass # 哎呀!
好好編程,「哎呀」什麼?由於我忽然發現,目前尚未定義角色的生命值(大家盡情吐槽吧)!因而,在GameObject
的__init__
方法前面追加:開發
class GameObject: blood = 10 # 初始生命值 pass # 剩餘同前
而後又忽然想到,角色死沒死不也是用這個定義的嗎?因而加了幾個方法和屬性:get
class GameObject: indicating_char = '' blood = 10 alive = True def __init__(self, pos): pass # 同前 def step(self): pass def die(self): pass def check(self): if self.blood < 1: self.alive = False def full_step(self): self.check() if not self.alive: board[self.pos] = 0 self.die() else: self.step()
好,如今把豌豆射手裏的那個pass
改爲:it
for obj in board[self.pos:]: if isinstance(obj, BaseZombie): obj.blood -= 1.5 # 這裏原來是pass
可是由於沒有BaseZombie
,咱們也不能使用Peashooter
。好,如今,3,2,1,放殭屍!class
咱們即將親手創造遊戲中的大壞蛋:殭屍!來吧,面對這個基礎類······import
class BaseZombie(GameObject): indicating_char = 'b' def __init__(self, pos, speed, harm, die_to_exit=False): super().__init__(pos) self.speed = speed self.harm = harm self.die_to_exit = die_to_exit def step(self): if board[self.pos - self.speed] == 0: orig_pos = self.pos self.pos -= self.speed board[orig_pos] = 0 board[self.pos] = self elif isinstance(board[self.pos - 1], Plant): board[self.pos - 1].blood -= 1 else: self.pos -= 1 board[self.pos + 1] = 0 board[self.pos] = self def die(self): if self.die_to_exit: import sys sys.exit()
好,讓咱們的新類們去IPython裏大展身手吧!
In [1]: import game as g In [2]: def step(): ...: for obj in g.board: ...: if isinstance(obj, g.GameObject): ...: obj.step() ...: In [3]: g.Sunflower(0) In [4]: step() In [5]: step() In [6]: step() In [7]: step() In [8]: g.sunlight Out[8]: 100 In [9]: g.Peashooter(1) In [10]: g.BaseZombie(9, 1, 1) In [11]: step() In [12]: step() ...... In [18]: step() In [19]: g.board Out[19]: [s, p, 0, 0, 0, 0, 0, 0, 0, 0]
看來,豌豆射手戰勝殭屍了!
下次,趕快把BaseZombie
的子類編出來後,咱們就要開始開發用戶界面了!歡迎來看!