This 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.
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.
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.
Download the packages before compiling Python program (duck_hunt.py). 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.
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.
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
Download Python 3.4.4 from its website. Select 64- or 32-bit software based on your Windows PC configuration. Install it.
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’.
To avoid errors, update pip module type python -m pip install pip –upgrade. This will download the latest version of pip.
Download Python packages from www.lfd.uci.edu/~gohlke/pythonlibs/. 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.