Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Block
- {
- //Note that static variables are only created once, so even if i create 1000 Blocks there is only one "Brushes" array
- static SolidBrush[] Brushes = {new SolidBrush(Color.Red), new SolidBrush(Color.Green), ...};
- static int BlockWidth = 64;
- static int BlockHeight = 64;
- int BrushIndex;
- public Block(int brushIndex)
- {
- BrushIndex = brushIndex;
- }
- public SolidBrush GetBrush()
- {
- return Brushes[BrushIndex];
- }
- }
- class Tetronimo
- {
- public Point Position; // A point is just an X and Y coordinate (both ints)
- public Block[,] Blocks;
- public int Width; // just sotres the width of the array so i dont have to check
- public int Height; // Same but height
- TetornimoType Type; // Stores the type in case i need it.
- GameGrid Grid; // this is the game grid, just so we know its width and height from in this class
- public Tetronimo(TetornimoType type, GrameGrid grid)
- {
- Type = type;
- Grid = grid;
- switch(Type)
- {
- case TetornimoType.Square
- Width = 2;
- Height = 2;
- Blocks = new Block[Width, Height];
- Blocks[0, 0] = new Block(0);
- Blocks[0, 1] = new Block(0);
- Blocks[1, 0] = new Block(0);
- Blocks[1, 1] = new Block(0);
- break;
- case TetronimoType.Line:
- ...
- break;
- ... // All the other Types
- }
- }
- public bool MoveLeft()
- {
- if(Position.X > 0)
- {
- Position.X = Position.X - 1;
- return true;
- }
- else
- {
- return false; // Cant move left because its already against the left wall
- }
- }
- public bool MoveLeft()
- {
- if(Position.X < (Grid.Width - 1))
- {
- Position.X = Position.X + 1;
- return true;
- }
- else
- {
- return false; // Cant move right because its already against the right wall
- }
- }
- }
- class GameGrid
- {
- Block[,] Grid;
- int Width;
- int Height;
- Tetronimo CurrentPiece;
- public GameGrid(int width, int height)
- {
- Width = width;
- Height = height;
- Grid = new Block[Width, Height];
- // Notice i pass in "this" so that the tetronimo knows how wide the grid is in the MoveRight() function
- CurrentPiece = new Tetornimo(TetornimoType.Square, this)
- }
- public void Draw(Graphics g)
- {
- Rectangle Cell;
- // Draw Grid
- for(int x = 0; x < Width; x++)
- {
- for(int y = 0; y < Height; y++)
- {
- if(Grid[x, y] != null)
- {
- // setup rectangle for this block
- Cell.X = x * Block.BlockWidth;
- Cell.Y = y * Block.BlockHeight;
- Cell.Width = Block.BlockWidth;
- Cell.Height = Block.BlockHeight;
- g.FillRectangle(Grid[x, y].GetBrush(), Cell);
- }
- }
- }
- //Draw Tetronimo
- for(int x = 0; x < CurrentPiece.Width; x++)
- {
- for(int y = 0; y < CurrentPice.Height; y++)
- {
- if(CurrentPiece.Blocks[x, y] != null)
- {
- //Adding the Position of the tetronimo gives
- //us the position the block should be drawn on the grid
- Cell.X = (x + CurrentPiece.Position.X) * Block.BlockWidth;
- Cell.Y = (y + CurrentPiece.Position.Y) * Block.BlockHeight;
- Cell.Width = Block.BlockWidth;
- Cell.Height = Block.BlockHeight;
- g.FillRectangle(CurrentPiece.Blocks[x, y].GetBrush(), Cell);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement