Skip to content

Instantly share code, notes, and snippets.

@SebastianCastilloDev
Created September 12, 2023 20:43
Show Gist options
  • Save SebastianCastilloDev/466e1daab2e45729bd9ab53c247aaa17 to your computer and use it in GitHub Desktop.
Save SebastianCastilloDev/466e1daab2e45729bd9ab53c247aaa17 to your computer and use it in GitHub Desktop.
algunas animaciones en consola c#
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.Serialization.Formatters;
using System.Security.Claims;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace aiep
{
internal class Animacion
{
int xOrigen;
int yOrigen;
public Animacion(int xOrigen, int yOrigen)
{
this.xOrigen = xOrigen;
this.yOrigen = yOrigen;
}
public void ImprimirCaracter(char caracter, int x, int y)
{
Console.SetCursorPosition(xOrigen + x, yOrigen + y);
Console.Write(caracter);
}
public void CaracterIntermitente(char caracter, int x, int y, int velocidadIntermitencia)
{
do
{
ImprimirCaracter(caracter, x, y);
Thread.Sleep(velocidadIntermitencia);
ImprimirCaracter(' ', x, y);
Thread.Sleep(velocidadIntermitencia);
} while (true);
}
public void StringIntermitente(string texto, int x, int y, int velocidadIntermitencia)
{
do
{
Console.SetCursorPosition(xOrigen+x, yOrigen+y);
Console.Write(texto);
Thread.Sleep(velocidadIntermitencia);
Console.SetCursorPosition(xOrigen + x, yOrigen + y);
Console.Write(EspaciosBlanco(texto));
Thread.Sleep(velocidadIntermitencia);
} while (true);
}
private string EspaciosBlanco(string texto)
{
int longitud = texto.Length;
string espacios = "";
return espacios.PadRight(longitud, ' ');
}
public void Cargando(int velocidad)
{
Console.Write("Cargando");
for(int i = 0; i < 10; i++)
{
Thread.Sleep(velocidad);
Console.Write(".");
}
}
public void EfectoMaquina()
{
string texto = "Este es un texto";
int velocidad = 50; // milisegundos
for (int i = 0; i < texto.Length; i++)
{
Console.Write(texto[i]);
Thread.Sleep(velocidad);
}
}
}
public class Spinner
{
static string[,] sequence = null;
public int Delay { get; set; } = 200;
int totalSequences = 0;
int counter;
public Spinner()
{
counter = 0;
sequence = new string[,] {
{ "/", "-", "\\", "|" },
{ ".", "o", "0", "o" },
{ "+", "x","+","x" },
{ "V", "<", "^", ">" },
{ ". ", ".. ", "... ", "...." },
{ "=> ", "==> ", "===> ", "====>" },
// ADD YOUR OWN CREATIVE SEQUENCE HERE IF YOU LIKE
};
totalSequences = sequence.GetLength(0);
}
/// <summary>
///
/// </summary>
/// <param name="sequenceCode"> 0 | 1 | 2 |3 | 4 | 5 </param>
public void Turn(string displayMsg = "", int sequenceCode = 0)
{
counter++;
Thread.Sleep(Delay);
sequenceCode = sequenceCode > totalSequences - 1 ? 0 : sequenceCode;
int counterValue = counter % 4;
string fullMessage = displayMsg + sequence[sequenceCode, counterValue];
int msglength = fullMessage.Length;
Console.Write(fullMessage);
Console.SetCursorPosition(Console.CursorLeft - msglength, Console.CursorTop);
}
}
public class Figuras
{
protected static int origRow;
protected static int origCol;
public void RectanguloRelleno()
{
Console.Clear();
Console.CursorVisible = false; // Ocultar el cursor
int width = 20;
int height = 10;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Console.SetCursorPosition(x, y);
Console.Write("*");
}
}
Console.CursorVisible = true; // Mostrar el cursor nuevamente
}
public void RectanguloConTexto(string texto)
{
int width = texto.Length;
int height = 5;
Rectangulo("*",width+10, height);
WriteAt(texto, 5, 2);
Console.SetCursorPosition(0,10);
}
public void Rectangulo(string caracter, int width, int height)
{
//origRow = Console.CursorTop;
//origCol = Console.CursorLeft;
for (int i = 0; i < height; i++)
{
WriteAt(caracter, 0, i);
WriteAt(caracter, width-1, i);
}
for (int i = 1; i < width-1; i++)
{
WriteAt(caracter, i, 0);
WriteAt(caracter, i, height - 1);
}
}
protected static void WriteAt(string s, int x, int y)
{
try
{
Console.SetCursorPosition(origCol + x, origRow + y);
Console.Write(s);
}
catch (ArgumentOutOfRangeException e)
{
Console.Clear();
Console.WriteLine(e.Message);
}
}
}
public class TextoColores
{
public void TextoIntermitenteColores()
{
Console.CursorVisible = false;
string[] colores = { "red", "green", "blue", "yellow", "cyan" };
int indiceColor = 0;
int velocidadCambioColor = 100; // Milisegundos
while (!Console.KeyAvailable)
{
Console.Clear();
Console.ForegroundColor = ObtenerColorSiguiente(colores, ref indiceColor);
Console.WriteLine("Texto con cambio de color");
Thread.Sleep(velocidadCambioColor);
}
Console.CursorVisible = true;
}
static ConsoleColor ObtenerColorSiguiente(string[] colores, ref int indice)
{
ConsoleColor color = Enum.Parse<ConsoleColor>(colores[indice], true);
indice = (indice + 1) % colores.Length;
return color;
}
public void CaracteresColores()
{
string texto = "Este es el texto";
int veces = 10;
int velocidad = 20;
for (int i = 0; i < veces;i++)
{
foreach (char c in texto)
{
Console.ForegroundColor = (ConsoleColor)new Random().Next(1, 16);
Console.Write(c);
Thread.Sleep(velocidad);
}
}
}
public void TextoColor(string texto, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine(texto);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment