vonko1988

C#JustPong(MyFirstGame)

Mar 12th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.80 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace PongGame
  5. {
  6.  
  7.     class JustPongGame
  8.     {
  9.         static int firstPlayerPadSize = 4;
  10.         static int secondPlayerPadSize = 4;
  11.         static int ballPositionX = 0;
  12.         static int ballPositionY = 0;
  13.         //determines if the ball direction is up and right
  14.         static bool ballDirectionUp = true;
  15.         static bool ballDirectionRight = false;
  16.         static int firstPlayerPosition = 0;
  17.         static int secondPlayerPosition = 6;
  18.         static int firstPlayerScore = 0;
  19.         static int secondPlayerScore = 0;
  20.         static Random randomGenerator = new Random();
  21.  
  22.         static void RemoveScrollBars()
  23.         {
  24.             Console.BackgroundColor = ConsoleColor.White;
  25.             Console.ForegroundColor = ConsoleColor.Blue;
  26.             Console.WindowHeight = 18;
  27.             Console.BufferHeight = Console.WindowHeight;
  28.             Console.BufferWidth = Console.BufferWidth;
  29.         }
  30.  
  31.         private static void PrintAtPosition(int x, int y, char symbol)
  32.         {
  33.             Console.SetCursorPosition(x, y);
  34.             Console.WriteLine(symbol);
  35.         }
  36.  
  37.         private static void SetInitialPositions()
  38.         {
  39.             firstPlayerPosition = Console.WindowHeight / 2 - firstPlayerPadSize / 2;
  40.             secondPlayerPosition = Console.WindowHeight / 2 - secondPlayerPadSize / 2;
  41.             ballPositionX = Console.WindowWidth / 2;
  42.             ballPositionY = Console.WindowHeight / 2;
  43.         }
  44.  
  45.         static void DrawFirstPlayer()
  46.         {
  47.             for (int y = firstPlayerPosition; y < firstPlayerPosition + firstPlayerPadSize; y++)
  48.             {
  49.                 PrintAtPosition(0, y, '|');
  50.                 PrintAtPosition(1, y, '|');
  51.             }
  52.         }
  53.  
  54.         static void DrawSecondPlayer()
  55.         {
  56.             for (int z = secondPlayerPosition; z < secondPlayerPosition + secondPlayerPadSize; z++)
  57.             {
  58.                 PrintAtPosition(Console.WindowWidth - 1, z, '|');
  59.                 PrintAtPosition(Console.WindowWidth - 2, z, '|');
  60.             }
  61.         }
  62.  
  63.         private static void DrawBall()
  64.         {
  65.             PrintAtPosition(ballPositionX, ballPositionY, '@');
  66.         }
  67.  
  68.         private static void PrintResult()
  69.         {
  70.             Console.SetCursorPosition(Console.WindowWidth / 2 - 2, 0);
  71.             Console.WriteLine("{0} - {1}", firstPlayerScore, secondPlayerScore);
  72.         }
  73.  
  74.         private static void MoveFirstPlayerUp()
  75.         {
  76.             if (firstPlayerPosition > 0)
  77.             {
  78.                 firstPlayerPosition--;
  79.             }
  80.         }
  81.  
  82.         private static void MoveFirstPlayerDown()
  83.         {
  84.             if (firstPlayerPosition < Console.WindowHeight - firstPlayerPadSize - 1)
  85.             {
  86.                 firstPlayerPosition++;
  87.             }
  88.         }
  89.  
  90.         private static void MoveSecondPlayerUp()
  91.         {
  92.             if (secondPlayerPosition > 0)
  93.             {
  94.                 secondPlayerPosition--;
  95.             }
  96.         }
  97.  
  98.         private static void MoveSecondPlayerDown()
  99.         {
  100.             if (secondPlayerPosition < Console.WindowHeight - secondPlayerPadSize-1)
  101.             {
  102.                 secondPlayerPosition++;
  103.             }
  104.         }
  105.  
  106.         private static void SecondPlayerAIMove()
  107.         {
  108.  
  109.             int randomNumber = randomGenerator.Next(0, 101);
  110.  
  111.                 if (randomNumber < 60)
  112.                 {
  113.                     if (ballDirectionUp == true)
  114.                     {
  115.                         MoveSecondPlayerUp();
  116.                     }
  117.                     else
  118.                     {
  119.                         MoveSecondPlayerDown();
  120.                     }
  121.                 }
  122.         }
  123.  
  124.         private static void MoveBall()
  125.         {
  126.             if (ballPositionY == 0)
  127.             {
  128.                 ballDirectionUp = false;
  129.             }
  130.             if (ballPositionY == Console.WindowHeight - 1)
  131.             {
  132.                 ballDirectionUp = true;
  133.             }
  134.  
  135.             if ((ballPositionX == Console.WindowWidth - 3) &&
  136.                 (ballPositionY >= secondPlayerPosition) &&
  137.                 (ballPositionY <= secondPlayerPosition + secondPlayerPadSize))
  138.             {
  139.                 ballDirectionRight = false;
  140.             }
  141.  
  142.             if ((ballPositionX == 2) &&
  143.                 (ballPositionY >= firstPlayerPosition) &&
  144.                 (ballPositionY <= firstPlayerPosition + firstPlayerPadSize))
  145.             {
  146.                 ballDirectionRight = true;
  147.             }
  148.  
  149.             if ((ballPositionX == 0) && ((ballPositionY < firstPlayerPosition)
  150.                 || (ballPositionY > firstPlayerPosition + firstPlayerPadSize)))
  151.             {
  152.                 secondPlayerScore++;
  153.                 Console.ReadKey();
  154.                 Main();
  155.             }
  156.  
  157.             if ((ballPositionX == Console.WindowWidth - 1) && ((ballPositionY < secondPlayerPosition) ||
  158.                 (ballPositionY > secondPlayerPosition + secondPlayerPadSize)))
  159.             {
  160.                 firstPlayerScore++;
  161.                 Console.ReadKey();
  162.                 ballDirectionRight = false;
  163.                 Main();
  164.             }
  165.  
  166.             if (ballDirectionUp)
  167.             {
  168.                 ballPositionY--;
  169.             }
  170.             else
  171.             {
  172.                 ballPositionY++;
  173.             }
  174.  
  175.             if (ballDirectionRight)
  176.             {
  177.                 ballPositionX++;
  178.             }
  179.             else
  180.             {
  181.                 ballPositionX--;
  182.             }
  183.  
  184.         }
  185.  
  186.         private static void WinGame()
  187.         {
  188.             if (firstPlayerScore == 15)
  189.             {
  190.                 Console.Clear();
  191.                 Console.SetCursorPosition(Console.WindowWidth / 2 - 5, 0);
  192.                 Console.WriteLine("Player 1 wins!");
  193.                 firstPlayerScore = 0;
  194.                 secondPlayerScore = 0;
  195.                 Console.ReadKey();
  196.                 Main();
  197.             }
  198.             if (secondPlayerScore == 15)
  199.             {
  200.                 Console.Clear();
  201.                 Console.SetCursorPosition(Console.WindowWidth / 2 - 5, 0);
  202.                 Console.WriteLine("Player 2 wins!");
  203.                 firstPlayerScore = 0;
  204.                 secondPlayerScore = 0;
  205.                 Console.ReadKey();
  206.                 Main();
  207.             }
  208.         }
  209.  
  210.         static void Main()
  211.         {
  212.             RemoveScrollBars();
  213.             SetInitialPositions();
  214.  
  215.             while (true)
  216.             {
  217.                 if (Console.KeyAvailable)
  218.                 {
  219.                     ConsoleKeyInfo pressedKey = Console.ReadKey();
  220.                     if (pressedKey.Key == ConsoleKey.UpArrow)
  221.                     {
  222.                         MoveFirstPlayerUp();
  223.                     }
  224.                     if (pressedKey.Key == ConsoleKey.DownArrow)
  225.                     {
  226.                         MoveFirstPlayerDown();
  227.                     }
  228.                 }
  229.  
  230.                 SecondPlayerAIMove();
  231.                 MoveBall();
  232.                 Console.Clear();
  233.                 DrawFirstPlayer();
  234.                 DrawSecondPlayer();
  235.                 DrawBall();
  236.                 PrintResult();
  237.                 WinGame();
  238.                 Thread.Sleep(60);
  239.             }
  240.         }
  241.  
  242.     }
  243. }
  244.  
  245. /*_________________________________
  246. |              1-0                 |
  247. |                                  |
  248. ||                                 |
  249. ||                                 |
  250. ||                                ||
  251. ||              *                 ||
  252. |                                 ||
  253. |                                 ||
  254. |                                  |
  255. |                                  |
  256. |                                  |
  257. |                                  |                
  258. |___________________________________*/
Advertisement
Add Comment
Please, Sign In to add comment