Advertisement
nonogamer9

cool ball demo i wrote on c#

Dec 30th, 2023
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | Haiku | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         Console.CursorVisible = false;
  9.  
  10.         int consoleWidth = Console.WindowWidth;
  11.         int consoleHeight = Console.WindowHeight;
  12.  
  13.         int ballX = consoleWidth / 2;
  14.         int ballY = consoleHeight / 2;
  15.  
  16.         int speedX = 1;
  17.         int speedY = 1;
  18.  
  19.         while (true)
  20.         {
  21.             Console.Clear();
  22.  
  23.             ballX += speedX;
  24.             ballY += speedY;
  25.  
  26.             if (ballX <= 0 || ballX >= consoleWidth - 1)
  27.             {
  28.                 speedX = -speedX;
  29.             }
  30.  
  31.             if (ballY <= 0 || ballY >= consoleHeight - 1)
  32.             {
  33.                 speedY = -speedY;
  34.             }
  35.  
  36.             Console.SetCursorPosition(ballX, ballY);
  37.             Console.Write("O");
  38.  
  39.             Console.SetCursorPosition(0, 0);
  40.             Thread.Sleep(50);
  41.         }
  42.     }
  43. }
  44.  
Tags: ball
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement