XNA开发实用教程——游戏最基本代码

来源:互联网 发布:amd怎么优化显卡性能 编辑:程序博客网 时间:2024/05/27 20:52

 

XNA开发实用教程
三峡大学土木水电学院肖泽云
本教程的主要目的是让你看完后,真正体会一下什么是XNA?XNA中主要包括哪些部分?相信你自己,在看完整个教程后,你也能设计自己的三维场景!祝你成功!
XNA是基于DirectX3D游戏开发环境。XNA Game Studio Express 中将包含以 .NET Framework 2.0 为基础、并加入游戏应用所需之函式库所构成的 XNA Framework;由一系列工具所构成、让开发者能以更简易的方式将 3D 内容整合到游戏中的 XNA Framework Content Pipeline

一、游戏最基本代码
每个XNA游戏都从这里出发,其代码如下:
[Game1.cs]
#region Using Statements  //引用
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.Net;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace BasicWindowsGame
{
    public class Game1 : Microsoft.Xna.Framework.Game //继承Game类
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()  //初始化
        {
            base.Initialize();
        }
        protected override void LoadContent()  //导入目录,每次游戏启动时都会启动
        {
            // 创建一个精灵,用于绘制图片
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }
        protected override void UnloadContent()  //卸载目录
        {
            // TODO: Unload any non ContentManager content here
        }
        protected override void Update(GameTime gameTime)  /// 更新。用于检测碰撞、输入等
        {
            // 设置游戏结束事件
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            //添加更新的对象代码
            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)  //当绘制时被调用
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
            // 添加绘图代码
            base.Draw(gameTime);
        }
    }
}
其显示结果如下图所示:



原创粉丝点击