Development of games using Windows GDI (4)

Source: Internet
Author: User
Tags map class

Article 4


The previous framework seems to have some meaning. However, no gaming process is involved. So this article is about to start writing the real game logic.
You only need to add the primary logic to the code you wrote last time.
Next we will analyze the logic of tank games. First, the tank will certainly be galloping in the sand field, so we will build this sand field next.
We already have a map file edited by the map editor, so we only need to display the map according to the terrain file, which is relatively easier than the map editor.
However, we need to deal with different terrain.
Through observation, we can find that this battlefield can be divided into the following layers, from the bottom up is the bottom layer of the ground, then the obstacle, then you can draw tanks, then draw trees.
For ease of management, we use a map class for ground drawing and management. The map class loads map data at the same time.
Let's first draw the ground, void cgamemap: drawground (HDC) {cmemdc bmp dc (HDC); bmp dc. SelectObject (m_htilebmp );
For (INT ny = 0; ny <c_map_h; ny ++) for (INT Nx = 0; NX <c_map_w; NX ++) {bmp dc. bitblt (HDC, nx * c_tile_w, Ny * c_tile_h, c_tile_w, c_tile_h, 0, 0 );}}

Because we have a brick map, we only need to paste the first brick (ground) directly to the screen. We have pasted c_map_h * c_map_w in two cycles: horizontal and vertical. Here we have 25*18 = 450.

After the ground is pasted, it can be covered on the ground, such as brick walls, steel walls, snow and water. Then draw all the tanks. Then paste the forest. In this way, tanks can run on water and snow. The effect of moving through the woods. The wall is the area where tanks are not allowed to enter.
Finally, you can draw GUI elements like scores at the top. Our game rendering function is as follows: // draw the ground m_ogamemap.drawmap (memdc. getdc (); // draw each entity unit drawentities (memdc. getdc ());
M_ogamemap.drawforest (memdc. getdc ());
// Draw the interface drawgui (memdc. getdc ());
Remember, the forest painting needs to be transparent so that the tank can have a feeling of hiding it.

In drawentities, we plot various combat units, explosion scenes, and shuttle bullets.
We also judge the logic of the game in every cycle of the game.
The game logic mainly involves the movement of various entities, as well as the bullet judgment, as well as the collision judgment. We put these judgments into the gamelogic function. The logic judgment of the game is the brain of the program. Let's move it to the next article.
For obstacle display, we can read the map file to determine the obstacle to be displayed at each location. We put all the obstacles in one image and select different images based on coordinates.
Next we will detail how each object is drawn. Entities include enemy tanks, our tanks, bullets, and explosion scenes. These are all movable units. We use a base class to describe them.
Class cspirit.
However, the moving speed of each unit is not the same. We can use a moving step to differentiate it. Then add the location parameter and size parameter, and the other is the current orientation. Different subclasses are used for different entities.
This is the benefit of Object-oriented Inheritance. We only need to write the mobile logic once. Other units only need to inherit this base class.


The rendering function is very simple, that is, to draw the image directly to the Target DC based on the current position information and the large and small state of spirit. However, the background color must be used for Transparent Display so that the image can be integrated, otherwise, you will see black blocks moving, which is not natural.
Spirit selects different images based on the direction. We place the images of tanks in each direction in a bitmap, and select different source coordinates based on the direction to display the corresponding images to the screen.
At the same time, we can set two images to show the effects of rolling the tank wheels alternately. The current screen is determined by the m_ncurpic variable. The display function is as follows: int NPIC = (m_ndir = enum_stop )? 0: m_ndir; memdc. transparentblt (HDC, m_nx, m_ny, m_nwidth, m_nheight, m_ncurpic * m_nwidth, NPIC * m_nheight, RGB (0xff, 0xff, 0xff ));
We only need to change the location of the tank in the game logic m_nx, m_ny can produce the effect of tank movement. At the same time, it makes sense to move the tank based on the obstacle judgment, without moving across the wall.

How do I feel like a game screen through several layers of painting? However, no complicated game logic judgment is added here, and there are still bullets and explosion effects that have not yet been added. Enemy tanks are randomly moved on the screen. You can use the arrow keys to move your tanks, it's a bit gaming. The next section describes the game logic. Current game code download http://download.csdn.net/source/1782392
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.