c# – 绘制“地板”纹理

c# – 绘制“地板”纹理,第1张

概述好吧,让我说我有一些地板或其他东西的瓷砖纹理.而且我希望我的玩家能够继续前行. 如何设置此瓷砖使其成为一个地板? 我需要这个瓷砖纹理遍布整个屏幕宽度吗? 我怎么做的? 谢谢 如果你想要一个非常简单的方法,这里是: 首先,您创建一个新类并将其命名为Tile: using System;using System.Collections.Generic;using System.Linq;usin 好吧,让我说我有一些地板或其他东西的瓷砖纹理.而且我希望我的玩家能够继续前行.
如何设置此瓷砖使其成为一个地板?
我需要这个瓷砖纹理遍布整个屏幕宽度吗?
我怎么做的?
谢谢解决方法 如果你想要一个非常简单的方法,这里是:
首先,您创建一个新类并将其命名为Tile:

using System;using System.Collections.Generic;using System.linq;using System.Text;using Microsoft.Xna.Framework; // Don't forget those,they will let youusing Microsoft.Xna.Framework.Content; // access some class like:using Microsoft.Xna.Framework.Graphics; // Texture2D or Vector2namespace Your_Project _name{    class Tile    {    }{

到目前为止一直很好,现在在你的类中创建纹理和位置,如下所示:

namespace Your_Project _name{    class Tile    {        Texture2D texture;        Vector2 position;        public voID Initialize()        {        }        public voID Draw()        {        }    }{

如你所见,我还创建了两个方法,Initialize和Draw,现在我们将初始化我们的
公共voID Initialize()中的tile纹理的纹理和位置,
我不知道你如何使用你的ContentManager,但这是一个简单的方法:

public voID Initialize(ContentManager Content){    texture = Content.Load<Texture2D>("YourfloorTexture"); //it will load your texture.    position = new Vector2(); //the position will be (0,0)}

现在我们需要多次绘制纹理,我们将如何做到这一点? thasc说的方式,代码可能更复杂,但这是你会理解的,我会添加一个SpriteBatch,所以我可以绘制.所有这些都是在public voID Draw()中完成的:

public voID Draw(SpriteBatch spriteBatch)    {        for (int i=0; i<30;i++) //will do a loop 30 times. Each Time i will =                                 //a valor from 0 to 30.        {            spriteBatch.Draw(texture,position,color.White);            //Will draw the texture once,at the position Vector2             //right Now position = (0,0)            spriteBatch.Draw(texture,new Vector2((int)i,(int)i),color.White);            //Will Draw the texture 30 times,the first time on the position (0,0)            //Second Time on (1,1) .. third (2,2) etc...            spriteBatch.Draw(texture,new Vector2((int)position.X + (i * texture.WIDth),(int)position.Y + (i * texture.Height),color.White));            //Will Draw the Texture 30 times Spaced by the WIDth and height            //of the texture (this is the code you need)         }    }

我没试过但它应该可以工作,现在它只是一个样本,你可以弄清楚剩下的.还有很多其他方法可以做到,但这个方法非常简单.好的,现在最后一步是实现这个类,所以进入你的主要类,你有你所有的代码,在此之前:

public Game1()

创建tile类的新实例

Tile tile;

并在受保护的覆盖中初始化voID Initialize():

tile = new Tile();tile.Initialize(Content);

现在你必须在屏幕上绘制它,在类的末尾找到protected overrIDe voID Draw(GameTime gameTime)并调用我们类的draw方法:

spriteBatch.Begin();tile.Draw(spriteBatch);spriteBatch.End();

这是完成简单的简单平铺系统的所有步骤.正如我所说,还有很多其他方法你只需阅读有关它们的教程或自己创建它们.

总结

以上是内存溢出为你收集整理的c# – 绘制“地板”纹理全部内容,希望文章能够帮你解决c# – 绘制“地板”纹理所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1217160.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存