Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 中断Windows Phone开发人员冲突检测_C#_Xna_Windows Phone_Collision Detection - Fatal编程技术网

C# 中断Windows Phone开发人员冲突检测

C# 中断Windows Phone开发人员冲突检测,c#,xna,windows-phone,collision-detection,C#,Xna,Windows Phone,Collision Detection,我正在学习XNA突破克隆的教程,我正在为Windows Phone编程。本教程在球和砖的碰撞检测完成之前结束。砖块以行和列的形式放在屏幕上。我得到了桨的边界框检测,它将球反射到桨上,但砖块需要同样的能力。我正在为我的主要游戏类“球和砖”粘贴代码。如果有人能给我一个如何实现这一点的想法,我将不胜感激。我是否需要一组矩形,或者是否有不同的方法来实现这一点 using System; using System.Collections.Generic; using System.Linq; using

我正在学习XNA突破克隆的教程,我正在为Windows Phone编程。本教程在球和砖的碰撞检测完成之前结束。砖块以行和列的形式放在屏幕上。我得到了桨的边界框检测,它将球反射到桨上,但砖块需要同样的能力。我正在为我的主要游戏类“球和砖”粘贴代码。如果有人能给我一个如何实现这一点的想法,我将不胜感激。我是否需要一组矩形,或者是否有不同的方法来实现这一点

using System;
using System.Collections.Generic;
using System.Linq;
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.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace bouncingBlocks
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D texture1;
    Texture2D texture2;

    public Vector2 spritePosition1;
    Vector2 spritePosition2;
    Texture2D mainBackground;
    Vector2 mainBackgroundPos;
    Vector2 spriteSpeed1 = new Vector2(50.0f, 50.0f);
    Vector2 spriteSpeed2 = new Vector2(100.0f, 100.0f);
    int sprite1Height;
    int sprite1Width;
    int sprite2Height;
    int sprite2Width;
    Texture2D paddleTexture;
    Rectangle screenRectangle;

    int bricksWide;
    int bricksHigh;
    Texture2D brickImage;
    Block[,] bricks;

    Vector2 ballPos;

    SoundEffect soundEffect;

    //objects
    paddle paddle;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferredBackBufferWidth = 320; 
        graphics.PreferredBackBufferHeight = 480;

        screenRectangle = new Rectangle(
            0,
            0,
            graphics.PreferredBackBufferWidth,
            graphics.PreferredBackBufferHeight
            );



        // Frame rate is 30 fps by default for Windows Phone.
        TargetElapsedTime = TimeSpan.FromTicks(333333);
    }

    /// <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
        TouchPanel.EnabledGestures = GestureType.FreeDrag;


        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);
        spriteBatch = new SpriteBatch(GraphicsDevice);

        texture1 = Content.Load<Texture2D>("Ball");
        texture2 = Content.Load<Texture2D>("Ball");

        mainBackground = Content.Load<Texture2D>("Background");
        mainBackgroundPos.X = 0;
        mainBackgroundPos.Y = 0;
        paddleTexture = Content.Load<Texture2D>("GlowPaddle");
        paddle = new paddle(paddleTexture, screenRectangle);

        brickImage = Content.Load<Texture2D>("BlueBlock");
        bricksWide = 4;
        bricksHigh = 2;

        soundEffect = Content.Load<SoundEffect>("Windows Ding");

        spritePosition1.X = 0;
        spritePosition1.Y = 0;

        spritePosition2.X = graphics.GraphicsDevice.Viewport.Width - texture1.Width;
        spritePosition2.Y = graphics.GraphicsDevice.Viewport.Height - texture1.Height;

        sprite1Height = texture1.Bounds.Height;
        sprite1Width = texture1.Bounds.Width;

        sprite2Height = texture2.Bounds.Height;
        sprite2Width = texture2.Bounds.Width;
        StartGame();
        // TODO: use this.Content to load your game content here
    }

    /// <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
    }


    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
        ButtonState.Pressed)
            this.Exit();

        // Move the sprite around.
        UpdateSprite(gameTime, ref spritePosition1, ref spriteSpeed1);
        UpdateSprite(gameTime, ref spritePosition2, ref spriteSpeed2);

        CheckForCollision();
        paddCollision(paddle.GetBounds());



        paddle.Update();



        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    public Rectangle BallGetBounds()
    {
        return new Rectangle(
            (int)spritePosition1.X,
            (int)spritePosition1.Y,
            texture1.Width,
            texture1.Height);
    }

    private void StartGame()
    {

        paddle.SetInStartPosition();

        bricks = new Block[bricksWide, bricksHigh];
        for (int y = 0; y < bricksHigh; y++)
        {
            Color tint = Color.White;
            switch (y)
            {
                case 0:
                    tint = Color.Blue;
                    break;
                case 1:
                    tint = Color.Red;
                    break;
                case 2:
                    tint = Color.Green;
                    break;
                case 3:
                    tint = Color.Yellow;
                    break;
                case 4:
                    tint = Color.Purple;
                    break;
            }
            for (int x = 0; x < bricksWide; x++)
            {
                bricks[x, y] = new Block(
                brickImage,
                new Rectangle(
                x * brickImage.Width,
                y * brickImage.Height,
                brickImage.Width,
                brickImage.Height),
                tint);
            }
        }
    }


    public void blockCollision(Rectangle ballRec)
    {

        Rectangle newballLocation = new Rectangle(
            (int)spritePosition1.X,
            (int)spritePosition1.Y,
            texture1.Width,
            texture1.Height);
        if (ballRec.Intersects(newballLocation))
        {
            spritePosition1.Y = ballRec.Y - texture1.Height;
            spriteSpeed1.Y *= -1;

        }
    }

    public void paddCollision(Rectangle paddleRec)
    {
        Rectangle ballLocation = new Rectangle(
            (int)spritePosition1.X,
            (int)spritePosition1.Y,
            texture1.Width,
            texture1.Height);
        if (paddleRec.Intersects(ballLocation))
        {

            spritePosition1.Y = paddleRec.Y - texture1.Height;
            spriteSpeed1.Y *= -1;
        }
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);


        spriteBatch.Begin();
        spriteBatch.Draw(mainBackground, mainBackgroundPos, Color.White);


        spriteBatch.End();

        spriteBatch.Begin();
        foreach (Block brick in bricks)
            brick.Draw(spriteBatch);
        paddle.Draw(spriteBatch);
        spriteBatch.End();

        // Draw the sprite.
        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

        spriteBatch.Draw(texture1, spritePosition1, Color.White);
        spriteBatch.End();

        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.Opaque);
        spriteBatch.Draw(texture2, spritePosition2, Color.Gray);
        spriteBatch.End();

        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }

    void UpdateSprite(GameTime gameTime, ref Vector2 spritePosition, ref Vector2 spriteSpeed)
    {
        // Move the sprite by speed, scaled by elapsed time.
        spritePosition +=
            spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

        int MaxX =
            graphics.GraphicsDevice.Viewport.Width - texture1.Width;
        int MinX = 0;
        int MaxY =
            graphics.GraphicsDevice.Viewport.Height - texture1.Height;
        int MinY = 0;

        // Check for bounce.
        if (spritePosition.X > MaxX)
        {
            spriteSpeed.X *= -1;
            spritePosition.X = MaxX;
        }

        else if (spritePosition.X < MinX)
        {
            spriteSpeed.X *= -1;
            spritePosition.X = MinX;
        }

        if (spritePosition.Y > MaxY)
        {
            spriteSpeed.Y *= -1;
            spritePosition.Y = MaxY;
        }

        else if (spritePosition.Y < MinY)
        {
            spriteSpeed.Y *= -1;
            spritePosition.Y = MinY;
        }

    }

    void CheckForCollision()
    {
        BoundingBox bb1 = new BoundingBox(new Vector3(spritePosition1.X - (sprite1Width / 2), 
    spritePosition1.Y - (sprite1Height / 2), 0), new Vector3(spritePosition1.X + (sprite1Width / 2),   
    spritePosition1.Y + (sprite1Height / 2), 0));

        BoundingBox bb2 = new BoundingBox(new Vector3(spritePosition2.X - (sprite2Width / 2),


   spritePosition2.Y - (sprite2Height / 2), 0), new Vector3(spritePosition2.X + (sprite2Width / 2),  
   spritePosition2.Y + (sprite2Height / 2), 0));

        if (bb1.Intersects(bb2))
        {
            soundEffect.Play();
        }

    }

  }
  }
