티스토리 뷰

프로그래밍/C#

C# 콘솔 테트리스

뽀로로친구에디 2019. 3. 6. 09:26

C# 콘솔 테트리스





using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Timers;

using System.Diagnostics;

using System.Media;

using System.Resources;

using System.IO;

using System.Reflection;


namespace Tetris1

{

    static class Program

    {

        public static string sqr = "■";

        public static int[,] grid = new int[23, 10];

        public static int[,] droppedtetrominoeLocationGrid = new int[23, 10];

        public static Stopwatch timer = new Stopwatch();

        public static Stopwatch dropTimer = new Stopwatch();

        public static Stopwatch inputTimer = new Stopwatch();

        public static int dropTime, dropRate = 300;

        public static bool isDropped = false;

        static Tetrominoe tet;

        static Tetrominoe nexttet;

        public static ConsoleKeyInfo key;

        public static bool isKeyPressed = false;

        public static int linesCleared = 0, score = 0, level = 1;


        static void Main()

        {

            SoundPlayer sp = new SoundPlayer();

            sp.SoundLocation = Environment.CurrentDirectory + "\\01_-_Tetris_Tengen_-_NES_-_Introduction.wav";

            sp.PlayLooping();


            drawBorder();

            Console.SetCursorPosition(4, 5);

            Console.WriteLine("Press any key");

            Console.ReadKey(true);

            sp.Stop();

            sp.SoundLocation = Environment.CurrentDirectory + "\\music.wav";

            sp.PlayLooping();

            timer.Start();

            dropTimer.Start();

            long time = timer.ElapsedMilliseconds;

            Console.SetCursorPosition(25, 0);

            Console.WriteLine("Level " + level);

            Console.SetCursorPosition(25, 1);

            Console.WriteLine("Score " + score);

            Console.SetCursorPosition(25, 2);

            Console.WriteLine("LinesCleared " + linesCleared);

            nexttet = new Tetrominoe();

            tet = nexttet;

            tet.Spawn();

            nexttet = new Tetrominoe();


            Update();


            sp.Stop();

            sp.SoundLocation = Environment.CurrentDirectory + "\\08_-_Tetris_Tengen_-_NES_-_Game_Over.wav";

            sp.Play();

            Console.SetCursorPosition(0, 0);

            Console.WriteLine("Game Over \n Replay? (Y/N)");

            string input = Console.ReadLine();


            if (input == "y" || input == "Y")

            {

                int[,] grid = new int[23, 10];

                droppedtetrominoeLocationGrid = new int[23, 10];

                timer = new Stopwatch();

                dropTimer = new Stopwatch();

                inputTimer = new Stopwatch();

                dropRate = 300;

                isDropped = false;

                isKeyPressed = false;

                linesCleared = 0;

                score = 0;

                level = 1;

                GC.Collect();

                Console.Clear();

                Main();

            }

            else return;


        }


        private static void fillGrid()

        {

            for (int i = 0; i < 23; ++i)

            {

                Console.SetCursorPosition(1, i);

                for (int j = 0; j < 10; j++)

                {

                    Console.Write(sqr);

                }

                Console.WriteLine();

            }

        }


        private static void Update()

        {

            while (true)//Update Loop

            {

                dropTime = (int)dropTimer.ElapsedMilliseconds;

                if (dropTime > dropRate)

                {

                    dropTime = 0;

                    dropTimer.Restart();

                    tet.Drop();

                }

                if (isDropped == true)

                {

                    tet = nexttet;

                    nexttet = new Tetrominoe();

                    tet.Spawn();


                    isDropped = false;

                }

                int j;

                for (j = 0; j < 10; j++)

                {

                    if (droppedtetrominoeLocationGrid[0, j] == 1)

                        return;

                }


                Input();

                ClearBlock();

            } //end Update

        }

        private static void ClearBlock()

