Exploring Classes and Objects in Python Programming

Slide Note
Embed
Share

Dive into the world of classes and objects in Python programming with a focus on designing complex applications. This chapter covers analyzing and designing classes, with a specific look at the Bear class. Learn how to represent and draw the world using lists and Turtles. Code listings provide practical examples for implementing graphical simulations.


Uploaded on Sep 07, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Python Programming in Context Chapter 11

  2. Objectives To explore classes and objects further To design a large multiclass application To implement a graphical simulation using objects

  3. Designing Classes Read the description Look for nouns and verbs Nouns become classes

  4. Analyzing the Bear Class A bear should know The world it belongs to. Its specific world location given as (x,y). How long it has gone withoutfood. How long it has gone without breeding. A bear should be able to Return its location, both the x and y value. Set location, both the x and y value. Set the world that it belongs to. Appear if it is newly born. Hide if it dies. Move from one location to another. Live for one time unit (includes breeding,eating,moving,and dying).

  5. Figure 11.1

  6. Representing and Drawing the World Use a list of lists Use a Turtle to draw the world Use Turtles to represent each life form

  7. Figure 11.2

  8. Figure 11.3

  9. Listing 11.1 def __init__(self, mx, my): self.maxX = mx self.maxY = my self.thingList = [] self.grid = [] for arow in range(self.maxY): row = [] for acol in range(self.maxX): row.append(None) self.grid.append(row) self.wturtle = turtle.Turtle() self.wscreen = turtle.Screen() self.wscreen.setworldcoordinates(0,0,self.maxX-1,self.maxY-1) self.wscreen.addshape("Bear.gif") self.wscreen.addshape("Fish.gif") self.wturtle.hideturtle()

  10. Listing 11.2 def draw(self): self.wscreen.tracer(0) self.wturtle.forward(self.maxX-1) self.wturtle.left(90) self.wturtle.forward(self.maxY-1) self.wturtle.left(90) self.wturtle.forward(self.maxX-1) self.wturtle.left(90) self.wturtle.forward(self.maxY-1) self.wturtle.left(90) for i in range(self.maxY-1): self.wturtle.forward(self.maxX-1) self.wturtle.backward(self.maxX-1) self.wturtle.left(90) self.wturtle.forward(1) self.wturtle.right(90) self.wturtle.forward(1) self.wturtle.right(90) for i in range(self.maxX-2): self.wturtle.forward(self.maxY-1) self.wturtle.backward(self.maxY-1) self.wturtle.left(90) self.wturtle.forward(1) self.wturtle.right(90) self.wscreen.tracer(1)

  11. Listing 11.3 (Part 1) def freezeWorld(self): self.wscreen.exitonclick() def addThing(self, athing, x, y): athing.setX(x) athing.setY(y) self.grid[y][x] = athing athing.setWorld(self) self.thingList.append(athing) athing.appear() def delThing(self,athing): athing.hide() self.grid[athing.getY()][athing.getX()] = None self.thingList.remove(athing) def moveThing(self,oldx,oldy,newx,newy): self.grid[newy][newx] = self.grid[oldy][oldx] self.grid[oldy][oldx] = None

  12. Listing 11.3 (Part 2) def getMaxX(self): return self.maxX def getMaxY(self): return self.maxY def liveALittle(self): if self.thingList != [ ]: athing = random.randrange(len(self.thingList)) randomthing = self.thingList[athing] randomthing.liveALittle() def emptyLocation(self,x,y): if self.grid[y][x] == None: return True else: return False def lookAtLocation(self,x,y): return self.grid[y][x]

  13. Fish Class Create a class to represent a fish Give the turtle a shape that looks like a fish

  14. Listing 11.4 class Fish: def __init__(self): self.turtle = turtle.Turtle() self.turtle.up() self.turtle.hideturtle() self.turtle.shape("Fish.gif") self.xpos = 0 self.ypos = 0 self.world = None self.breedTick = 0

  15. Listing 11.5 (Part 1) 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

  16. Listing 11.5 (Part 2) def appear(self): self.turtle.goto(self.xpos, self.ypos) self.turtle.showturtle() def hide(self): self.turtle.hideturtle() def move(self,newx,newy): self.world.moveThing(self.xpos,self.ypos,newx,newy) self.xpos = newx self.ypos = newy self.turtle.goto(self.xpos, self.ypos)

  17. Neighbor Squares For any square, (x,y), where are the neighbors Above and below, side to side Necessary for eating and for moving Create offset lists

  18. Figure 11.4

  19. Figure 11.5

  20. Listing 11.6 def liveALittle(self): 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 = adjfish + 1 if adjfish >= 2: self.world.delThing(self) else: self.breedTick = self.breedTick + 1 if self.breedTick >= 12: self.tryToBreed() self.tryToMove()

  21. Listing 11.7 def tryToBreed(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] nextx = self.xpos + randomOffset[0] nexty = self.ypos + randomOffset[1] while not (0 <= nextx < self.world.getMaxX() and 0 <= nexty < self.world.getMaxY() ): randomOffsetIndex = random.randrange(len(offsetList)) randomOffset = offsetList[randomOffsetIndex] nextx = self.xpos + randomOffset[0] nexty = self.ypos + randomOffset[1] if self.world.emptyLocation(nextx,nexty): childThing = Fish() self.world.addThing(childThing,nextx,nexty) self.breedTick = 0

  22. Listing 11.8 def tryToMove(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] nextx = self.xpos + randomOffset[0] nexty = self.ypos + randomOffset[1] while not(0 <= nextx < self.world.getMaxX() and 0 <= nexty < self.world.getMaxY() ): randomOffsetIndex = random.randrange(len(offsetList)) randomOffset = offsetList[randomOffsetIndex] nextx = self.xpos + randomOffset[0] nexty = self.ypos + randomOffset[1] if self.world.emptyLocation(nextx,nexty): self.move(nextx,nexty)

  23. Bear Class Similar to Fish Bears eat Fish Must be in neighboring location

  24. Listing 11.9 class Bear: def __init__(self): self.turtle = turtle.Turtle() self.turtle.up() self.turtle.hideturtle() self.turtle.shape("Bear.gif") self.xpos = 0 self.ypos = 0 self.world = None self.starveTick = 0 self.breedTick = 0

  25. Listing 11.10 def liveALittle(self): self.breedTick = self.breedTick + 1 if self.breedTick >= 8: self.tryToBreed() self.tryToEat() if self.starveTick == 10: self.world.delThing(self) else: self.tryToMove()

  26. Listing 11.11 def tryToEat(self): offsetList = [(-1,1) ,(0,1) ,(1,1), (-1,0) ,(1,0), (-1,-1),(0,-1),(1,-1)] adjprey = [] 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): adjprey.append(self.world.lookAtLocation(newx,newy)) if len(adjprey)>0: randomprey = adjprey[random.randrange(len(adjprey))] preyx = randomprey.getX() preyy = randomprey.getY() self.world.delThing(randomprey) self.move(preyx,preyy) self.starveTick = 0 else: self.starveTick = self.starveTick + 1

  27. Figure 11.6

  28. Listing 11.13 def liveALittle(self): self.breedTick = self.breedTick + 1 if self.breedTick >= 5: self.tryToBreed()

  29. isinstance Boolean function that returns True or False Asks whether an object is an instance of a particular class

  30. Listing 11.14 if isinstance(self.world.lookAtLocation(ix,iy),Plant):

  31. Figure 11.7

More Related Content