Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. class Block
  2. {
  3.     //Note that static variables are only created once, so even if i create 1000 Blocks there is only one "Brushes" array
  4.     static SolidBrush[] Brushes = {new SolidBrush(Color.Red), new SolidBrush(Color.Green), ...};
  5.     static int BlockWidth = 64;
  6.     static int BlockHeight = 64;
  7.    
  8.     int BrushIndex;
  9.    
  10.     public Block(int brushIndex)
  11.     {
  12.         BrushIndex = brushIndex;
  13.     }
  14.    
  15.     public SolidBrush GetBrush()
  16.     {
  17.         return Brushes[BrushIndex];
  18.     }
  19. }
  20.  
  21.  
  22.  
  23. class Tetronimo
  24. {
  25.     public Point Position;  // A point is just an X and Y coordinate (both ints)
  26.     public Block[,] Blocks;
  27.     public int Width; // just sotres the width of the array so i dont have to check
  28.     public int Height; // Same but height
  29.     TetornimoType Type; // Stores the type in case i need it.
  30.     GameGrid Grid; // this is the game grid, just so we know its width and height from in this class
  31.    
  32.     public Tetronimo(TetornimoType type, GrameGrid grid)
  33.     {
  34.         Type = type;
  35.         Grid = grid;
  36.        
  37.         switch(Type)
  38.         {
  39.             case TetornimoType.Square
  40.                 Width = 2;
  41.                 Height = 2;
  42.                 Blocks = new Block[Width, Height];
  43.                 Blocks[0, 0] = new Block(0);
  44.                 Blocks[0, 1] = new Block(0);
  45.                 Blocks[1, 0] = new Block(0);
  46.                 Blocks[1, 1] = new Block(0);
  47.                 break;
  48.            
  49.             case TetronimoType.Line:
  50.                 ...
  51.                 break;
  52.                
  53.                 ... // All the other Types
  54.         }
  55.     }
  56.    
  57.     public bool MoveLeft()
  58.     {
  59.         if(Position.X > 0)
  60.         {
  61.             Position.X = Position.X - 1;
  62.             return true;
  63.         }
  64.         else
  65.         {
  66.             return false; // Cant move left because its already against the left wall
  67.         }
  68.     }
  69.    
  70.     public bool MoveLeft()
  71.     {
  72.         if(Position.X < (Grid.Width - 1))
  73.         {
  74.             Position.X = Position.X + 1;
  75.             return true;
  76.         }
  77.         else
  78.         {
  79.             return false; // Cant move right because its already against the right wall
  80.         }
  81.     }
  82. }
  83.  
  84.  
  85. class GameGrid
  86. {
  87.     Block[,] Grid;
  88.     int Width;
  89.     int Height;
  90.     Tetronimo CurrentPiece;
  91.    
  92.     public GameGrid(int width, int height)
  93.     {
  94.         Width = width;
  95.         Height = height;
  96.         Grid = new Block[Width, Height];
  97.        
  98.         // Notice i pass in "this" so that the tetronimo knows how wide the grid is in the MoveRight() function
  99.         CurrentPiece = new Tetornimo(TetornimoType.Square, this)
  100.     }
  101.    
  102.     public void Draw(Graphics g)
  103.     {
  104.        
  105.         Rectangle Cell;
  106.        
  107.         // Draw Grid
  108.         for(int x = 0; x < Width; x++)
  109.         {
  110.             for(int y = 0; y < Height; y++)
  111.             {
  112.                 if(Grid[x, y] != null)
  113.                 {
  114.                     // setup rectangle for this block
  115.                     Cell.X = x * Block.BlockWidth;
  116.                     Cell.Y = y * Block.BlockHeight;
  117.                     Cell.Width = Block.BlockWidth;
  118.                     Cell.Height = Block.BlockHeight;
  119.                    
  120.                     g.FillRectangle(Grid[x, y].GetBrush(), Cell);
  121.                 }
  122.             }
  123.         }
  124.        
  125.         //Draw Tetronimo
  126.         for(int x = 0; x < CurrentPiece.Width; x++)
  127.         {
  128.             for(int y = 0; y < CurrentPice.Height; y++)
  129.             {
  130.                 if(CurrentPiece.Blocks[x, y] != null)
  131.                 {
  132.                     //Adding the Position of the tetronimo gives
  133.                     //us the position the block should be drawn on the grid
  134.                     Cell.X = (x + CurrentPiece.Position.X) * Block.BlockWidth;
  135.                     Cell.Y = (y + CurrentPiece.Position.Y) * Block.BlockHeight;
  136.                    
  137.                     Cell.Width = Block.BlockWidth;
  138.                     Cell.Height = Block.BlockHeight;
  139.                    
  140.                     g.FillRectangle(CurrentPiece.Blocks[x, y].GetBrush(), Cell);
  141.                 }
  142.             }
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement