可点击的瓷砖

时间:2011-03-11 14:38:36

标签: xna xna-4.0

我最近决定使用XNA框架学习和制作小型2D游戏。所以我开始编辑Kurt Jaegers的“XNA 4.0游戏开发实例”一书中的一个例子,atm我被困在展示精灵...... 我想做一些类似麻将纸牌的东西,当你选择两个相同的碎片时,瓷砖会被破坏。所以我展示了一块随机碎片,但不知道如何绘制不同颜色的点击瓷砖,以表明我选择了它。 这是主要的Game1课程:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Texture2D titleScreen;
    Texture2D backgroundscreen;
    Texture2D playingTiles;
    Board gameBoard;
    Vector2 gameBoardDisplayOrigin = new Vector2(70, 89);
    enum GameStates { TitleScreen, Playing };
    GameStates gameState = GameStates.TitleScreen;
    Rectangle EmptyPiece = new Rectangle(247, 165, 40, 40);
    const float MinTimeSinceLastInput = 0.25f;
    float timeSinceLastInput = 0.0f;


    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here
        this.IsMouseVisible = true;
        graphics.PreferredBackBufferWidth = 800;
        graphics.PreferredBackBufferHeight = 600;
        graphics.ApplyChanges();
        gameBoard = new Board();

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here

        titleScreen = Content.Load<Texture2D>(@"Textures\TitleScreen");
        backgroundscreen = Content.Load<Texture2D>(@"Textures\Background");
        playingTiles = Content.Load<Texture2D>(@"Textures\MSuXTiles");
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    /// /// *******************   UPDATE   *********************
    /// ********************************************************
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();



        // TODO: Add your update logic here
        switch (gameState)
        {
            case GameStates.TitleScreen:
                if (Keyboard.GetState().IsKeyDown(Keys.Space))
                {
                    gameBoard.ClearBoard();
                    gameBoard.GenerateNewTiles();
                    //playerScore = 0;
                    gameState = GameStates.Playing;
                }
                break;
            case GameStates.Playing:
                timeSinceLastInput += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (timeSinceLastInput >= MinTimeSinceLastInput)
                {
                    HandleMouseInput(Mouse.GetState());

                }



                //gameBoard.GenerateNewTiles();
                break;
        }


        base.Update(gameTime);
    }


    /// **********************   DRAW   ************************
    /// ********************************************************
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
        if (gameState == GameStates.TitleScreen)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(titleScreen, new Rectangle(0, 0, this.Window.ClientBounds.Width,
                this.Window.ClientBounds.Height), Color.White);
            spriteBatch.End();
        }
        if (gameState == GameStates.Playing)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(backgroundscreen, new Rectangle(0, 0,
                this.Window.ClientBounds.Width, this.Window.ClientBounds.Height), Color.White);
            for (int x = 0; x < Board.BoardWidth; x++)
                for (int y = 0; y < Board.BoardHeight; y++)
                {
                    int pixelX = (int)gameBoardDisplayOrigin.X + (x * GameTile.TileWidth);
                    int pixelY = (int)gameBoardDisplayOrigin.Y + (y * GameTile.TileHeight);
                    //rysujemy tlo dla przezroczystych obrazkow
                    /*spriteBatch.Draw(
                        playingTiles,
                        new Rectangle(pixelX, pixelY, GameTile.TileWidth, GameTile.TileHeight),
                        EmptyPiece,
                        Color.White);*/
                    spriteBatch.Draw(
                        playingTiles,
                        new Rectangle(pixelX, pixelY, GameTile.TileWidth, GameTile.TileHeight),
                        gameBoard.GetSourceRect(x, y),
                        Color.White);


                }
            //this.Window.Title = playerScore.ToString();
            spriteBatch.End();
        }

        base.Draw(gameTime);
    }


    //input handling
    private void HandleMouseInput(MouseState mouseState)
    {
        int x = ((mouseState.X - (int)gameBoardDisplayOrigin.X) / GameTile.TileWidth);
        int y = ((mouseState.Y - (int)gameBoardDisplayOrigin.Y) / GameTile.TileHeight);
        if ((x >= 0) && (x < Board.BoardWidth) &&
          (y >= 0) && (y < Board.BoardHeight))
        {
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                gameBoard.TintSelectedTile(x, y);
                timeSinceLastInput = 0.0f;

            }
            if (mouseState.RightButton == ButtonState.Pressed)
            {
                timeSinceLastInput = 0.0f;
            }
        }
    }
}

董事会成员:

class Board
{
    Random rand = new Random();

    public const int BoardWidth = 10;
    public const int BoardHeight = 10;

