How do I get a maze to load on my form?

时间:2018-04-26 17:01:05

标签: c#

I am creating a pacman style game for uni. Currently I am trying to get the the maze to load but it wont appear on the form and i don't see any problems with the code.

This maps the contents of a text file so it can be used as a map for a game:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace MazeForm
{
    class Cell
    {
        const int CellSize = 20; //cell dimension in pixels
        private int x, y;
        private char type;
        private bool isVisible = true;

        //Constructor
        public Cell(int x, int y, char type)
        {
            this.type = type;
            this.x = x;
            this.y = y;
        }

        //Set cell as visited (no pill inside it)
        public bool IsVisible
        {
            get
            {
                return isVisible;
            }

            set
            {
                isVisible = value;
            }
        }

        public char CellType
        {
            get { return type; }
            set { type = value; }
        }

        //draw the cell 
        public virtual void DrawBackground(Graphics g)
        {

            switch (type)
            {
                case 'w'://wall
                    g.FillRectangle(Brushes.Black, x * CellSize, y * CellSize, CellSize, CellSize);
                    break;
                case 'p'://path
                    g.FillRectangle(Brushes.White, x * CellSize, y * CellSize, CellSize, CellSize);
                    break;
                case 's'://starting position
                    g.FillRectangle(Brushes.Blue, x * CellSize, y * CellSize, CellSize, CellSize);
                    g.DrawString("S", new Font("Arial", 12, FontStyle.Bold), new SolidBrush(Color.Red), new Point(x * CellSize, y * CellSize));
                    break;
                case 'f'://finishing line
                    g.FillRectangle(Brushes.Blue, x * CellSize, y * CellSize, CellSize, CellSize);
                    g.DrawString("F", new Font("Arial", 12, FontStyle.Bold), new SolidBrush(Color.Red), new Point(x * CellSize, y * CellSize));
                    break;
                case 'g'://guard position
                    g.FillRectangle(Brushes.Blue, x * CellSize, y * CellSize, CellSize, CellSize);
                    g.DrawString("G", new Font("Arial", 12, FontStyle.Bold), new SolidBrush(Color.Red), new Point(x * CellSize, y * CellSize));
                    break;
                default:
                    break;
            }
        }
    }
}

And this is a form that displays the map and I can't see where I've gone wrong:

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;
using System.IO;

namespace MazeForm
{
    public partial class Level1 : Form
    {
        private const int CellSize = 20;
        private Cell[,] map;

        public Level1()
        {
            InitializeComponent();
        }

        private Cell[,] CreateArray(string mapFile)
        {
            int numRows = 0;
            int numColumns = 0;

            string line = null;

            try
            {
                StreamReader mapData = new StreamReader(mapFile);

                line = mapData.ReadLine();
                numColumns = line.Length;

                while (line != null)
                {
                    numRows++;
                    line = mapData.ReadLine();
                }

                mapData.Close();

            }
            catch (System.IO.IOException e)
            {

                MessageBox.Show(e.Message);
            }

            Cell[,] map = new Cell[numColumns, numRows];

            try
            {
                StreamReader mapData = new StreamReader(mapFile);

                line = mapData.ReadLine();

                int rowIndex = 0;

                while (line != null)
                {
                    for (int columnIndex = 0; columnIndex < line.Length; columnIndex++)
                    {
                        map[columnIndex, rowIndex] = new Cell(columnIndex, rowIndex, line[columnIndex]);

                    }

                    rowIndex++;

                    line = mapData.ReadLine();
                }

                mapData.Close();
            }
            catch (System.IO.IOException e)
            {

                MessageBox.Show(e.Message);
            }

            return map;
        }

        private void Level1_Load(object sender, EventArgs e)
        {
            map = CreateArray("..\\..\\Resources\\Maps\\Map1.txt");

            mazePictureBox.Width = map.GetLength(0) * CellSize;
            mazePictureBox.Height = map.GetLength(1) * CellSize;
        }

        private void mazePictureBox_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            for (int row = 0; row < map.GetLength(0); row++)
                for (int column = 0; column < map.GetLength(1); column++)
                {
                    map[row, column].DrawBackground(g);
                }
        }
    }
}

0 个答案:

没有答案