[QT] It took two months to use QT to realize Super Mario. Who said QT can't write games? [Source code attached]

[Written in front with some emotion] 

        The project for the C++ course lasted more than two months and more than 4,000 lines of code were written. Finally, QT was used to implement Super Mario from the bottom. Due to my limited ability, I referred to the source code of a great master ( Master Portal ), but he wrote it in VS, and I wrote it in QT. There is still a big gap in some aspects. I was very desperate at the beginning. I rewrote the project 5 or 6 times, so much so that I once doubted whether I should write a project like this (this is the importance of planning at the beginning), but when I finished writing the collision box with reference to the source code , I knew I could make this project. The following code is all my own improvisation. 

        Compared with other students who are also good at using the platform, the time and amount of code I spent should be two to three times as much as them (a great guy in the dormitory is very good at writing on the platform, but I think he only has more than 1,000 OK). What's the difficulty? The first is to build the collision box. This is the top priority. When you handle the collision box, your project will be half completed. The second is the processing of pictures. This project refers to Nintendo’s approach and adopts the method of dismantling pictures and putting them into puzzles. As shown below, the collection of Mario pictures is nothing more than using a "screenshot" method to cut out the Marios used one by one, and then assemble them into the appropriate positions. I feel that this step takes up 80% of my time, because for This picture is not very detailed, so I can only try it step by step, coordinate by coordinate. It is like a very simple interface with very simple functions. However, because the picture is very troublesome to assemble, it took more than two hours to complete the 100 lines. hours, and so on. I don’t want to write about the difficult part anymore. If I wanted to write about it, it would be thousands of words, but I think it’s meaningless. Writing reports is the most annoying thing (laughs).

        Although it takes a long time to write a game using the bottom layer, the amount of code is large, and QT is used, which is very inconvenient. But through the development of this game, I have completely gone through the game development process and gained a deeper understanding of game development (a sentence from the heart, but it feels like a cliche). Those who use platforms and even engines The students who write will not realize the vastness of the bottom ( hehe ).

[Written in front, project defects] 

  • Collision box: The collision box is not handled very well, so there is a problem with collision, so Mario is set to be invincible, but the death animation is also written. If you don’t believe me, press S.
  • Process processing: I didn’t even write the interface ( ) at first , but I later added it. But there is no restart after death. This seems to be quite simple, but it is not written. The teacher didn't say anything during the acceptance check. After all, I was the only one who wrote the bottom layer (handsome).
  • To save trouble at that time, most paths used absolute paths, so if you run the project, you need to replace the path with your own, as shown below.

【Project Operation】

        I probably don’t even need to write it. ↓←→Control squatting and movement respectively. X jump Z fire bullets 

        Cool operation: S Death T upgrade

【Project show】

I can’t play the video, so I’ll post some GIFs.

 

 

 

 

 [Summary of QT knowledge]

Written for myself, I also encountered some QT knowledge issues in this project. Let me sort them out.

1. Forced type conversion (parent rotor)

TortoiseKing *M = dynamic_cast<TortoiseKing*>(b);

 b is an instance of the parent class, and the TortoiseKing class is a subclass

2. Calculate timestamp

QDateTime::currentMSecsSinceEpoch();//13 digits, accurate to milliseconds
QDateTime::currentSecsSinceEpoch();//10 digits, accurate to seconds
long long int Level::calcTime()
{
//    计算当前的时间戳
    return QDateTime::currentMSecsSinceEpoch();
}

3. Image flipping

pix = horFilp((pix.toImage());
QPixmap Level::horFilp(QImage image)
{
    image = image.mirrored(true, false);
    QPixmap pix1 =   QPixmap::fromImage(image);
    return pix1;

}

 As above, flip the left-facing one to the right-facing one and vice versa.

4. Picture interception

marioPix = QPixmap(s);
marioPix = marioPix.copy(x + j * mx, y + i * ny, width, heigh);
marioPix = marioPix.scaled(width*2.5,heigh*2.5);

 Analysis : Assign the picture s to marioPix. The function of copy (a, b, c, d) is to intercept the picture from the a and b positions of marioPix, and intercept the picture of c*d size. scaled(a,b), the width and height are enlarged by a and b times respectively

It’s been more than a month, and there are still some others that I won’t sort out.

[Write on the back]

QT is really not suitable for making games... The drawing events are not easy to handle, so I only wrote one drawing event, but the difficulty lies in the setting of global variables. However, the underlying implementation is quite impressive for students, so the course results are pretty good. Top three!

【Source code】

Link: https://pan.baidu.com/s/1fKZbTg6HQUNZAInXu-cFLw?pwd=qtnb 
Extraction code: qtnb

Guess you like

Origin blog.csdn.net/m0_59792745/article/details/125670499