        {

            int combo = 0;

            for (int i = 0; i < 23; i++)

            {

                int j;

                for (j = 0; j < 10; j++)

                {

                    if (droppedtetrominoeLocationGrid[i, j] == 0)

                        break;

                }

                if (j == 10)

                {


                    SoundPlayer sp = new SoundPlayer();

                    sp.SoundLocation = Environment.CurrentDirectory + "\\151022__bubaproducer__laser-shot-silenced.wav";

                    sp.Play();


                    linesCleared++;

                    combo++;

                    for (j = 0; j < 10; j++)

                    {

                        droppedtetrominoeLocationGrid[i, j] = 0;

                    }

                    int[,] newdroppedtetrominoeLocationGrid = new int[23, 10];

                    for (int k = 1; k < i; k++)

                    {

                        for (int l = 0; l < 10; l++)

                        {

                            newdroppedtetrominoeLocationGrid[k + 1, l] = droppedtetrominoeLocationGrid[k, l];

                        }

                    }

                    for (int k = 1; k < i; k++)

                    {

                        for (int l = 0; l < 10; l++)

                        {

                            droppedtetrominoeLocationGrid[k, l] = 0;

                        }

                    }

                    for (int k = 0; k < 23; k++)

                        for (int l = 0; l < 10; l++)

                            if (newdroppedtetrominoeLocationGrid[k, l] == 1)

                                droppedtetrominoeLocationGrid[k, l] = 1;

                    Draw();

                }

            }


         


            if (combo == 1)

                score += 40 * level;

            else if (combo == 2)

                score += 100 * level;

            else if (combo == 3)

                score += 300 * level;

            else if (combo > 3)

                score += 300 * combo * level;


            if (linesCleared < 5) level = 1;

            else if (linesCleared < 10) level = 2;

            else if (linesCleared < 15) level = 3;

            else if (linesCleared < 25) level = 4;

            else if (linesCleared < 35) level = 5;

            else if (linesCleared < 50) level = 6;

            else if (linesCleared < 70) level = 7;

            else if (linesCleared < 90) level = 8;

            else if (linesCleared < 110) level = 9;

            else if (linesCleared < 150) level = 10;



            if (combo > 0)

            {

                Console.SetCursorPosition(25, 0);

                Console.WriteLine("Level " + level);

                Console.SetCursorPosition(25, 1);

                Console.WriteLine("Score " + score);

                Console.SetCursorPosition(25, 2);

                Console.WriteLine("LinesCleared " + linesCleared);

            }


            dropRate = 300 - 22 * level;


        }

        private static void Input()

        {

            if (Console.KeyAvailable)

            {

                key = Console.ReadKey();

                isKeyPressed = true;

            }

            else

                isKeyPressed = false;


            if (Program.key.Key == ConsoleKey.LeftArrow & !tet.isSomethingLeft() & isKeyPressed)

            {

                for (int i = 0; i < 4; i++)

                {

                    tet.location[i][1] -= 1;

                }

                tet.Update();

                //    Console.Beep();

            }

            else if (Program.key.Key == ConsoleKey.RightArrow & !tet.isSomethingRight() & isKeyPressed)

            {

                for (int i = 0; i < 4; i++)

                {

                    tet.location[i][1] += 1;

                }

                tet.Update();

            }

            if (Program.key.Key == ConsoleKey.DownArrow & isKeyPressed)

            {

                tet.Drop();

            }

            if (Program.key.Key == ConsoleKey.Spacebar & isKeyPressed)

            {

                for (; tet.isSomethingBelow() != true;)

                {

                    tet.Drop();

                }

            }

            if (Program.key.Key == ConsoleKey.UpArrow & isKeyPressed)

            {

                //rotate

                tet.Rotate();

                tet.Update();

            }

        }

        public static void Draw()

        {

            for (int i = 0; i < 23; ++i)

            {

                for (int j = 0; j < 10; j++)

                {

                    Console.SetCursorPosition(1 + 2 * j, i);

                    if (grid[i, j] == 1 | droppedtetrominoeLocationGrid[i, j] == 1)

                    {

                        Console.SetCursorPosition(1 + 2 * j, i);

                        Console.Write(sqr);

                    }

                    else

                    {

                        Console.Write("  ");

                    }

                }


            }

        }


        public static void drawBorder()

        {

            for (int lengthCount = 0; lengthCount <= 22; ++lengthCount)

            {

                Console.SetCursorPosition(0, lengthCount);

                Console.Write("*");

                Console.SetCursorPosition(21, lengthCount);

                Console.Write("*");

            }

            Console.SetCursorPosition(0, 23);

            for (int widthCount = 0; widthCount <= 10; widthCount++)

            {

                Console.Write("*-");

            }


        }


    }


    public class Tetrominoe

    {

        public static int[,] I = new int[1, 4] { { 1, 1, 1, 1 } };//3

        public static int[,] O = new int[2, 2] { { 1, 1 }, { 1, 1 } };

        public static int[,] T = new int[2, 3] { { 0, 1, 0 }, { 1, 1, 1 } };//3

        public static int[,] S = new int[2, 3] { { 0, 1, 1 }, { 1, 1, 0 } };//4

        public static int[,] Z = new int[2, 3] { { 1, 1, 0 }, { 0, 1, 1 } };//3