//球类

        using System;
        using System.Collections.Generic;
        using System.Linq;
        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.Input.Touch;
        using Microsoft.Xna.Framework.Media;

        namespace bouncingBlocks
        {
            class Ball
            {

                Texture2D texture1;
                Texture2D texture2;
                int sprite1Height;
                int sprite1Width;
                public Vector2 spritePosition1;

                Vector2 spriteSpeed1 = new Vector2(50.0f, 50.0f);


                public Ball()
                {

                }
            }
        }

若您知道砖块的位置和尺寸,只需将返回BoudingBox的属性添加到砖块类中即可。您还可以将该属性添加到ball。BoundingSphere而不是BoundingBox

  public BoundingBox Boundingbox
  {
      return new BoundingBox(new Vector3(this.position.X, position.Y, 0f),
                             new Vector3(this.position.X + spritewidth,position.Y+spriteheight, 0f));
  }
然后当你检查碰撞时,它看起来像这样

    foreach(Brick b in bricks)
    {
       if(ball.BoundingSphere.Intersects(b.Boundingbox))
       {
           b.Alive=false;
       }
    }

若您知道砖块的位置和尺寸,只需将返回BoudingBox的属性添加到砖块类中即可。您还可以将该属性添加到ball。BoundingSphere而不是BoundingBox

  public BoundingBox Boundingbox
  {
      return new BoundingBox(new Vector3(this.position.X, position.Y, 0f),
                             new Vector3(this.position.X + spritewidth,position.Y+spriteheight, 0f));
  }
然后当你检查碰撞时,它看起来像这样

    foreach(Brick b in bricks)
    {
       if(ball.BoundingSphere.Intersects(b.Boundingbox))
       {
           b.Alive=false;
       }
    }

我想我没有在我贴在上面的课上跑球。我在主游戏类中将它声明为向量spriteposition1,同时它的纹理声明也在主游戏类中。我可以在主游戏类中声明一个boundingsphere并与上面提到的foreach代码一起使用吗?可以,但这不是一个好的做法。我总是为不同的事情单独上课。请记住,如果对象正在移动,则必须在每次更新时更新/创建边界框/球体。我想我没有在上面粘贴的类中运行球。我在主游戏类中将它声明为向量spriteposition1,同时它的纹理声明也在主游戏类中。我可以在主游戏类中声明一个boundingsphere并与上面提到的foreach代码一起使用吗?可以,但这不是一个好的做法。我总是为不同的事情单独上课。记住,如果对象正在移动,则必须在每次更新时更新/创建边界框/球体。