9 Replies - 1480 Views - Last Post: 08 December 2015 - 03:27 PM Rate Topic: -----

#1 lpaoloni   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-December 15

2D Array of Picture Boxes for Break Out Game C#

Posted 08 December 2015 - 06:21 AM

How do I create a 2-dimensional array of PictureBoxes?

I'm trying to create a Break Out game in C# and I am currently using a PictureBox for the Bat, Ball, Environment and Brick. Now I'm wanting to create multiple Brick PictureBoxes using a 2-dimensional array which inherits the same collision and properties (except color/colour) as the original Brick. Please state if you need to see my code to help. I am only wanting to know if it's possible and an example of how someone would do it.

Is This A Good Question/Topic? 0
  • +

Replies To: 2D Array of Picture Boxes for Break Out Game C#

#2 lpaoloni   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-December 15

Re: 2D Array of Picture Boxes for Break Out Game C#

Posted 08 December 2015 - 06:27 AM

How do I create a 2-dimensional array of PictureBoxes?

I'm trying to create a Break Out game in C# and I am currently using a PictureBox for the Bat, Ball, Environment and Brick.
Now I'm wanting to create multiple Brick PictureBoxes using a 2-dimensional array which inherits the same collision and properties (except color/colour) as the original Brick.
Please state if you need to see my code to help. I am only wanting to know if it's possible and an example of how someone would do it.

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7289
  • View blog
  • Posts: 24,660
  • Joined: 05-May 12

Re: 2D Array of Picture Boxes for Break Out Game C#

Posted 08 December 2015 - 07:20 AM

Merging duplicate threads.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7289
  • View blog
  • Posts: 24,660
  • Joined: 05-May 12

Re: 2D Array of Picture Boxes for Break Out Game C#

Posted 08 December 2015 - 07:31 AM

View Postlpaoloni, on 08 December 2015 - 08:21 AM, said:

Please state if you need to see my code to help.

Yes. Please post your code in code tags.

View Postlpaoloni, on 08 December 2015 - 08:21 AM, said:

I am only wanting to know if it's possible and an example of how someone would do it.

Yes, it is possible, but there are several reasons not to do it this way:
1. It would be incredibly slow and a resource hog. Each picture box is a window and Windows highly recommends keeping the number of window handles per application a low as possible. Notice that MineSweeper does not use one window per square. There is a reason for this (beyond making it harder for bots).
2. WinForms is at end of life. WPF underlying use of XAML is the way of the future. If you want to see how to implement something like Breakout using WPF, take a quick read of Advanced MVVM by Josh Smith where he implements Bubble Burst using WPF. Many of the techniques you'll need for Breakout can be found in how how he did Bubble Burst.
Was This Post Helpful? 0
  • +
  • -

#5 lpaoloni   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-December 15

Re: 2D Array of Picture Boxes for Break Out Game C#

Posted 08 December 2015 - 08:57 AM

