Hi am learning c# myself.I have created a Breakout using c#.But am struck with one problem.When I run the program the paddle and the ball moves properly,but when the ball hits the brick it, the brick does not disappear.

The problem I believe is with the Invalidate() method.I think when I call invalidate() the entire bricks are redrawn including those that are deleted.Kindly look into the code and suggest me some modifications.

Most of the code is just initialization,the logic would be some 30 lines(max).Form1_paint and the timer_tick are the function that I believe the errors are there
Look into line 114;(How to make rectangle dissappear)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Breakout
{
    public partial class Form1 : Form
    {
        private const int WIDTH = 500;
        private const int NBRICKS_PER_ROW = 10;
        private const int NBRICK_ROWS = 10;
        private const int BRICK_SEP = 2;
        private const int BRICK_WIDTH = (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;
        private const int BRICK_HEIGHT = 8;
       
        Rectangle gameBall = new Rectangle(165,212,15,15);
        Rectangle gamePaddle = new Rectangle(180, 434, 72, 10);
        Rectangle[,] brick=new Rectangle[10,10];

        static Timer mytimer = new Timer();
        int yVel = 5;
        int xVel = 2;

        Message mBox = new Message();

        Graphics g;

        public Form1()
        {
            this.Width = WIDTH;
            this.MaximizeBox = false;            
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DoubleBuffered = true;
            mytimer.Tick += new EventHandler(timer_tick);          
            mytimer.Interval = 10;
            mytimer.Start();
        }
        
        void Brick(PaintEventArgs e)
        {
            Pen p = new Pen(Color.Blue, 1.0f);
            Brush b;
           
            int x = 0, y = 0;
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (i < 2) b = new SolidBrush(Color.Red);
                    else if (i >= 2 & i < 4) b = new SolidBrush(Color.Orange);
                    else if (i >= 4 & i < 6) b = new SolidBrush(Color.Yellow);
                    else if (i >= 6 & i < 8) b = new SolidBrush(Color.Green);
                    else b = new SolidBrush(Color.Cyan);
                    brick[i,j] = new Rectangle(x + 10, y + 10, BRICK_WIDTH, BRICK_HEIGHT);
                    x += (BRICK_WIDTH + BRICK_SEP);
                    e.Graphics.DrawRectangle(p, brick[i,j]);
                    e.Graphics.FillRectangle(b, brick[i,j]);
                }
                x = 0;
                y += BRICK_HEIGHT + BRICK_SEP;
            }
        }

        public  void GameBall(Brush b,Pen p,PaintEventArgs e)
        {          
            
            e.Graphics.DrawEllipse(p, gameBall);
            e.Graphics.FillEllipse(b, gameBall); 
        }

        void Paddle(Brush b, Pen p, PaintEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.DarkGreen, gamePaddle);
            e.Graphics.FillRectangle(b, gamePaddle); 
        }
               
        private void Form1_Paint(object sender,PaintEventArgs e)
        {            
            Pen p = new Pen(Color.Blue, 1.0f);
            Brush b = new SolidBrush(Color.Black);

            //Draw Ball
            GameBall(b, p, e);

           //Draw Paddle
            Paddle(b, p, e);

           //Draw Brick
            Brick(e);          
       }

        private void timer_tick(object sender, EventArgs tick)
        {

            Invalidate(gameBall);
           if (gameBall.IntersectsWith(gamePaddle) || gameBall.Y < 10)
               yVel = -yVel;         
           
           for (int i = 0; i < 10; i++)
           {
               for (int j = 0; j < 10; j++)
               {
                   if (gameBall.IntersectsWith(brick[i, j]))
                   {
//how to make one particular rectangular to dissappear
                       brick[i, j].Width = 0;
                       brick[i, j].Height = 0;
                       Invalidate(brick[i,j]);
                       yVel = -yVel;
                   }
               }
           }          
            
           if (gameBall.X< 0 || gameBall.X>500)
               xVel = -xVel;
            gameBall.Location = new Point(gameBall.X+xVel, gameBall.Y + yVel);
        }
        
        //Move paddle along with mouse
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            Invalidate(gamePaddle);
            if (e.X > 0 & e.X < this.Width - 72)gamePaddle.Location = new Point(e.X, gamePaddle.Y);
        }
       
    }
}

Recommended Answers

All 7 Replies

Hi god of the water, welcome at Daniweb.:)
Line 31 does absolutely nothing, so why is it there?
You are setting the Width and the Height of the Brick that is hit to zero. But in your Brick method (should call it DrawBricks!) You are using predefined constants for the Width and Height, so the Brick will never vanish. Hope this helps.

Hi god of the water, welcome at Daniweb.:)
Line 31 does absolutely nothing, so why is it there?
You are setting the Width and the Height of the Brick that is hit to zero. But in your Brick method (should call it DrawBricks!) You are using predefined constants for the Width and Height, so the Brick will never vanish. Hope this helps.

