View difference between Paste ID: ygZjBmaD and DHCVmeAP
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading;
6
 
7
namespace BrickGame
8
{
9
    class Program
10
    {
11
        static string[] board;
12
        const string PLAYER = "^";
13
        const string OBSTACLE = "#";
14
        static void Main(string[] args)
15
        {  
16
            int playerPosition  = 1;
17
            Random generatorRandom = new Random();
18
            bool isStruck = false;
19
            NewBoard(10);
20
            SetPlayer(playerPosition);
21
            ShowBoard();
22
            //Loop 
23
            while (!isStruck)
24
            {
25
                //controls
26
                if (Console.KeyAvailable)
27
                {
28
                    ConsoleKeyInfo keyPressed = Console.ReadKey(true);
29
                    if (keyPressed.Key == ConsoleKey.RightArrow)
30
                    {
31
                        if (playerPosition < 2)
32
                        {
33
                            playerPosition++;
34
                        }
35
                    }
36
                    if (keyPressed.Key == ConsoleKey.LeftArrow)
37
                    {
38
                        if (playerPosition > 0)
39
                        {
40
                            playerPosition--;
41
                        }
42
                    }
43
                }
44
 
45
                //Collision check 
46
                int nearestObstaclePosition = board[board.Length - 2].IndexOf(OBSTACLE);
47
                if (playerPosition == nearestObstaclePosition)
48
                {
49
                    isStruck = true;
50
                }
51
 
52
                
53
                //Creating a new obstacle
54
                int obstaclePosition = generatorRandom.Next(3);
55
                string obstacle = SetObstacle(obstaclePosition);
56
 
57
                //Moving the game board down
58
                for (int i = board.Length - 2; i > 0; i--)
59
                {
60
                    board[i] = board[i - 1];
61
                }
62
                board[0] = obstacle;
63
 
64
                SetPlayer(playerPosition);
65
                ShowBoard();
66
                Thread.Sleep(600);
67
                
68
            }
69
 
70
 
71
            Console.Clear();
72
            Console.WriteLine("GAME OVER");
73
 
74
 
75
            Console.ReadKey();
76
        }
77
 
78
        private static void NewBoard(int boardSize)
79
        {
80
            board = new string[boardSize];
81
            for (int i = 0; i < board.Length; i++)
82
            {
83
                board[i] = "";
84
            }
85
        }
86
 
87
        private static string SetObstacle(int position)
88
        {
89
            string line = "   ";
90
            line = line.Insert(position, OBSTACLE);
91
            return line;
92
        }
93
 
94
        
95
        private static void SetPlayer(int position)
96
        {
97
            string line = "   "; //in quotation marks we insert 3 spaces
98
            line = line.Insert(position, PLAYER);
99
            board[board.Length - 1] = line;
100
        }
101
 
102
 
103
 
104
        private static void ShowBoard()
105
        {
106
            Console.Clear();
107
            for (int i = 0; i < board.Length; i++)
108
            {
109
                Console.WriteLine(board[i]);
110
            }
111
        }
112
    }
113
}
114
 
115