    private GameTile[,] boardOfTiles = new GameTile[BoardWidth, BoardHeight];
    //constructor
    public Board()
    {
        ClearBoard();
    }
    public void ClearBoard()
    {
        for (int x = 0; x < BoardWidth; x++)
            for (int y = 0; y < BoardHeight; y++)
                boardOfTiles[x, y] = new GameTile("Empty,Four");
    }
    //methods
    public Rectangle GetSourceRect(int x, int y)
    {
        return boardOfTiles[x, y].GetSourceRect();
    }
    public string GetSquare(int x, int y)
    {
        return boardOfTiles[x, y].TileType;
    }
    public void SetSquare(int x, int y, string tileName)
    {
        boardOfTiles[x, y].SetTile(tileName);
    }
    public void RandomPiece(int x, int y)
    {
        boardOfTiles[x, y].SetTile(GameTile.TileTypes[rand.Next(0, GameTile.MaxTilesColumnIndex),
            rand.Next(0, GameTile.MaxTilesRowIndex)]);
    }
    public void GenerateNewTiles()
    {

        for (int y = 0; y < Board.BoardHeight; y++)
            for (int x = 0; x < Board.BoardWidth; x++)
            {
                if (GetSquare(x, y) == "Empty,Four")
                {
                    RandomPiece(x, y);
                }
            }
    }

} 和GameTile类:

class GameTile
{
    public static string[,] TileTypes =
    {
        {"Circle,One", "Circle,Two", "Circle,Three", "Circle,Four", "Circle,Five",
        "Circle,Six", "Circle,Seven", "Circle,Eight", "Circle,Nine"},

        {"Bamboo,One", "Bamboo,Two", "Bamboo,Three", "Bamboo,Four", "Bamboo,Five",
        "Bamboo,Six", "Bamboo,Seven", "Bamboo,Eight", "Bamboo,Nine"},

        {"Character,One", "Character,Two", "Character,Three", "Character,Four", "Character,Five",
        "Character,Six", "Character,Seven", "Character,Eight", "Character,Nine"},

        {"Honor,One", "Honor,Two", "Honor,Three", "Honor,Four", "Something1,Five",
        "Something2,Six", "Something3,Seven", "Something4,Eight", "Something5,Nine"},

        {"Dragon,One", "Dragon,Two", "Dragon,Three", "Empty,Four", "Something6,Five",
        "Something7,Six", "Something8,Seven", "Something9,Eight", "Something10,Nine"},

        {"Flower,One", "Flower,Two", "Flower,Three", "Flower,Four", "Something11,Five",
        "Something12,Six", "Something13,Seven", "Something14,Eight", "Something15,Nine"},

        {"Season,One", "Season,Two", "Season,Three", "Season,Four", "Something16,Five",
        "Something17,Six", "Something18,Seven", "Something19,Eight", "Something20,Nine"}
    };




    public const int TileHeight = 40;
    public const int TileWidth = 40;

    public const int MaxTilesRowIndex = 9;
    public const int MaxTilesColumnIndex = 7;

    private const int textureOffsetX = 1;
    private const int textureOffsetY = 1;
    private const int texturePaddingX = 1;
    private const int texturePaddingY = 1;

    private string tileType = "";

    public string TileType
    {
        get { return tileType; }
    }

    //konstruktory
    public GameTile(string type)
    {
        tileType = type;
    }
    //metody
    public void SetTile(string type)
    {
        tileType = type;
    }



    public Rectangle GetSourceRect()
    {
        int x = textureOffsetX;
        int y = textureOffsetY;


        foreach (string s in tileType.Split(','))
        {
            switch(s)
            {
                case "One":
                    //y = y;
                    break;
                case "Two":
                    y = y + (TileHeight + texturePaddingY);
                    break;
                case "Three":
                    y = y + 2 * (TileHeight + texturePaddingY);
                    break;
                case "Four":
                    y = y + 3 * (TileHeight + texturePaddingY);
                    break;
                case "Five":
                    y = y + 4 * (TileHeight + texturePaddingY);
                    break;
                case "Six":
                    y = y + 5 * (TileHeight + texturePaddingY);
                    break;
                case "Seven":
                    y = y + 6 * (TileHeight + texturePaddingY);
                    break;
                case "Eight":
                    y = y + 7 * (TileHeight + texturePaddingY);
                    break;
                case "Nine":
                    y = y + 8 * (TileHeight + texturePaddingY);
                    break;

                case "Circle":
                    //x = x;
                    break;
                case "Bamboo":
                    x = x + (TileWidth + texturePaddingX);
                    break;
                case "Character":
                    x = x + 2 * (TileWidth + texturePaddingX);
                    break;
                case "Honor":
                    x = x + 3 * (TileWidth + texturePaddingX);
                    break;
                case "Dragon":
                    x = x + 4 * (TileWidth + texturePaddingX);
                    break;
                case "Flower":
                    x = x + 5 * (TileWidth + texturePaddingX);
                    break;
                case "Season":
                    x = x + 6 * (TileWidth + texturePaddingX);
                    break;
            }
        }
        return new Rectangle(x, y, TileHeight, TileWidth);
    }




}

这就是它的外观: http://i55.tinypic.com/2yuizbl.png

1 个答案:

答案 0 :(得分:2)

您可以做的一件简单事情是,在SpriteBatch.Draw()调用点击的图块,将Color.White更改为其他颜色 - 可能是Red。这将使图块以新颜色着色。

如果您更喜欢其他效果,可以将点击的图块渲染为单独的RenderTarget2D,执行您想要渲染目标的任何增强,然后将渲染目标绘制到后台缓冲区。

相关问题