Forget to remove the declaration in line 31.
Yes am using predefined constants!! thanks
So how to remove the brick then.
Help me out!!!

Constants are a very good habbit to have. They come in handy in many cases. But don't use width and height constants if you want to manipulate width and height!

Constants are a very good habbit to have. They come in handy in many cases. But don't use width and height constants if you want to manipulate width and height!

Ok, but is there any built in property like brick[i,j].remove(),So as to remove the bricks.
If not how to do it then?

Arrays don't have a remove method, a List<T> has. Where T could be a Brick object of a to be defined Brick class. This Brick class could have a property Empty(true or false). With that property your List could figure out which Bricks to draw and which not.

I have modified the code and it works now.The brick gets disappeared.I changed from array to list and it worked.Here is the modified code.

The code is fine but the ball traces the same path,there is some logic problem in ball movement,can some one help me with that

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Breakout
{
    public partial class Form1 : Form
    {
#region variables
        private const int WIDTH = 500;
        private const int NBRICKS_PER_ROW = 10;
        private const int NBRICK_ROWS = 10;
        private const int BRICK_SEP = 2;
        private const int BRICK_WIDTH = (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;
        private const int BRICK_HEIGHT = 8;
       
        Rectangle gameBall = new Rectangle(165,212,15,15);
        Rectangle gamePaddle = new Rectangle(180, 434, 72, 10);
        Rectangle nothing = new Rectangle(0, 0, 0, 0);

        List<Rectangle> brick = new List<Rectangle>();

        static Timer mytimer = new Timer();
        int yVel = 5;
        int xVel = 2;
        PaintEventArgs pt;
        Message mBox = new Message();

#endregion

        public Form1()
        {
            this.Width = WIDTH;
            this.MaximizeBox = false;            
            InitializeComponent();
    }

        private void Form1_Load(object sender, EventArgs e)
        {
            DoubleBuffered = true;
            mytimer.Tick += new EventHandler(timer_tick);          
            mytimer.Interval = 10;
            mytimer.Start();
        }

        void Brick(Brush b, Pen p, PaintEventArgs e)
        {    
            int x = 0, y = 0,count=0;
            for (int i = 0; i < 10; i++)
            {               
                for (int j = 0; j < 10; j++)
                {
                    if (i < 2) b = new SolidBrush(Color.Red);
                    else if (i >= 2 & i < 4) b = new SolidBrush(Color.Orange);
                    else if (i >= 4 & i < 6) b = new SolidBrush(Color.Yellow);
                    else if (i >= 6 & i < 8) b = new SolidBrush(Color.Green);
                    else b = new SolidBrush(Color.Cyan);                  
                    Rectangle temp = new Rectangle(x + 10, y +10,            BRICK_WIDTH, BRICK_HEIGHT);
                    brick.Add(temp);
                    x += (BRICK_WIDTH + BRICK_SEP);
                   e.Graphics.DrawRectangle(p, brick[count]);
                    e.Graphics.FillRectangle(b, brick[count]);
                    count++;
                }
                x = 0;
                y += BRICK_HEIGHT + BRICK_SEP;
            }
        }

        public  void GameBall(Brush b,Pen p,PaintEventArgs e)
        {          
            
            e.Graphics.DrawEllipse(p, gameBall);
            e.Graphics.FillEllipse(b, gameBall); 
        }

        void Paddle(Brush b, Pen p, PaintEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.DarkGreen, gamePaddle);
            e.Graphics.FillRectangle(b, gamePaddle); 
        }
               
        private void Form1_Paint(object sender,PaintEventArgs e)
        {
            Pen p = new Pen(Color.Blue, 1.0f);
            Brush b = new SolidBrush(Color.Black);

            //Draw Ball
            GameBall(b, p, e);

           //Draw Paddle
            Paddle(b, p, e);

           //Draw Brick
            Brick(b,p,e);    
       }      

        private void timer_tick(object sender, EventArgs tick)
        {
           Invalidate();
            
           if (gameBall.IntersectsWith(gamePaddle) || gameBall.Y < 10)
               yVel = -yVel;
           if (gameBall.X < 0 || gameBall.X > 500)
               xVel = -xVel;
           
           for (int i = 0; i < 100; i++)
           {               
                   if (gameBall.IntersectsWith(brick[i]))
                   {
                       brick.RemoveAt(i);
                       brick.Insert(i, nothing);
                       yVel = -yVel;       
                                           
                   }               
           }        
           gameBall.Location = new Point(gameBall.X+xVel, gameBall.Y + yVel);
        }
        
        //Move paddle along with mouse
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            
            if (e.X > 0 & e.X < this.Width - 72)gamePaddle.Location = new Point(e.X, gamePaddle.Y);
        }
       
    }
}

Arrays don't have a remove method, a List<T> has. Where T could be a Brick object of a to be defined Brick class. This Brick class could have a property Empty(true or false). With that property your List could figure out which Bricks to draw and which not.

Thank You it worked!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.