[STM32 learn to make STM32 game console]

In 2020, the new recruits of the Association for Science and Technology need to give training questions to let the freshmen know about the microcontroller, so I am planning to write a game machine problem here, let the freshmen use the microcontroller to implement a game machine. There are 3 games to choose from, namely: Greedy Snakes, Hamsters and Sokobans are aimed at 0-based new students, so they don't make too many requirements on the indicators, as long as they achieve results.
I am responsible for snake eating and sokoban

Reference blog
(Principle and Application of Single Chip Microcomputer) Game Design of Large LCD Screen (Sokoban)
Snake game based on stm32, 0.96 inch OLED (detailed source code comment)

Sokoban

Sokoban, as the name suggests, is to control the people in the game to push all the boxes to the designated position even if the level is cleared. This game is quite common on previous mobile phones. This topic is mainly to ask the freshmen to find some maps of Sokoban by themselves, and display the elements in the map on the selected screen, and then use the buttons/joysticks to control the movement of the people in the game, and complete the levels and the number of levels. The more, the higher the score, 5 caps.

Basic design ideas

OLED screen

The biggest difficulty in making a game console with a single chip microcomputer is the screen. You have to use the single chip microcomputer to control the various elements in the topic display map, such as the box of the Sokoban box, the wall, the characters, etc., so choose the screen according to the game you want to achieve. , And then it’s very important to be familiar with my own screen.
When I implement the solution myself, I choose the commonly used OLED screen. OLED screen is very convenient for displaying some simple information. It is recommended that you must learn how to use it. In many cases, some designs An OLED screen is sufficient.

OLED control

The OLED bought on a certain treasure is generally SSD1306, and the communication method is generally SPI or IIC, whichever is fine. When I use the IO port to simulate IIC and SPI, the SPI speed will be faster, and there is also parallel port control. Way, this is that the communication is relatively convenient, but it takes up less IO ports. If you want to play more advanced, you can buy a punctual atom OLED screen. Although it is a bit more expensive, it has better performance and various control methods.
Regarding how the single-chip microcomputer controls the OLED, I will not go into details here. The information on the Internet is very complete and the explanation is very clear. The main process is as follows.
MCU control OLED process

Map size design

The resolution of a common OLED screen is 128*64, and the resolution can be understood as dots. Each dot is a small LED. Write 1 to this small LED, and it will light up, and write 0 to go out. By designing these points one by one, you can display characters, Chinese characters and other commonly used information. Those familiar with OLED control will know that OLED writes 8 points (one column) at a time, so it is more convenient to design the elements of the map to be a multiple of 8, so the design of the screen display is as follows:
Screen design
The following is the actual screen displayed on the OLED ( Take the first level as an example):