        public static int[,] J = new int[2, 3] { { 1, 0, 0 }, { 1, 1, 1 } };//3

        public static int[,] L = new int[2, 3] { { 0, 0, 1 }, { 1, 1, 1 } };//3

        public static List<int[,]> tetrominoes = new List<int[,]>() { I, O, T, S, Z, J, L };


        private bool isErect = false;

        private int[,] shape;

        private int[] pix = new int[2];

        public List<int[]> location = new List<int[]>();


        public Tetrominoe()

        {

            Random rnd = new Random();

            shape = tetrominoes[rnd.Next(0, 7)];

            for (int i = 23; i < 33; ++i)

            {

                for (int j = 3; j < 10; j++)

                {

                    Console.SetCursorPosition(i, j);

                    Console.Write("  ");

                }


            }

            Program.drawBorder();

            for (int i = 0; i < shape.GetLength(0); i++)

            {

                for (int j = 0; j < shape.GetLength(1); j++)

                {

                    if (shape[i, j] == 1)

                    {

                        Console.SetCursorPosition(((10 - shape.GetLength(1)) / 2 + j) * 2 + 20, i + 5);

                        Console.Write(Program.sqr);

                    }

                }

            }

        }


        public void Spawn()

        {

            for (int i = 0; i < shape.GetLength(0); i++)

            {

                for (int j = 0; j < shape.GetLength(1); j++)

                {

                    if (shape[i, j] == 1)

                    {

                        location.Add(new int[] { i, (10 - shape.GetLength(1)) / 2 + j });

                    }

                }

            }

            Update();

        }


        public void Drop()

        {


            if (isSomethingBelow())

            {

                for (int i = 0; i < 4; i++)

                {

                    Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] = 1;

                }

                Program.isDropped = true;

                SoundPlayer sp = new SoundPlayer();

                sp.SoundLocation = Environment.CurrentDirectory + "\\455193__screamr__plastic-donk-4.wav";

                sp.Play();


            }

            else

            {

                for (int numCount = 0; numCount < 4; numCount++)

                {

                    location[numCount][0] += 1;

                }

                Update();

            }

        }


        public void Rotate()

        {

            List<int[]> templocation = new List<int[]>();

            for (int i = 0; i < shape.GetLength(0); i++)

            {

                for (int j = 0; j < shape.GetLength(1); j++)

                {

                    if (shape[i, j] == 1)

                    {

                        templocation.Add(new int[] { i, (10 - shape.GetLength(1)) / 2 + j });

                    }

                }

            }


            if (shape == tetrominoes[0])

            {

                if (isErect == false)

                {

                    for (int i = 0; i < location.Count; i++)

                    {

                        templocation[i] = TransformMatrix(location[i], location[2], "Clockwise");

                    }

                }

                else

                {

                    for (int i = 0; i < location.Count; i++)

                    {

                        templocation[i] = TransformMatrix(location[i], location[2], "Counterclockwise");

                    }

                }

            }


            else if (shape == tetrominoes[3])

            {

                for (int i = 0; i < location.Count; i++)

                {

                    templocation[i] = TransformMatrix(location[i], location[3], "Clockwise");

                }

            }


            else if (shape == tetrominoes[1]) return;

            else

            {

                for (int i = 0; i < location.Count; i++)

                {

                    templocation[i] = TransformMatrix(location[i], location[2], "Clockwise");

                }

            }



            for (int count = 0; isOverlayLeft(templocation) != false | isOverlayRight(templocation) != false | isOverlayBelow(templocation) != false; count++)

            {

                if (isOverlayLeft(templocation) == true)

                {

                    for (int i = 0; i < location.Count; i++)

                    {

                        templocation[i][1] += 1;

                    }

                }


                if (isOverlayRight(templocation) == true)

                {

                    for (int i = 0; i < location.Count; i++)

                    {

                        templocation[i][1] -= 1;

                    }

                }

                if (isOverlayBelow(templocation) == true)

                {

                    for (int i = 0; i < location.Count; i++)

                    {

                        templocation[i][0] -= 1;

                    }

                }

                if (count == 3)

                {

                    return;

                }

            }


            location = templocation;


        }


        public int[] TransformMatrix(int[] coord, int[] axis, string dir)

        {

            int[] pcoord = { coord[0] - axis[0], coord[1] - axis[1] };

            if (dir == "Counterclockwise")

            {

                pcoord = new int[] { -pcoord[1], pcoord[0] };

            }

            else if (dir == "Clockwise")

            {

                pcoord = new int[] { pcoord[1], -pcoord[0] };

            }


            return new int[] { pcoord[0] + axis[0], pcoord[1] + axis[1] };

        }


