I am trying to make a tank game that operates like the old "Game and Watch" consoles. It has two buttons (I may add two more later) and nine textboxes, which makes for a pretty small playing field. A visual aid can be found HERE.
In order to make the tank move around, it would be pretty easy just to make a whole bunch of if loops like this:
private void btnRight_Click(object sender, EventArgs e) { if (txtBoxA1.Text =="[l'l]") { txtBoxA1.Text=""; txtBoxA2.Text="[l'l]"; } } |
Needless to say, this would be effective, but incredibly impractical. For instance, suppose I decide to expand the playing field later on and end up having to make four new loops for each cell? To put it lightly, that would be a pain in the butt.
So, is there another way? Is there a way I could use a single switch statement to take care of this? The following statement would return a variable that says whether or not the tank can move.
| switch(Tank) { case "A1": //set variables for A1 break; case "A2": //set variables for A2 break; } |
My last idea is the one that I would prefer to be able to use, but I'm not so sure how I might make it work. I would like to assign each text box a coordinate variable. I want cell A1 to be called 0,0; cell B3 to be called1,2 and so on.
If I can't figure out how to do that, I would at least like to assign each cell an X and a Y variable so that if I say X+1, the tank will move to the box that is X+1.
In the end I would like to be able to manipulate the tank with commands like these:
|
Tank = 2,0; |
AlanPosted Aug 16, 2008, 7:51 AM
Why don't you create a two dimensional array of textboxes. For example, in Form1_Load :
TextBox[,] cell = new TextBox[3,3]
{
{txtBoxA1, txtBoxA2, txtBoxA3},
{txtBoxB1, txtBoxB2, txtBoxB3},
{txtBoxC1, txtBoxC2, txtBoxC3}
};
cell[0,0] will now represent txtBoxA1 and cell[1,2] will be txtBoxB3.
This should make it easier to generalize your code for moving the tank around.
AnthonyPosted Aug 16, 2008, 10:48 AM
0_o? Um. Yes, that would be the smart thing to do, wouldn't it?