locked
C# maze tile based program RRS feed

  • Question

  • I am creating a tile based maze using a file and loading a file to create a maze. I have a player which is a picturebox which i need to check whether the tile they are moving to is a wall or a path. My code is is below. I'm not sure how to approach it so any help would be appreshated

      public void loadlvl(int userlvlchoice)
            {
          
                string mapchoice = "";
            
                if (userlvlchoice == 1)
                {
                    mapchoice = "Lvl1Map.txt";
                    // assigns the value of mapchoice to the string
                }
                else if (userlvlchoice == 2)
                {
                    mapchoice = "Lvl2Map.txt";
                }

                else if (userlvlchoice == 3)
                {

                }
                else if (userlvlchoice == 4)
                {

                }
                else if (userlvlchoice == 5)
                {

                }

                else if (userlvlchoice == 6)
                {

                }


                using (StreamReader reader = new StreamReader(mapchoice))
                {
                    int r = 0;
                    int c = 0;
                  
                   

                    while (!reader.EndOfStream)
                    {
                        string csvline = reader.ReadLine();
                  
                        foreach (string value in csvline.Split(','))
                        {
                           cell cell = new cell();
                            cell.Size = new Size(30, 30);
                            cell.Location = new Point(30 * c, 30 * r);
                            cell.SizeMode = PictureBoxSizeMode.StretchImage;

                            

                            if (value == "p")
                            {
                                cell.ImageLocation = "lightskyblue.png";
                                // sets the image of the path to a skyblue tile
                                cell.celltype = "path";

                               
                         
                            }
                            else if (value == "w")
                            {
                                cell.ImageLocation = "blue.png";
                                // sets the image of the wall to a blue tile
                                cell.celltype = "wall";

                            }


                            pnlTilePanel.Controls.Add(cell);
                            // adds a cell to the panel 

                            c++;
                        }

                        r++;
                        c = 0;
                    }
                }

       
               
            }


            private void btn_selectlvl_Click(object sender, EventArgs e)
            {

                int userlvlchoice;

                userlvlchoice = Convert.ToInt32(Nudchooselvl.Value);
                // stores the value of the numericupanddown when the button is clicked 


                loadlvl(userlvlchoice);
                //passes the userlvlchoice into loadlvl
                btn_ExitGame.Hide();
                lbl_Selectlvl.Hide();
                Nudchooselvl.Hide();
                btn_selectlvl.Hide();
                btn_playgame.Show();

                //hides all the user iterface items that are not involved with game directly
            }

            private void btn_playgame_Click(object sender, EventArgs e)
            {
                // getplayerstartlocation();
                pbx_player.Show();
                pbx_enemy.Show();
                btn_playgame.Hide();
                btn_ExitGame.Hide();

            }

            private void btn_ExitGame_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Exiting game");
                Environment.Exit(0);
                // exits the game upon click event
            }


            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {

                int leftvalue;
                int topvalue;
                int tiledimension = 30;
                int panelwidth;
                int panelheight;
            

                leftvalue = pbx_player.Left;

                //stores the value of location of the bottom left cornor of the picturebox
                topvalue = pbx_player.Top;
                //stores the value of location of the top right hand cornor of the picturebox

                panelheight = pnlTilePanel.Height;

                panelwidth = pnlTilePanel.Width;

                if (e.KeyData == Keys.D || e.KeyData == Keys.Right)
                //controllable by two keys
                {

                    //updates the bottom left cornor value by tiledimension which moves the player right by 1 tile
                    pbx_player.Left = leftvalue + tiledimension;

                }

                else if (e.KeyData == Keys.A || e.KeyData == Keys.Left)
                //controllable by two keys
                {
                    pbx_player.Left = leftvalue - tiledimension;
                    //updates the bottom left cornor value by -tiledimension which moves the player left by 1 tile

                }

                else if (e.KeyData == Keys.W || e.KeyData == Keys.Up)
                //controllable by two keys
                {
                    pbx_player.Top = topvalue - tiledimension;
                    //updates the top right hand cornor value by -tiledimensionwhich moves the player up by 1 tile

                }
                else if (e.KeyData == Keys.S || e.KeyData == Keys.Down)
                //controllable by two keys
                {

                    pbx_player.Top = topvalue + tiledimension;
                    //updates the top right hand cornor value by tiledimension which moves the player down by 1 tile


                }

                if(pbx_player.Bounds.IntersectsWith())
                {


                }



            }

            private void tmr_Gametimer_Tick(object sender, EventArgs e)
            {
                int time = 0;

                lbl_showtimer.Text = "Timer: " + time;

            }


            // public void getplayerstartlocation()
            // {

            //   Random r = new Random();

            //   int randomxpostion = r.Next(1, 24);
            //int randomypostion = r.Next(1, 24);
            //int startxlocation=0;
            // int startylocation = 0;

            // startxlocation = (randomxpostion * 30);
            //startylocation = (randomypostion * 30);

            // pbx_player.Location = new Point (startxlocation, startylocation);

            


        }
          
        }

    Thursday, December 6, 2018 10:55 AM

All replies

  • Well, the first step is the Single Responsibility Principle SRP. This means separating logic from UI and from persistence.

    We need at least a Game class, a Board class and a Player class.

    The game class is the overall logic parent. It coordinates loading and saving games, boards and players. It also hosts the games logic.

    The board class is responsible for the playground, here your maze. It knows the players in the maze.

    The player class represents us, the players individual values.

    Start by implementing these classes first. Then do the first visualization. The logic should be a separate project from your UI in your solution.

    Do not waste time on levels. They come later, but in the simple case they are just different board instances. Also make use of class diagrams. It helps to visualize your code and you can use it to show us your architecture, which in such cases is of interest.

    -- UPDATE --

    I would think about a architecure like this

    Where PlayerOnBoard is a decorator of Player.



    Thursday, December 6, 2018 11:36 AM