Basic XNA OOP for Beginners

本文介绍如何在XNA游戏开发中应用面向对象编程(OOP),通过创建Sprite基类及Ship子类来组织游戏代码,使游戏开发更加模块化。

Basic XNA OOP for Beginners


by percent20

German Translation

The Problem:


    Often overlooked in development is the beginner to a technology. This is defiantly prevalent in the XNA community as more and more tutorials come out only to show the coolest new thing that we have figured out how to do, and we want to share with everyone else so they can do it too.

    BUT, what about everyone else that wants to learn how to do game development in XNA. As of now the only thing they can really do is goof around with writing all their code in the Game1.cs file in the update draw etc… methods with no real Object Oriented Programming (OOP) to help make things more organized.
很多人都将XNA程序逻辑编写在Update和Draw里面,对于一些简单的实例程序是可以接受的,但是对于复杂的程序来说,这种方式意味着混乱的代码风格。

One of many possible Solutions:


    There are a lot of beginners that may know conceptually how to use OOP, but not able to use it when it comes to actually programming. This tutorial is geared towards those people that just need a nudge in the right direction for things to start making sense with OOP. Since we dont want to get too complex lets just start out with a basic Sprite Class that all Sprites will inherit from.

知道OOP和在实际中应用OOP不能简单划等号。

 

    using System;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
 
    namespace Game
    {
        public class Sprite
        {
            private Texture2D _texture;
            protected Vector2 _position;
            private Color _tint = Color.White;
 
            public Color Tint
            {
                get { return _tint; }
                set { _tint = value; }
            }
 
            public Vector2 Position
            {
                get { return _position; }
                set { _position = value; }
            }
 
            public Texture2D Texture
            {
                get { return _texture; }
                set { _texture = value; }
            }
 
            public virtual void Update(GameTime gt){}
 
            public virtual void Draw(SpriteBatch sb)
            {
                sb.Draw(this.Texture, this.Position, this.Tint);
            }
 
        }
    }
 
 




    So basically these are the basics that you need for each sprite that you want to use in your game. Lets run through it real quick.

这是一个sprite的基类。

    First, you have your properties which for a beginner is just enough to draw the sprite to the screen. You don't really need much more usually. There is the Texture2D which is what you will use to give the sprite the texture to load on the screen. You have a Vector2 that is going to be the position you want that sprite to draw on the screen. Then you have the color so you can set the tint of the sprite we have set this default to white so it shows the pure color of the sprite.

sprite的基本属性:texture+position+color

    Second, you have two virtual methods that can be overridden to allow functionality. As you can see there is the Update method and the Draw method. You will pretty much need both to do something with your sprite. To put it simple form, sort of, you will call the Update method of the Sprite class in the update method of the main game loop, and the Draw method of the Sprite class in the Draw method of the main game loop. I will give examples of both later on so if you didn't catch that don't worry.

两个需方法,一个负责update,一个负责draw.

    Now that we have your base Sprite class lets look at how it can be used. The following is a class for a ship that extends the Sprite class and has some functionality in it. I am not going to code out everything but it has just enough to give you an idea of what's going on.

然后我们可以从sprite基类中派生我们需要的具体类。

 

    using System;
    using System.Collections.Generic;
    using System.Text;
 
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework;
 
    namespace Game
    {
        class Ship : Sprite
        {
            public override void Update(GameTime gt)
            {
                // Call the method that moves the sprite
                // on the screen by adjusting 
                // the position property that was inherited
                // from the Sprite base class
            }
 
            // No real need yet to override the draw method
            // but it is there for you to do so if need be
            // for example if you wanted to call a differnt
            // overload of the spritebatches draw method.
        }
    }
 
 




    I left some basic comments in the code above to further explain what and where you can do things in code. Basically though you will spend your time in the Ship class writing your code for the ship class and not having to rewrite code that the sprite itself is needing. This is very useful because you don't want to rewrite the same code in several places then have to go back and change them in several places.
继承的功能。


    Finally, for our basics of OOP in XNA we will look at what needs to be done to setup these classes we created and how to use them. It is fairly simple and all done in the Game1.cs file. What we are going to do is create a Ship and just a Sprite so that you can see how they work. So lets do that then i'll give a tip on how to streamline the initial setup of these sprites.

    First we will look at creating and initializing objects. We will need 3 things, Sprite object, Ship object and a SpriteBatch Object. So here is the code for initializing those objects.

