0

我有这段代码可以在 Qbert 板上绘制框,我将如何弄清楚如何检测踩到了哪些色块?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace QBert
{
    public class Map
    {
        public int[,] board;
        public Color[] blockColors = new Color[] { Color.Blue, Color.Green }; //this makes it so it takes one time to step  to get to green
        Texture2D block;



        public Map(Texture2D block)   //draws the map of the blocks
        {
            this.block = block;

            board = new int[8, 7] 
            {
                { 0, 0, 0, 1, 0, 0, 0 },
                { 0, 0, 1, 1, 0, 0, 0 },
                { 0, 0, 1, 1, 1, 0, 0 },
                { 0, 1, 1, 1, 1, 0, 0 },
                { 0, 1, 1, 1, 1, 1, 0 },
                { 1, 1, 1, 1, 1, 1, 0 },
                { 1, 1, 1, 1, 1, 1, 1 },
                { -1, -1, -1, -1, -1, -1, -1 },

            }
            ;
        }

        public Vector2 GetSquareCoords(int x, int y) //cordinates of the block
        {
            int ofs = block.Width / 2;
            ofs *= y % 2;

            return new Vector2(x * block.Width + ofs, y * 96); // 96  
        }

        public Vector2 GetSquareCenter(int x, int y)  //method for to jump on the middle of a block
        {
            Vector2 coords = GetSquareCoords(x, y);

            return new Vector2(coords.X + block.Width / 2, coords.Y + 32); //32
        }


        public Vector2 GetNextSquare(bool down, bool left, Vector2 position)  //this is how you jump to a next square
        {
            // If on even row, right is directly below and left is below and to the left
            // If on odd row, left is directly below and right is below and to the right
            int next_x = 0, next_y = 0;
            int x = (int)position.X;
            int y = (int)position.Y;

            if (down)
            {
                next_y = y + 1;
                if (left)
                {
                    next_x = x - 1;  // -1
                }
                else
                {
                    next_x = x;
                }

            }
            else
            {
                next_y = y - 1;

            }

            if (y % 2 == 0)
            {

                if (left)
                    next_x = x - 1;
                else
                    next_x = x;


            }
            else
            {
                if (left)
                    next_x = x;
                else
                    next_x = x + 1;  //+1 

            }


            if (next_x < 0)
            {
                next_x += 1;

            }
            if (next_x > 6)
            {
                next_x -= 1;

            }
            if (next_y < 0)
            {
                next_y += 1;

            }
            if (next_y > 7)
            {
                next_y -= 1;

            }


            if (board[next_y, next_x] == 0)
            {
                return new Vector2(x, y);
            }
            else
            {
                return new Vector2(next_x, next_y);
            }


        }


        public void Draw(SpriteBatch spriteBatch) //draws the blocks and colors of the block
        {
            int drawXOffset = 30;
            int drawYOffset = 60;


            for (int x = 0; x < 7; x++)
                for (int y = 0; y < 7; y++)
                {
                    Vector2 coord = GetSquareCoords(x, y);
                    if (board[y, x] > 0)
                        spriteBatch.Draw(block, new Rectangle(drawXOffset + (int)coord.X, drawYOffset + (int)coord.Y, block.Width, block.Height), blockColors[board[y, x] - 1]);




                }
        }
    }
    } 

我试图让代码检测绘制的块数,以便我知道它们何时都是某种颜色。

我需要让它变成某种颜色的方块才能结束游戏。

现在,我将它从蓝色块颜色开始,然后变为绿色块,我将如何让它检测到如果所有绿色块都被踩到了游戏结束?

4

4 回答 4

3

在你的Update方法中,你会想要这样的东西:

bool finished = true;
for (int x = 0; x < 7; x++)
{
    for (int y = 0; y < 7; y++)
    {
        if (board != 0 && board != 2) // 2 is green
        {
            finished = true;
            break;
        }
    }
    if (finished)
        break;
}
if (finished)
{
    // Move to next level
}
于 2012-05-23T01:27:32.000 回答
2

我认为这样的事情是你想要的

public Vector2 GetNextSquare(bool down, bool left, Vector2 position)  
{
    int x = (int)position.X;
    int y = (int)position.Y;

//...other code

   if (blockColors[board[next_y, next_x]] == Color.Green)
   {
      //End
   }
   else if (board[next_y, next_x] == 0)
   {
        return new Vector2(x, y);
   }
   else
   {
        return new Vector2(next_x, next_y);
   }
}
于 2012-05-23T01:36:26.213 回答
1

通常你有某种数据代表你的游戏领域,而渲染代码只是渲染领域的视觉表示。您的游戏代码仅适用于内部字段表示(即在您的情况下,具有“颜色”属性的立方体对象集)。

您绝对可以检查屏幕上的颜色,但这需要更多的努力。

于 2012-05-23T01:22:03.130 回答
0

@Jaview“你是什么意思,你能告诉我一个我如何检查的例子吗?”

这是一个如何在 XNA 中获取像素的示例:

ResolveTexture2D backBufferData;

backBufferData = new ResolveTexture2D(
    graphics.GraphicsDevice,
    graphics.GraphicsDevice.PresentationParameters.BackBufferWidth,
    graphics.GraphicsDevice.PresentationParameters.BackBufferHeight,
    1,
    graphics.GraphicsDevice.PresentationParameters.BackBufferFormat
);

Rectangle sourceRectangle = new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1);

Color[] retrievedColor = new Color[1];
graphics.GraphicsDevice.ResolveBackBuffer(backBufferData);

backBufferData.GetData<Color>(
    0,
    sourceRectangle,
    retrievedColor,
    0,
1);

selectedColor = retrievedColor[0];
于 2012-05-23T08:08:02.160 回答