How to Port a Mini Dinosaur Game to AVR64DD32 Nano

How to Port a Mini Dinosaur Game to AVR64DD32 Nano

Today’s project is the porting of a mini dinosaur game by HonestQiao, who is a keen supporter of our Funpack activities. During the game porting process, a line-following sensor was added to check finger sliding actions, along with background music, so let’s take a look.

The project has been uploaded to the Electronic Forest:

https://www.eetree.cn/project/detail/1716

Project Introduction

01

This project is derived from the mini game “Dino Game Arduino Edition“, which has been ported to run on the AVR64DD32 Nano development board, and is well adapted to the DFRobot’s 0.96-inch high-definition color TFT display.
The “Dino Game for Arduino” is a mini game that runs on Arduino UNO. Google has a web version of this mini game:Chrome Dino Game Online (https://dino-chrome.com/). The basic gameplay is to control the small dinosaur character on the screen to jump at the right time to avoid colliding with the incoming trees. Interested friends can experience it through the online version.

Design Concept

02

The original Dino Game for Arduino runs on Arduino UNO + ST7735 (1.8 Inch TFT Display 128×160). Porting it to the AVR64DD32 Nano development board requires ensuring the program runs correctly and adapting to the display used in this project.The display used in this project is DFRobot_0.96, which uses the SPI interface and can be directly connected to the SPI pins of the AVR64DD32 Nano development board.
The basic logic of the game is to first draw the small dinosaur, then draw the trees, and during the refresh process, move the trees. If a tree collides with the small dinosaur, the game fails. During the game, you can control the small dinosaur to jump to avoid the trees.
This project also adds a buzzer to play background music, uses buttons to implement the jump action of the small dinosaur, and adds a line-following sensor to check for finger sliding, increasing the fun of the game operation.

Hardware Introduction

03

AVR64DD32 Nano development board:

  • [Funpack Activity Board] AVR64DD32 Curiosity Nano Development Board

  • Display: DFRobot_0.96 HD Resolution Display

  • Passive Buzzer: General Version

  • Regular Button: General Version

  • Line Following Sensor: General Version

  • Several Wires

Project Circuit Diagram

How to Port a Mini Dinosaur Game to AVR64DD32 Nano
Hardware Connection Diagram
How to Port a Mini Dinosaur Game to AVR64DD32 Nano

Project Code Explanation

04

During the project code design process, the Arduino core libraries and third-party libraries were used as follows:
  • SPI: SPI.h, for driving the display
  • Software Serial: SoftwareSerial.h, the onboard debugger corresponds to RX-PD5, TX-PD4. Through software serial, information can be directly output to the USB connection port to view output on the computer.
  • Display Driver Core: Used Adafruit_GFX, a very powerful display driver library
  • Display Driver Libraries: Adafruit_ST7735, Adafruit_ST7789
  • Background Music Playback: TonePlayer.h, simulates background music playback, song.h defines the background music
  • ZCD Library: zcd.h, for specific usage of ZCD, you can refer to
    microchip-pic-avr-examples/avr64dd32-getting-started-with-zcd-mplabx: Code examples for the ZCD peripheral. (github.com)
Main Function Descriptions:
  • *resetFunc(): Resets the device (restarts)
  • button(): Button processing, uses interrupts to improve processing efficiency
  • game_over(): Handles game over
  • update_game(): Handles game screen updates
  • display_game(): Handles main display logic of the game screen
  • drawBitmap(): Bitmap drawing
  • setup(): Setup and initialization
  • loop(): Main loop
  • interruptFunction(): ZCD interrupt call
For project code details, click “Read More” at the end.

Project Display

05

How to Port a Mini Dinosaur Game to AVR64DD32 Nano

In the code, the small dinosaur’s jump operation is triggered by either buttons or line-following sensor interrupts.
During the refresh process, trees will randomly appear on the right and continue moving left until they disappear off the screen.
Based on the duration of the game, scores are calculated and displayed at the top.
The grass in the scene will change at regular intervals.
Two different types of clouds will be displayed at the top and will continue moving to the right until they disappear.

Project Key Points

06

Display Adaptation

During the actual adaptation of the display used in this project, it was found that the ST7735 driver did not work well, and finally, the ST7789 driver was used. However, the actual display area was larger than the screen display area, so some positioning adjustments were made to ensure the display window matched the display. In fact, ST7735 and ST7789 mainly differ in resolution.

Background Music Processing

As mentioned before, due to the frequency limitations of the AVR64DD32 itself, while refreshing the SPI screen and playing background music, it inevitably leads to potential stuttering in background music playback. If there is not much information to draw on the screen, the situation is acceptable; however, when there is a lot of content, it is significantly affected. In actual processing, unnecessary screen updates should be minimized. For example, in the source code, the score is displayed every loop, but after optimization, it is only displayed when there is a change.

Character Motion Effect Processing

In the game interface, the small dinosaur’s body generally remains unchanged, but its two legs continuously alternate to create a motion effect. This is achieved by alternately drawing different bitmaps. Detailed definitions of the bitmaps related to the character can be found in the sprite.c file. When the small dinosaur jumps after a button press, the display may flicker due to refresh speed limitations.

Button Interrupt Handling

The code utilizes button interrupts for processing, which can improve efficiency. There was consideration for using a sound sensor to control the small dinosaur to jump when saying jump loudly. However, the sound sensor available only has analog output, which requires data reading through ADC, affecting the actual code’s running efficiency, so it was ultimately not adopted.

Basic Usage of ZCD

If you are developing using the Arduino IDE, there are basic examples of ZCD usage in the example code. During the process of using ZCD to play background music, it was found that it caused the PC2 button to become unresponsive. After researching the supporting code for ZCD, the following code was discovered:

PORTC.PIN2CTRL = PORT_ISC_INPUT_DISABLE_gc;

Additionally, the button line used is a two-line to three-line interface, which just happens to use PC2 and the nearby GND. Therefore, a line-following sensor was added to assist with button processing, which can trigger the action with a finger slide, and the effect is quite good.

If you find this interesting, click “Read the Original” to view the code and experience it together.

END

Hard禾 Academy

The Hard禾 team is committed to providing standardized core skill courses for electronic engineers and students in related fields, helping everyone effectively enhance their professional abilities at all stages of learning and work.

How to Port a Mini Dinosaur Game to AVR64DD32 Nano

Hard禾 Academy

Let’s explore and advance together in the field of electronics

Follow the Hard禾 public account for instant access to classes

How to Port a Mini Dinosaur Game to AVR64DD32 Nano

Click to read the original for more

Leave a Comment

×