It is also possible to use C language to design the game of greedy snake in single chip microcomputer

Publisher:huanguuLatest update time:2015-07-03 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
In the MCU, I used to see that the seniors used assembly or FPGA language to write Snake. Then I was thinking, C language is the simplest language. Why not write it in C language? First of all, we must know. When designing MCU games. We must know the concept of game design. For most MCU + LCD game designs, the basic front and back methods are adopted, that is, one interrupt and one loop. The LCD part is basically designed in a fixed dot matrix form. What is a fixed dot matrix? ? First of all, it is clear that the game we designed is not Warcraft or CS, but a black and white fixed dot matrix game, such as the common and famous games Snake or Tetris. Each of their dots is fixed in advance, and it is a relatively large dot. This kind of special game machine glass is molded by the manufacturer, with fixed COM and SEG lines, and then connected to a special MCU, such as the commonly used 6502 instruction set MCU, commonly used stc and at series, their information can be checked at www.51hei.com . For amateur games, we usually use a 128*64 LCD to display. So when we design, we should first divide the 128*64 LCD into blocks, that is, divide it into fixed dot matrix. The basic dot matrix of LCD is 128*64, which is ¥##¥. In short, it is a lot of dots, but we don't have to calculate so many dots, unless the game you make is very interesting. For example, if you only use the 64*64 area on the left to make a greedy snake, then how much space do you plan to use for your greedy snake? If it is 8*8 dots, then each dot is 64/8, 64/8, which is 8*8 basic dot matrix. However, if you want to have more fun, of course you need to have a 16*16 dot activity space, so each fixed dot matrix will occupy 4*4 basic dot matrix. Please note that these 4*4 things are 16*16 on the 64*64 LCD, and each one must be used for independent calculation.

 The second is the clock: This is actually the speed of the game. For general retarded game machines, it also represents the difficulty. Objects move once when each clock arrives. For example, Tetris moves down one level at a time. Racing games move forward one step at a time. Generally, the time of this type of clock is between 0.X seconds and 1 second. Objects move at a regular and uniform speed, which makes people feel that they are moving continuously.

Third movement: Here, I will first introduce two common rules for the movement of objects in retarded game machines: soft body transmission and rigid body transmission. The representative work of rigid body transmission is Tetris. The so-called rigid body is a hard thing that does not rotate much when moving (note that Tetris can rotate, but in fact it does not rotate through an algorithm, it is purely extracted in the form of an array, that is, a block is made into a lattice structure with 4 modes, which is actually 4 directions, haha). For the transmission of a rigid body, a fixed lattice moves in one direction (most likely input by the user) at the arrival of each clock. If expressed in coordinates, all the basic lattices of the object move one unit in one direction (X or Y) at the same time. The representative work of soft body transmission
is the greedy snake. When the greedy snake runs, it is not the whole snake that moves in one direction (haha, the snake's body is stiff), but at the arrival of each clock, the object is driven by the energy head (such as the snake head), and the direction of each point propagates to the next point, and then it moves one step in the new direction. After moving, the next point gets the direction of the previous point and
moves one step in the same way, so it will immediately fill the place of the previous point, and so on. It seems that it is not said. It doesn't matter if you don't understand it, because the actual algorithm can be simplified (only fools will move one point at a time). In fact, when designing the greedy snake, you only need to remove the dot matrix of the snake's tail and then put a dot matrix in the new direction of the snake's head. During this period, you need to record the position of the fixed dot matrix of each snake body, and refresh the position of each point after each movement moment.

The fourth display interface: What we usually use is a dot-matrix LCD, which is a large area of ​​dots, 128*64, 132*64, 240*128, etc. These are also called strip screens, which means writing one line at a time - 8 dots (some also provide the function of writing one dot, but it is expensive, at least I don't have it). So what if you only want to write one dot? Then you have to read out the strip where the dot is located first, and then put it back on the LCD after AND, OR operations. At this time, it involves a problem of reading LCD. Some LCDs provide the reading function, and they remember what you have written on it very clearly, but some cheap ones don't work. So what should we do? It doesn't matter. You extract a space in the memory and virtualize an LCD. Every time you write on the real LCD, you also write to which virtual LCD in the memory. Then when you want to read the value of the LCD, you actually read the data on the virtual LD, and then write it to the LCD again after AND or OR. Remember to write it to the virtual LCD. You can call this buffer video memory,

Fifth. Game flow: The snake moves in a fixed direction. When each movement clock arrives, it needs to do the following: 1. Determine the food sign. If there is no food, place one. When placing it, it must not overlap with the snake body. 2. Get the user's key value. The snake takes a step and determines whether it is killed. If not, it determines whether it has eaten. If not, it waits for the next movement clock. If it has eaten, it will increase a little. Set a sign that there is no food. Then wait for the next moment to come. Haha, in fact, the program is so simple. Only the LCD part and the key part of the basic design are related to the single-chip microcomputer. The rest are program thinking and algorithms. For rookies, the difficulty lies in thinking, not the single-chip microcomputer. The source code of this program can be downloaded from  http://www.51hei.com/bbs/dpj-18901-1.html  .

The simulation results are as follows:

 


 

Keywords:MCU Reference address:It is also possible to use C language to design the game of greedy snake in single chip microcomputer

Previous article:STC89C52 MCU receiving (RX) C language program based on NRF24L01 wireless module
Next article:Keep Running: Light Cube on a Breadboard

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号