Friday, July 31, 2009

C++ moving an object within a 2D array with user inputs?

For the sake of people understanding i'm going to simplify this a little bit but i have a 2D array that's a square of X's around the border with a couple x's scattered inside. I need to place an "O" inside that array and have it move around with user inputs and know when it has hit an "X" or gone out of the box. Most importantly what i need to know is how to have an object move around in an array with inputs, i could probly figure out how to make it know when it hit an x. If anyone can help it would save my weekend. Thank's a lot!

C++ moving an object within a 2D array with user inputs?
I don't know what type of input you are going to have. I am going to assume keyboard arrow keys. I will normally map the key strokes to direction


EG.


8 --%26gt; UP


2 --%26gt; Down


4 --%26gt; Left


6 --%26gt; Right


7 --%26gt; left+up


1 --%26gt; left+down





This way you can write another procedure to interpret the direction and update current position (X,Y)


EG





Input to move function


X = 3 , Y = 5, Direction = Left + Up





Output





X = 2, Y = 4 %26lt;---- X = X - 1, Y = Y -1





You can write a nice big switch around the calculations with boundary checks.





One thing to know though when you say UP the position goes down and when you say down the position goes up. This is because the coordinate system you use.





You can use XOR put to move objects around. When you draw something with XOR put first time the image is drawn, second time same place if you draw using XOR put the image is erased.





If you get stuck email me.





best of luck.


No comments:

Post a Comment