CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Predator vs. Prey Simulation

Predator-Prey simulations are a mechanism for realizing data trends for particular environmental details. They can be used to demonstrate periodic cycles in population densities based on competition between species, as well as examine the affects of environmental changes. In this assignment, you are going to write a basic, continuous predator-prey simulation.


Setup

Create a directory assignment4 under assignments in your cs120 directory. All code for the assignment should be stored in this directory.

cd ~/cs120/assignments
mkdir assignment4
cd assignment4

Details

Your program will have a single predator, and a single prey. These will be represented by turtle objects in the turtle window. Both the predator and prey should start in random locations within the turtle window. You should set the image of these turtle objects to that of an image that represents stereotypical predator-prey entities.

Your program should read (from the command line), parameters that dictate how fast the predator is, and at what distance the predator is attracted by the prey. You should also read (from the command line) the normal speed of the prey, the running speed of the prey, and the distance at which the prey should scramble. These values will dictate the behavior of your simulation, and should be stored in variables that are globally accessible within the file.

In addition to the above values, you should store how many steps your simulation will execute in a global variable at the top of your file. This allows you to alter the duration of the simulation easily, without having to type the value every time to the command line.

Both the predator and prey operate in one of two states: normal and alert. For the prey, in normal mode it uses its normal speed, and uses its run speed in alert mode. The predator only has one speed, and should use that speed in both modes.

For both entities, normal mode consists of randomly moving within the turtle window. You will chose a random angle, and move in that direction that entities speed number of pixels. You will have to perform some trig calculations to decompose these Polar Coordinates into Cartesian Coordinates. In alert mode, both entities move based on the angle of incidence between them. The predator will move towards the prey in alert mode, while the prey will always move away from the predator in alert mode.

All movement should be contained to the turtle window. Towards that end, you should write an in_bounds function that takes a x, y coordinate as input, and returns True if and only if the point x, y is within the turtle window. It should return False otherwise. Your random movement code should only allow the random movement if the point the turtle would move to is still within the turtle window bounds. You can use the math.atan2 function to compute the inverse tangent.

While we can make some (relatively) strong assumptions that the predators behavior will usually keep it within the turtle window, that is not necessarily the case for the prey's alert behavior. It will likely be very common that the prey will run past the border of the simulation in alert mode. You should allow the prey to run outside the border of the window, but it should enter a new "correction" mode, which moves the prey towards the origin until it is back in bounds.

Your program should include the traditional header, use appropriate variable names, and nicely label all values printed to the terminal. Submission are to be done through cseval.roanoke.edu through the Assignment 4 link. You should submit a tar file for this assignment. You can do so by issuing the following commands:

cd ~/cs120/assignments
tar czvf assignment4.tgz assignment4/

Both partners must submit through cseval!


"Hacker" Prompt

  1. Attractive Oasis: A randomly moving prey is nice, but not realistic. Typically prey are attracted to their food source, such as a grassy meadow or water source. Randomly choose a point in the turtle window, and designate that point to be the oasis, an attraction point for the prey. Draw an image at this point, representing the oasis.

  2. The Middle Child: In the real world, the food chain dictates who is a predator, and who is a prey. More often than not, even a predator can find itself the prey of some bigger creature. Add a third entity to your simulation, which is a predator to the previously defined predator.

    You will have to alter the behavior of the original predator, so that it enters into a "scramble" mode if it is witin a certain distance to their newly created predator.


Last modified: Fri Oct 4 10:32:58 EDT 2013