Jono_2007
Jono_2007

Reputation: 63

Deleting a rectangle class

I'm creating a breakout game in c#, I am using boolean values to tell the program whether to draw the brick or not, i have used rectangle classes to put over the drawn rectangles to be able to tell if the ball has hit the bricks

This is the code i used so far:

Rectangle brick4 = new Rectangle((490), (50), (50), (20));
bool hitBrick4 = brick4.Contains(x, y);
if (hitBrick4)
{
    brick4 = new Rectangle(0, 0, 0, 0);
    brick4draw = false;
    yChange = -yChange;
    bricksdestroyed = bricksdestroyed + 1;
   lblBricksDestroyed.Text = "Bricks Hit: " + bricksdestroyed;
}

I am trying to delete the rectangle class after the brick has been hit by setting it to zero's, but the rectangle is still there and can still be hit, triggering the hit brick code.

Any suggestions, sorry if i am not being very clear.

Upvotes: 3

Views: 1636

Answers (3)

CandorZ
CandorZ

Reputation: 11

OMG, I was looking for a similar thing but was amazed by the solution I found! You can just use VisualBasic PowerPacks, it is included with my version of Visual Studio 2008

Here's a sample code that will draw a rectangle over a TextBox, i.e. I am giving it a custom border [code]

Dim x = TextBox1.Location.X
Dim y = TextBox1.Location.Y
Dim width = TextBox1.Width
Dim height = TextBox1.Height
Dim ShapeContainer1 As New Microsoft.VisualBasic.PowerPacks.ShapeContainer
Me.Controls.Add(ShapeContainer1)
Dim RectangleShape1 As New Microsoft.VisualBasic.PowerPacks.RectangleShape
ShapeContainer1.Shapes.AddRange(New Microsoft.VisualBasic.PowerPacks.Shape() {RectangleShape1})
RectangleShape1.Location = New System.Drawing.Point(x - 1, y - 1)
RectangleShape1.Size = New System.Drawing.Size(width + 1, height + 1)
RectangleShape1.BorderColor = Color.MistyRose
ShapeContainer1.Refresh()

Code is self describing but if you'd have any problem, just leave a message... Yes, if you want to remove the rectangle, just dispose the controls(either the Rectangle or the ShapeContainer in whole), no painting, no hassle!

Upvotes: 0

Shadow Wizard
Shadow Wizard

Reputation: 66389

No need to create new Rectangle, there is already static member for this:

if (hitBrick4 && brick4 != Rectangle.Empty)
{
    brick4  = Rectangle.Empty;
    ...
}

Upvotes: 0

Donnie
Donnie

Reputation: 46913

This isn't going to scale well at all, since you appear to be declaring a new variable for every brick.

Look into putting the Rectangles into a List of some variety. Then iterate over that list to see if a brick has been hit. When it has, remove it from the List, and redraw.

Upvotes: 1

Related Questions