通过选择改变级别

时间:2015-05-03 21:33:36

标签: c# xna

嘿所以我在这里有这个代码,我想要做的是制作2个更相似的关卡,所以当我有瓷砖地图2和3时,我会有一个菜单,玩家可以选择在某个地图上玩。我想知道如何制作这个菜单,请你给我一些见解,从哪里开始我可以使用哪种类型的代码等。

简单的选择菜单外观:

                      Levels

             Level 1

             Level 2

             Level 3 

然后用户点击它就会触发/打开其中一个图块地图。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MarsCitadel
{
static class Tilemap

{

#region declarations

    public const int TileWidth = 32;
    public const int TileHeight = 32;
    public const int MapWidth = 50;
    public const int MapHeight = 50;

    public const int FloorTileStart = 0; // 32 x 32 rectangle from sprite sheet
    public const int FloorTileEnd = 3;
    public const int WallTileStart = 4;
    public const int WallTileEnd = 7;

    static private Texture2D texturebarrens;
    static private Texture2D textureroad;
    //static private List<Rectangle> tiles = new List <Rectangle> ();
    static private int[,] mapSquares = new int[MapWidth, MapHeight];
    static private Random rand = new Random();

    #endregion

    #region intialization

    static public void intialization(Texture2D tiletexture, Texture2D tiletexture2)
    {
        texturebarrens = tiletexture;
        textureroad = tiletexture2;
        //tiles.Clear();
        //tiles.Add(new Rectangle(0, 0, TileWidth, TileHeight));

        for (int x = 0; x < MapWidth; x++)
        {
            for (int y = 0; y < MapHeight; y++)
            {
                mapSquares[x, y] = FloorTileStart;
            }
        }

        GenerateRandomMap();
    }

    #endregion

    #region information about map squares

    static public int getsquareatpixelX(int pixelX)
    {
        return pixelX / TileWidth;
    }
    static public int getsquareatpixelY(int pixelY)
    {
        return pixelY / TileHeight;
    }

    static public Vector2 getsquarebypixel(Vector2 pixellocation)
    {
        return new Vector2(getsquareatpixelX((int)pixellocation.X), getsquareatpixelY((int)pixellocation.Y));
    }

    static public Vector2 Getsquarecenter(int squareX, int squareY)
    {
        return new Vector2((squareX * TileWidth) + (TileWidth / 2), (squareY * TileHeight) + (TileHeight / 2));
    }

    static public Vector2 Getsquarecenter(Vector2 square)
    {
        return Getsquarecenter((int)square.X, (int)square.Y);
    }

    static public Rectangle Squareworldrectangle(int x, int y)
    {
        return new Rectangle(x * TileWidth, y * TileHeight, TileWidth, TileHeight);
    }

    static public Rectangle Squareworldrectangle(Vector2 square)
    {
        return Squareworldrectangle((int)square.X, (int)square.Y);
    }

    public static Rectangle Squarescreenrectangle(int x, int y)
    {
        return Camera.Transform(Squareworldrectangle(x, y));
    }

    public static Rectangle Squarescreenrectangle(Vector2 square)
    {
        return Squareworldrectangle((int)square.X, (int)square.Y);
    }

    #endregion

    #region information about map tiles

    static public int GetTileAtSquare(int tileX, int tileY)
    {
        if (((tileX >= 0) && (tileX <= MapWidth)) && ((tileX >= 0) && (tileX <= MapHeight)))
        {
            return mapSquares[tileX, tileY];
        }
        else return -1;
    }

    static public void SetTileAtSquare(int tileX, int tileY, int tile)
    {
        if (((tileX >= 0) && (tileX <= MapWidth)) && ((tileX >= 0) && (tileX <= MapHeight)))
        {
            mapSquares[tileX, tileY] = tile;
        }
    }

    static public int GetTileAtPixel(int pixelX, int pixelY)
    {
        return GetTileAtSquare(getsquareatpixelX(pixelX), getsquareatpixelY(pixelY));
    }

    static public int GetTileAtPixel(Vector2 pixelLocation)
    {
        return GetTileAtPixel((int)pixelLocation.X, (int)pixelLocation.Y);
    }

    static public bool IsWallTile(int tileX, int tileY)
    {
        int tileindex = GetTileAtSquare(tileX, tileY);
        if (tileindex == -1) return false;
        return tileindex >= WallTileStart;
    }

    static public bool IsWallTile(Vector2 square)
    {
        return IsWallTile((int)square.X, (int)square.Y);
    }

    static public bool IsWallTileByPixel(Vector2 pixelLocation)
    {
        return IsWallTile(getsquareatpixelX((int)pixelLocation.X), getsquareatpixelY((int)pixelLocation.Y));
    }

    #endregion

    #region drawing

    static public void Draw(SpriteBatch spritebatch)
    {
        //int startX = getsquareatpixelX((int)Camera.Position.X);
        //int endX = getsquareatpixelX((int)Camera.Position.X + Camera.ViewPortWidth);

        //int startY = getsquareatpixelY((int)Camera.Position.Y);
        //int endY = getsquareatpixelY((int)Camera.Position.Y + Camera.ViewPortHeight);
        int startX = 0; int endX = 50;
        int startY = 0; int endY = 50;

        for (int x = startX; x <= endX; x++)
        {
            for (int y = startY; y <= endY; y++)
            {
                if ((x >= 0) && (y >= 0) && (x < MapWidth) && (y < MapHeight))
                {
                    spritebatch.Draw(texturebarrens, new Rectangle(x * 32, y * 32, 32, 32), Color.White);
                }
            }
        }

        spritebatch.Draw(textureroad, new Rectangle(0 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(1 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(2 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(3 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(4 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(5 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(6 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(7 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(8 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(9 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(10 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(11 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(12 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(13 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(14 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(15 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(16 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(17 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(18 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(19 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(20 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(21 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(22 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(23 * 32, 7 * 32, 32, 32), Color.White);
        spritebatch.Draw(textureroad, new Rectangle(24 * 32, 7 * 32, 32, 32), Color.White);




    }

    #endregion

    #region map generation

    static public void GenerateRandomMap()
    {
        int WallchancePerSquare = 10;
        int floorTile = rand.Next(FloorTileStart, FloorTileEnd + 1);
        int wallTile = rand.Next(WallTileStart, WallTileEnd + 1);

        for (int x = 0; x < MapWidth; x++)
        {
            for (int y = 0; y < MapHeight; y++)
            {
                mapSquares[x, y] = floorTile;
                // if (x == 0 || y == 0 || x == MapWidth - 1 || y == MapHeight - 1)
                //{
                //mapSquares[x, y] = wallTile;
                // continue;
                // }

                if (x == 1 || y == 1 || x == MapWidth - 2 || y == MapHeight - 2)
                {
                    continue;
                }

                if (rand.Next(0, 100) < WallchancePerSquare)
                {
                    mapSquares[x, y] = wallTile;
                }
            }
        }
    }

    #endregion

}
}

1 个答案:

答案 0 :(得分:0)

使Tilemap类抽象化,然后地图的每个版本都可以是Tilemap的不同子类(可能有其他方法来优化它,尝试查找一些设计模式,如“装饰器”或“策略”模式)。创建一个Menu类,允许用户选择一个地图,比如用各种按钮。然后单击按钮应该实例化Tilemap的相应子类,任何其他代码都可以将这些对象称为Tilemap而不知道实现了哪个子类。