# CPSC120BFundamentals of Computer Science I

## Assignment 4

Conditionals

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.

### Details

Your program will have a single predator, and a single prey. These will be represented by images in the graphics window. Both the predator and prey should start in random locations within the graphics window. You should choose images that represent 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.

Your simulation should continue until the predator catches the prey. You will need a function to determine if the predator has caught the prey.

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 graphics window. You will chose a random angle, and move in that direction using the appropriate 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 graphics 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 graphics window. It should return False otherwise. Your random movement code should only allow the random movement if the point the predator/prey 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 graphics 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.

### "Hacker" Prompt

Each week, additional exercises related to the assignment will be provided at the end. These exercises are typically more challenging than the regular assignment. Bonus points will be provided to students who complete any of the "Hacker" level assignments.

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 graphics 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.

The assignment will be graded on the following requirements according to the course’s programming assignment rubric.

Functionality (75%): A functional program will:

• Draw two images on the graphics window, one the predator and one for the prey.
• Prompt the user to enter 5 integers via the command line, representing the parameters necessary for each parameter of the program.
• Use conditional statements to modify the behavior of the predator based off proximity to the prey.
• Use conditional statements to modify the behavior of the prey based off proximity to the predator.
• Make sure the predator does not leave the graphical window.
• Make sure that, if the prey does leave the graphical window, it enters a state that brings it back into the graphical window..

Style (25%): A program with good style will:

• include a header comment signifying the authors of the file,
• avoid magic numbers (literal primitive numbers),
• use meaningful names for variables and functions,
• have statements that are small (80 characters or less including leading space) and do one thing, and
• have functions that are small (40 lines or less including comments) and do one thing
• have a comment above functions that includes the purpose, the pre-conditions, and the post-conditions of the function.
• have spaces after commas in argument lists and spaces on both sides of binary operators (=, +, -, *, etc.).

One of the first successful video games was Pong. Back in the 1970's a game of Pong was so complicated that devices were sold that played only Pong. Today, Pong is the "Hello World" version of a video game; It's typically the first one you write.

### Details

Create a Python program that uses the graphics module to create the game Pong like the following:

1. The ball should be animated so that it bounces off of the left, right, and top of the window.
2. The paddle should be controlled by clicking and dragging the mouse.
3. The paddle should be at the bottom of the window and it should only move horizontally.
4. When the paddle and the ball collide, the ball should bounce like it hit the bottom of the window.
5. If the ball goes off the bottom of the window, it should display a game over message.

The above video uses the draw rectangle and draw oval functions, but you can use the images, but feel free to use any images you want.

### Extra

Add one or more of the following:

1. Multiplayer - The original pong game is a two-player game. Make both players keyboard controlled.
2. Spin - The two-player version of the game is much more fun if you have some control over where the ball goes. Use the velocity of the paddle when it hits the ball to change the ball's trajectory.
3. Obstacles - If the user has control over the path of the ball, then it should be possible to avoid hitting obstacles. Add additional sprites that when hit bounce the ball, speed up the ball, teleport the ball, etc. to make the game more interesting.

The assignment will be graded on the following requirements according to the course’s programming assignment rubric.

Functionality (75%): A functional program will:

• ball is animated to bound off of window edges
• paddle is controlled by mouse drag
• paddle is at the bottom of the window and only moves horizontally
• ball bounces off of the paddle
• displays game over message when ball is off the window

Style (25%): A program with good style will:

• include a header comment signifying the authors of the file,
• avoid magic numbers (literal primitive numbers),
• use meaningful names for variables and functions,
• have statements that are small (80 characters or less including leading space) and do one thing, and
• have a comment above functions that includes the purpose, the pre-conditions, and the post-conditions of the function.
• have spaces after commas in argument lists and spaces on both sides of binary operators (=, +, -, *, etc.).

## Submission

Submit your program as a .py file on the course Inquire page before class on Wednesday October 12th.