# Duckhunt video game using Atmel Mega32

### Introduction to Duck Hunt

For our final project in ECE476, we implemented a multi-duck and multi-player version of the Nintendo classic Duck Hunt on the Atmel Mega32 microcontroller.

In 1985, Nintendo released a game for the Nintendo Entertainment System (NES) called Duck Hunt, and it was hugely popular with kids at the time. When we were doing the lunar lander lab, we began to wonder what other old school games could be remade on the MCU. Ultimately, we decided on Duck Hunt.

The essence of the game is that you are a hunter, with your trusty dog, hunting ducks. The game consists of your dog scaring the ducks out of the long grass, you shoot them down, and the dog retrieves them. The ducks are shot using the NES light-sensitive zapper gun. Our implementation allowed the player to select one or two ducks to be flying at a time. There is also an option for a second player to control the ducks using a Sega Genesis controller. If the shooter can down enough ducks each level, he advances until beating the game. Our game comes complete with sound effects and slightly annoying music, as per the NES design

### Rationale and sources of our project idea

Our project idea comes from the hugely successful 1985 Nintendo Duck Hunt game. We have all played that game and are interested in recreating the classic game using two Atmel Mega32 microcontrollers.

Background math

The math behind our duck hunt game is based on the lunar lander video game we did in our lab. We decided to express distance in our game in terms of pixels and time in term of frames, so the speed of a duck will be expressed as pixels/frames. For the video generation part of our game, we need to keep track of the speed, and direction of two ducks flying on the screen (in the two duck mode of the game). Each ducks can move in possibly 8 directions (up, down, left, right and four diagonal directions), and have a minimum speed of 0 and a maximal speed that is depended on whether if the duck is being controlled by a player or is it on a random movement course. When the duck is controlled by a player, the max speed is 1 pixel/frame in vertical and horizontal directions and slightly faster in diagonal directions. When the duck is uncontrolled, it moves at the same 1 pixel/frame for all 8 directions because we normalized the speed at diagonal directions to match that of the vertical and horizontal.

Our version of Duck Hunt included both sound effects and music. A second MCU was necessary for sound because we wanted to play back sampled sounds in flash and we were afraid that one MCU alone would not be able to handle the TV signal, game logistics, Sega controller, and sound at once. The high level design of the sound was based on Pulse Width Modulation performed on a second MCU. Music was created using the DDS method similar to the Cricket Call Synthesizer Lab. A sine table was loaded into memory and the width of the pulse on the output of the PWM (via the OCR) was determined by the sine table value. Stepping through the sine table more quickly produced higher frequency notes while stepping through the sine table slowly created low frequency notes. The speed at which the sine table was stepped through was determined by an increment value that was added to an accumulator, which was the index into the sine table. We used a timer interrupt that updated the accumulator every time the timer overflowed. The value of the increment to add to the accumulator in order to get the desired frequency is determined by:

1/frequency = (size of accumulator/increment) x (# counts in a pulse/clk frequency)
increment = (2^32)x(256/16,000,000)x(frequency)
increment = 68719x(frequency)

An array of frequency values were loaded into flash corresponding to the notes in a song. The highest frequency note played was 2349 Hz. According to the formula above, this gives an increment size of 161,420,931. With this size increment, there would be about 27 samples taken from the sine table which is still pretty accurate for one cycle of a sine.

### Logical Structure

We originally thought about going all the way and make the game color, but that turned out to be impossible with just our mega 32 MCU. We needed a second MCU for the sound/music in our game because the first MCU canāt handle everything all at once; it is already busy enough with drawing the TV and making all the calculations for the game.

### Standards

We used the NTSC standard for generating video signal. We generated a non-interlaced, black and white TV signal, which means our game can be played on any standard NTSC CRT TV. To the best of our knowledge, our game didnāt involve any IEEE standards.

The patents relevant to our project are all related to either the gun or the controller. The gun we used was a NES light-sensitive zapper gun, it involved patient # 4813682-āVideo target control and sensing circuit for photosensitive gunā and patent # 4395045-āTelevision precision target shooting apparatus and methodā. We also used the Sega Genesis controller, which is patented by NES.

## Parts list:

STK500: \$15
Custom Board w/ RS232 Connector: \$7
2x Power Supply: \$10
2x Mega32 MCU: \$16
Duck Hunt Gun: Free
Sega Controller: Free (borrowed)
Resistors, Cables, Capacitors, Op-amp: Free (lab)
Television: Free (lab)
Total: \$48

For more detail: Duckhunt video game