πŸŽ‰ Celebrating 25 Years of GameDev.net! πŸŽ‰

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

breakout in c#

Started by
22 comments, last by Tom Sloper 3Β years, 3Β months ago

C# running on .NET Framework/ .NET Core 5 (or just .NET 5) has a lot of classes which define such boolean members. For example String.IsNullOrEmpty as relative prominent connotation. And yeah, to negate that, you have to write

if(!string.IsNullOrEmpty(myString))

This can be confusing but it is the way how Microsoft designed it

Advertisement

can I get an answer to my question not just a running commentary

Ask your question, Phil. And be nice.

-- Tom Sloper -- sloperama.com

@Tom Sloper sorry tom I did not mean to be rude

I have got the ball to collide with the paddle and bounce off, is there a simple way to make the ball hit the brick and bounce off and erase the brick.

pbivens67 said:
and erase the brick

This again?

You clear the screen every frame and only draw what needs to be drawn. But you can't even draw a brick that's not there, so why does the game still know about a removed brick? Remove it from whatever list you have it in and when you redraw the screen, it CAN'T be drawn.

Also, don't worry about bouncing off the bricks yet. Just let the ball pass through the bricks and remove them. Then after you get that working, then get the collision response (ricochet) working. I'm assuming you have the ball bouncing off the walls already?

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

I am using a list to store the bricks, is there a way to remove the bricks from the list.

I don't know C# but this is close.

foreach (var brickIndex in bricks) {
  if (brickIndex.colliding()) {
    bricks.RemoveAt(brickIndex);
  }
}

Though I'm not sure how to resolve brickIndex to an object if it is an int.

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

I have stubbed out some code in a c# console app. I am attempting to implement a collision detection method. Am I on the right track with my code?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListBricks
{
    class Program
    {
        class Brick
        {
            public int x1;
            public int y1;
            public int width1;
            public int height1;
            public int x2;
            public int y2;
            public int width2;
            public int height2;
            public bool Colliding()
            {
                if(x1<=x2+width2&&x1+width1>=x2&&y1<=y2+height2&&y1+height1>=y2)
                {
                    return true;
                }
                return false;
            }
        }
        static void Main(string[] args)
        {
            List<Brick> bricks = new List<Brick>();
            int count = 0, x = 0, y = 0, xstep = 0, ystep = 0;
                for (int i = 0; i <= 640; i += 80)
                {
                bricks.Add(new Brick());
                bricks[count].x1 = i;
                bricks[count].y1 = 0;
                bricks[count].width1 = 80;
                bricks[count].height1 = 40;
                bricks[count].x2 = x;
                bricks[count].y2 = y;
                bricks[count].width2 = 10;
                bricks[count].height2 = 10;
                x += xstep;
                y += ystep;
                count++;
                }
                for (int i = 0; i <= 640; i += 80)
                {
                bricks.Add(new Brick());
                bricks[count].x1 = i;
                bricks[count].y1 = 40;
                bricks[count].width1 = 80;
                bricks[count].height1 = 40;
                bricks[count].x2 = x;
                bricks[count].y2 = y;
                bricks[count].width2 = 10;
                bricks[count].height2 = 10;
                x += xstep;
                y += ystep;
                count++;
                }
                for (int i = 0; i <= 640; i += 80)
                {
                bricks.Add(new Brick());
                bricks[count].x1 = i;
                bricks[count].y1 = 80;
                bricks[count].width1 = 80;
                bricks[count].height1 = 40;
                bricks[count].x2 = x;
                bricks[count].y2 = y;
                bricks[count].width2 = 10;
                bricks[count].height2 = 10;
                x += xstep;
                y += ystep;
                count++;
                }
                foreach(Brick brickIndex in bricks)
                {
                if (brickIndex.Colliding()) 
                {
                Console.WriteLine(brickIndex.x1);
                Console.WriteLine(brickIndex.y1);
                Console.WriteLine(brickIndex.width1);
                Console.WriteLine(brickIndex.height1);
//              bricks.RemoveAt(brickIndex.x);
//              bricks.RemoveAt(brickIndex.y);
                }
            }
        }
    }
}

@pbivens67 Could you explain how this part works?

public bool Colliding()
{
    if(x1<=x2+width2&&x1+width1>=x2&&y1<=y2+height2&&y1+height1>=y2)
    {
        return true;
    }
    return false;
}

I was under the impression that you'd want to compare the boundaries with the brick with the position of the ball,
not check whether the members of a brick are sane. But perhaps I'm just not understanding the members properly.
Also, for higher ball speeds or slower machines, it may be beneficial to go with some intersection math here, but I get why it's nice to start out with a naive approach.

This topic is closed to new replies.

Advertisement