# Assignment 8: Roly Poly Predator Prey

## The idea

In this assignment you’ll create a simple two player game using:

• The 5x7 display
• The accelerometer
• PC to Arduino communication

The 5x7 display will be the display for the game and each player will be able to control a character on the screen. It’ll be a simple predator-prey game: One player will be the predator and will try to catch the prey. The other player will try to evade the predator.

The prey player will control their character via the Arduino using the accelerometer. Tilting the Arduino board forward will allow them to move up the screen, tilting be backward will allow them to move backward, etc. The prey’s character will be represented by a single solid dot on the screen.

The other player will be a “predator” and will control their character via the cursor keys on the PC keyboard. The predator will be represented by a 2x2 grid of dots that blink at 4Hz. The predator is larger than the prey, but will only be allowed to move at half the speed of the prey.

The game will be played in a series of rounds. Rounds will last 15 seconds or less. Each round starts with the predator and prey in random, but non-touching locations. A round ends when either:

• The predator touches the prey (and wins a point)
• The prey survives the full 15s (and wins a point)

There will be a 4s pause between rounds. During this time the screen will:

1. Show the winner of the last round near the center of the screen.
2. The bottom row of the screen will indicate the ratio of wins to loses. If the scores are equal, only the center dot will be lit. If the winner of the last round is ahead by two points or less, the center and right-of-center dots will be lit. If they are behind by two points or less, the center and left-of-center dots will be lit. If they are ahead by more than two, the center and right two dots will be lit. If they are behind by more than two the center and left two dots will be lit.

Here are a few examples from several different games:

• The Prey won the last round and scores are tied:
• The Prey won the last round but is losing by 1-2 points overall.
• The Predator won the last round and is winning by 1-2 points overall.
• The Predator won the last round but is losing by more than 2 pointsoverall.

You can select the speeds that the characters move, but:

• It should be satisfying to play. Characters shouldn’t be either sluggish or too fast to control.
• The prey should be able to move twice as fast as the predator.
• You can, optionally, have the prey accelerate based on the level of tilt, but the maximum speed is still 2x that of the predator.

## The Java side of the communication

We will continue using the Java SerialComm class, which you created during the last studios. In order to recognize arrow keys we will also utilize the StdDraw class’s isKeyPressed method (StdDraw is a part of Sedgewick & Wayne’s Standard Library).

isKeyPressed() will show the StdDraw class window and you will need to ensure it has focus when typing arrow keys that you want isKeyPressed() to recognize. There’s no need to use other features of StdDraw.

### The Arduino

#### Hardware

You will need to connect both the 5x7 display and the accelerometer.

#### Challenges

Think carefully about how you will represent and update the LEDs on the screen. Also think about how you will represent and update the positions of characters.

## Suggested Development Order

2. Implement the “prey” first.

3. Next implement the “predator”. It may be best to initially use individual letters, like u, d, l, and r to control the predator. This will allow you to test your progress via the Serial Monitor. Since you have to press Enter each time you press a key in the Serial Monitor it won’t be a very satisfying game, but it will be sufficient to test your code.

4. Implement the game logic (the rounds, between round times, and scoring).

5. Start on the Java code. Start with a simple program that will be able to detect when the arrow keys are pressed.

6. Finish the SerialComm class using your prior work.

7. Devise a way to send bytes to the Arduino to relay the user’s intent. Keep in mind that you don’t want to flood the Arduino (send bytes faster than it can process). You may need to consider using delta timing in Java.

## Demo

The following video shows a brief demo of the expected behavior. You can select the speeds of the characters, but the basic motion, screens, and blinking behavior should be matched.

# The check-in

Verify that you have completed the following files:

• cse132-assignment7/
• RolyPoly/
• RolyPoly.ino
• PCPlayer.java
• SerialComm.java
1. Commit all your code. Do not ask to be checked out by a TA until after you have made certain that your work is committed. Failing to do this may result in you losing points on your assignment.

2. Follow the checklist below to see if you have everything done before demo your assignment to a TA.

• Your sketch allows a person to control the prey character via tilting the accelerometer.
• Your sketch allows a person to control the predator character via serial input.
• You Java code allows the PC player to control the predator via the arrow keys.
• You have implemented the between-round screens and scoring.
3. Check out with a TA.

## The rubric

• 100 pts: total
• 30 pts: Arduino
• 10 pts: Display is wired correctly and there is no flickering
• 4 pts: The prey is represented as a single dot
• 6 pts: The predator is represented as a two-by-two square that blinks at 4 Hz
• 10 pts: When the game boots, the predator and prey are placed in random locations on the display and are not touching
• 20 pts: Java
• 10 pts: The Java program uses StdDraw to read the arrow keys and decide the controls for the predator
• 10 pts: The predator can be controlled over SerialComm from the Java program
• 40 pts: Gameplay
• 8 pts: The prey can be controlled with the accelerometer
• 5 pts: The predator moves at half the maximum speed of the prey
• 5 pts: Each round lasts 15s, with a 4s pause before the start of the next round
• 10 pts: During the pause between rounds, the most recent winner and the ratio of predator/prey wins is shown on the display
• 8 pts: The game is ended if the predator touches the prey
• 4 pts: The wins for predator/prey are correctly recorded
• 10 pts: Code style