C# XNA 随机磁贴引擎 [英] C# XNA Random tile engine

查看:48
本文介绍了C# XNA 随机磁贴引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void Draw(SpriteBatch theSpriteBatch)
    {
        Random rand = new Random();
        for(int y = 0; y < map.GetLength(0); y++) {
            for(int x = 0; x < map.GetLength(1); x++) {
                theSpriteBatch.Draw(tile[rand.Next(0,3)], new Rectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight), 
                    Color.White);
            }
        }
    }

当我这样做时,它只会闪烁瓷砖并不断重绘它们.我该怎么做才能获得仅绘制一次的随机效果?有没有办法可以用鼠标点击这些瓷砖并让它们改变?另外,有没有办法让一个图块比其他图块更流行?

When I do this it just flickers the tiles and constantly redraws them. What do I do to get the random effect with it only drawing once? Is there a way I could click on these tiles with the mouse and have them change? Also, is there a way to make one tile more prevalent than the others?

推荐答案

我相信您希望只随机生成图块一次,然后每次绘制随机序列强>.请记住,XNA 中的 Draw 每帧"运行一次,通常每秒超过一次!

I believe you are looking to only randomly generate the tiles one time and then draw THAT random sequence every time. Remember, Draw in XNA runs every "frame", which is generally way more than once per second!

将您当前的循环复制到一个新区域:您的地图加载.还要添加一个 2D 数据结构(数组或其他)来存储切片生成的结果.我正在调用我的示例二维整数数组 tileNums.现在,保持循环不变,但更改内部结构以存储结果而不是绘制:

Copy your current loops over to a new area: your map loading. Also add a 2D data structure (array or whatever) to store the results of your tile generation. I'm calling my sample 2D integer array tileNums. Now, keep the loops as they are now but change the innards to store the results instead of draw:

    Random rand = new Random();
    for(int y = 0; y < map.GetLength(0); y++) {
        for(int x = 0; x < map.GetLength(1); x++) {
            tileNums[x, y] = rand.Next(0,3);
        }
    }

现在只需更改当前 Draw 循环的内部结构,使其不再随机生成,而是从这些数据中获取:

Now just change the innards of your current Draw loop to no longer generate randomly and instead to take from this data:

    //Random object no longer needed
    for(int y = 0; y < map.GetLength(0); y++) {
        for(int x = 0; x < map.GetLength(1); x++) {
            theSpriteBatch.Draw(tile[tileNums[x, y]], new Rectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight), 
                Color.White);
        }
    }

这篇关于C# XNA 随机磁贴引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