C ++ code tank wars (1), Code tank wars

Source: Internet
Author: User

C ++ code tank wars (1), Code tank wars

I have a special liking for the tank war because this game was made when I first participated in a Programming Competition in college. The language I used at that time was Java. The competition made me realize the power of object-oriented, and I started to access the design model from that time. For me, the tank wars have extraordinary significance, so we must use C ++ for implementation.

Tank wars

 

We still use EasyX to create this game program in the console program. The main task of this article is to draw a white main battle tank on the screen and control its forward direction through the direction key. The effect is as follows:

 

Next we will officially start.

Layout

In this project, we encapsulate the functions related to the EasyX canvas in a Graphic class and create two files: Graphic. h and Graphic. cpp.

Graphic. h

 

# Ifndef _ GRAPHIC_H __

# Define _ GRAPHIC_H __

# Include <graphics. h>

# Define SCREEN_WIDTH 1024

# Define SCREEN_HEIGHT 768

Class Graphic

{

Public:

Static void Create;

Static void Destroy;

Static int GetScreenWidth;

Static int GetScreenHeight;

Private:

Static int m_screen_width;

Static int m_screen_height;

};

# Endif

 

Graphic. cpp

# Include "Graphic. h"

Int Graphic: m_screen_width = SCREEN_WIDTH;

Int Graphic: m_screen_height = SCREEN_HEIGHT;

Void Graphic: Create

{

Initgraph (m_screen_width, m_screen_height );

}

Void Graphic: Destroy

{

Closegraph;

}

Int Graphic: GetScreenWidth

{

Return m_screen_width;

}

Int Graphic: GetScreenHeight

{

Return m_screen_height;

}

This class uses static member variables and static member functions. Note that static member variables are initialized outside the class. This class can be used to create and destroy canvases, and other code can get the size of canvases at any time through this class.

Tank abstract class

Because our program uses EasyX to draw on the screen, various elements need to be stored in a data structure that can be traversed for easy operation, so we will use polymorphism when implementing tank code. Create an abstract class for the tank. Create the Tank. h file and add the following code:

All things that need to be referenced by tanks are defined in this file. An Enumeration type is defined here, indicating that the direction is used. Our program only takes four directions into consideration. If we need to allow the tank to have eight forward directions, we can expand the other directions here.

In the tank abstract class, we have defined two functions. We are familiar with the Display and Move functions. Many functions are used in the Star project, mainly to draw and Move ourselves on the screen.

M_dir In the attribute stores the direction of the tank. Both Display and Move need to use it.

Main battle tank

The so-called main battle tank is the tank controlled by the players. Only one of all the tanks can be controlled. This is special.

Create the MainTank. h file and write the following code:

This class inherits the Tank class and assigns initial values to each attribute during initialization. By default, the main battle tank starts in the middle of the screen and is in the upward direction. The color is white.

The shape of our main battle tank is as follows:

Let's see how to implement it. The code for creating the file MainTank. cpp is as follows:

SetDir

This is simple, that is, modifying the value of the member variable. This function can be used to change the direction of a tank.

DrawTankBody

This function draws the theme part of the tank, a square tank body and two rectangular tracks. Because the tank is driving up and down and the left and right shapes are different, a parameter is used to draw different shapes.

Display

This is the core method for drawing external calls. There are two main tasks:

  • Determine the direction of the tank, and then call DrawTankBody to draw the tank body
  • Draw a barrel Based on the travel direction
Move

Every time this function is executed, the tank moves forward the m_step length. When the screen edge is exceeded, it appears again from the other side, and the travel direction remains unchanged. Is it easy.

Keyboard event listening

Finally, the main program is ready. Create the file main. cpp and add the following code:

If you have any questions during the learning process or want to obtain learning resources, join the learning exchange group.

639368839. Let's learn C/C ++ together!

 

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.