Get up to 80 % extra points for free! More info:

Bouncing Balls in C# .NET Console

For a long time I was thinking that it would be nice to demonstrate why we should work with objects and what they're actually good for. This program is intended for students that are just beginning to learn object-oriented programming. While making this project, some interesting things appeared that got my attention, and I'm happy to share them with you.

Balls Movement

Because of the bouncing, I don't only set just one coordinate when moving the ball, but both of them. The reason is that I can add the vectors in both directions to the current position and not to worry about anything else. Rendering at specific coordinates has proven to be handly in school classes, so I don't even consider any different solution (such as refreshing the entire console). The coordinates origin is laceted in the top-left corner, in case you don't know.

Wall Bouncing System

It may sound trivial to somebody, but it took me a while to find a simple way to bounce the ball off the wall in any direction! Going through all options and responding to them was simply a bad approach and I had to reject it. Then I tried to use a vector which has two coordinates - X and Y. So all I have to do is to change one or both coordinates, in case there's bouncing from a corner. It doesn't matter from which wall the ball was bounced or what the direction is. It just works perfectly.


Of course, a timer is preferable than pausing the thread using the Sleep method, but I wanted the code to be as simple as possible. And I also tested both variants, and there seems to be no noticeable difference, so the Sleep method wins in this case.

Console Flashing

The momement was lagging, especially at higher speeds. However, I figured out that simply hiding the scrollbar greatly improves the performance and overall result. Another thing - while the program is running, no character must be rendered outside the console window. If that happens, an exception occurs and the program crashes, except when any character is rendered down below the console border, then the character is rendered and the entire console is scrolled down by one line. Once the top line is rendered, the console scrolls back up. And about the flashing, it took me a while to find solution to this simple problem...

Conditions in the Move() Method

It's interesting what a small change from if to if else can cause. The difference can be seen when the ball bounces off a corner (at that moment, both X and Y vector components should change), and only the first vector component changes, causing what I described above. Try it for yourself!

Where are the Colors?!!

There are no colors because of simplicity. It's not difficult to implement balls with random colors, or changing color to reflect movement direction change, so I leave it to those who are interested in adding this into the program :-)


Program was created in 2018.



By downloading the following file, you agree to the license terms

Downloaded 47x (183.23 kB)
Application includes source codes in language C# .NET


All articles in this section
C# .NET Community Projects - Object-Oriented Programming
Program has been written for you by fictum.jiri
User rating:
No one has rated this quite yet, be the first one!