Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Runtime;
- namespace Breakout
- {
- class MyProgram
- {
- int xPosition = 10;
- int yPosition = 12;
- // Vilket håll bollen åker åt, positivt = till höger, negativt = till vänster
- int xDirection = -1;
- int yDirection = -1;
- int leftWall = 0;
- int rightWall = 20;
- int topWall = 0;
- int bottomEdge = 15;
- int paddlePosition = 1;
- int paddleWidth = 5;
- int delay = 200;
- Brick[] bricks;
- int brickRowCount = 4;
- int brickWidth = 2;
- int brickHeight = 1;
- char ballChar = '@';
- char verticalWallChar = '|';
- char horizontalWallChar = '-';
- char cornerWallChar = '+';
- char paddleChar = '=';
- char brickChar = '#';
- public void Run()
- {
- Console.CursorVisible = false;
- int arenaWidth = rightWall - leftWall - 1;
- #region Draw Walls
- for (int i = 0; i <= bottomEdge - topWall; i++)
- {
- Console.SetCursorPosition(leftWall, topWall + i);
- Console.Write(verticalWallChar);
- Console.SetCursorPosition(rightWall, topWall + i);
- Console.Write(verticalWallChar);
- }
- Console.SetCursorPosition(leftWall, topWall);
- Console.Write(cornerWallChar);
- for (int i = 0; i < rightWall - leftWall - 1; i++)
- {
- Console.Write(horizontalWallChar);
- }
- Console.Write(cornerWallChar);
- #endregion
- #region Create Bricks
- bricks = new Brick[arenaWidth / brickWidth * brickRowCount];
- for (int x = 0; x < arenaWidth / brickWidth; x++)
- {
- for (int y = 0; y < brickRowCount; y++)
- {
- Console.SetCursorPosition(leftWall + 1 + x * brickWidth, topWall + 1 + y * brickHeight);
- for (int i = 0; i < brickWidth; i++)
- {
- Console.Write(brickChar);
- }
- //Notera att min kod inte helt fungerar som den ska nu,
- //då jag var mitt i att implementera bredd och höjd på brickorna
- Brick newBrick = new Brick();
- newBrick.xPos = x + leftWall + 1;
- newBrick.yPos = y + topWall + 1;
- newBrick.width = brickWidth;
- newBrick.height = brickHeight;
- newBrick.exists = true;
- bricks[x * brickRowCount + y] = newBrick;
- }
- }
- #endregion
- bool clearLastPosition = false;
- bool running = true;
- while (running)
- {
- #region Clear Previous Drawings
- if (clearLastPosition)
- {
- //Ta bort förra bollen
- Console.SetCursorPosition(xPosition, yPosition);
- Console.Write(" ");
- Console.SetCursorPosition(paddlePosition, bottomEdge);
- for (int i = 0; i < paddleWidth; i++)
- {
- Console.Write(" ");
- }
- }
- else
- {
- clearLastPosition = true;
- }
- #endregion
- #region Move Positions
- // Flytta bollen åt det hållet angivet av direction
- xPosition += xDirection;
- yPosition += yDirection;
- //Flytta plattan baserat på key presses
- while (Console.KeyAvailable)
- {
- ConsoleKey key = Console.ReadKey(true).Key;
- switch (key)
- {
- case ConsoleKey.LeftArrow:
- paddlePosition -= 1;
- break;
- case ConsoleKey.RightArrow:
- paddlePosition += 1;
- break;
- }
- // "Clampa" paddleposition mellan väggarna
- paddlePosition = Math.Min(Math.Max(paddlePosition, leftWall + 1), rightWall - paddleWidth);
- }
- #endregion
- #region Draw Ball
- Console.SetCursorPosition(xPosition, yPosition);
- Console.Write(ballChar);
- #endregion
- #region Draw Paddle
- Console.SetCursorPosition(paddlePosition, bottomEdge);
- for (int i = 0; i < paddleWidth; i++)
- {
- Console.Write(paddleChar);
- }
- #endregion
- Thread.Sleep(delay);
- #region Check Collision
- bool horizontalTileBlocked = TileHasWall(xPosition + xDirection, yPosition);
- bool verticalTileBlocked = TileHasWall(xPosition, yPosition + yDirection);
- //Om det är blockerat antingen horisontellt eller vertikalt, flippa respektive direction
- //Om det är varken eller, kolla om det är blockerat diagonalt
- if (horizontalTileBlocked || verticalTileBlocked)
- {
- if (horizontalTileBlocked)
- {
- xDirection *= -1;
- }
- if (verticalTileBlocked)
- {
- yDirection *= -1;
- }
- }
- else if (TileHasWall(xPosition + xDirection, yPosition + yDirection))
- {
- xDirection *= -1;
- yDirection *= -1;
- }
- #endregion
- }
- }
- public bool TileHasWall(int x, int y)
- {
- //Positionen är i en horisontell vägg
- if (x <= leftWall || x >= rightWall)
- {
- return true;
- }
- //Positionen är i den övre väggen
- if (y <= topWall)
- {
- return true;
- }
- //Positionen är i plattan
- if (y == bottomEdge && paddlePosition <= x && x < paddlePosition + paddleWidth)
- {
- return true;
- }
- for (int i = 0; i < bricks.Length; i++)
- {
- bool collidesOnX = bricks[i].xPos <= x && x <= bricks[i].xPos + bricks[i].width;
- bool collidesOnY = bricks[i].yPos <= y && y <= bricks[i].yPos + bricks[i].height;
- if (bricks[i].exists && collidesOnX && collidesOnY)
- {
- bricks[i].exists = false;
- Console.SetCursorPosition(x, y);
- Console.Write(" ");
- return true;
- }
- }
- return false;
- }
- }
- struct Brick
- {
- public int xPos;
- public int yPos;
- public int width;
- public int height;
- public bool exists;
- }
- }
- //Order of operations:
- //Ta bort gamla bollen/plattan
- //Uppdatera positionerna
- //Rita nya bollen/plattan
- //Sleep
- //Kolla kollision
Add Comment
Please, Sign In to add comment