C + + Learning tour Three I have a date with Super Mary. _c language

Source: Internet
Author: User
Tags arrays garbage collection

First of all, I say to C + + the most intuitive feeling it! familiar with the. NET Smart tips, beginning to find C + + no hint at all. Then google a bit, download a visual assist this plug-in, than the VS automatic hint much better. Then, it's customary to write all the declarations and method implementations in the same file in. Net. But C + + is not the case. He is a statement in the header file, implemented in the source file, to say the truth, at the beginning and how to get used to. Then gradually became accustomed to it.  Then, write C + + file is really a hell of a pain, he is no more than. NET, Microsoft has been better than you, in C + +, a lot of things need to write their own. First, a destructor that needs to release its own resources. and. NET has a GC to automatically garbage collection, the release of resources does not matter to you. No way, only their own release. Be a good programmer to follow the rules. This is my most intuitive experience of C + +.
Speak to the right roll, and talk about this Super Mario game. First of all, I am the class structure of the game classification, if there is no place, I implore you to correct me.

From the hierarchical structure, it is divided into these layers ① image layer, ② logic layer, ③ structure and table.

Image layer includes ① image base class Mybitmap,② game background Mybkskyàmybitmap,③ game picture Myaniobjàmybitmap,④ Magic Attack Myanimagicàmybitma.
The logic layer includes ① game logic gamemap,② clock handling Myclock,③ font processing Myfont,④ tracking print Filereport,⑥ player control.
The structure and table include ① ELF structure Role,② goods structure Mapobject,③ map Information table MapInfo.
The structure of each class is so like, is a mule or Mara out to sneak. Let's take a look down.
The structure of the image layer is so simple, the logic layer only needs to determine "which image, which frame" These two parameters, you can draw all the pictures on the screen.
Tell me about the base class of a picture. The architecture of his source code is this way.
Today, first of all, the most basic image class Mybitmap:
List of member function features:

Copy Code code as follows:

Function initializes the image based on a bitmap file
parameter of the application instance handle resource ID landscape bitmap number of vertical bitmaps
void Init (hinstance hinstance,int iresource,int row,int col);
Feature setting Environment information
The destination DC (the DC to draw the image), the temporary DC, the height of the area to draw
void Setdevice (HDC hdest,hdc hsrc,int wwin,int Hwin);--Damn, paint in C + + requires a HDC, the device context needs to be a Windows that contains information about the drawing properties of a device, such as a monitor or printer Data. All drawing calls are made through device context objects that encapsulate the Windows APIs used to draw lines, shapes, and text. Device context allows device-independent drawing in Windows. The device context can be used to draw to a screen, printer, or metafile.. NET does not need this context object, he has a. NETFramework don't need this fart.
Feature Set Picture location 1
Parameter setting method Horizontal ordinate
void SetPos (int istyle,int x,int y);
Feature picture Display
How to display the parameters of a picture
void Draw (DWORD dwrop);
Feature Picture Zoom display
Scaling in the vertical direction of the parameter
void Stretch (int x,int y);
Feature Picture Zoom display
In-direction scaling scaling Image ID (portrait number)
void Stretch (int x,int y,int ID);
Feature displays pictures at specified locations
Join Horizontal Ordinate
void Show (int x,int y);
function to center horizontally show pictures
Enter the parameter ordinate
void showcenter (int y);
feature to tile a picture in one area
Coordinates picture ID (landscape number) of the upper-right border of the entry parameter
void Showloop (int left,int top,int right,int bottom,int iframe);
Feature irregular picture display
Enter parameter horizontal ordinate picture ID (landscape number)
void Shownoback (int x,int y,int iFrame);
Feature irregular picture tile horizontally
Horizontal ordinate picture ID (landscape number) tile
void Shownobackloop (int x,int y,int iframe,int inum);
Animation playback
function to automatically play all the frames of the picture, the function is not implemented, but will definitely use:
Enter a parameter without
void Showani ();
function to set animation coordinates
Join Horizontal Ordinate
void Setani (int x,int y);
Member data
Trace Print Class
Filereport F;
Image handle
Hbitmap hBm;
According to the ranks of the average divided into several
int inum;
int jnum;
Split by row and column, the width of each picture (obviously the size of each picture is consistent, after derivation, the width here is no use meaning)
int width;
int height;
Wide screen height
int screenwidth;
int screenheight;
DC to draw a picture
HDC hdcdest;
Temporary DC to select a picture
HDC hdcsrc;
Current position
int xpos;
int ypos;
is in animation playback (feature not implemented)
int Istartani;

Some of the functions and variables of this base class, in this game is not used, is the previous several games retained, so it looks a bit messy. The main image feature of this game is done by its derived classes. Because the base class encapsulates physical layer information (DCs and handles), it is easier to write derived classes, Can make me focus on the logical meaning.
The function implementation of the base class is simple, mainly the following points:
1. Picture initialization:
Copy Code code as follows:

According to the program instance handle, the resource ID of the bitmap file, import the bitmap, get the bitmap handle
Hbm=loadbitmap (Hinstance,makeintresource (Iresource));
Get information about the bitmap file
GetObject (Hbm,sizeof (BITMAP), &BM);
Calculates the width and height of each picture according to the number of pictures in the vertical direction (for Super Mary, the wide-high information is handled by the derived class)
Width=bm.bmwidth/inum;
Height=bm.bmheight/jnum;

