Game components-game components

Source: Internet
Author: User
Game Components

The tetrisgame class also has all game components in the components attribute, which inherits from the game class. You can add any subclass inherited from the gamecomponent class to this list, and this subclass will be automatically called when your game is started and updated. When you draw a game, the subclass of the gamecomponent class will not be called because the gamecomponent class does not have the draw method. However, you can implement your own painting method, or just use the drawablegamecomponent class, which has the draw method. XNa has no direct draw method support for game components. You must call it in person to ensure that all components are called in the correct order. For this and other reasons (forcing you to use this model with almost no benefit makes unit testing more difficult, and your own game classes may be more effective or special ), you will not use many game components after this book. This is usually a good idea, but you can live without it, because in any case you have to create your own game components and you have to call the draw method for them. It is not very meaningful to use the update methods only.

As I mentioned in chapter 1, the basic idea is to allow users to collaborate and share their game components so that others can use part of their game engine.

For example, a frame computing component or even a fully developed 3D terrain rendering engine may be executed as a game component, but simply because someone does not use a game component does not mean it is difficult to copy it. For example, you have a complex game component, such as a terrain rendering module, which may also involve some other classes and use its own rendering engine, if you just copy a file, this engine will not work beyond your engine. Insert externalCodeYou often need to rebuild several times until it can be used in your own engine. In Beta 1 of xNa framework, xNa game studio Express has a graphical designer for game components. You can drag components to your game class or even other components, add features to your game without writing a line of code. This feature is very complex and has a bunch of bugs. It cannot run on Xbox 360 and is discarded in Beta 2 release of xNa framework.

It is not certain whether game components are used.ProgramThe lack of a designer and calling those draw methods in person is not a hassle. Many game components may be used and it is useful to know all their foundations. In the case of Tetris, pay attention to the following components:

    • The grid itself has color bricks and the bricks currently falling

    • The scoreboard has the current level, score, highest record, and number of destroyed rows.

    • The model box of the next brick in the game

    • Simpler things, such as frame counters, processing input, etc.

I decided to implement the grid and the next brick feature of Tetris as a game component. All the code is implemented in this simple way, just a few new classes for them. If you will reuse the scoreboard, for example, you may keep it on the game components, but I don't consider any other games. I like to use the scoreboard to write one.

Take a closer look at the game class and the components to be added to it (4-2 ).


Figure 4-2

The gray arrows indicate that these methods are automatically called because the tetrisgrid and nextblock classes are added to the component list of the game class. Call the draw method in tetrisgrid in tetrisgame. Draw, and the latter calls the nextblock. Draw method. Tetrisgame only has an instance of the tetrisgrid class. Nextblock instances are only used within the tetrisgrid class.

You can see that the use of game components for these three categories forces you to consider the call sequence, and it makes your game more organized just because you didn't put everything into a single category. This is a good thing. If you are an experienced programmer and can do everything on your own, it may be a good idea for beginners to consider game componentization in xNa in advance.

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.