Among them, the elements in the map can draw 8*8 pixel patterns by themselves, or you can directly use the scheme given by the author in the reference link. I used this pattern directly:
Map feature display scheme
Then how to display these elements on the OLED screen Up? The easiest way is to use computer drawing software to model the picture. This method will not be detailed here, just search and learn it on the Internet (you can go to the tutorial of Atom or Du Yang's studio)
. A few pictures can’t be saved directly with drawing software, so it’s recommended to draw pictures directly on the mold taking software. Anyway, it’s only a 64-pixel picture, which is pretty good. The
following are some pictures I drew for reference only:
character
Box arrived
box
destination
In short, get this The data of a few dots can be displayed on the OLED, so there is no problem with the interface

Map display

Since we design each map element to be 8*8, it is very convenient to display on the OLED. You can refer to other people's maps or design your own maps. You can choose a few from the reference article I posted:
Map 1
Map 2
Map 3
Display on OLED:
The map is displayed on the OLED
The map is displayed on the OLED

Basic information display

On the right side of the screen we want to display some basic information, such as the current number of levels, the number of steps that have been taken, and so on. You can freely design it here as a performance indicator, such as making some information that already has several boxes in place.

*Welcome screen display (not required)

Before entering the game, you can display a welcome interface to show what game it is, and there is a prompt to start the game. This can also be used as a play indicator, which can be freely designed by yourself

The above is what we have to do on the screen, and the most important thing. After completing these, the rest is very simple.

control

In this question, you need something to control the characters in the game up, down, left, and right. Two solutions are given here, but the solutions are not limited. You can use other solutions, such as making a control on your mobile phone. Interface, then let the mobile phone and the microcontroller communicate (using the Bluetooth module), etc.

keyboard

The simplest keyboard is composed of light touch buttons. Since the new students have just started electronic design and are not very familiar with some basic operations, students who choose the keyboard must solder a keyboard by themselves, such as this form:
Keyboard reverse
Keyboard front
I solder it casually, so The soldering is particularly ugly. Don’t spray, if you do it seriously, it’s definitely better soldering than me.
In short, just bring out 4 touch buttons to control the four directions up, down, left, and right, and you can get the keyboard, so many games don’t use it. If you do, just do it on it, such as snake-eating

Joystick module

The generally used joystick module is as shown in the figure below. It is easy to buy on a treasure:
Joystick module
the specific usage of this module is not detailed here, and it is left to the new students to explore by themselves. Using this module can make things look more playful Sense, and can add points

The above is the control plan

MCU used

There are no restrictions on the MCU used. I implemented my own scheme on STM32. It can also be done with 51, because some of the materials I refer to are all made with 51.
If it is the kind of cute new who has never known MCU before , It is recommended to start from 51, not in a hurry. Learning 51 will give you a certain understanding of microcontrollers and how to program microcontrollers. In this way, learning STM32 will be easier to understand, but there is no need to learn 51. It is not necessary to learn from Guo Tianxiang’s book. In fact, it’s almost enough to learn serial communication, and then you can try to transfer to STM32

Finished product display

Below is the finished product I made myself, to show the effect a little bit
After playing for a while
Push the box up one space
Show clearance interface

Greedy snake

After the above study in Sokoban, in fact, the production of Snake is relatively simple, mainly depends on how your c language program is written, because the OLED, keyboard and other controls are already familiar, and only some are given below. Design ideas, other
students who are qualified can try to implement a greedy snake in C language on their computer, refer to the code on the Internet, then it is better to write on the MCU, you have to consider The only thing is how to combine some of the things you use in the C language with your hardware, such as OLED

Map and snake body design

Everyone knows that there are many kinds of snake-eating maps that have been played in Nokia before, so you must first think about how to design the map before implementing the game. I used the simplest map, that is, the one
In the form of a large rectangular frame, in addition to the map, you need to design the snake body. Everyone knows that a common OLED screen has a resolution of only 128 64. Therefore, if your snake body is designed with a 1 to 1, it will be very It’s hard to see, so students who use OLED should design the snake body as 2*2 or other.
In the reference article provided at the top of this article, this is the effect. You can refer to it:
Greedy snake
because I just verify the plan , And my program is transplanted from the C language program I wrote before, here I choose the simplest solution

Other options

Some students may be novices who have not been in touch with single-chip microcomputers before and cannot master OLED proficiently. Here I will give a simpler solution: the use of dot
matrix is ​​similar to the digital tube, which is convenient to control and easy to distinguish. Simple operation
If you want to use dot matrix, you can refer to the following video:
[51 MCU] Homemade Snake Game Machine
MCU Driven Dot Matrix Screen Snake Game 8051 The
above two works are made with dot matrix, relatively For OLED, this solution will be simpler. The only thing to pay attention to is the use of dot matrix. This thing has pitfalls in use. I will talk about it when I talk about it.

Finished product display

Snake eating is much simpler than Sokoban. I won’t go into details about the program here. If you have learned a little bit of C language and refer to some codes on the Internet, you should be able to get it out. Again, pay attention to the coordination with the hardware. Whether you are using OLED or dot matrix, the
following is the finished product I made by myself, for reference only, don’t spray :
game interface
Welcome Screen

Guess you like

Origin blog.csdn.net/zyhdaun/article/details/109086429