Sunday, July 21, 2024

More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Search in posts
Search in pages

Duck Hunt Game Using Arduino And Python

Duck hunt game is a shooting game based on pygame, using Arduino and an accelerometer sensor. In this project a gun pointer is made on the screen, which is moved along x and y coordinates of the accelerometer, and a switch is used to fire at the target.

The gun pointer is aimed by tilting the hardware (accelerometer). A push-to-on switch (S1) is pressed (pulled like a trigger) to shoot and send a signal to the PC that a shot has been fired. The gun pointer is moved towards the bird, and the bird is killed if the positions coincide.

Two or more birds can be shot at a time. Accuracy of the aim reduces if you are continuously shooting and moving the Arduino board. Screenshot of the duck hunt game is shown in Fig. 1.

Screenshot of the duck hunt game
Fig. 1: Screenshot of the duck hunt game

This game uses the basic concept of interfacing between Arduino and Python using pyserial package. Python version used in this project is 3.4.4. Details on Python packages are given in the software section.

Circuit and working

Circuit diagram of the duck hunt game using Arduino Uno and Python is shown in Fig. 2. The x and y pins of the accelerometer are connected to analogue pins A0 and A1 of Arduino. The accelerometer can have different values according to different manufactures. The x and y analogue values are displayed on the serial monitor of Arduino IDE.

- Advertisement -
Circuit diagram of the duck hunt game using Arduino Uno
Fig. 2: Circuit diagram of the duck hunt game using Arduino Uno

Tilt your hardware and note down the minimum and maximum values for x and y. Put these values in Arduino code. This will be mapped from 0-20 in the program. Then, send serial data to Python and press S1 to connect it to pin 2 of Board1, which is used to fire the gun. The accelerometer is powered by 5V pin of the Arduino board. The accelerometer used for this project is shown in Fig. 3, and the author’s prototype is shown in Fig. 4.

ADXL355 accelerometer sensor
Fig. 3: ADXL355 accelerometer sensor
Fig. 4: Author’s prototype


Python packages

Download the packages before compiling Python program ( These can be downloaded by typing ‘unofficial windows binaries Python extension packages’ on Google. Download 3.4.4 version of Python.

Python packages used in the program are pyserial, pygame, random, time and math, of which you would only have to download pyserial and pygame; the rest are pre-installed.

- Advertisement -

The game is designed on pygame library that shows the images of birds and the gun with the entire graphical interface. Communication between Python code and Arduino is done using serial communication; therefore you need to add your COM port number after the program starts. This will prompt you for a confirmation. Press y to start the game. The program screen will appear along with a circle.

Birds or ducks appear at random locations with random directions. Directions are depicted using numbers, where 1 represents north, 2 represents north-east and moving in clockwise direction to 8, which represents north-west.

At every 200ms, a 4-byte data is sent from Arduino that includes a start byte (*), followed by the location of x and y coordinates scaled between 0 and 20, and ending with 0 or 1, where 1 depicts that a gun was fired using S1.

Byte 1 is sent continuously so that data is not lost. After Python code detects the firing of gun, it sends feedback to Arduino and the byte is changed back to 0. Frames of birds are taken from a .GIF file, and these are loaded one by one on the screen to show motion of the birds.

In the end, total score is shown on the screen along with the time taken and the shooting accuracy. It is possible to kill two or more coincident birds at one shot, and extra points are awarded for this.

Arduino code

Software (duck_hunt.ino) is written in Arduino programming language. Arduino Uno Board1 is programmed using Arduino IDE software. Connect Arduino board to the PC and select the correct COM port in Arduino IDE. Compile the program (sketch). Select the correct board from Tools→Board menu in Arduino IDE and upload the sketch.

Steps for testing

Step 1

Download Python 3.4.4 from its website. Select 64- or 32-bit software based on your Windows PC configuration. Install it.

Step 2

Go to command prompt Start→Run. Type cmd, and change your folder directory (where Python was installed). In my case it was in C drive, so I typed ‘cd C:\Python34’.

Step 3

To avoid errors, update pip module type python -m pip install pip –upgrade. This will download the latest version of pip.

Step 4

Download Python packages from Search for pyserial and pygame (these two packages are required) from the list and download cp34 version or the latest one in pyserial. Also, download amd64 if your Windows is 64-bit or else download the 32-bit version.

Step 5

Save .whl files in a folder and install using pip. Type ‘C:\Python34\Scripts\pip’. Install [[PATH]]\[[FILENAME]].whl. And, import the package in Python shell. It should work properly.

Step 6

After installing Python and its libraries, test Python code. It is simple—code given with a set of images has to be kept in the same folder. Open the code; there is a variable called PORT. Change that COM port number as per your PC, after you have attached, Arduino board.

Step 7

Attach the USB cable and after uploading the code in Arduino, run Python code by pressing F5. This will open a GUI with four birds flying around on the screen and a scope view of a gun as shown in Fig. 1.

Step 8

The gun’s point can be changed by tilting the hardware, and bullets can be fired using S1.

Step 9

When you shoot while moving, the aim would not be accurate. After killing four birds, if time limit (150 seconds) is over, status of your game will be displayed on the screen.

Download source folder



Unique DIY Projects

Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components