Main Content

Designing and Building a PIC32 Video Game

Two students at Cornell University have developed a video game based on a PIC32 Microcontroller using a new version of the boids algorithm.

We created a multiplayer video game that runs on a PIC32 microcontroller (Figure 1). Our game simulates predator-prey interactions in nature. Through distinct game modes, players can experience what it is like to be both the hunter and the hunted. The player uses a joystick to control a Salmon or a Tuna, earning points by eating minnows. The game is lost when time runs out or the player is eaten by the Shark.

We based our game on natural predator-prey interactions between fish and their food. During gameplay, players use joysticks to control their avatars—a Salmon or a Tuna. As the player swims around the ocean, minnows automatically evade the player, according to the “boids algorithm,” an algorithm that simulates the flocking behavior of birds.

The idea for the game sprouted from an assignment in Cornell University’s ECE 4760 course, which required us to implement boids on a PIC32. After successfully animating 200 boids, we added a predator for the boids to avoid. The stationary predator activated our imaginations. “What if we could control the predator,” we thought. The simple idea of controlling the predator developed quickly into a complete gaming system. We combined different menus, instructions, modes, players, and sound effects, with nostalgic joysticks and a button to bring the experience to life.

A player may select from three different modes: Timed, Race, and Shark. In Timed mode, one or two players eat as many minnows as they can in 60 seconds. In Race mode, two players compete to be the first player to eat 50 minnows. Shark mode adds another link in the food chain, challenging the player to act as both predator and prey.

In Shark mode, players can showcase their minnow-eating skills, while being chased by a shark as the game boundaries shrink. The player is slowly forced into a corner with the shark and minnows, thus increasing the difficulty of the game the longer the player survives. Figure 2 shows how the elements appear on the screen.

HARDWARE
Joysticks: The players interact with the game primarily through joysticks. Each Joystick is an eight-way input device that allows the player to move an avatar (Tuna for Player 1, Salmon for Player 2). There are five pins on each joystick—one pin per cardinal direction plus one for the ground.

There is no datasheet for the joysticks, so we had to deduce their internal schematics, ourselves. We assumed there must be internal switches that are opened and closed when the joystick is moved. Using a multimeter, we experimentally determined which of the five pins were activated given an input direction. Then, once we had assigned four of the pins to a direction, we were left with one unassigned pin, which we tied to the ground.

To integrate the joysticks with the PIC32 microcontroller, a 10kΩ pull-up resistor was used to tie the four directional pins high to 3.3V on the PIC32, while the fifth pin was tied to the ground (Figure 3). When the joystick is moved, the pins corresponding to the direction of the movement go low, whereas the non-pressed pins remain high.

Select Button: The player makes selections using a push-button. To do this, one terminal of the button was pulled up to 3.3V via a 10kΩ resistor, while the other terminal was tied to the ground (Figure 4). Our button is idle high and goes low when pressed. We were able to debounce the button to account for false contacts that may occur during oscillations of the spring inside it.

Audio: Our game comes equipped with two sound effects, “Ow” and “Chomp.” When a player eats a minnow and scores a point, the minnow says, “Ow.” When the shark inevitably eats the player, the shark says, “Chomp.” Dr. Bruce Land kindly stepped in as our voice actor, and we recorded the samples using an iPhone.

To get the sound to play, we use DMA to send audio to a digital-to-analog converter, which then gets sent to a speaker. Our digital-to-analog converter (DAC) interfaces with the speakers via an audio socket. We tied the front pin on the audio socket to the ground, and we tied one of the two rear pins to one of DAC output A and DAC output B (Figure 5). Then, we plug a speaker into the 3.5mm headphone jack.

We wrote a Python script, utilizing the .wav package, to convert the iPhone audio recordings to an array of integers. The DAC reads this array and converts each integer to a voltage to produce sound on the speakers.

We also had to make the audio file as small as possible to fit inside the PIC32’s extremely limited flash memory. We implemented a simple compression strategy by averaging two data packets to reduce the number of audio samples by a factor of two. The resulting sound effects took up 70KB out of the available 128.”

Link to article