基本信息
源码名称:python面向对象编程实例ants vs bees
源码大小:6.16M
文件格式:.rar
开发语言:Python
更新时间:2021-01-07
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
本实例为用python语言编写,以python的面向对象、列表、局部变量等部分知识为基础,内容类似于popcap的植物大战僵尸的游戏项目
本实例为UC berkeley课程CS61A的project,代码框架由CS61A课程提供,其余部分由上传者独立完成,详见https://inst.eecs.berkeley.edu/~cs61a/sp18/proj/ants/
class Insect(object):
"""An Insect, the base class of Ant and Bee, has armor and a Place."""
is_ant = False
damage = 0
watersafe = False
def __init__(self, armor, place=None):
"""Create an Insect with an ARMOR amount and a starting PLACE."""
self.armor = armor
self.place = place # set by Place.add_insect and Place.remove_insect
def reduce_armor(self, amount):
"""Reduce armor by AMOUNT, and remove the insect from its place if it
has no armor remaining.
>>> test_insect = Insect(5)
>>> test_insect.reduce_armor(2)
>>> test_insect.armor
3
"""
self.armor -= amount
if self.armor <= 0:
self.place.remove_insect(self)
def action(self, colony):
"""The action performed each turn.
colony -- The AntColony, used to access game state information.
"""
def __repr__(self):
cname = type(self).__name__
return '{0}({1}, {2})'.format(cname, self.armor, self.place)
class Bee(Insect):
"""A Bee moves from place to place, following exits and stinging ants."""
name = 'Bee'
damage = 1
watersafe = True
def sting(self, ant):
"""Attack an ANT, reducing its armor by 1."""
ant.reduce_armor(self.damage)
def move_to(self, place):
"""Move from the Bee's current Place to a new PLACE."""
self.place.remove_insect(self)
place.add_insect(self)
def blocked(self):
"""Return True if this Bee cannot advance to the next Place."""
# Phase 4: Special handling for NinjaAnt
# BEGIN Problem 7
return (self.place.ant is not None) and self.place.ant.blocks_path
# END Problem 7
def action(self, colony):
"""A Bee's action stings the Ant that blocks its exit if it is blocked,
or moves to the exit of its current place otherwise.
colony -- The AntColony, used to access game state information.
"""
if self.blocked():
self.sting(self.place.ant)
elif self.armor > 0 and self.place.exit is not None:
self.move_to(self.place.exit)
class Ant(Insect):
"""An Ant occupies a place and does work for the colony."""
is_ant = True
implemented = False # Only implemented Ant classes should be instantiated
food_cost = 0
blocks_path = True
container = False
def __init__(self, armor=1):
"""Create an Ant with an ARMOR quantity."""
Insect.__init__(self, armor)
def can_contain(self, other):
# BEGIN Problem 9
"*** YOUR CODE HERE ***"
if self.container and not other.container and self.ant==None:
return True
return False
# END Problem 9