(Thank you for merging them)
I understand WinForms is at end of life now but we've been given the task to create Break out using it. I've already created it by drawing it all on one picture box but found it to produce a terrible amount of graphics flicker (even double buffer wasn't helping).It seems to be working completely fine at the moment and was going to implement only 5 more picture boxes. Unless there is a way to draw an array of bricks inside the brickPictureBox and still have collision applied from the ballPictureBox?
Here is my code: (Note I've only started programming on C# less than 3 weeks ago so please point out any bad programming)


        
        #region VARIABLES
        public int ballX = 4, ballY = 4;
        public int score = 0;
        public int lives;
        #endregion

        #region FORM1
        public Form1()
        {
            InitializeComponent();
            draw_Ball();
        }
        #endregion

        #region FORM1_LOAD
        private void Form1_Load(object sender, EventArgs e)
        {
            playBtn.Enabled = false;
            drawBricks.Visible = false; 
            lblScore.Text = ("Score: " + score);
            bricksPictureBox.BackColor = Color.Red;
        }
        #endregion

        #region BUTTONS
        private void insertBtn_Click(object sender, EventArgs e)
        {
                playBtn.Enabled = true;
                lives = Convert.ToInt32(lblLives.Text);
                lives = 5;
                lblLives.Text = lives.ToString();
        }
        private void playBtn_Click(object sender, EventArgs e)
        {
            MoveBall();
            bricksPictureBox.Visible = true;
            bricksPictureBox();
            Score();
            timer1.Interval = 28;
            timer1.Enabled = true;
        }
        private void stopBtn_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }
        private void displayBricksBtn_Click(object sender, EventArgs e)
        {
            draw_Bricks();
        }       
        private void exitBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region TIMER
        private void timer1_Tick(object sender, EventArgs e)
        {
            checkCollision();
            draw_Bat();
        }
        #endregion

        #region METHODS
        public void Score()
        {
            if (score == 0)
            {
                lives = Convert.ToInt32(lblLives.Text);
                lives--;
                lblLives.Text = lives.ToString();..
            }

            if (lives == 0 & score == 0)
            {

                playBtn.Enabled = false;
                MessageBox.Show("End of game!");
                exitBtn.Visible = true;
            }
            if (lives <= 5 & score >= 60)
            {
                score = 0;
                lblScore.Text = ("Score: " + score);

                lives = Convert.ToInt32(lblLives.Text);
                lives++;
                lblLives.Text = lives.ToString();
            }
        }


        public void draw_Bricks()
        {
            drawBricks.Visible = true;

        }
        public void draw_Ball()
        {
            // picture box in the shape of a circle
            System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
            gp.AddEllipse(0, 0, drawBall.Width - 10, drawBall.Height - 5);
            Region rg = new Region(gp);
            drawBall.Region = rg;
            // ...end circle
        }
        public void draw_Bat()
        {
            drawBat.Left = Cursor.Position.X - (drawBat.Width / 2);
        }
        public void MoveBall()
        {
            drawBall.Top = 50;
            drawBall.Left = 50;
            ballX = 4;
            ballY = 4;

        }
        public void checkCollision()
        {
            drawBall.Left += ballX;
            drawBall.Top += ballY;

            if (drawBall.Bottom >= drawBat.Top && drawBall.Bottom <= drawBat.Bottom && drawBall.Left >= drawBat.Left && drawBall.Right <= drawBat.Right)
            {
                ballY += 2;
                ballX += 2;
                ballY = -ballY;
            }

            //Left of backgroundPic
            if (drawBall.Left <= backgroundPic.Left)
            {
                ballX = -ballX;
            }
            //Right of backgroundPic
            if (drawBall.Right >= backgroundPic.Right)
            {
                ballX = -ballX;
            }
            //Top of backgroundPic
            if (drawBall.Top <= backgroundPic.Top)
            {
                ballY = -ballY;
            }
            //Bottom of backgroundPic
            if (drawBall.Bottom >= backgroundPic.Bottom)
            {
                timer1.Enabled = false;
                lives = Convert.ToInt32(lblLives.Text);
                lives--;
                lblLives.Text = lives.ToString();
            }

            if (drawBricks.Visible == true)
            {
                if (drawBall.Bottom >= drawBricks.Top && drawBall.Bottom <= drawBricks.Bottom && drawBall.Left >= drawBricks.Left && drawBall.Right <= drawBricks.Right)
                {
                    ballY = -ballY;
                    drawBricks.Visible = false;

                    score += 60;
                    lblScore.Text = ("Score: " + score);
                }
            }
        }
            #endregion
    }

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7289
  • View blog
  • Posts: 24,660
  • Joined: 05-May 12

Re: 2D Array of Picture Boxes for Break Out Game C#

Posted 08 December 2015 - 02:57 PM

Usually for games, you want to do your own painting and not depend on somebody else. To wit, you'll want to override the paint and background paint events to more efficiently redraw the game area and avoid the flicker. Since you are not showing how that picture box is being composed an rendered it is hard to comment directly about it. Irregardless, you'll want to do the painting yourself within your form (or a user control) instead of depending on the picture box.

Detecting collisions can be as simple as bounding box checks of brick vs. ball, or you could go down to the level of doing radius checks to verify the bounding box check.
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15561
  • View blog
  • Posts: 62,303
  • Joined: 12-June 08

Re: 2D Array of Picture Boxes for Break Out Game C#

Posted 08 December 2015 - 03:07 PM

@skydiver - or have an array of 'rectangle' objects.. check if something is 'inside' them or not.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7289
  • View blog
  • Posts: 24,660
  • Joined: 05-May 12

Re: 2D Array of Picture Boxes for Break Out Game C#

Posted 08 December 2015 - 03:11 PM

Yes, that works great when the brick is always larger than the ball and corner hits don't count.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15561
  • View blog
  • Posts: 62,303
  • Joined: 12-June 08

Re: 2D Array of Picture Boxes for Break Out Game C#

Posted 08 December 2015 - 03:12 PM

Certainly, the original worked that way. ;)
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7289
  • View blog
  • Posts: 24,660
  • Joined: 05-May 12

Re: 2D Array of Picture Boxes for Break Out Game C#

Posted 08 December 2015 - 03:27 PM

Sorry, I was never a Breakout fan so I didn't notice that behavior. Got bored quickly within minutes. Archanoid did capture my interest for one afternoon on my C-64, but sadly the tape only got loaded in once. I don't think I ever loaded in up again after playing with it the first day.

Anyway, if that's how the original behaves, then the collision detection becomes so much simpler.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1