We are going to design and implement a game similar to Doodle Jump. It is based on Pygame and Accelerometer. There are an endless series of platforms. The goal is to keep the jumping alien from falling. Our final game box acts as an embedded device with a user interface based on the RPi 4. The integrated circuit including the Pi and accelerometer which carry out computation based on the users’ behavior and respond accordingly in real time.
Adafruit LIS3DH Triple-Axis Accelerometer is used to control the x direction of the player. We wired it with the Raspberry Pi
using I2C interface.
We connected Pi 3.3V power to sensor Vin, Pi GND to sensor GND, Pi SCL to sensor SCL, Pi SDA to sensor SDA, and Pi GPIO6 to sensor INT for interrupt service. Any general GPIO pin without other functions can be chosen to connect to INT.
Fig 1: WiringTo use the libraries, we installed the dependencies and Adafruit CircuitPython LIS3DH library on the board. Note that we need to run "sudo pip3" to install bus device dependencies system-wide. Otherwise there would be an error message that the bus device is not found.
Fig 2: Accelerometer Testing Results
Four categories of platforms are generated randomly. The green ones are basic, the blue ones are moving platforms, the red ones
will break if jumped on. Additionally, springs are randomly appended to green platforms to give the player a speed boost. The information of
each platform are stored in a python list. Four integers are used to determine the x and y coordinates, the category and the state of the
platform. The last parameter is only for the blue and red platforms to decide the moving direction and which image to display.
The x positions are totally random within display range. A variable is used to keep track of the difficulty level, and the y coordinates are random integers within a range near the difficulty level. As the player moves up, the distances along y axis becomes larger.
We keep the player jumping by giving it an initial velocity along y axis. If it collides with a basic or moving platform, the velocity is reset
to the initial one. If the player steps on a spring, it gets a larger initial speed. To mimic a gravity effect, the velocity is updated by
adding gravity each time, and the position is updated by adding the velocity. The x coordinate is updated using the same logic, except that
we get the acceleration from the sensor. Note that the velocity is set to be zero if we detect the direction has changed. This enables the
player to move in the desired direction immediately, instead of waiting for the deceleration.
If the highest position of the player reaches two-thirds of the screen, it's time to move the whole system up. The is realized by subtracting an integer when displaying the images. We only need to increase this number when update is required.
After testing our game several times, we found it's very likey to fail before the player gets a chance to jump on any platform. Because the user needs some
time to react at the very beginning. A protection mechanism is developed to solve this problem. A flag is set in the initialization to
enable the player to safely jump on the bottom edge. Once it collides to a platform, the flag is cleared and the game functions normally.
What's more, when the player reaches the right or left edge, it comes back from another side, which is helpful when the distance along x axis is too large.
To deliver a better game experience, we use a battery pack to charge the Raspberry-Pi and modified the bash script to have the game run automaticly.
There are two buttons on the main menu. Hitting "Play" enters game mode. Hitting "Score" enters a scoreboard that shows the top three scores. This is implemented using a list and sorting it each time. If the user breaks the record when a game ends, the screen shows "New High Score!" as a reminder. Otherwise, it displays "Game Over". We also create a jumping animation on the menu. The properties of the player needs to be reset after each game to prevent the menu animation and the game interfering with each other.
We recreated a fully functional Doodle Jump based on pygame, accelerometer and Raspberry-Pi 4.
It has mature platform and score caculating systems. Moreover, a protection mechanism was developed
for more user-friendly experience. We met all our goals we set at the beginning.
|Bread Board||Had beforehand|
|Raspberry Pi 4||Lab component|
|Electrical Wires||Lab component|
Please refer to the Code Appendix part on the web page.