Skip to content

Instantly share code, notes, and snippets.

@MladenMladenov
Last active August 29, 2015 14:25
Show Gist options
  • Save MladenMladenov/c0a1f5c326085d7515a7 to your computer and use it in GitHub Desktop.
Save MladenMladenov/c0a1f5c326085d7515a7 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
class PingPongLAB
{
static int firstPlayerPadSize = 15;
static int secondPlayerPadSize = 4;
static int ballPositionX = 5;
static int ballPositionY = 5;
static bool ballDirectionUp = true;
static bool ballDirectionRight = true;
static int firstPlayerPosition = 0;
static int secondPlayerPosition = 0;
static int firstPlayerResult = 0;
static int secondPlayerResult = 0;
static Random randomGenerator = new Random();
static void RemoveScrollBars()
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BufferHeight = Console.WindowHeight;
Console.BufferWidth = Console.BufferWidth;
}
static void DrawFirstPlayer()
{
for (int y = firstPlayerPosition; y < firstPlayerPosition + firstPlayerPadSize; y++)
{
PrintAtPosition(0, y, '|');
PrintAtPosition(1, y, '|');
}
}
private static void PrintAtPosition(int x, int y, char symbol)
{
Console.SetCursorPosition(x, y);
Console.Write(symbol);
}
static void DrawSecondPlayer()
{
for (int y = secondPlayerPosition; y < secondPlayerPosition + secondPlayerPadSize; y++)
{
PrintAtPosition(Console.WindowWidth - 1, y, '|');
PrintAtPosition(Console.WindowWidth - 2, y, '|');
}
}
static void SetInicialPositions()
{
firstPlayerPosition = Console.WindowHeight/2 - firstPlayerPadSize/2;
secondPlayerPosition = Console.WindowHeight / 2 - secondPlayerPadSize / 2;
SetBallAtTheMiddleOfTheGameField();
}
static void SetBallAtTheMiddleOfTheGameField()
{
ballPositionX = Console.WindowWidth / 2;
ballPositionY = Console.WindowHeight / 2;
}
static void DrawBall()
{
PrintAtPosition(ballPositionX, ballPositionY, '@');
}
private static void PrintResult()
{
Console.SetCursorPosition(Console.WindowWidth / 2 - 1, 0);
Console.Write("{0}-{1}", firstPlayerResult, secondPlayerResult);
}
static void MoveFirstPlayerDown()
{
if (firstPlayerPosition < Console.WindowHeight - firstPlayerPadSize)
{
firstPlayerPosition++;
}
}
static void MoveFirstPlayerUp()
{
if (firstPlayerPosition > 0 )
{
firstPlayerPosition--;
}
}
static void MoveSecondPlayerDown()
{
if (secondPlayerPosition < Console.WindowHeight - secondPlayerPadSize)
{
secondPlayerPosition++;
}
}
static void MoveSecondPlayerUp()
{
if (secondPlayerPosition > 0)
{
secondPlayerPosition--;
}
}
static void SecondPlayerAIMove()
{
int randomNumber = randomGenerator.Next(1, 101);
// if (randomNumber == 0)
// {
// MoveSecondPlayerUp();
// }
// if (randomNumber == 1)
// {
// MoveSecondPlayerDown();
// }
if (randomNumber < 70)
{
if (ballDirectionUp == true)
{
MoveSecondPlayerUp();
}
else
{
MoveSecondPlayerDown();
}
}
}
static void MoveBall()
{
if (ballPositionY == 0)
{
ballDirectionUp = false;
}
if (ballPositionY == Console.WindowHeight - 1)
{
ballDirectionUp = true;
}
if ( ballPositionX == Console.WindowWidth-1)
{
SetBallAtTheMiddleOfTheGameField();
ballDirectionRight = false;
ballDirectionUp = true;
firstPlayerResult++;
Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);
Console.WriteLine("First Player Wins");
Console.ReadKey();
}
if (ballPositionX == 0 )
{
SetBallAtTheMiddleOfTheGameField();
ballDirectionRight = true;
ballDirectionUp = true;
secondPlayerResult++;
Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);
Console.WriteLine("Second Player Wins");
Console.ReadKey();
}
if (ballPositionX < 3)
{
if ( ballPositionY >= firstPlayerPosition
&& ballPositionY < firstPlayerPosition + firstPlayerPadSize)
{
ballDirectionRight = true;
}
}
if (ballPositionX >= Console.WindowWidth- 3- 1)
{
if ( ballPositionY >= secondPlayerPosition
&& ballPositionY < secondPlayerPosition + secondPlayerPadSize)
{
ballDirectionRight = false;
}
}
if (ballDirectionUp)
{
ballPositionY--;
}
else
{
ballPositionY++;
}
if (ballDirectionRight)
{
ballPositionX++;
}
else
{
ballPositionX--;
}
}
static void GameOver()
{
if (firstPlayerResult == 1)
{
Console.WriteLine("First Player Wins");
GameOver();
return;
}
else if (secondPlayerResult == 1)
{
Console.WriteLine("Second Player Wins");
GameOver();
return;
}
}
static void Main()
{
RemoveScrollBars();
SetInicialPositions();
while (true)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (keyInfo.Key == ConsoleKey.UpArrow)
{
MoveFirstPlayerUp();
}
if (keyInfo.Key == ConsoleKey.DownArrow)
{
MoveFirstPlayerDown();
}
}
SecondPlayerAIMove();
MoveBall();
Console.Clear();
DrawFirstPlayer();
DrawSecondPlayer();
DrawBall();
PrintResult();
// GameOver();
Thread.Sleep(60);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment