項目說明:https://inst.eecs.berkeley.edu/~cs61a/sp21/proj/ants/
螞蟻大戰蜜蜂 靈感來源:植物大戰僵屍(Plants Vs. Zombies,PVZ)
Phase 1: https://www.cnblogs.com/ikventure/p/14986805.html
Phase 2: https://www.cnblogs.com/ikventure/p/14988467.html
Phase 3: https://www.cnblogs.com/ikventure/p/14992754.html
Phase 4: https://www.cnblogs.com/ikventure/p/14994734.html
效果展示: https://www.cnblogs.com/ikventure/p/15001093.html
Phase 2: Ants!
添加更多的螞蟻類型。
Problem 4 (3 pt)
豌豆射手ThrowerAnt費用是3,為節省費用,建立費用為2的豌豆射手子類,長距離射手LongThrower和短距離射手ShortThrower:
Class | Food Cost | Initial Health |
---|---|---|
ShortThrower | 2 | 1 |
LongThrower | 2 | 1 |
長距離射手理論射程為 5~float('inf'),短距離射手理論射程為 0~3,只需要在兩個類變量中加入max_range和min_range,再在nearest_bee中加入距離變量distance即可。
注意:LongThrower和ShortThrower的類變量implemented設置為True。
代碼:
class ThrowerAnt(Ant):
"""ThrowerAnt throws a leaf each turn at the nearest Bee in its range."""
name = 'Thrower'
implemented = True
damage = 1
# ADD/OVERRIDE CLASS ATTRIBUTES HERE
food_cost = 3
max_range = float('inf')
min_range = 0
def nearest_bee(self, beehive):
"""Return the nearest Bee in a Place that is not the HIVE (beehive), connected to
the ThrowerAnt's Place by following entrances.
This method returns None if there is no such Bee (or none in range).
"""
# BEGIN Problem 3 and 4
next_place = self.place
distance = 0
while not next_place.is_hive():
if self.min_range <= distance <= self.max_range and next_place.bees:
return bee_selector(next_place.bees)
next_place = next_place.entrance
distance += 1
return None
# END Problem 3 and 4
class ShortThrower(ThrowerAnt):
"""A ThrowerAnt that only throws leaves at Bees at most 3 places away."""
name = 'Short'
food_cost = 2
# OVERRIDE CLASS ATTRIBUTES HERE
# BEGIN Problem 4
implemented = True # Change to True to view in the GUI
max_range = 3
# END Problem 4
class LongThrower(ThrowerAnt):
"""A ThrowerAnt that only throws leaves at Bees at least 5 places away."""
name = 'Long'
food_cost = 2
# OVERRIDE CLASS ATTRIBUTES HERE
# BEGIN Problem 4
implemented = True # Change to True to view in the GUI
min_range = 5
# END Problem 4
Problem 5 (3 pt)
FireAnt機制:
- 受傷害量為amount的傷害時,對當前位置的所有蜜蜂造成amount的傷害(等量反傷)
- 生命值health跌至0時,對當前位置的所有蜜蜂造成類變量damage=3的傷害,再從場地中移除。
步驟分解:
- 獲得螞蟻位置:self.place
- 獲得當前位置的蜜蜂列表 self.place.bees
- 對列表中的所有蜜蜂造成amount傷害,遍歷列表時可能會刪除元素(bee),所以for循環中使用self.place.bees[:],使用bee.reduce_health(amount)
- 螞蟻生命值降至0以下,遍歷蜜蜂列表,bee.reduce_health(self.damage),這個self指的螞蟻
- 要求使用父類的reduce_health回收螞蟻,直接super().reduce_health(0)
注意每次需要應用的類都要將類變量implemented設置為True。
代碼:
class FireAnt(Ant):
"""FireAnt cooks any Bee in its Place when it expires."""
name = 'Fire'
damage = 3
food_cost = 5
# OVERRIDE CLASS ATTRIBUTES HERE
# BEGIN Problem 5
implemented = True # Change to True to view in the GUI
# END Problem 5
def __init__(self, health=3):
"""Create an Ant with a HEALTH quantity."""
super().__init__(health)
def reduce_health(self, amount):
"""Reduce health by AMOUNT, and remove the FireAnt from its place if it
has no health remaining.
Make sure to reduce the health of each bee in the current place, and apply
the additional damage if the fire ant dies.
"""
# BEGIN Problem 5
"*** YOUR CODE HERE ***"
self.health -= amount
for bee in self.place.bees[:]:
bee.reduce_health(amount)
if self.health <= 0:
for bee in self.place.bees[:]:
bee.reduce_health(self.damage)
super().reduce_health(0)
# END Problem 5