创建一个sprite,ship,spritebatch

 

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        SpriteBatch spriteBatch;
        Sprite sprite;
        Ship ship;
 
        // skip some code to the initialize method
        protected override void Initialize()
        {
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
 
            sprite = new Sprite();
            // we will set a position so the sprites don't overlap
            // each other when drawn
            sprite.Position = new Vector2(100, 100);
 
            ship = new Ship();
            ship.Position = Vector2.Zero;
 
            base.Initialize();
        }
 
 




    What we have done is created an instance of SpriteBatch, sprite, and ship next step is to load up our sprites into each of the objects. So here is the appropriate code for that. Remember this is JUST going to be the method used to load the textures to the sprites. This should give you an understanding of what to do.

 

    protected override void LoadGraphicsContent(bool loadAllContent)
    {
        if (loadAllContent)
        {
            sprite.Texture = content.Load<Texture2D>(@"sprite");
            ship.Texture = content.Load<Texture2D>(@"ship");
        }
    }
 
 




    Here we have given the sprite and the ship textures to display on the screen. So now we have setup a sprite and a ship, given them positions to draw, and given them the graphics needed to draw on the screen. Next lets do the code to actually draw them on the screen so if you are setting stuff up on the fly while reading this you can actually see something up on the screen. So here is the code you will need to draw your sprites to the XNA window.

 

    protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
 
        spriteBatch.Begin();
 
        sprite.Draw(spriteBatch);
        ship.Draw(spriteBatch);
 
        spriteBatch.End();
 
        base.Draw(gameTime);
    }
 
 




    In the draw method we are calling the ship and sprite object's Draw method and sending it the SpriteBatch so it may use that to draw to the screen. At this point you should be able to actually see something on the screen. WOOHOO, but next how will you get the sprites to actually do anything? We'll call the update methods of both the ship and the sprite objects. Here is the code to do this.

 

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
 
        sprite.Update(gameTime);
        ship.Update(gameTime);
 
        base.Update(gameTime);
    }
 
 




    So now you can go in and play with the Update method in ship class and get your ship to move around and do other stuff too. (I suggest not messing with the Update class in the Sprite class itself)

    One quick thing. Back up at the top of the code where we initialized the objects I recommend setting some custom constructors to make that easier so you don't have to call the properties all the time right after initialization. Just a quick tip.

使用构造器比逐一给属性赋值要好。

    I want to share some closing thoughts on this. This is only one of many ways to do OOP with XNA and is good for beginners doing things such as pong or space invaders or even a basic Super Mario Brothers type game. However, if you want to get more complex I suggest taking the concepts learned here and PLANNING out a better way to do OOP for more complicated games. This is only meant to get started using OOP not to be the end all for OOP in XNA.

在XNA中使用OOP。

内容概要:本文围绕“新型电力系统下多分布式电源接入配电网承载力评估方法”的研究展开,提供了完整的Matlab代码实现方案。研究聚焦于高比例可再生能源背景下,光伏、风电等分布式电源大规模接入对配电网承载能力的影响,构建了包含电力系统建模、优化算法设计、关键性能指标计算在内的综合评估体系。通过IEEE标准测试系统(如IEEE 33节点)进行仿真验证,深入分析系统在不同渗透率、不同接入位置及多种运行场景下的电压稳定性、潮流分布特性与设备利用率,量化评估配电网的接纳能力边界。研究不仅实现了学术模型的工程化复现,还涵盖了阻抗建模、稳定性判据、灵敏度分析等核心技术模块,为新型电力系统的规划、运行与优化提供科学依据和技术支撑。; 适合人群:具备电力系统分析基础、熟悉Matlab/Simulink仿真环境的科研人员、电气工程及相关专业的硕士/博士研究生,以及从事新能源并网、智能配电网规划与运行的工程技术与管理人员。; 使用场景及目标:①复现高水平学术论文中关于分布式电源承载力评估的模型与算法;②开展含高比例分布式电源的配电网安全性与稳定性研究;③掌握基于Matlab的电力系统仿真建模、优化求解与数据分析方法;④支撑科研课题申报、学位论文撰写、工程项目可行性论证及技术方案设计。; 阅读建议:建议结合文中提供的“公众号——荔枝科研社”获取全套资源,包括源代码、仿真模型、详细说明文档及案例数据,确保结果的可复现性。学习过程中应按照技术路线循序渐进,重点关注建模假设、算法流程与仿真结果的物理意义解读,并鼓励在现有模型基础上进行参数调整与功能拓展,以深化对配电网承载力内在机理的理解。
源码直接下载地址: https://pan.quark.cn/s/cd32d71a9d1b ### 日常AD管理常用工具详解 #### 一、SC系统服务修改工具 **SC**(Service Control Manager)是一个功能强大的命令行工具,旨在用于对Windows服务进行有效管理。该工具赋予管理员多种操作权限,包括创建、移除、暂停、继续运行以及调整服务属性等。对于那些由恶意软件生成且无法通过常规的MMC控制面板进行删除的服务,SC工具展现出其独特的优势。例如,为了终止一个名为`MyService`的服务,可以运用以下命令: ``` sc stop MyService ``` 若需调整服务属性,比如设定启动方式为自动,则可以使用: ``` sc config MyService start= auto ``` #### 二、Redirusr与Redircmp 这两个工具的核心功能在于执行用户的策略重定向,确保新加入域的设备不会立即遵循默认的策略集,而是会被临时分配到特定的OU中,并实施特定的策略,例如安装必要的补丁或安全软件。**Redircmp**工具专门用于将用户或计算机对象转移至另一个OU,其使用语法如下: ``` redircmp "CN=Users,DC=example,DC=com" "CN=New Users,DC=example,DC=com" ``` 其中,`CN=Users,DC=example,DC=com`代表当前所在的OU,而`CN=New Users,DC=example,DC=com`则指代目标OU。 #### 三、Set 这是一种基础的命令行工具,其作用在于检索当前登录环境的详细信息,涵盖用户是否成功登录到了域,以及具体登录...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值