joll05

Breakout 2

Oct 28th, 2021 (edited)
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7. using System.Runtime;
  8.  
  9. namespace Breakout
  10. {
  11.     class MyProgram
  12.     {
  13.         int xPosition = 10;
  14.         int yPosition = 12;
  15.  
  16.         // Vilket håll bollen åker åt, positivt = till höger, negativt = till vänster
  17.         int xDirection = -1;
  18.         int yDirection = -1;
  19.  
  20.         int leftWall = 0;
  21.         int rightWall = 20;
  22.         int topWall = 0;
  23.         int bottomEdge = 15;
  24.  
  25.         int paddlePosition = 1;
  26.         int paddleWidth = 5;
  27.  
  28.         int delay = 200;
  29.  
  30.         Brick[] bricks;
  31.         int brickRowCount = 4;
  32.         int brickWidth = 2;
  33.         int brickHeight = 1;
  34.  
  35.         char ballChar = '@';
  36.         char verticalWallChar = '|';
  37.         char horizontalWallChar = '-';
  38.         char cornerWallChar = '+';
  39.         char paddleChar = '=';
  40.         char brickChar = '#';
  41.  
  42.         public void Run()
  43.         {
  44.             Console.CursorVisible = false;
  45.             int arenaWidth = rightWall - leftWall - 1;
  46.  
  47.             #region Draw Walls
  48.             for (int i = 0; i <= bottomEdge - topWall; i++)
  49.             {
  50.                 Console.SetCursorPosition(leftWall, topWall + i);
  51.                 Console.Write(verticalWallChar);
  52.  
  53.                 Console.SetCursorPosition(rightWall, topWall + i);
  54.                 Console.Write(verticalWallChar);
  55.             }
  56.  
  57.             Console.SetCursorPosition(leftWall, topWall);
  58.             Console.Write(cornerWallChar);
  59.             for (int i = 0; i < rightWall - leftWall - 1; i++)
  60.             {
  61.                 Console.Write(horizontalWallChar);
  62.             }
  63.             Console.Write(cornerWallChar);
  64.             #endregion
  65.  
  66.             #region Create Bricks
  67.             bricks = new Brick[arenaWidth / brickWidth * brickRowCount];
  68.             for (int x = 0; x < arenaWidth / brickWidth; x++)
  69.             {
  70.                 for (int y = 0; y < brickRowCount; y++)
  71.                 {
  72.                     Console.SetCursorPosition(leftWall + 1 + x * brickWidth, topWall + 1 + y * brickHeight);
  73.                     for (int i = 0; i < brickWidth; i++)
  74.                     {
  75.                         Console.Write(brickChar);
  76.                     }
  77.  
  78.                     //Notera att min kod inte helt fungerar som den ska nu,
  79.                     //då jag var mitt i att implementera bredd och höjd på brickorna
  80.                     Brick newBrick = new Brick();
  81.                     newBrick.xPos = x + leftWall + 1;
  82.                     newBrick.yPos = y + topWall + 1;
  83.  
  84.                     newBrick.width = brickWidth;
  85.                     newBrick.height = brickHeight;
  86.  
  87.                     newBrick.exists = true;
  88.  
  89.                     bricks[x * brickRowCount + y] = newBrick;
  90.                 }
  91.             }
  92.             #endregion
  93.  
  94.             bool clearLastPosition = false;
  95.             bool running = true;
  96.             while (running)
  97.             {
  98.                 #region Clear Previous Drawings
  99.                 if (clearLastPosition)
  100.                 {
  101.                     //Ta bort förra bollen
  102.                     Console.SetCursorPosition(xPosition, yPosition);
  103.                     Console.Write(" ");
  104.  
  105.                     Console.SetCursorPosition(paddlePosition, bottomEdge);
  106.                     for (int i = 0; i < paddleWidth; i++)
  107.                     {
  108.                         Console.Write(" ");
  109.                     }
  110.                 }
  111.                 else
  112.                 {
  113.                     clearLastPosition = true;
  114.                 }
  115.                 #endregion
  116.  
  117.                 #region Move Positions
  118.                 // Flytta bollen åt det hållet angivet av direction
  119.                 xPosition += xDirection;
  120.                 yPosition += yDirection;
  121.  
  122.                 //Flytta plattan baserat på key presses
  123.                 while (Console.KeyAvailable)
  124.                 {
  125.                     ConsoleKey key = Console.ReadKey(true).Key;
  126.  
  127.                     switch (key)
  128.                     {
  129.                         case ConsoleKey.LeftArrow:
  130.                             paddlePosition -= 1;
  131.                             break;
  132.  
  133.                         case ConsoleKey.RightArrow:
  134.                             paddlePosition += 1;
  135.                             break;
  136.                     }
  137.  
  138.                     // "Clampa" paddleposition mellan väggarna
  139.                     paddlePosition = Math.Min(Math.Max(paddlePosition, leftWall + 1), rightWall - paddleWidth);
  140.                 }
  141.                 #endregion
  142.  
  143.                 #region Draw Ball
  144.                 Console.SetCursorPosition(xPosition, yPosition);
  145.                 Console.Write(ballChar);
  146.                 #endregion
  147.  
  148.                 #region Draw Paddle
  149.                 Console.SetCursorPosition(paddlePosition, bottomEdge);
  150.  
  151.                 for (int i = 0; i < paddleWidth; i++)
  152.                 {
  153.                     Console.Write(paddleChar);
  154.                 }
  155.                 #endregion
  156.  
  157.                 Thread.Sleep(delay);
  158.  
  159.                 #region Check Collision
  160.                 bool horizontalTileBlocked = TileHasWall(xPosition + xDirection, yPosition);
  161.                 bool verticalTileBlocked = TileHasWall(xPosition, yPosition + yDirection);
  162.  
  163.                 //Om det är blockerat antingen horisontellt eller vertikalt, flippa respektive direction
  164.                 //Om det är varken eller, kolla om det är blockerat diagonalt
  165.                 if (horizontalTileBlocked || verticalTileBlocked)
  166.                 {
  167.                     if (horizontalTileBlocked)
  168.                     {
  169.                         xDirection *= -1;
  170.                     }
  171.  
  172.                     if (verticalTileBlocked)
  173.                     {
  174.                         yDirection *= -1;
  175.                     }
  176.                 }
  177.                 else if (TileHasWall(xPosition + xDirection, yPosition + yDirection))
  178.                 {
  179.                     xDirection *= -1;
  180.                     yDirection *= -1;
  181.                 }
  182.                 #endregion
  183.             }
  184.         }
  185.  
  186.         public bool TileHasWall(int x, int y)
  187.         {
  188.             //Positionen är i en horisontell vägg
  189.             if (x <= leftWall || x >= rightWall)
  190.             {
  191.                 return true;
  192.             }
  193.  
  194.             //Positionen är i den övre väggen
  195.             if (y <= topWall)
  196.             {
  197.                 return true;
  198.             }
  199.  
  200.             //Positionen är i plattan
  201.             if (y == bottomEdge && paddlePosition <= x && x < paddlePosition + paddleWidth)
  202.             {
  203.                 return true;
  204.             }
  205.  
  206.             for (int i = 0; i < bricks.Length; i++)
  207.             {
  208.                 bool collidesOnX = bricks[i].xPos <= x && x <= bricks[i].xPos + bricks[i].width;
  209.                 bool collidesOnY = bricks[i].yPos <= y && y <= bricks[i].yPos + bricks[i].height;
  210.                 if (bricks[i].exists && collidesOnX && collidesOnY)
  211.                 {
  212.                     bricks[i].exists = false;
  213.                     Console.SetCursorPosition(x, y);
  214.                     Console.Write(" ");
  215.                     return true;
  216.                 }
  217.             }
  218.  
  219.             return false;
  220.         }
  221.     }
  222.  
  223.     struct Brick
  224.     {
  225.         public int xPos;
  226.         public int yPos;
  227.  
  228.         public int width;
  229.         public int height;
  230.  
  231.         public bool exists;
  232.     }
  233. }
  234.  
  235. //Order of operations:
  236. //Ta bort gamla bollen/plattan
  237. //Uppdatera positionerna
  238. //Rita nya bollen/plattan
  239. //Sleep
  240. //Kolla kollision
  241.  
Add Comment
Please, Sign In to add comment