Let's talk about a game background class Mybksky
Class Description: This is a class that deals specifically with the game background. In the horizontal version of the game or shooting game, there is a background screen, such as mountains, Sky, clouds, stars and so on. These pictures are generally only 1 to twice times the width of the screen, and then move like a scroll, even into a piece, feeling like a long picture. This class is specifically designed to deal with this background. In the Super Mary enhanced version, the main checkpoint is 3, each with a background picture, from the water pipe in, there are two, with a full black picture. A total of four pictures. These four graphs are of the same size and are arranged vertically in a bitmap file. Mybksky this class, derived from Mybitmap. Because the background picture only needs to complete the circular movement effect, only need to implement one function, does not need to care about any other question (for example handle, DC). The coding is simple and reflects the object-oriented benefits again.
The principle of implementation:
How do you keep a picture moving like a scroll? Quite simply, suppose you have a vertical split line that divides the picture into two parts. Show the right section first, then the left part to the end of the picture. Move the split line to the right, and the picture will be displayed in a loop.
List of member function features:
Copy Code code as follows:

Class Mybksky:public Mybitmap
{
Public
Mybksky ();
~mybksky ();--destructors, how about C + +, you have the resources to release yourself. We. NET has a Terminator function that also has a taste for destructors, but does not necessarily release resources yourself.
Show
Feature displays a background.
Enter a parameter without
void Drawroll (); Loop Fill Space
Feature displays a background and scales the picture
Scaling in the vertical direction of the parameter
void Drawrollstretch (int x,int y);
feature specifies that a background is displayed and the image is scaled to use this function in the game
In-direction zoom scale background picture ID (portrait number)
void Drawrollstretch (int x,int y,int ID)--operation of Middle horizontal axis
Feature Set Picture location
Enter the new horizontal ordinate
void MoveTo (int x,int y);
function cycle Move Split Line
The distance that the incoming parameter split line moves
void Moveroll (int x);
Data
Split Line Horizontal axis
int xseparate;
};

Take a look at the picture display class Myaniobj
Class Description: This class is responsible for the display of pictures in the game. Menu background, customs clearance and game end of the hint picture, processed by Mybitmap (static picture of the same size). The game background is handled by Mybksky. The rest of the pictures, that is, all the pictures in the game process, are myaniobj processing.
Technical principle: The game picture size is inconsistent, specific in the Super Mary, can be divided into two categories: rectangular pictures and irregular pictures. In the bitmap file, each picture is arranged vertically, and the frames are arranged horizontally. Use two arrays to store the width and height of each picture. To facilitate display of a picture, use an array to store the ordinate of each picture (the position in the upper-left corner of the image file). When used, the logical layer specifies which frame of "which picture" is displayed in the "where". So the display function of the picture is realized.
List of member function features:
Copy Code code as follows:

Class Myaniobj:public Mybitmap
{
Public
Myaniobj ();
~myaniobj ();
Init list
Function initialization width array height Array Ordinate array whether there is a black-and-white chart
Argument width array address height array address picture number whether there are black-and-white charts (0 no, 1 have)
(Picture ordinate information is computed from the function)
void initanilist (int *pw,int *ph,int inum,int ismask);
Feature initializes some special bitmaps, such as the size of each picture, or some other pattern.
Parameter 1 parameter 2 of the entry parameter initialization method
(for future extensions, the goal is to eliminate the problem of wide-high arrays)
void initanilist (int style,int a,int b);
Show
Feature display picture (irregular picture)
Enter the horizontal ordinate (position to display) Picture ID (portrait number), picture frame (horizontal number)
void DrawItem (int x,int y,int id,int iframe);
Feature display picture (rectangular picture)
Enter the horizontal ordinate (position to display) Picture ID (portrait number), picture frame (horizontal number)
void Drawitemnomask (int x,int y,int id,int iframe);
feature specifies the width, showing part of the picture (rectangular picture)
Enter the horizontal ordinate (position to display) Picture ID (Portrait number), display width picture frame (horizontal number)
void drawitemnomaskwidth (int x,int y,int id,int w,int iframe);
function to play an animation that loops through the frames
The ID of the picture in the horizontal ordinate (position to display) (portrait number)
void Playitem (int x,int y,int ID);
Width array supports up to 20 pictures
int wlist[20];
Height array supports up to 20 pictures
int hlist[20];
Ordinate arrays support up to 20 pictures
int ylist[20];
The current frame when the animation is played
int iframeplay;
};

Take a look at the Magic attack class Myanimagic
Class Description: Players have two types of attack: ordinary Attack (bullets), Magic Attack (Whirlwind). This class is specifically designed to deal with cyclones. My initial idea was to use special BitBlt methods to create effects, such as or, with, XOR, or. Tried a few times, all failed. Finally, you can only use the old method of "first and last". This class can be viewed as a simplified version of Myaniobj, supporting only the display of irregular pictures.
List of member function features:
Copy Code code as follows:

Class Myanimagic:public Mybitmap
{
Public
Myanimagic ();
~myanimagic ();
Init list
Function initialization width array height array ordinate array (must have Black-and-white chart)
Argument width array address height array address picture number
(Picture ordinate information is computed from the function)
void initanilist (int *pw,int *ph,int inum);
Feature set DC
The incoming parameter displays the DC temporary DC (for the picture handle selection) temporary DC (for effect implementation)
void Setdevice (HDC hdest,hdc hsrc,hdc htemp);
Show
Feature displays a frame of a picture
Enter parameter horizontal ordinate (display position) Picture ID (Portrait number) frame (horizontal number)
void DrawItem (int x,int y,int id,int iframe);
Array of widths
int wlist[20];
Array of heights
int hlist[20];
Ordinate arrays
int ylist[20];
Temporary DC for special effects, function not implemented L
HDC hdctemp;
};

This is some of my architecture

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.