Exploring Critters: A Simulation in Python

Slide Note
Embed
Share

Uncover the world of critters in Python with a fascinating simulation where animals engage in behaviors like eating, fighting, and moving. The program features classes like Critter, Ant, Bird, Hippo, Vulture, and WildCat, each exhibiting unique characteristics and actions. Dive into the development strategy, critter exercises like the Cougar class, and ideas for maintaining proper state to enhance your coding skills.


Uploaded on Sep 26, 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. CSc 110, Autumn 2016 Lecture 32: Critters Adapted from slides by Marty Stepp and Stuart Reges

  2. CSc 110 Critters Ant Bird Hippo Vulture WildCat (creative) behavior: eat fight get_color color to display get_move movement __str__ eating food animal fighting letter to display

  3. A Critter subclass class name(Critter): class Critter: def eat() def fight(opponent) # ROAR, POUNCE, SCRATCH def get_color() # returns a hex string def get_move() # returns NORTH, SOUTH, EAST, WEST, CENTER def __str__() # returns True or False

  4. How the simulator works "Go" loop: move each animal (get_move) if they collide, fight if they find food, eat Next move? % Simulator is in control! get_move is one move at a time (no loops) Keep state (fields) to remember future moves

  5. Development Strategy Simulator helps you debug smaller width/height fewer animals "Tick" instead of "Go" Write your own main call your animal's methods and print what they return

  6. Critter exercise: Cougar Write a critter class Cougar: Method __init__ eat fight get_color Behavior Always eats. Always pounces. Blue if the Cougar has never fought; red if he has. Walks west until he finds food; then walks east until he finds food; then goes west and repeats. "C" get_move __str__

  7. Ideas for state You must not only have the right state, but update that state properly when relevant actions occur. Counting is helpful: How many total moves has this animal made? How many times has it eaten? Fought? Remembering recent actions in fields is helpful: Which direction did the animal move last? How many times has it moved that way? Did the animal eat the last time it was asked? How many steps has the animal taken since last eating? How many fights has the animal been in since last eating?

  8. Critter exercise: Anteater Write a critter class Cougar: Method __init__ eat fight get_color Behavior Eats 3 pieces of food and then stops randomly chooses between pouncing and roaring pink if hungry and red if full walks up two and then down two get_move "a" if hungry "A" otherwise __str__

Related