XNa advanced programming: Xbox 360 and Windows 4-2

Source: Internet
Author: User
4.2 game Class

You have used the previous chapters. Game Class, but these chapters mainly refer Program Class and unit test call Run Method to start the game and how to use it Initialize , Update And Draw And does not discuss the basic design. Of course, it would be enough to master some simple games. However, as the game grows and requires more features, you may need to consider the design of the game.

Game Class provides Graphicsdevicemanager Instance and save the corresponding variables of the Content Manager. Then Program Class to create your game class instance and call its Run Method To run the game. And previously used hosting DirectX And OpenGL The difference is that now you don't have to manage your own game windows, create game loops, and process Windows Messages, etc. XNa You have already done it for you. Because of this, you can place your game in the non-window class and Windows Event The Xbox 360 Run on the platform!

You can use Window Properties to perform operations on the game window, such as setting the window title, allowing users to change the window size, calling the underlying Windows Handle for interoperability and so on. All these methods are available in The Xbox 360 The platform does not work. The Xbox 360 There is no window, no window title, and of course the window size cannot be changed. As in the previous game, you used Window. Title Attribute to set simple text to show the current level and score to the user. This is because XNa Font is not supported. To display text on the screen, you must create a bitmap font and render all the letters one by one. You will use this feature in the next several games and the tetris games in this chapter. Texturefont Class.

In addition, you can use the following method in the game constructor to set your preferred resolution. This example uses 1024 × 768 Full Screen mode:
Graphics. preferredbackbufferwidth =   1024 ;
Graphics. preferredbackbufferheight =   768 ;
Graphics. isfullscreen =   True ;

Of course, there is no guarantee that the game will run correctly at the configured resolution. For example 1024 × 768 Resolution is set on the System 1600 × 1200 The maximum resolution available for the game.

As you know, the game must call each frame during rendering. Update And Draw Method, But how do you induplicate ate new game components
Without overloading the game class itself?
Let's take a look. Tetris The relationship between the game class and Component Design ( 4-1 ).


Figure 4-1


We can see that there are three game categories, instead of one as in the previous game, to make the main game category shorter. Basegame Class stores the Image Device Manager and content manager, as well as the width and height of the resolution currently used by the game. Update And Draw Method to process new Input , Sound And Texturefont In this way, you do not need to update them in the main game class. Tetrisgame Class is used to load all images from the content pipeline and initialize all Sprites And game components, which will be introduced later.

Finally, Testgame Class inherited from Tetrisgame Class to access all Textures , Sprites And game components, which are only used for unit testing in debugging mode. Testgame The functions of the class are similar to those of the previous chapters, but here it is separated from the main class of the game and organized in a better way. Tetrisgame Class uses several unit tests to ensure that each part of the game works as expected.

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.