[Index page]
[Download source code]
Dream come true XNA (1)-Hello XNA
Author: webabcd
Introduction
XNA: Starting from Hello XNA
Example
1. To facilitate the demonstration of various knowledge points in an XNA program, I wrote the following code and loaded the corresponding Demo by pressing the keyboard.
Game1.cs
/** Right-click the project and choose Properties> XNA Game Studio, you can select either HiDef standard or Reach standard * HiDef standard-Complete Set (generally used for Xbox 360 or GPU to support PCs with DirectX 10 or more) * subset of Reach Standard-HiDef, used when the video card is poor (generally used for Windows Phone phones) */using System; using System. collections. generic; using System. linq; using Microsoft. xna. framework; using Microsoft. xna. framework. audio; using Microsoft. xna. framework. content; using Microsoft. xna. framework. gamerServices; using Micr Osoft. xna. framework. graphics; using Microsoft. xna. framework. input; using Microsoft. xna. framework. media; using XNA. component. sprite; namespace XNA {// Initialize and LoadContent at startup, UnloadContent public class Game1: Microsoft. xna. framework. game {// graphics Device (graphics card) Manager. The GraphicsDeviceManager _ graphics object is used for all tasks that XNA performs in the Game window; // The Link Dictionary <Keys, string> _ demos; public Game1 () {_ graph Ics = new GraphicsDeviceManager (this); _ graphics. preferredBackBufferWidth = 1280; // The width of the game window _ graphics. preferredBackBufferHeight = 720; // The height of the game window // Content. rootDirectory-after compilation, the storage path of the Content resource is Content. rootDirectory = "Content";} protected override void Initialize () {// display the mouse pointer (default value: false) // this. isMouseVisible = true; // each Demo corresponds to a button. Press the corresponding button to load the corresponding Demo _ demos = new Dictionary <Keys, string> (); _ de Mos. add (Keys. a, "XNA. component. sprite. hello "); _ demos. add (Keys. b, "XNA. component. sprite. drawDemo "); _ demos. add (Keys. c, "XNA. component. sprite. drawString "); _ demos. add (Keys. d, "XNA. component. sprite. spriteSortModeDemo "); _ demos. add (Keys. e, "XNA. component. sprite. blendStateDemo "); _ demos. add (Keys. f, "XNA. component. sprite. animation "); _ demos. add (Keys. g, "XNA. component. sprite. animatingSprite "); _ demos. ad D (Keys. h, "XNA. component. control. keyboardControl "); _ demos. add (Keys. i, "XNA. component. control. mouseControl "); _ demos. add (Keys. j, "XNA. component. control. gamePadControl "); _ demos. add (Keys. k, "XNA. component. audio. XACT "); _ demos. add (Keys. l, "XNA. component. audio. soundEffectDemo "); _ demos. add (Keys. m, "XNA. component. audio. songDemo ");........................ base. initialize ();} protected override Void LoadContent () {} protected override void UnloadContent () {} private Keys _ prevKey = Keys. none; protected override void Update (GameTime gameTime) {IGameComponent component; // obtain the key information of the current user. Keys [] keys = Keyboard. getState (). getPressedKeys (); if (keys. length> 0 & keys [0]! = _ PrevKey) {// loads the Demo if (_ demos. containsKey (keys [0]) {component = (IGameComponent) Activator. createInstance (Type. getType (_ demos [keys [0]), this); Components. clear (); Components. add (component) ;}_ prevKey = keys [0];} // in the base. one logic in Update (gameTime) is to call the Update () method base of all IGameComponent objects in the Components set. update (gameTime);} protected override void Draw (GameTime gameTime) {GraphicsDevice. clear (Color. cornflowerBlue); base. draw (gameTime );}}}
2. Hello XNA (Press keyboard A to load this Demo)
Component/Sprite/Hello. cs
Using System; using System. collections. generic; using System. linq; using Microsoft. xna. framework; using Microsoft. xna. framework. audio; using Microsoft. xna. framework. content; using Microsoft. xna. framework. gamerServices; using Microsoft. xna. framework. graphics; using Microsoft. xna. framework. input; using Microsoft. xna. framework. media; namespace XNA. component. sprite {// Initialize first at startup, then LoadContent, UnloadContent public class Hello: Microsoft. xna. framework. drawableGameComponent {// SpriteBatch _ spriteBatch; // 2D Texture object Texture2D _ texture2D; public Hello (Game game): base (game) {}/// <summary> /// initialization before the game is running /// </summary> public override void Initialize () {base. initialize () ;}/// <summary> // load resources required for the game, such as and sound effects /// </summary> protected override void LoadContent () {// instantiate SpriteBatch _ spriteBatch = new SpriteBatch (Game. graphicsDevice);/** the XNAContent project in the solution is the Content Pipeline ), it is used to manage game resources such as music. * The Asset Name field in the attribute of each resource file is used to identify the resource Name in the content pipeline. (No file extension, the same Name cannot be used in the same folder) * In essence, the content pipeline will. jpg. png. game resource files such as mp3 are converted into an XNA internal format that is easy to use during compilation * // loads the Image/Son into the Texture2D object _ texture2D = Game. content. load <Texture2D> ("Image/Son") ;}/// <summary> // manually release the object. This method is automatically called when the game exits. // note: XNA automatically recycles garbage. // </summary> protected override void UnloadContent () {}/// <summary> /// logical computing before Draw /// </summary> /// <param name = "gameTime"> current time object of the game </param> public override void Update (GameTime gameTime) {base. update (gameTime );} /// <summary> /// draw on the game window /// </summary> /// <param name = "gameTime"> current time object of the game </ param> public override void Draw (GameTime gameTime) {// clear all objects in the Game window, and use the CornflowerBlue color as the background Game. graphicsDevice. clear (Color. cornflowerBlue); // SpriteBatch. draw ()-used to Draw an image, which should be in SpriteBatch. begin () and SpriteBatch. call _ spriteBatch between End. begin (); _ spriteBatch. draw (_ texture2D, new Vector2 (Game. window. clientBounds. width-_ texture2D. width)/2, (Game. window. clientBounds. height-_ texture2D. height)/2), Color. white); _ spriteBatch. end (); base. update (gameTime );}}}
OK
[Download source code]