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);
}
}
}