View difference between Paste ID: wijHas96 and RZqNTvdD
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Collections.Generic;
3
using System.Threading;
4
5
namespace FallingRocks
6
{
7
    class Game
8
    {
9
        static char[] r = new char[] { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';' };
10
        static Random rand = new Random();
11
        static List<Rock> rocks;
12
        static int playGroundWidth = 30;
13
        static long scores;
14
        public class Dwarf // singletone
15
        {
16
            private static Dwarf instance = null;
17
            private string body;
18
            private int x; // position of '('
19
            private Dwarf()
20
            {
21
                body = "(0)";
22
                x = playGroundWidth / 2 - 1;
23
                Print();
24
            }
25
            public static Dwarf GetInstance()
26
            {
27
                if (instance == null)
28
                {
29
                    instance = new Dwarf();
30
                }
31
                return instance;
32
            }
33
            public void MoveLeft()
34
            {
35
                if (x > 0)
36
                {
37
                    x--;
38
                }
39
            }
40
            public void MoveRight()
41
            {
42
                if (x < playGroundWidth - 2)
43
                {
44
                    x++;
45
                }
46
            }
47
            public void Print(bool hit = false)
48
            {
49
                Console.SetCursorPosition(x, Console.WindowHeight - 1);
50
                Console.ForegroundColor = hit ? ConsoleColor.Red : ConsoleColor.White;
51
                Console.Write(hit ? "XXX" : body);
52
            }
53
            public bool Overlap(Rock r)
54
            {
55
                if (Console.WindowHeight - 1 == r.y)
56
                {
57
                    if (x == r.x || x + 1 == r.x || x + 2 == r.x)
58
                    {
59
                        return true;
60
                    }
61
                }
62
                return false;
63
            }
64
        }
65
        public class Rock
66
        {
67
            private char body;
68
            public int x, y;
69
            public ConsoleColor colour;
70
            private ConsoleColor GetRandomConsoleColor()
71
            {
72
                int r = rand.Next(16);
73
                switch (r)
74
                {
75
                    case 0: return ConsoleColor.White;
76
                    case 1: return ConsoleColor.Blue;
77
                    case 2: return ConsoleColor.Cyan;
78
                    case 3: return ConsoleColor.DarkBlue;
79
                    case 4: return ConsoleColor.DarkCyan;
80
                    case 5: return ConsoleColor.DarkGray;
81
                    case 6: return ConsoleColor.White;
82
                    case 7: return ConsoleColor.DarkGreen;
83
                    case 8: return ConsoleColor.DarkMagenta;
84
                    case 9: return ConsoleColor.DarkRed;
85
                    case 10: return ConsoleColor.DarkYellow;
86
                    case 11: return ConsoleColor.Gray;
87
                    case 12: return ConsoleColor.Green;
88
                    case 13: return ConsoleColor.Magenta;
89
                    case 14: return ConsoleColor.Red;
90
                    case 15: return ConsoleColor.Yellow;
91
                    default: return ConsoleColor.Black;
92
                }
93
            }
94
            public Rock()
95
            {
96
                body = r[rand.Next(r.Length)];
97
                x = rand.Next(playGroundWidth);
98
                y = 0;
99
                colour = GetRandomConsoleColor();
100
            }
101
            public bool MoveDown()
102
            {
103
                if (y < Console.WindowHeight - 1)
104
                {
105
                    y++;
106
                    return true;
107
                }
108
                return false;
109
            }
110
            public void Print()
111
            {
112
                Console.SetCursorPosition(x, y);
113
                Console.ForegroundColor = colour;
114
                Console.Write(body);
115
            }
116
        }
117
        static void Initialisation()
118
        {
119
            Console.BackgroundColor = ConsoleColor.Black;
120
            Console.CursorVisible = false;
121
            Console.Title = "Falling Rocks";
122
            Console.BufferHeight = Console.WindowHeight;
123
            Console.BufferWidth = Console.WindowWidth = 50;
124
            scores = 0;
125
            rocks = new List<Rock>();
126
        }
127
        static void Main()
128
        {
129
            Initialisation();
130
            int livesCount = 3;
131
            Dwarf dwarf = Dwarf.GetInstance();
132
            List<Rock> removeObsoletes;
133
            bool hit = false;
134
            ConsoleKeyInfo pressedKey;
135
            while (true)
136
            {
137-
                for (int i = 0, count = rand.Next(playGroundWidth / (scores > 200 ? 8 : 10)); i < count; i++)
137+
138
                for (int i = 0, count = rand.Next(playGroundWidth / (scores > 200 ? 7 : 10)); i < count; i++)
139
                {
140
                    rocks.Add(new Rock());
141
                }
142
                // moving our dwarf
143
                while (Console.KeyAvailable)
144
                {
145
                    pressedKey = Console.ReadKey(true);
146
                    if (pressedKey.Key == ConsoleKey.LeftArrow || pressedKey.Key == ConsoleKey.A) // <--
147
                    {
148
                        dwarf.MoveLeft();
149
                    }
150
                    if (pressedKey.Key == ConsoleKey.RightArrow || pressedKey.Key == ConsoleKey.D) // -->
151
                    {
152
                        dwarf.MoveRight();
153
                    }
154
                    if (pressedKey.Key == ConsoleKey.Escape) // Esc
155
                    {
156
                        Environment.Exit(0); // leave game
157
                    }
158
                }
159
                removeObsoletes = new List<Rock>(); // rocks, which are to leave the screen
160
                hit = false;
161
                foreach (Rock rock in rocks)
162
                {
163
                    if (!rock.MoveDown())
164
                    {
165
                        removeObsoletes.Add(rock);
166
                    }
167
                    if (dwarf.Overlap(rock)) // if hit
168
                    {
169
                        livesCount--;
170
                        hit = true;
171
                        Console.Beep();
172
                        if (livesCount == 0) // game over
173
                        {
174
                            Console.ForegroundColor = ConsoleColor.Red;
175
                            Console.Out.WriteLine("GAME OVER !!!");
176
                            Console.ForegroundColor = ConsoleColor.White;
177
                            Environment.Exit(0);
178
                        }
179
                    }
180
                }
181
                Console.Clear();
182
                foreach (Rock old in removeObsoletes) // removing rocks, which are to leave the screen
183
                {
184
                    rocks.Remove(old);
185
                    scores++;
186
                }
187
                if (hit)
188
                {
189
                    rocks.Clear();
190
                    scores -= 10;
191
                }
192
                // printing
193
                foreach (Rock rock in rocks)
194
                {
195
                    rock.Print();
196
                }
197
                dwarf.Print(hit);
198
                Console.ForegroundColor = ConsoleColor.White;
199
                Console.SetCursorPosition(playGroundWidth + 5, 5);
200
                Console.Out.WriteLine("Lives: {0}", livesCount);
201
                Console.SetCursorPosition(playGroundWidth + 5, 7);
202
                Console.Out.WriteLine("Scores: {0}", scores);
203
                Thread.Sleep(150);
204
            }
205
        }
206
    }
207
}