        public bool isSomethingBelow()

        {

            for (int i = 0; i < 4; i++)

            {

                if (location[i][0] + 1 >= 23)

                    return true;

                if (location[i][0] + 1 < 23)

                {

                    if (Program.droppedtetrominoeLocationGrid[location[i][0] + 1, location[i][1]] == 1)

                    {

                        return true;

                    }

                }

            }

            return false;

        }

        public bool? isOverlayBelow(List<int[]> location)

        {

            List<int> ycoords = new List<int>();

            for (int i = 0; i < 4; i++)

            {

                ycoords.Add(location[i][0]);

                if (location[i][0] >= 23)

                    return true;

                if (location[i][0] < 0)

                    return null;

                if (location[i][1] < 0)

                {

                    return null;

                }

                if (location[i][1] > 9)

                {

                    return null;

                }

            }

            for (int i = 0; i < 4; i++)

            {

                if (ycoords.Max() - ycoords.Min() == 3)

                {

                    if (ycoords.Max() == location[i][0] | ycoords.Max() - 1 == location[i][0])

                    {

                        if (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] == 1)

                        {

                            return true;

                        }

                    }


                }

                else

                {

                    if (ycoords.Max() == location[i][0])

                    {

                        if (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] == 1)

                        {

                            return true;

                        }

                    }

                }

            }


            return false;

        }



        public bool isSomethingLeft()

        {

            for (int i = 0; i < 4; i++)

            {

                if (location[i][1] == 0)

                {

                    return true;

                }

                else if (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1] - 1] == 1)

                {

                    return true;

                }

            }

            return false;

        }

        public bool? isOverlayLeft(List<int[]> location)

        {

            List<int> xcoords = new List<int>();

            for (int i = 0; i < 4; i++)

            {

                xcoords.Add(location[i][1]);

                if (location[i][1] < 0)

                {

                    return true;

                }

                if (location[i][1] > 9)

                {

                    return false;

                }

                if (location[i][0] >= 23)

                    return null;

                if (location[i][0] < 0)

                    return null;

            }

            for (int i = 0; i < 4; i++)

            {

                if (xcoords.Max() - xcoords.Min() == 3)

                {

                    if (xcoords.Min() == location[i][1] | xcoords.Min() + 1 == location[i][1])

                    {

                        if (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] == 1)

                        {

                            return true;

                        }

                    }


                }

                else

                {

                    if (xcoords.Min() == location[i][1])

                    {

                        if (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] == 1)

                        {

                            return true;

                        }

                    }

                }

            }

            return false;

        }

        public bool isSomethingRight()

        {

            for (int i = 0; i < 4; i++)

            {

                if (location[i][1] == 9)

                {

                    return true;

                }

                else if (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1] + 1] == 1)

                {

                    return true;

                }

            }

            return false;

        }

        public bool? isOverlayRight(List<int[]> location)

        {

            List<int> xcoords = new List<int>();

            for (int i = 0; i < 4; i++)

            {

                xcoords.Add(location[i][1]);

                if (location[i][1] > 9)

                {

                    return true;

                }

                if (location[i][1] < 0)

                {

                    return false;

                }

                if (location[i][0] >= 23)

                    return null;

                if (location[i][0] < 0)

                    return null;

            }

            for (int i = 0; i < 4; i++)

            {

                if (xcoords.Max() - xcoords.Min() == 3)

                {

                    if (xcoords.Max() == location[i][1] | xcoords.Max() - 1 == location[i][1])

                    {

                        if (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] == 1)

                        {

                            return true;

                        }

                    }


                }

                else

                {

                    if (xcoords.Max() == location[i][1])

                    {

                        if (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] == 1)

                        {

                            return true;

                        }

                    }

                }

            }

            return false;

        }

        public void Update()

        {

            for (int i = 0; i < 23; i++)

            {

                for (int j = 0; j < 10; j++)

                {

                    Program.grid[i, j] = 0;

                }

            }

            for (int i = 0; i < 4; i++)

            {

                Program.grid[location[i][0], location[i][1]] = 1;

            }

            Program.Draw();

        }

    }

}



'프로그래밍 > C#' 카테고리의 다른 글

c# 얼굴감지  (0) 2019.11.19
c# 엑셀파일 불러오는 방법  (0) 2019.11.19
c# QR코드 발생기 소스코드  (0) 2019.03.04
C# 드래그 앤 드롭 픽쳐 이미지  (0) 2019.02.28
C# 구구단 게임 만들기  (0) 2019.02.28
댓글
최근에 달린 댓글
글 보관함
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글