开发者

Problem with Rectangle.Contains method

开发者 https://www.devze.com 2023-02-26 02:21 出处:网络
i built a space invaders game, and the problem is , that the hit sensitivity is way too sensitive. Here is how i trace whether the player was hit.

i built a space invaders game, and the problem is , that the hit sensitivity is way too sensitive. Here is how i trace whether the player was hit.

 bool playersWasShot=false;
 foreach (var shotsInvader in Invadershots)
 {    // below is the Area representing the image size. and the location of the invaders shot
      if(playership.Area.Contains(shotsInvader.Location))
      {
           //player was shot game over.
           playersWasShot=true;
           break;
      }
 }

The area property from the PlayerShip class:

// move simply updates the position of the ship on the x-axis as i move the ship.
// as you can guess. the second variable represents the size of the picture

Area =new Rectangle(new Point(move,900), Properties.Resources.player.Size); 

The shot l开发者_如何转开发ocation property in the Shots class is being updated as the shot travels to the player..

// the shot, travels to the player on the y-axis only
Location=new Point(Location.X, invaderShotLocation);

i checked in the debugger when the game is over..and this is what i got:

This line:

if(playership.Area.Contains(shotsInvader.Location))
{
//i put a debugging point inside this if statment!!!
}

represents info on the ship:

player Area {X=90,Y=900,Width=54,Height=33}
Location    {X=60,Y=900}

represents info on the invaders shot:

Location {X=140,Y=900}

The shot didnt even go inside the players ship..how come

        public static Bitmap SHIP = Properties.Resources.player;
    public Point Location;
    public int move=10;
    public Rectangle Area { set { } get { return new Rectangle(new Point(move, 900), Properties.Resources.player.Size); } }

    public void Draw(Graphics g)
    {

Those are the 2 methods in the ship class that govern all the movement and the drawing

        if (game.Alive) //checks if the player is alive.
        {
             // draws the SHIP picture. 900 is a fixed field..thats where the ship is
             // it is on the y axis that never changes.. x-axis do change..depending
          // on where i move the ship to.
                 g.DrawImage(SHIP, move, 900); 
                Location = new Point(move, 900); 
        }
        else
        {

            g.DrawImage(SHIP, move, 900, SHIP.Width, SHIP.Height);
            Location = new Point(move, 900);
        }
    }



    public void Move(Direction d)
    {

        switch (d)
        {

// an enum is passed when i press the arrows indicating to move left or right case Direction.LEFT: move -= 10; break; case Direction.RIGHT: move += 10; break; } Area = new Rectangle(new Point(move, 900), SHIP.Size);

}

NOTE= SAME LOGIC WAS APPLIED WHEN AN INVADER IS BEING HIT BY THE PLAYER, AND THE INVADER DOES DISAPPEAR ONLY WHEN THE SHOT HITS HIM

Bug found in the Shot class. the players and invaders shots share:

..i found the bug. it was in the shot class. i modified it now. issue has been solved


It seems like the shot did go into the ship. The shot is at (140, 900) and the ship's Area corners are at (90, 900) and (144, 900). (140, 900) is inside of that.

What is the "Location {X=60,Y=900}" part listed after the ship's Area listing? Are you perhaps painting the ship in a different spot than the hit test Area?


Sounds like you have a bug in PlayerShip.Area.Contains. Double check the logic.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号