Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON PROGRAM: Using the bear-fish-plant simulation (you can find the source files in the dropbox for this homework in D2L), add a Berry class so

PYTHON PROGRAM:

Using the bear-fish-plant simulation (you can find the source files in the dropbox for this homework in D2L), add a Berry class so that the bears have another food source. Consider that Berry affects the starvation counter of Bear as well. This class behaves much like the Plant class. Please use bear.py and fish.py given below:

bear.py

import turtle import random from Fish import Fish class Bear(object):  def __init__(self):  self.bturtle = turtle.Turtle() self.bturtle.hideturtle() self.bturtle.shape("Bear.gif") # will only work if the file name has also  # been registered with the World class   self.xpos = 0 self.ypos = 0 self.world = None self.breedTick = 0 self.starveTick = 0 def setX(self, newx):  self.xpos = newx   def setY(self, newy):  self.ypos = newy   def getX(self):  return self.xpos def getY(self):  return self.ypos def setWorld(self, aworld):  self.world = aworld   def appear(self):  self.bturtle.goto(self.xpos, self.ypos) self.bturtle.showturtle() def hide(self):  self.bturtle.hideturtle() def move(self, newx, newy):  self.world.moveLifeForm(self.xpos, self.ypos, newx, newy) self.xpos = newx  self.ypos = newy  self.bturtle.goto(self.xpos, self.ypos) def tryToMove(self):  newLocation = self._getOffsetLocation() if self.world.emptyLocation(newLocation[0], newLocation[1]):  self.move(newLocation[0], newLocation[1]) def tryToBreed(self):  newLocation = self._getOffsetLocation() if self.world.emptyLocation(newLocation[0], newLocation[1]):  child = Bear() self.world.addLifeForm(child, newLocation[0], newLocation[1]) self.breedTick = 0 def _getOffsetLocation(self):  offsetList = [(-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1), (0, -1), (1, -1)] randomOffsetIndex = random.randrange(len(offsetList)) randomOffset = offsetList[randomOffsetIndex] newx = self.xpos + randomOffset[0] newy = self.ypos + randomOffset[1] while not (0 <= newx < self.world.getMaxX() and  0 <= newy < self.world.getMaxY()):  randomOffsetIndex = random.randrange(len(offsetList)) randomOffset = offsetList[randomOffsetIndex] newx = self.xpos + randomOffset[0] newy = self.ypos + randomOffset[1] return newx, newy def tryToEat(self):  newLocation = self._getOffsetLocation() if (not self.world.emptyLocation(newLocation[0], newLocation[1])) and \ isinstance(self.world.lookAtLocation(newLocation[0], newLocation[1]), Fish):  self.world.delLifeForm(self.world.lookAtLocation(newLocation[0], newLocation[1])) self.move(self.world.lookAtLocation(newLocation[0], newLocation[1])) self.starveTick = 0 else:  self.starveTick += 1 def liveALittle(self): # take a turn  self.breedTick += 1 if self.breedTick >= 8:  self.tryToBreed() self.tryToEat() if self.starveTick == 10:  self.world.delLifeForm(self) else:  self.tryToMove() ------------------------------------------------------------------------------------------- fish.py 
import turtle import random class Fish(object):  def __init__(self):  self.fturtle = turtle.Turtle() self.fturtle.hideturtle() self.fturtle.shape("Fish.gif") # will only work if the file name has also  # been registered with the World class   self.xpos = 0 self.ypos = 0 self.world = None self.breedTick = 0 def setX(self, newx):  self.xpos = newx   def setY(self, newy):  self.ypos = newy   def getX(self):  return self.xpos def getY(self):  return self.ypos def setWorld(self, aworld):  self.world = aworld   def appear(self):  self.fturtle.goto(self.xpos, self.ypos) self.fturtle.showturtle() def hide(self):  self.fturtle.hideturtle() def move(self, newx, newy):  self.world.moveLifeForm(self.xpos, self.ypos, newx, newy) self.xpos = newx  self.ypos = newy  self.fturtle.goto(self.xpos, self.ypos) def tryToMove(self):  newLocation = self._getOffsetLocation() if self.world.emptyLocation(newLocation[0], newLocation[1]):  self.move(newLocation[0], newLocation[1]) def tryToBreed(self):  newLocation = self._getOffsetLocation() if self.world.emptyLocation(newLocation[0], newLocation[1]):  child = Fish() self.world.addLifeForm(child, newLocation[0], newLocation[1]) self.breedTick = 0 def _getOffsetLocation(self):  offsetList = [(-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1), (0, -1), (1, -1)] randomOffsetIndex = random.randrange(len(offsetList)) randomOffset = offsetList[randomOffsetIndex] newx = self.xpos + randomOffset[0] newy = self.ypos + randomOffset[1] while not (0 <= newx < self.world.getMaxX() and  0 <= newy < self.world.getMaxY()):  randomOffsetIndex = random.randrange(len(offsetList)) randomOffset = offsetList[randomOffsetIndex] newx = self.xpos + randomOffset[0] newy = self.ypos + randomOffset[1] return newx, newy def liveALittle(self): # take a turn  offsetList = [(-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1), (0, -1), (1, -1)] adjfish = 0 for offset in offsetList:  newx = self.xpos + offset[0] newy = self.ypos + offset[1] if 0 <= newx < self.world.getMaxX() and 0 <= newy < self.world.getMaxY():  if (not self.world.emptyLocation(newx, newy)) and \ isinstance(self.world.lookAtLocation(newx, newy), Fish):  adjfish += 1 if adjfish >= 2:  self.world.delLifeForm(self) else:  self.breedTick += 1 if self.breedTick >= 12:  self.tryToBreed() self.tryToMove() 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

T Sql Window Functions For Data Analysis And Beyond

Authors: Itzik Ben Gan

2nd Edition

0135861446, 978-0135861448

More Books

Students also viewed these Databases questions

Question

=+16-6 Explain the func- tion of sensory adaptation.

Answered: 1 week ago

Question

I am paid fairly for the work I do.

Answered: 1 week ago

Question

I receive the training I need to do my job well.

Answered: 1 week ago