How to create a screensaver in Core C#






3.50/5 (2 votes)
Create a screensaver in Core C#
This program was written as an old style screensaver as a part of a training course. The screen saver is a simple program which uses random numbers to display dots on screen in different colors. It also uses a simple logic to keep removing dots by overwriting them with blank spaces, so that the screen does not fill up. There is a
for
loop to slow down the appearance of dots, i.e., stars.
This code is not the last code, suggestions and improvements are welcome.
Note: The % operator is used to generate actual randomness on screen, using many if
-else
s. Only later was it discovered that one can also pass a seed number for effective random values.
Note: This program generates only 10000 times. One can also pass a command line argument to run the loop indefinitely and prompt user to press Control+c to exit from the program.
using System;
class set_cursor2
{
int lastnum;
private int RandomNumber(int min, int max)
{
Random random = new Random();
lastnum = random.Next(min, max);
if (lastnum%3 == 0 || lastnum%4==0 )
{
lastnum = random.Next(min, max);
}
if (lastnum%5 == 0 || lastnum%6==0 || lastnum%7==0 )
{
lastnum = random.Next(min, max);
}
return lastnum;
}
static void Main(string[] args)
{
set_cursor2 obj = new set_cursor2();
int lastrow,lastcol;
int i,j,t;
Console.ResetColor();
Console.Clear();
Console.SetWindowSize(100,50);
Console.BufferHeight = 500;
Console.BufferWidth = 200;
Console.CursorSize = 50;
Console.CursorVisible = true;
i = 1;
do
{
j= obj.RandomNumber(1,99);
Console.CursorLeft = j;
lastcol = j;
j= obj.RandomNumber(1,49);
lastrow = j;
Console.CursorTop = j;
if (i%3 == 0)
Console.ForegroundColor = ConsoleColor.White;
else if (i%4 == 0)
Console.ForegroundColor = ConsoleColor.Cyan;
else
Console.ForegroundColor = ConsoleColor.DarkBlue;
if ((i+j) % 2 == 0 || (i+j) % 3 == 0 || (i+j) % 5 == 0)
Console.WriteLine(" ");
else
Console.WriteLine(".");
// Console.WriteLine(". {0} {1}",lastrow,lastcol);
for(t=0;t<19999999;t++);
i++;
}
while (i<=10000);
Console.WriteLine("\n\n Finish");
Console.ReadLine();
}
}
Anyone can use the color codes to change the layout of the screen. The following are some popular color codes:
Color | Description |
---|---|
Black | The color black. |
Blue | The color blue. |
Cyan | The color cyan (blue-green). |
DarkBlue | The color dark blue. |
DarkCyan | The color dark cyan (dark blue-green). |
DarkGray | The color dark gray. |
DarkGreen | The color dark green. |
DarkMagenta | The color dark magenta (dark purplish-red). |
DarkRed | The color dark red. |
DarkYellow | The color dark yellow (ochre). |
Gray | The color gray. |
Green | The color green. |
Magenta | The color magenta (purplish-red). |
Red | The color red. |
White | The color white. |
Yellow | The color yellow. |