Anthony

Anthony

  • NA
  • 17
  • 0

Can I make my own X and Y variables?

Jul 30 2008 1:37 PM

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;

Tankx = Tankx + 1;

if (Tanky == < 0)
{Tanky = 0;}

I understand if you are having trouble understanding my post. I'm not sure if I am explaining myself clearly or not. If, however, you do understand my problem, could you give me a nudge in the right direction?

Answers (2)