© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
41 replies
Xellez

❔ Is this the right way to implement an Interface

    interface IObstacles
    {
        void Draw(Graphics g);
    }

    class Redbox : IObstacles
    {
        Pen Pen = new Pen(Color.Red);

        Random Random = new Random();
        Position Position;

        float width;
        float height;

        public Redbox(float x, float y)
        {
            Position = new Position(x, y);

            width = Random.Next(30, 100);
            height = Random.Next(30, 100);
        }


        public void Draw(Graphics g)
        {
            g.DrawRectangle(Pen, Position.X , Position.Y, width, height);
        }
    }

    class Bluebox: IObstacles
    {
        Pen Pen = new Pen(Color.Blue);

        Random Random = new Random();
        Position Position;

        float width;
        float height;

        public Bluebox(float x, float y)
        {
            Position = new Position(x, y);

            width = Random.Next(30, 100);
            height = Random.Next(30, 100);
        }

        public void Draw(Graphics g)
        {
            g.DrawRectangle(Pen, Position.X, Position.Y, width, height);
        }

    }

    class VerticaLine : IObstacles
    {
        Pen Pen = new Pen(Color.Yellow);

        Position Position;

        float Radius;

        public VerticaLine(float x, float y, float radius)
        {
            Position = new Position(x, y);
            Radius = radius;
        }

        public void Draw(Graphics g)
        {
            g.DrawLine(Pen, Position.X, Position.Y, Position.X, Radius);
        }
    }

    class HorizonaLine : IObstacles 
    {
        Pen Pen = new Pen(Color.Green);

        Position Position;

        float Radius;

        public HorizonaLine(float x, float y, float radius)
        {
            Position = new Position(x, y);
            Radius = radius;
        }

        public void Draw(Graphics g)
        {
            g.DrawLine(Pen, Position.X, Position.Y, Radius, Position.Y);
        }
    }
    interface IObstacles
    {
        void Draw(Graphics g);
    }

    class Redbox : IObstacles
    {
        Pen Pen = new Pen(Color.Red);

        Random Random = new Random();
        Position Position;

        float width;
        float height;

        public Redbox(float x, float y)
        {
            Position = new Position(x, y);

            width = Random.Next(30, 100);
            height = Random.Next(30, 100);
        }


        public void Draw(Graphics g)
        {
            g.DrawRectangle(Pen, Position.X , Position.Y, width, height);
        }
    }

    class Bluebox: IObstacles
    {
        Pen Pen = new Pen(Color.Blue);

        Random Random = new Random();
        Position Position;

        float width;
        float height;

        public Bluebox(float x, float y)
        {
            Position = new Position(x, y);

            width = Random.Next(30, 100);
            height = Random.Next(30, 100);
        }

        public void Draw(Graphics g)
        {
            g.DrawRectangle(Pen, Position.X, Position.Y, width, height);
        }

    }

    class VerticaLine : IObstacles
    {
        Pen Pen = new Pen(Color.Yellow);

        Position Position;

        float Radius;

        public VerticaLine(float x, float y, float radius)
        {
            Position = new Position(x, y);
            Radius = radius;
        }

        public void Draw(Graphics g)
        {
            g.DrawLine(Pen, Position.X, Position.Y, Position.X, Radius);
        }
    }

    class HorizonaLine : IObstacles 
    {
        Pen Pen = new Pen(Color.Green);

        Position Position;

        float Radius;

        public HorizonaLine(float x, float y, float radius)
        {
            Position = new Position(x, y);
            Radius = radius;
        }

        public void Draw(Graphics g)
        {
            g.DrawLine(Pen, Position.X, Position.Y, Radius, Position.Y);
        }
    }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ Is there a better way to implement this "pattern"?
C#CC# / help
3y ago
✅ Why is this generic mess invalid? ('Does not implement interface' error)
C#CC# / help
8mo ago
How to override virtual interface in an interface
C#CC# / help
4y ago
Is this the right directory for JwtHandler.cs?
C#CC# / help
4y ago