Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, I got a simple movement test going on. I got a picturebox to move with the W,D,S,A keys. I also got a simple collision detection, if the picturebox collides with another picturebox it changes to the color black. I'm having a hard time making the picturebox not go through the other one does anyone knows how? here is my code.

C#
bool move = true;


  private void Form1_KeyDown(object sender, KeyEventArgs e)
  {
      int x = pictureBox1.Location.X;
      int y = pictureBox1.Location.Y;

      if (e.KeyCode == Keys.W) y -= 5;
      else if (e.KeyCode == Keys.S) y += 5;
      else if (e.KeyCode == Keys.A) x -= 5;
      else if (e.KeyCode == Keys.D) x += 5;

      {
          pictureBox1.Location = new Point(x, y);
      }

          if (move == false)
          {


              pictureBox1.Location = new Point(0, 0);



          }
      }





  private void Form1_Load(object sender, EventArgs e)
  {
      timer1.Interval = 50;

      timer1.Start();

  }

  private void timer1_Tick(object sender, EventArgs e)
  {



      if (pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds))
      {
          pictureBox1.BackColor = Color.Black;

          move = false;

      }
      else
      {
          pictureBox1.BackColor = Color.Blue;


      }
  }




Will appreciate all help thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 9-Jul-12 17:14pm    
I'm not sure if it makes any sense to fix such a bad code. Could you write everything just accurately. Look: ridiculous if (move == false) ... (do I have to explain why this is just silly?), hard-coded 5, 50, etc. This is not programming, just making a mess. And you did not explained the required behavior, nor did you explain where it fails...
--SA
MR. AngelMendez 10-Jul-12 0:48am    
yeah thats code that I was testing but forgot to take off, remember im learning how to do this so i dont really care about neatness right now. And just to let you know... I did explained the required behavior and that is to make picturebox1 not go through picturebox2 as seen on the last sentence of the explanation. It's a simple collision behavior like in a game when the character runs to a wall but doesn't go through, thats what I need help in.
Sergey Alexandrovich Kryukov 11-Jul-12 15:17pm    
"Not really care about neatness" is not a practical approach. The code should be reasonably neat but written reasonably quickly, because re-writing is very likely (unless one want to deceive oneself :-) and polishing something which need replacement due to conceptual problem would be a waste of time. Writing dirty code is pretty much useless...
--SA

1 solution

I'm not sure why you are using a timer to check your collision when it is your movement that can cause the collision. I highly suggest that before you write a single line of code you come up with a flowchart of just what your code should be doing. It would seem that the most logical flow would be something like this:

Get Keystroke
Move box
Has collision occured?
reverse move
change color
Else
continue to watch for a keystroke

You have no need for state management of whether or not move is capable with more generic logic like above. So get rid of the stupid timer, put all logic into method calls MoveBlock(Keys key) InCollisionState() for example. Then try it once again.

You do realize that since your timer controls the check you run a risk of making several moves before the timer event is handled???? That timer event will queue behind any pending key stroke moves.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900