[2021 Spring] CS61A Project 3: Ants Vs. SomeBees (Phase 4)


項目說明: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 4: Water and Might

Problem 10 (2 pt)

目前place有Hive和基本場地兩種類型,現在引入水池類型Water。
只有watersafe的insect可以放置在Water中,在Insect引入is_watersafe的類屬性,蜜蜂可以飛,所以Bee.is_watersafe是True。

Water類:
add_insect方法,放置insect到place上,如果insect不是watersafe的,將該insect的生命值降至0,調用Insect類的reduce_health方法。

class Insect:
    """An Insect, the base class of Ant and Bee, has health and a Place."""

    damage = 0
    # ADD CLASS ATTRIBUTES HERE
    is_watersafe = False
class Bee(Insect):
    """A Bee moves from place to place, following exits and stinging ants."""

    name = 'Bee'
    damage = 1
    # OVERRIDE CLASS ATTRIBUTES HERE
    is_watersafe = True
class Water(Place):
    """Water is a place that can only hold watersafe insects."""

    def add_insect(self, insect):
        """Add an Insect to this place. If the insect is not watersafe, reduce
        its health to 0."""
        # BEGIN Problem 10
        "*** YOUR CODE HERE ***"
        super().add_insect(insect)
        if not insect.is_watersafe:
            insect.reduce_health(insect.health)
        # END Problem 10

Problem 11 (2 pt)

引入水上射手ScubaThrower,繼承自ThrowerAnt,可以放置在Water場地中,只需要重寫name, is_watersafe, food_cost。

Class Food Cost Initial Health
ScubaThrower 6 1

代碼:

# BEGIN Problem 11
# The ScubaThrower class
class ScubaThrower(ThrowerAnt):
    """ScubaThrower is a watersafe ThrowerAnt."""

    name = 'Scuba'
    food_cost = 6
    is_watersafe = True
    implemented = True
# END Problem 11

Extra Credit (2 pt)

引入QueenAnt,一種防水的ScubaThrower,除了標准的ScubaThrower.action,每次行動時,它身后的所有螞蟻damage加倍(不包括FireAnt的反傷)。

Class Food Cost Initial Health
QueenAnt 7 1

QueenAnt有三種特殊的規則:

  1. queen生命值降至0,蜜蜂勝利。任一蜜蜂到達tunnel終點,蜜蜂同樣勝利。調用bees_win()。
  2. 只能存在一個真的queen,在第一個queen之后實例化的都是冒充者impostor,在第一次行動時生命值降為0,不會有增傷效果或者射手的效果;impostor死亡后游戲繼續。
  3. 真queen不能被移除 ,嘗試移除queen不會有效果,也不會造成異常。需要重寫QueenAnt中的Ant.remove_from。

思路:

  1. 同一個類的所有實例擁有相同的類屬性。要讓第一個實例為真,后面的所有實例為假,可以先設置is_true為真,在第一個實例化后修改類屬性。
  2. 通過Place.exit.ant獲取QueenAnt后面的螞蟻。
  3. 為了避免重復加倍螞蟻的damage,需要對有倍傷buff的螞蟻進行標記。在Ant中添加類屬性buffed = False。
  4. 加倍傷buff時,注意容器類螞蟻內部的contained螞蟻damage也要加倍。
# BEGIN Problem EC
class QueenAnt(ScubaThrower):  # You should change this line
# END Problem EC
    """The Queen of the colony. The game is over if a bee enters her place."""

    name = 'Queen'
    food_cost = 7
    # OVERRIDE CLASS ATTRIBUTES HERE
    # BEGIN Problem EC
    implemented = True   # Change to True to view in the GUI
    is_truequeen = True
    # END Problem EC

    def __init__(self, health=1):
        # BEGIN Problem EC
        "*** YOUR CODE HERE ***"
        super().__init__(health)
        self.is_truequeen = QueenAnt.is_truequeen
        if self.is_truequeen:
            QueenAnt.is_truequeen = False
        # END Problem EC

    def action(self, gamestate):
        """A queen ant throws a leaf, but also doubles the damage of ants
        in her tunnel.

        Impostor queens do only one thing: reduce their own health to 0.
        """
        # BEGIN Problem EC
        "*** YOUR CODE HERE ***"
        if not self.is_truequeen:
            self.reduce_health(self.health)
        else:
            ScubaThrower.action(self, gamestate)
            behind = self.place.exit
            while behind:
                if behind.ant:
                    if not behind.ant.buffed:
                        behind.ant.damage *= 2
                        behind.ant.buffed = True
                    if behind.ant.is_container() and behind.ant.contained_ant:
                        if not behind.ant.contained_ant.buffed:
                            behind.ant.contained_ant.damage *= 2
                            behind.ant.contained_ant.buffed = True
                behind = behind.exit
        # END Problem EC

    def reduce_health(self, amount):
        """Reduce health by AMOUNT, and if the True QueenAnt has no health
        remaining, signal the end of the game.
        """
        # BEGIN Problem EC
        "*** YOUR CODE HERE ***"
        if not self.is_truequeen:
            Insect.reduce_health(self, amount)
        else:
            self.health -= amount
            if self.health <= 0:
                bees_win()
    
    def remove_from(self, place):
        if not self.is_truequeen:
            Ant.remove_from(self, place)
        # END Problem EC

Test


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM