Author Topic: Simplest way to detect solidity using Windows Form Labels...in an array  (Read 5853 times)

SmartBoy16

  • Contributor
  • Fanatic
  • **
  • Posts: 587
  • Looking for inspiration.....
    • View Profile
    • Email
This is somewhat related, but i am having a few issues for solidity.

I am making a Diver-dives-into-water-to-get-the-treasure-and-avoid-sharks game for a final. the game is made up of Windows form labels, a "lblPlayer" label as the player "sprite", and a "Grid[Row, Col]" resembling tiles. so far this is what i have to detect solidity.
Code: [Select]
// Move Player
            x += dx;
            y += dy;
            PixelX = Convert.ToInt32(x);
            PixelY = Convert.ToInt32(y);

bool HitSolid = false;
            // Tile Interactions
            for (int Row = 0; Row < Gh; Row++)
            {
                for (int Col = 0; Col < Gw; Col++)
                {
                    if (lblPlayer.Bounds.IntersectsWith(Grid[Row, Col].Bounds))
                    {
                        //if (Grid[Row, Col].BackColor == Color.Gold) DONT DELETE THIS!!! THIS IS FOR GETTING THE TREASURE!!!
                        //{
                        //    btnMap.Enabled = true;
                        //}
                        if ((Grid[Row, Col].BackColor == Color.Gray) || (Grid[Row, Col].BackColor == Color.SandyBrown)) // if the color of the tile is gray (rock) or SandyBrown (ground)
                        {
                            HitSolid = true;
                            //Check Vertical Collision
                            if (dy != 0) //if moving up or down
                            {
                                if (y + 20 > Grid[Row, Col].Bounds.Top && dy > 0) // if passed the top and moving downward
                                {
                                    y = Grid[Row, Col].Bounds.Top - 22;
                                    break;
                                }
                                else if (y < Grid[Row, Col].Bounds.Bottom && dy < 0) // else if passed the bottom and moving upward
                                {
                                    y = Grid[Row, Col].Bounds.Bottom + 2;
                                    break;
                                }
                            }
                            //Check Horizontal collision
                            if (dx != 0)  // if moving left or right
                            {
                                if (x + 20 > Grid[Row, Col].Bounds.Left && dx > 0) // if passed the Leftside and moving right
                                {
                                    x = Grid[Row, Col].Bounds.Left - 22;
                                    break;
                                }
                                else if (x < Grid[Row, Col].Bounds.Right && dx < 0) // else if passed rightside and moving left
                                {
                                    x = Grid[Row, Col].Bounds.Right + 2;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (HitSolid) break;
            }
            if (HitSolid)
            {
                PixelX = Convert.ToInt32(x);
                PixelY = Convert.ToInt32(y);
            }



            // Draw Player
            lblPlayer.Location = new Point(PixelX, PixelY);

this is all done in the timer with a 1 millisecond interval. (basically, as fast as it can go)
im using a keydown for movement and a keyup to stop the player

this is the link to the program in question: http://www.box.net/shared/6ao5z2qmzu


Looking to the skies.....

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Two questions:

1. Is there a question in there somewhere?  Or were you just stating facts?

2. Why are you running the solidity check every frame?  Why not just run it once when the player taps up, down, left or right?
Edward Dassmesser

SmartBoy16

  • Contributor
  • Fanatic
  • **
  • Posts: 587
  • Looking for inspiration.....
    • View Profile
    • Email
1. The question is, Is there an easier way to do this? (forgot to put that in there)

2. it was the way i was taught.
   is there a way i can check once I push u/d/l/r, like in the KeyDown function?
Looking to the skies.....

Jam0864

  • Contributor
  • Fanatic
  • **
  • Posts: 744
    • MSN Messenger - marmalade0864@hotmail.com
    • View Profile
    • Jam0864's Content Dump
    • Email
EDIT:// Nevermind, didn't read through all the posts.

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
You shouldn't need a timer at all for any of this.  Just run the collision detection code once, when the player taps a direction key.  I'd have to know more about how your labels are laid out, but if they are in a grid, you shouldn't have to run through two loops either.  You should just have to check the tile the player sprite is moving into, to see if it's solid. If it is, do nothing.  If not, move the player into the new location.
Edward Dassmesser

SmartBoy16

  • Contributor
  • Fanatic
  • **
  • Posts: 587
  • Looking for inspiration.....
    • View Profile
    • Email
this is the code for making the tiles.
Code: [Select]
public Form1()
        {
            InitializeComponent();

            for (int Row = 0; Row < Gh; Row++) // draws grid
            {
                for (int Col = 0; Col < Gw; Col++)
                {
                    Grid[Row, Col] = new Label();
                    Grid[Row, Col].Size = new Size(19, 19);
                    Grid[Row, Col].Location = new Point(50 + Col * 20, 50 + Row * 20);
                    Grid[Row, Col].BackColor = Color.Aqua;
                    Grid[Row, Col].BorderStyle = BorderStyle.FixedSingle;

                    Controls.Add(Grid[Row, Col]);
                }
            }
        }


wait, did i mention that this is being done on a Windows Form?
Looking to the skies.....

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
It would be simpler to just draw colored rectangles during the OnPaint event instead of managing all those labels.  The TileShapeTest program I just uploaded in another discussion is a good example.

Code: [Select]
      System.Color[,] Grid;
...
      protected override void OnPaint(PaintEventArgs e)
      {
         base.OnPaint (e);

         for (short y=0; y<32; y++)
         {
            for (short x = 0; x<32; x++)
            {
               e.Graphics.FillRectangle(Grid[x,y].Color, x * 32, y * 32, 32, 32);
            }
         }
      }