Frage

Im doing a c# maze that should fill up the screen like

x x x x x x
x x       x
x x x x x x
x x  x x x x
x  x x x x x
x x x x  x x
   x x x x x
x    
x x x x x x

The x's are places randomly and the mouse is trying to find its way out. What I am having trouble with is finding a way to fill something up with x's. I tried creating a 2D string array full of x's and fill a label, but no luck.

What is the best way of doing this? Using a panel or maybe something I don't know about? I have to do this in WFA

War es hilfreich?

Lösung

Same example as my other answer but with Windows Forms :) Its nearly the same.

  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
      var blackPen = new Pen(Brushes.Black);
      for (int y = 0; y < 10; y++)
      {
        for (int x = 0; x < 10; x++)
        {
          e.Graphics.DrawLine(blackPen, new Point(x*20, y*20), new Point(x*20 + 10, y*20+10));
          e.Graphics.DrawLine(blackPen, new Point(x*20, y*20+10), new Point(x*20 + 10, y*20));
        }
      }
    }
  }

You have to add a Panel into your window (name it panel1 if it isn't yet) and an event handler for Paint

Nothing complicated.

enter image description here

Andere Tipps

To give you a quickstart with drawing in WPF I created a little example for you which directly draws X's into the window.

  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
      Background = Brushes.Transparent;
    }

    protected override void OnRender(DrawingContext dc)
    {
      base.OnRender(dc);
      var whitePen = new Pen();
      whitePen.Brush = Brushes.White;
      for (int y = 0; y < 10; y++)
      {
        for (int x = 0; x < 10; x++)
        {
          dc.DrawLine(whitePen, new Point(x*20, y*20), new Point(x*20 + 10, y*20+10));
          dc.DrawLine(whitePen, new Point(x*20, y*20+10), new Point(x*20 + 10, y*20));
        }
      }
    }
  }

You can add your condition like if (positions[x, y] == 1) around the DrawLine calls to prevent a specific X from being drawn.

enter image description here

After generating the random map and saving it on the 2d array, cycle trought every cell of the map (adding it to the label) and add a new line everytime it reaches the end of the row.

Also, what happened when you tried to fill the label with the 2d array map?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top