XNA准备篇(一)

高手请直接退出本页,谢谢......呵呵

关于xbox,相信很多人都有了解。那Xbox的游戏是如何开发的呢,下面让我们一起来学习这个Xbox的游戏开发。

首先,得准备开发相关的开发工具 XNA3.0 .

安装好XNA3.0后,它会自动附加到VS2008开发工具的项目模板里。

下面我们开始GAME:

我装的VS2008开发工具。

首先,打开VS2008--->新建项目--->展开Visual C#节点--->XNA Game Studio3.0(就是这强大的家伙了)--->Windows Game(3.0){为什么要选这个呢,因为我没钱买XBOX啊.......},然后建立一个叫“MyFirstGame”的项目吧。

XNA会为你自动建立模板的。

一、看一下项目中的Program.cs文件

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            using (Game1 game = new Game1())
            {
                game.Run();
            }
        }
    }

相信写过程序的人都清楚这是什么意思,这里主要是MyFirstGame程序的入口。

二、按程序入口的提示,我们一起去看看Game1.cs文件的代码。

using System;
using System.Collections.Generic;
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace MyFirstGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

看了上面的代码,如果你头晕的话,建议退出本页面吧。

下面我对这些方法进行逐一粗讲一下。

1、首先要继承 Microsoft.Xna.Framework.Game,不要问为什么,就这么写肯定对的,就好像大家写WinForm的时候一样继续Form。里面的就是包含一下游戏的事件、函数、属性等等的东西。

2、Game1():就是构造函数,这人名称是不是很眼熟啊,对了,就是在Program.cs文件里的东西了,这里主要是初始化程序的一些必需信息。

3、Initialize():这个重载函数主要是初始化一些控件之类的。

4、LoadContent():见名知意,就是加载游戏内容的。

5、UnloadContent():在游戏窗体关闭的时候被调用。

6、Update(GameTime gameTime):程序将会不断的更新操作,主要反应一个游戏内容更新时间关系,如果你需游戏的画面或者声音不断的更新切换这里就是最近的实现地方了。

7、Draw(GameTime gameTime):用于绘制图的,他会拿update的时间参数。

下面我来画一下这里函数的关系图,搞好关系是很重要的,要是我画的图有问题,欢迎指正,大家一起来学习、提高。

关系图:

原文地址:https://www.cnblogs.com/magic_evan/p/1861931.html