问题
I posted a Q. earlier about arrays and the replies lead me to change my game design. I'm building a 2D game map from tiles (Cells). I need to add a pen line to some cells, representing a wall you cannot pass through. This pen line is represented by Cell.TopWall, Cell.BottomWall etc etc of type boolean as shown in my code. This is my whole app so far. It draws the grid without the pen lines for walls. Take note of commented out code where i have tried different things like stating where the walls go with an array of 0's (no wall) and 1's (walls). My question is, can i get a hint on how to implement the walls where i would like? I hope i have shared enough information about my app. Thanks
Cell.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Map
{
public class Cell : PictureBox
{
bool LeftWall;
bool TopWall;
bool RightWall;
bool BottomWall;
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Map
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Array of tiles
/// </summary>
//PictureBox[,] Boxes = new PictureBox[4,4];
Cell[,] Map = new Cell[4,4];
/*int [][] Boxes = new int[][] {
new int[] {0,0}, new int[] {1,0,0,1},
new int[] {1,0}, new int[] {1,0,1,0},
new int[] {0,1}, new int[] {0,0,0,1},
new int[] {1,1}, new int[] {1,1,1,0},
new int[] {2,0}, new int[] {1,1,0,0},
new int[] {2,1}, new int[] {0,0,0,1},
new int[] {3,1}, new int[] {1,0,1,0},
new int[] {0,2}, new int[] {0,0,1,1},
new int[] {1,2}, new int[] {1,0,1,0},
new int[] {2,2}, new int[] {0,1,1,0}};*/
#region Runtime load
/// <summary>
/// Build the map when window is loaded
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
BuildMap();
}
#endregion
#region Build grid
/// <summary>
/// Draw every tile on the map
/// </summary>
public void BuildMap()
{
for (int row = 0; row < 3; row++)
{
for (int column = 0; column < 3; column++)
{
DrawBox(row, column);
}
}
//draw the exit box
DrawBox(1, 3);
}
#endregion
#region Draw
/// <summary>
/// Draw one tile
/// </summary>
/// <param name="row"></param>
/// <param name="column"></param>
public void DrawBox(int row, int column)
{
Map[row, column] = new Cell();
Map[row, column].Height = 100;
Map[row, column].Width = 100;
Map[row, column].BorderStyle = BorderStyle.FixedSingle;
Map[row, column].BackColor = Color.BurlyWood;
Map[row, column].Location = new Point((Map[row, column].Width * column), (Map[row, column].Height * row));
this.Controls.Add(Map[row, column]);
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
//formGraphics.DrawLine(myPen, 0, 0, 200, 200);
//myPen.Dispose();
//formGraphics.Dispose();
}
#endregion
}
}
来源:https://stackoverflow.com/questions/18325291/implementing-a-2d-map