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 int points = 0; | |
15 | static void Main(string[] args) | |
16 | { | |
17 | int playerPosition = 1; | |
18 | Random generatorRandom = new Random(); | |
19 | bool isStruck = false; | |
20 | NewBoard(10); | |
21 | SetPlayer(playerPosition); | |
22 | ShowBoard(); | |
23 | //Loop | |
24 | while (!isStruck) | |
25 | { | |
26 | //controls | |
27 | if (Console.KeyAvailable) | |
28 | { | |
29 | ConsoleKeyInfo keyPressed = Console.ReadKey(true); | |
30 | if (keyPressed.Key == ConsoleKey.RightArrow) | |
31 | { | |
32 | if (playerPosition < 2) | |
33 | { | |
34 | playerPosition++; | |
35 | } | |
36 | } | |
37 | if (keyPressed.Key == ConsoleKey.LeftArrow) | |
38 | { | |
39 | if (playerPosition > 0) | |
40 | { | |
41 | playerPosition--; | |
42 | } | |
43 | } | |
44 | } | |
45 | ||
46 | //Collision check | |
47 | int nearestObstaclePosition = board[board.Length - 2].IndexOf(OBSTACLE); | |
48 | if (playerPosition == nearestObstaclePosition) | |
49 | { | |
50 | isStruck = true; | |
51 | } | |
52 | else | |
53 | { | |
54 | points++; | |
55 | } | |
56 | ||
57 | ||
58 | //Creating a new obstacle | |
59 | int obstaclePosition = generatorRandom.Next(3); | |
60 | string obstacle = SetObstacle(obstaclePosition); | |
61 | ||
62 | //Moving the game board down | |
63 | for (int i = board.Length - 2; i > 0; i--) | |
64 | { | |
65 | board[i] = board[i - 1]; | |
66 | } | |
67 | board[0] = obstacle; | |
68 | ||
69 | SetPlayer(playerPosition); | |
70 | ShowBoard(); | |
71 | Thread.Sleep(600); | |
72 | ||
73 | } | |
74 | ||
75 | ||
76 | Console.Clear(); | |
77 | Console.WriteLine("GAME OVER"); | |
78 | Console.WriteLine($"You have earned {points} points"); | |
79 | ||
80 | Console.ReadKey(); | |
81 | } | |
82 | ||
83 | private static void NewBoard(int boardSize) | |
84 | { | |
85 | board = new string[boardSize]; | |
86 | for (int i = 0; i < board.Length; i++) | |
87 | { | |
88 | board[i] = ""; | |
89 | } | |
90 | } | |
91 | ||
92 | private static string SetObstacle(int position) | |
93 | { | |
94 | string line = " "; | |
95 | line = line.Insert(position, OBSTACLE); | |
96 | return line; | |
97 | } | |
98 | ||
99 | ||
100 | private static void SetPlayer(int position) | |
101 | { | |
102 | string line = " "; //in quotation marks we insert 3 spaces | |
103 | line = line.Insert(position, PLAYER); | |
104 | board[board.Length - 1] = line; | |
105 | } | |
106 | ||
107 | ||
108 | ||
109 | private static void ShowBoard() | |
110 | { | |
111 | Console.Clear(); | |
112 | for (int i = 0; i < board.Length; i++) | |
113 | { | |
114 | Console.WriteLine(board[i]); | |
115 | } | |
116 | Console.WriteLine($"Points: {points}"); | |
117 | } | |
118 | } | |
119 | } | |
120 |