3 Replies - 17082 Views - Last Post: 09 February 2015 - 09:18 PM Rate Topic: -----

#1 traderain   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-February 15

C# Tetris Console Game design

Posted 09 February 2015 - 12:40 PM

So I am making a C# tetris game (Console application) but i have got into a problem where I got flickering.
I am trying to use a buffer to solve it but I am not sure how to implement it. Any help apriciated.(The game is written at the writelines on hungarian the commenting is english and hungarian mixed. (Use Google translate but its obviuos mostly)
I supplied the project in a zip file.
Is This A Good Question/Topic? 0
  • +

Replies To: C# Tetris Console Game design

#2 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 451
  • View blog
  • Posts: 2,188
  • Joined: 07-April 08

Re: C# Tetris Console Game design

Posted 09 February 2015 - 12:43 PM

First, you didn't attach the zip file. Second, you need to post the relevant code in code tags as members are typically not going to download/run a zip/exe/etc let alone from someone who just joined.
Was This Post Helpful? 0
  • +
  • -

#3 traderain   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-February 15

Re: C# Tetris Console Game design

Posted 09 February 2015 - 12:47 PM

The block
using System;
using System.Collections.Generic;
using System.Text;

namespace Tetrisz
{
	public class Tetris
    {
        #region Változók
        public delegate void GameOverHandler();
		public delegate void BlockFixedHandler();
		public delegate void LinesDoneHandler(int Lines);
		public event GameOverHandler GameOver;
		public event BlockFixedHandler BlockFixed;
		public event LinesDoneHandler LinesDone;
		public enum Key
		{
			Left,
			Right,
			Up,
			Down,
			rRight,
			rLeft
		}
		private int[,] Container;
		private int posX;
		private int posY;
		private int[,] currBlock=null;
		private int[,] nextBlock = null;
		private Block bGen=new Block();
		private bool inGame;
		private bool shadow;
        #endregion
        #region tetrisz
        public Tetris(int Width, int Height)
		{
			shadow = true;
			if(Width >= 10 && Height >= 20)
			{
				Container = new int[Height, Width];
			}
			else
			{
				throw new Exception("legalább  10x20");
			}

		}
        #endregion
        #region indítás
        public void start()
		{
			inGame = true;
			posY = 0;
			posX=Container.GetUpperBound(1)/2;
			currBlock =nextBlock!=null?nextBlock:bGen.getRandomBlock();
			nextBlock = bGen.getRandomBlock();
			if(!canPosAt(currBlock, posX, posY))
			{
				gameOver();
			}
		}
        #endregion
        #region vége a játéknak
        public void gameOver()
		{
			inGame = false;
			if(GameOver != null)
			{
				GameOver();
			}
		}
        #endregion
        #region lépés
        public void step()
		{
			if(inGame)
			{
				if(canPosAt(currBlock, posX, posY + 1))
				{
					posY++;
				}
				else
				{
					Container=fixBlock(currBlock,Container, posX, posY);

					start();
				}
				int Lines=checkLines();
				if(LinesDone != null)
				{
					LinesDone(Lines);
				}
			}
		}
        #endregion
        #region mozgatás
        public void keyPress(Key k)
		{
			if(inGame)
			{
				int[,] temp;
				switch(k)
				{
					case Key.Down:
						step();
						break;
					case Key.Left:
						if(posX>0 && canPosAt(currBlock, posX - 1, posY))
						{
							posX--;
						}
						break;
					case Key.Right:
						if(posX<Container.GetUpperBound(0)-currBlock.GetUpperBound(0) && canPosAt(currBlock, posX +1, posY))
						{
							posX++;
						}
						break;
					case Key.rLeft:
						temp = Block.rotateL(currBlock);
						if(canPosAt(temp, posX, posY))
						{
							currBlock = Block.rotateL(currBlock);
						}
						break;
					case Key.rRight:
						temp = Block.rotateR(currBlock);
						if(canPosAt(temp, posX, posY))
						{
							currBlock = Block.rotateR(currBlock);
						}
						break;
					case Key.Up:
						while(canPosAt(currBlock, posX, posY+1))
						{
							step();
						}
						step();
						break;
					default:
						break;
				}
			}
		}
        #endregion
        #region mozgatás tesztelő
        private bool canPosAt(int[,] Block, int x, int y)
		{
			int[,] copy = (int[,])Container.Clone();

			if(x+Block.GetUpperBound(1) <= copy.GetUpperBound(1) && y+Block.GetUpperBound(0) <= copy.GetUpperBound(0))
			{
				for(int i = 0;i <= Block.GetUpperBound(1);i++)
				{
					for(int j = 0;j <= Block.GetUpperBound(0);j++)
					{
						if(Block[j, i] != 0)
						{
							if(copy[y + j, x + i] != 0)
							{
								return false;
							}
						}
					}
				}
				return true;
			}
			return false;
		}
        #endregion
        #region fix
        private int[,] fixBlock(int[,] Block,int[,] field, int x, int y)
		{
			if(x + Block.GetUpperBound(1) <= field.GetUpperBound(1) && y + Block.GetUpperBound(0) <= field.GetUpperBound(0))
			{
				for(int i = 0;i <= Block.GetUpperBound(1);i++)
				{
					for(int j = 0;j <= Block.GetUpperBound(0);j++)
					{
						if(Block[j, i] != 0)
						{
							field[y + j, x + i] = Block[j, i];
						}
					}
				}
			}
			if(BlockFixed != null)
			{
				BlockFixed();
			}
			return field;
		}
        #endregion
        #region ellenőrzés
        private int checkLines()
		{
			for(int i = 0;i < Container.GetUpperBound(0) + 1;i++)
			{
				bool isFullLine = true;
				for(int j = 0;j < Container.GetUpperBound(1) + 1;j++)
				{
					isFullLine = isFullLine && Container[i, j] != 0;
				}
				if(isFullLine)
				{
					removeLine(i--);
					return checkLines() + 1;
				}
			}
			return 0;
		}
        #endregion
        #region törlés
        private void removeLine(int index)
		{
			for(int i = index;i > 0;i--)
			{
				for(int j = 0;j <= Container.GetUpperBound(1);j++)
				{
					Container[i, j] = Container[i-1, j];
				}
			}
			for(int j = 0;j <= Container.GetUpperBound(1);j++)
			{
				Container[0, j] = 0;
			}
		}
		public int[,] Level
		{
			get
			{
				int[,] Block = (int[,])currBlock.Clone();
				int[,] temp = (int[,])Container.Clone();
				int add=0;


				for(int i = 0;i <= Block.GetUpperBound(0);i++)
				{
					for(int j = 0;j <= Block.GetUpperBound(1);j++)
					{
						if(Block[i, j] != 0)
						{
							Block[i, j] = 7;
						}
					}
				}
				temp=fixBlock(Block,temp,posX,posY);

				if(shadow)
				{
					for(int i = 0;i <= Block.GetUpperBound(0);i++)
					{
						for(int j = 0;j <= Block.GetUpperBound(1);j++)
						{
							if(Block[i, j] != 0)
							{
								Block[i, j] = 8;
							}
						}
					}

					while(canPosAt(Block, posX, posY + add))
					{
						add++;
					}

					if(posY + add - 1 > 0)
					{
						return (int[,])fixBlock(Block, temp, posX, posY + add - 1).Clone();
					}
				}
				return temp;
			}
		}
		public bool running
		{
			get
			{
				return inGame;
			}
		}
		public int[,] Next
		{
			get
			{
				return nextBlock;
			}
		}
		public bool shadowBlock
		{
			get
			{
				return shadow;
			}
			set
			{
				shadow = value;
			}
        }
        #endregion
    }
}



The tetris class
using System;
using System.Collections.Generic;
using System.Text;

namespace Tetrisz
{
	public class Tetris
    {
        #region Változók
        public delegate void GameOverHandler();
		public delegate void BlockFixedHandler();
		public delegate void LinesDoneHandler(int Lines);
		public event GameOverHandler GameOver;
		public event BlockFixedHandler BlockFixed;
		public event LinesDoneHandler LinesDone;
		public enum Key
		{
			Left,
			Right,
			Up,
			Down,
			rRight,
			rLeft
		}
		private int[,] Container;
		private int posX;
		private int posY;
		private int[,] currBlock=null;
		private int[,] nextBlock = null;
		private Block bGen=new Block();
		private bool inGame;
		private bool shadow;
        #endregion
        #region tetrisz
        public Tetris(int Width, int Height)
		{
			shadow = true;
			if(Width >= 10 && Height >= 20)
			{
				Container = new int[Height, Width];
			}
			else
			{
				throw new Exception("legalább  10x20");
			}

		}
        #endregion
        #region indítás
        public void start()
		{
			inGame = true;
			posY = 0;
			posX=Container.GetUpperBound(1)/2;
			currBlock =nextBlock!=null?nextBlock:bGen.getRandomBlock();
			nextBlock = bGen.getRandomBlock();
			if(!canPosAt(currBlock, posX, posY))
			{
				gameOver();
			}
		}
        #endregion
        #region vége a játéknak
        public void gameOver()
		{
			inGame = false;
			if(GameOver != null)
			{
				GameOver();
			}
		}
        #endregion
        #region lépés
        public void step()
		{
			if(inGame)
			{
				if(canPosAt(currBlock, posX, posY + 1))
				{
					posY++;
				}
				else
				{
					Container=fixBlock(currBlock,Container, posX, posY);

					start();
				}
				int Lines=checkLines();
				if(LinesDone != null)
				{
					LinesDone(Lines);
				}
			}
		}
        #endregion
        #region mozgatás
        public void keyPress(Key k)
		{
			if(inGame)
			{
				int[,] temp;
				switch(k)
				{
					case Key.Down:
						step();
						break;
					case Key.Left:
						if(posX>0 && canPosAt(currBlock, posX - 1, posY))
						{
							posX--;
						}
						break;
					case Key.Right:
						if(posX<Container.GetUpperBound(0)-currBlock.GetUpperBound(0) && canPosAt(currBlock, posX +1, posY))
						{
							posX++;
						}
						break;
					case Key.rLeft:
						temp = Block.rotateL(currBlock);
						if(canPosAt(temp, posX, posY))
						{
							currBlock = Block.rotateL(currBlock);
						}
						break;
					case Key.rRight:
						temp = Block.rotateR(currBlock);
						if(canPosAt(temp, posX, posY))
						{
							currBlock = Block.rotateR(currBlock);
						}
						break;
					case Key.Up:
						while(canPosAt(currBlock, posX, posY+1))
						{
							step();
						}
						step();
						break;
					default:
						break;
				}
			}
		}
        #endregion
        #region mozgatás tesztelő
        private bool canPosAt(int[,] Block, int x, int y)
		{
			int[,] copy = (int[,])Container.Clone();

			if(x+Block.GetUpperBound(1) <= copy.GetUpperBound(1) && y+Block.GetUpperBound(0) <= copy.GetUpperBound(0))
			{
				for(int i = 0;i <= Block.GetUpperBound(1);i++)
				{
					for(int j = 0;j <= Block.GetUpperBound(0);j++)
					{
						if(Block[j, i] != 0)
						{
							if(copy[y + j, x + i] != 0)
							{
								return false;
							}
						}
					}
				}
				return true;
			}
			return false;
		}
        #endregion
        #region fix
        private int[,] fixBlock(int[,] Block,int[,] field, int x, int y)
		{
			if(x + Block.GetUpperBound(1) <= field.GetUpperBound(1) && y + Block.GetUpperBound(0) <= field.GetUpperBound(0))
			{
				for(int i = 0;i <= Block.GetUpperBound(1);i++)
				{
					for(int j = 0;j <= Block.GetUpperBound(0);j++)
					{
						if(Block[j, i] != 0)
						{
							field[y + j, x + i] = Block[j, i];
						}
					}
				}
			}
			if(BlockFixed != null)
			{
				BlockFixed();
			}
			return field;
		}
        #endregion
        #region ellenőrzés
        private int checkLines()
		{
			for(int i = 0;i < Container.GetUpperBound(0) + 1;i++)
			{
				bool isFullLine = true;
				for(int j = 0;j < Container.GetUpperBound(1) + 1;j++)
				{
					isFullLine = isFullLine && Container[i, j] != 0;
				}
				if(isFullLine)
				{
					removeLine(i--);
					return checkLines() + 1;
				}
			}
			return 0;
		}
        #endregion
        #region törlés
        private void removeLine(int index)
		{
			for(int i = index;i > 0;i--)
			{
				for(int j = 0;j <= Container.GetUpperBound(1);j++)
				{
					Container[i, j] = Container[i-1, j];
				}
			}
			for(int j = 0;j <= Container.GetUpperBound(1);j++)
			{
				Container[0, j] = 0;
			}
		}
		public int[,] Level
		{
			get
			{
				int[,] Block = (int[,])currBlock.Clone();
				int[,] temp = (int[,])Container.Clone();
				int add=0;


				for(int i = 0;i <= Block.GetUpperBound(0);i++)
				{
					for(int j = 0;j <= Block.GetUpperBound(1);j++)
					{
						if(Block[i, j] != 0)
						{
							Block[i, j] = 7;
						}
					}
				}
				temp=fixBlock(Block,temp,posX,posY);

				if(shadow)
				{
					for(int i = 0;i <= Block.GetUpperBound(0);i++)
					{
						for(int j = 0;j <= Block.GetUpperBound(1);j++)
						{
							if(Block[i, j] != 0)
							{
								Block[i, j] = 8;
							}
						}
					}

					while(canPosAt(Block, posX, posY + add))
					{
						add++;
					}

					if(posY + add - 1 > 0)
					{
						return (int[,])fixBlock(Block, temp, posX, posY + add - 1).Clone();
					}
				}
				return temp;
			}
		}
		public bool running
		{
			get
			{
				return inGame;
			}
		}
		public int[,] Next
		{
			get
			{
				return nextBlock;
			}
		}
		public bool shadowBlock
		{
			get
			{
				return shadow;
			}
			set
			{
				shadow = value;
			}
        }
        #endregion
    }
}



The main class/program
#region Usings
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Win32.SafeHandles;
using System.Text;
#endregion

namespace Tetrisz
{
    #region screenBuff
    public class ScreenBuffer
    {
        //initiate important variables
        public static char[,] screenBufferArray = new char[80, 25]; //main buffer array
        public static string screenBuffer; //buffer as string (used when drawing)
        public static Char[] arr; //temporary array for drawing string
        public static int i = 0; //keeps track of the place in the array to draw to

        //this method takes a string, and a pair of coordinates and writes it to the buffer
        public static void Draw(string text, int x, int y)
        {
            //split text into array
            arr = text.ToCharArray(0, text.Length);
            //iterate through the array, adding values to buffer 
            i = 0;
            foreach (char c in arr)
            {
                screenBufferArray[x + i, y] = c;
                i++;
            }
        }

        public static void DrawScreen()
        {
            screenBuffer = "";
            //iterate through buffer, adding each value to screenBuffer
            for (int iy = 0; iy < 25 - 1; iy++)
            {
                for (int ix = 0; ix < 80; ix++)
                {
                    screenBuffer += screenBufferArray[ix, iy];
                }
            }
            //set cursor position to top left and draw the string
            Console.SetCursorPosition(0, 0);
            Console.Write(screenBuffer);
            screenBufferArray = new char[80, 25];
            //note that the screen is NOT cleared at any point as this will simply overwrite the existing values on screen. Clearing will cause flickering again.
        }

    }
    #endregion

    class Program
    {
        #region initializásció
        static SafeFileHandle h = CreateFile("CONOUT$", 0x40000000, 2, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
        static CharInfo[] buf = new CharInfo[80 * 25];
        static SmallRect rect = new SmallRect() { Left = 0, Top = 0, Right = 80, Bottom = 25 };
        private static Tetris t;
        private static Thread mover;
        private static bool drawLock;
        private static int points;
        private string paaath = (Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
        static int threadCounter = 0;
        private const string blokk = "█";
        private const string aktiv = "█";
        private const string jon = "█";
        private const string ures = " ";
        private const string atlatszo = "▒";
        private const string filepath = "c:/asd.txt";
        private const int Lepes = 5;
        private static bool kov;
        private static bool timerasdsdfsfg = false;
        private static bool zenebool = true;
        string idocucckombo = null;
        private static readonly int[,] urestomb = new int[,]
		{
			{0,0,0,0,0},
			{0,0,0,0,0},
			{0,0,0,0,0},
			{0,0,0,0,0}
		};
        #endregion

        #region GetRandomConsoleColor()
        private static Random _random = new Random();
        private static ConsoleColor GetRandomConsoleColor()
        {
            var consoleColors = Enum.GetValues(typeof(ConsoleColor));
            return (ConsoleColor)consoleColors.GetValue(_random.Next(consoleColors.Length));
        }
        #endregion

        #region Pinvoke
        [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern SafeFileHandle CreateFile(
            string fileName,
            [MarshalAs(UnmanagedType.U4)] uint fileAccess,
            [MarshalAs(UnmanagedType.U4)] uint fileShare,
            IntPtr securityAttributes,
            [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
            [MarshalAs(UnmanagedType.U4)] int flags,
            IntPtr template);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool WriteConsoleOutput(
          SafeFileHandle hConsoleOutput,
          CharInfo[] lpBuffer,
          Coord dwBufferSize,
          Coord dwBufferCoord,
          ref SmallRect lpWriteRegion);

        [StructLayout(LayoutKind.Sequential)]
        public struct Coord
        {
            public short X;
            public short Y;

            public Coord(short X, short Y)
            {
                this.X = X;
                this.Y = Y;
            }
        };

        [StructLayout(LayoutKind.Explicit)]
        public struct CharUnion
        {
            [FieldOffset(0)]
            public char UnicodeChar;
            [FieldOffset(0)]
            public byte AsciiChar;
        }

        [StructLayout(LayoutKind.Explicit)]
        public struct CharInfo
        {
            [FieldOffset(0)]
            public CharUnion Char;
            [FieldOffset(2)]
            public short Attributes;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct SmallRect
        {
            public short Left;
            public short Top;
            public short Right;
            public short Bottom;
        }


        #endregion
        [STAThread]
        static void Main(string[] args)
            {
                Console.SetWindowSize(80,25);
                ScreenBuffer sb = new ScreenBuffer();
                #region Változók
                #region Idő
                ThreadStart IdoThread = new ThreadStart(idothread);
                Thread timercucc = new Thread(IdoThread);
                #endregion
                #region Thread zene
                ThreadStart asd = new ThreadStart(zenethread);
                Thread ZeneThread = new Thread(asd);
                #endregion
                //ZeneThread.Start();
                int i = 1;
                string name = null;
                string vegleges=null;
                #endregion
                #region Névbekérés
               /* Console.WriteLine("Kerem adja meg a nevét!");
                name = Convert.ToString(Console.ReadLine());//név beolvasás
                Console.Clear();//ktörlés
                Console.WriteLine("Adja meg a nevét!");*/
#endregion
                Console.ForegroundColor = ConsoleColor.Gray;//betüszín
                Console.BackgroundColor = ConsoleColor.Black;//háttérszín
                Console.Clear();//képernyőttörlés
                Console.CursorVisible = false;//kurzor ne látszon
                #region utasítások
                writecol(@"
                    Tetrisz
        =======================================

            Controls:
            [→]   Jobra
            [←]   Balra
            [↑]   Blokk ledobása
            [↓]   le
            [Z]   Zene kikapcsolása
            [S]   jobra forgatás
            [A]   trippla jobbra forgatás
            [G]   szellem blokk kikapcs
            [N]   következő blokk ki/be
            [ESC] Kilépés

            Nyomj meg egy gombot a játék indításához...",ConsoleColor.Red);
                #endregion
                Console.ReadKey(true);
                #region Thread start
                //ZeneThread.Start();
                //timercucc.Start();
                #endregion
                Console.Clear();
                #region változók értékadása
                kov = true;
                points = 0;
                drawLock = false;
                t = new Tetris(63, 23); //TETRISZ méret
                t.LinesDone += new Tetris.LinesDoneHandler(t_LinesDone);////tetrisz sorok
                #endregion
                #region mover
                t.start();//tetrisz indítása
                mover = new Thread(new ThreadStart(stepper));
                mover.IsBackground = true;//mozgatás háttérben fut
                mover.Start();//mozgatás start
                #endregion
                // Gombok //
                #region gombok mozgatás stb
                while (t.running)
                {
                    Thread.Sleep(10);
                    if (Console.KeyAvailable)
                    {
                        lock (t)
                        {
                            switch (Console.ReadKey(true).Key)
                            {
                                case ConsoleKey.LeftArrow:
                                    t.keyPress(Tetris.Key.Left);
                                    break;
                                case ConsoleKey.RightArrow:
                                    t.keyPress(Tetris.Key.Right);
                                    break;
                                case ConsoleKey.UpArrow:
                                    t.keyPress(Tetris.Key.Up);
                                    threadCounter = 0;
                                    break;
                                case ConsoleKey.DownArrow:
                                    t.keyPress(Tetris.Key.Down);
                                    threadCounter = 0;
                                    break;
                                case ConsoleKey.A:
                                    t.keyPress(Tetris.Key.rLeft);
                                    break;
                                case ConsoleKey.S:
                                    t.keyPress(Tetris.Key.rRight);
                                    break;
                                case ConsoleKey.N:
                                    kov = !kov;
                                    break;
                                case ConsoleKey.G:
                                    t.shadowBlock = !t.shadowBlock;
                                    break;
                                case ConsoleKey.Escape:
                                    t.gameOver();
                                    break;
                                case ConsoleKey.Z:
                                        //ZeneThread.Suspend();
                                        break;
                                default:
                                    break;
                            }
                            drawField();
                        }
                    }
                }
                #endregion
                //Gombok//
                Thread.Sleep(1000);//Lépés ideje
                Console.Clear();//Refresh
                //endgame score
                #region Game over nagyszöveg
                vegleges = (name + ":" + points);
                timerasdsdfsfg = true;
                zenebool = false;
                string[] lines = {vegleges};
                //timercucc.Suspend();
                //ZeneThread.Suspend();
                File.Create(filepath);
                var g = File.OpenWrite(filepath);
               
                var scores = File.ReadAllLines(filepath)
                         .Select(x => x.Split(":".ToCharArray())) // split name and score
                         .Select(x => new
                         {
                             Name = x[0],
                             Score = int.Parse(x[1])
                         }) // create an object contains name and score for each line
                         .OrderByDescending(x => x.Score) // sort result by score
                         .Take(10); //get top 10
                Console.WriteLine("                          -----------");
                Console.WriteLine("                          HIGH SCORES");
                Console.WriteLine("                          -----------");
                Console.WriteLine("                          Name     Score");
                foreach (var score in scores)
                {

                    Console.WriteLine("                          " + i + "." + score.Name + "  " + score.Score);
                    i++;
                }
                #endregion//Endgame
                while (Console.ReadKey(true).Key != ConsoleKey.Enter);
                Console.ResetColor();//Alap szinek visszaállítás
                Console.CursorVisible = true;//Kurzor látható
                return;//viszatérés
            }
            //Eljárésok//
            #region Készsorok
            static void t_LinesDone(int Lines)
            {
                points += Lines;
            }
            #endregion
            #region timer
            static void stepper()
            {
                while (t.running)
                {
                    threadCounter = 0;
                    t.step();
                    drawField();
                    while (threadCounter < 1000 - (points * 5) && t.running)
                    {
                        Thread.Sleep(Lepes);
                        threadCounter += Lepes;
                    }
                }
            }
            #endregion
            #region kirajzolás
            private static void drawField()
            {
                while (drawLock);
                drawLock = true;
                int posX = Console.CursorLeft;
                int posY = Console.CursorTop;
                //Console.CursorLeft = 68;//pont kooorrsinátál
                //Console.CursorTop = 8;
                //Console.WriteLine("Pont: {0}", points);
                ScreenBuffer.Draw("Pont: "+points, 68, 8);
                
                Console.SetCursorPosition(posX, posY);
                writeArr(t.Level, true);
                Console.CursorLeft = 68;// ghostbox
                Console.CursorTop = 1;
                writeArr(urestomb, true);
                if (kov)
                {
                    Console.CursorLeft = 70;
                    Console.CursorTop = 2; // ghostbox ban
                    writeArr(t.Next, false);
                }
                Console.SetCursorPosition(posX, posY);
                drawLock = false;
            }
            #endregion
            #region tömb kiitás
            static void writeArr(int[,] qq, bool writeBorder)
            {
                int x = Console.CursorLeft;
                for (int i = 0; i <= qq.GetUpperBound(0); i++)
                {
                    if (writeBorder)
                    {
                        ScreenBuffer.Draw(jon,Console.CursorLeft,Console.CursorTop);
                    }
                    for (int j = 0; j <= qq.GetUpperBound(1); j++)
                    {
                        switch (qq[i, j])
                        {
                            case 1:
                                writeCol(blokk, ConsoleColor.White);
                                break;
                            case 2:
                                writeCol(blokk, ConsoleColor.Magenta);
                                break;
                            case 3:
                                writeCol(blokk, ConsoleColor.Blue);
                                break;
                            case 4:
                                writeCol(blokk, ConsoleColor.Green);
                                break;
                            case 5:
                                writeCol(blokk, ConsoleColor.Yellow);
                                break;
                            case 6:
                                writeCol(blokk, ConsoleColor.Red);
                                break;
                            case 7:
                                Console.BackgroundColor = ConsoleColor.Red;
                                writeCol(aktiv, ConsoleColor.Cyan);
                                Console.BackgroundColor = ConsoleColor.Black;
                                break;
                            case 8:
                                Console.BackgroundColor = ConsoleColor.Gray;
                                writeCol(atlatszo, ConsoleColor.Gray);
                                Console.BackgroundColor = ConsoleColor.Black;
                                break;
                            default:
                                Console.Write(ures);
                                break;
                        }
                        ScreenBuffer.DrawScreen();
                    }
                    if (writeBorder)
                    {
                        Console.WriteLine(jon);
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                    Console.CursorLeft = x;
                }
                for (int q = 0; q <= qq.GetUpperBound(1) + 2; q++)
                {
                    if (writeBorder)
                    {
                        Console.Write(jon);
                    }
                }
            }
            #endregion
            #region Kiiratás
            static void writeCol(string a, ConsoleColor B)/>
            {
                ConsoleColor c = Console.ForegroundColor;
                Console.ForegroundColor = b;
                ScreenBuffer.Draw(a, Console.CursorLeft, Console.CursorTop);
                Console.ForegroundColor = c;
            }
            #endregion
            #region Zenethread
           public static void zenethread()
            {
                do
                {
                    #region Zene
                    do
                    {
                        Console.Beep(658, 125);
                        Console.Beep(1320, 500);
                        Console.Beep(990, 250);
                        Console.Beep(1056, 250);
                        Console.Beep(1188, 250);
                        Console.Beep(1320, 125);
                        Console.Beep(1188, 125);
                        Console.Beep(1056, 250);
                        Console.Beep(990, 250);
                        Console.Beep(880, 500);
                        Console.Beep(880, 250);
                        Console.Beep(1056, 250);
                        Console.Beep(1320, 500);
                        Console.Beep(1188, 250);
                        Console.Beep(1056, 250);
                        Console.Beep(990, 750);
                        Console.Beep(1056, 250);
                        Console.Beep(1188, 500);
                        Console.Beep(1320, 500);
                        Console.Beep(1056, 500);
                        Console.Beep(880, 500);
                        Console.Beep(880, 500);
                        Thread.Sleep(250);
                        Console.Beep(1188, 500);
                        Console.Beep(1408, 250);
                        Console.Beep(1760, 500);
                        Console.Beep(1584, 250);
                        Console.Beep(1408, 250);
                        Console.Beep(1320, 750);
                        Console.Beep(1056, 250);
                        Console.Beep(1320, 500);
                        Console.Beep(1188, 250);
                        Console.Beep(1056, 250);
                        Console.Beep(990, 500);
                        Console.Beep(990, 250);
                        Console.Beep(1056, 250);
                        Console.Beep(1188, 500);
                        Console.Beep(1320, 500);
                        Console.Beep(1056, 500);
                        Console.Beep(880, 500);
                        Console.Beep(880, 500);
                        Thread.Sleep(500);
                        Console.Beep(1320, 500);
                        Console.Beep(990, 250);
                        Console.Beep(1056, 250);
                        Console.Beep(1188, 250);
                        Console.Beep(1320, 125);
                        Console.Beep(1188, 125);
                        Console.Beep(1056, 250);
                        Console.Beep(990, 250);
                        Console.Beep(880, 500);
                        Console.Beep(880, 250);
                        Console.Beep(1056, 250);
                        Console.Beep(1320, 500);
                        Console.Beep(1188, 250);
                        Console.Beep(1056, 250);
                        Console.Beep(990, 750);
                        Console.Beep(1056, 250);
                        Console.Beep(1188, 500);
                        Console.Beep(1320, 500);
                        Console.Beep(1056, 500);
                        Console.Beep(880, 500);
                        Console.Beep(880, 500);
                        Thread.Sleep(250);
                        Console.Beep(1188, 500);
                        Console.Beep(1408, 250);
                        Console.Beep(1760, 500);
                        Console.Beep(1584, 250);
                        Console.Beep(1408, 250);
                        Console.Beep(1320, 750);
                        Console.Beep(1056, 250);
                        Console.Beep(1320, 500);
                        Console.Beep(1188, 250);
                        Console.Beep(1056, 250);
                        Console.Beep(990, 500);
                        Console.Beep(990, 250);
                        Console.Beep(1056, 250);
                        Console.Beep(1188, 500);
                        Console.Beep(1320, 500);
                        Console.Beep(1056, 500);
                        Console.Beep(880, 500);
                        Console.Beep(880, 500);
                        Thread.Sleep(500);
                        Console.Beep(660, 1000);
                        Console.Beep(528, 1000);
                        Console.Beep(594, 1000);
                        Console.Beep(495, 1000);
                        Console.Beep(528, 1000);
                        Console.Beep(440, 1000);
                        Console.Beep(419, 1000);
                        Console.Beep(495, 1000);
                        Console.Beep(660, 1000);
                        Console.Beep(528, 1000);
                        Console.Beep(594, 1000);
                        Console.Beep(495, 1000);
                        Console.Beep(528, 500);
                        Console.Beep(660, 500);
                        Console.Beep(880, 1000);
                        Console.Beep(838, 2000);
                        Console.Beep(660, 1000);
                        Console.Beep(528, 1000);
                        Console.Beep(594, 1000);
                        Console.Beep(495, 1000);
                        Console.Beep(528, 1000);
                        Console.Beep(440, 1000);
                        Console.Beep(419, 1000);
                        Console.Beep(495, 1000);
                        Console.Beep(660, 1000);
                        Console.Beep(528, 1000);
                        Console.Beep(594, 1000);
                        Console.Beep(495, 1000);
                        Console.Beep(528, 500);
                        Console.Beep(660, 500);
                        Console.Beep(880, 1000);
                        Console.Beep(838, 2000);
                    } while (true);
                    #endregion
                } while (zenebool);
            }
            #endregion
            #region Idő
            public static void idothread()
            {
                var stopwatch = new Stopwatch();
                stopwatch.Start();

               while (true)
                {
                    Thread.Sleep(100);
                    Console.Title = String.Format("TETRISZ: {0:R}", stopwatch.Elapsed.TotalSeconds + " s");
                   if(timerasdsdfsfg == true)
                    break;
                }
            }
            #endregion
        #region Pinviko printout
            static void writecol(string a, ConsoleColor B)/>
            {
                byte x = 0, y = 0;
                // parsing to make it  fly
                // fill the buffer with the string 
                for (int ci = 0; ci < a.Length; ci++)
                {
                    switch (a[ci])
                    {
                        case '\n': // newline char, move to next line, aka y=y+1
                            y++;
                            x = 0;
                            break;
                        case '\r': // do nothing
                            break;
                        case ' ': // a space, move the cursor to the right
                            x++;
                            break;
                        default:
                            // calculate where we should be in the buffer
                            int i = y * 80 + x;
                            // color
                            // put the current char from the string in the buffer
                            buf[i].Attributes = (short)b;
                            buf[i].Char.AsciiChar = (byte)a[ci];
                            x++;
                            break;
                    }
                }
                // we handled our string, let's write the whole screen at once
                bool success = WriteConsoleOutput(h, buf,
                             new Coord() { X = 80, Y = 25 },
                             new Coord() { X = 0, Y = 0 },
                             ref rect);
            }
        #endregion
        //Eljárások//
      }
   }


Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6549
  • View blog
  • Posts: 22,493
  • Joined: 05-May 12

Re: C# Tetris Console Game design

Posted 09 February 2015 - 09:18 PM

There's a couple of issues here:

You don't fully utilize your import of WriteConsoleOutput(). You see to use it for drawing small parts of the screen, but then you end up going back to using Console.Write() to redraw the entire screen in ScreenBuffer.DrawScreen(). The WriteConsoleOutput() is much faster than Console.Write().

Your code is kind of convoluted, but from what I can follow, it looks like you draw the screen multiple times within your writeArr() because the call to ScreenBuffer.DrawScreen() lives inside your for loop.

You really should only draw things on screen when something changes, and only re-draw the minimum area needed instead of redrawing the entire screen. Your ScreenBuffer would be the perfect avenue to being able to implement this type of logic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1