Advertisement
PatrickAupperle

Game1.cs

Oct 24th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace Tetris
  13. {
  14.     /// <summary>
  15.     /// This is the main type for your game
  16.     /// </summary>
  17.     public class Game1 : Microsoft.Xna.Framework.Game
  18.     {
  19.         private const int _HorizOffset = 10;
  20.         private const int _VertOffset = 2;
  21.         private const int _Height = 40;
  22.         private const int _Width = 10;
  23.  
  24.  
  25.         readonly GraphicsDeviceManager _graphics;
  26.         SpriteBatch _spriteBatch;
  27.        
  28.         Block _block;
  29.         private TimeSpan _elapsedTime;
  30.         private TimeSpan _moveTimer;
  31.         private Texture2D _texture;
  32.         private List<Block> _blockList;
  33.         private List<bool[,]> _blockArray;
  34.  
  35.         public Game1()
  36.         {
  37.             _graphics = new GraphicsDeviceManager(this);
  38.             Content.RootDirectory = "Content";
  39.  
  40.         }
  41.  
  42.         /// <summary>
  43.         /// Allows the game to perform any initialization it needs to before starting to run.
  44.         /// This is where it can query for any required services and load any non-graphic
  45.         /// related content.  Calling base.Initialize will enumerate through any components
  46.         /// and initialize them as well.
  47.         /// </summary>
  48.         protected override void Initialize()
  49.         {
  50.             // TODO: Add your initialization logic here
  51.             _elapsedTime = new TimeSpan(0);
  52.             _blockList = new List<Block>();
  53.  
  54.             _blockArray = new List<bool[,]>
  55.                               {
  56.                                   //Straight Block
  57.                                   new[,]
  58.                                       {
  59.                                           {true, true, true, true}
  60.                                       },
  61.                                   new[,]
  62.                                       {
  63.                                           {true},
  64.                                           {true},
  65.                                           {true},
  66.                                           {true}
  67.                                       },
  68.                                   new[,]
  69.                                       {
  70.                                           {true, true, true, true}
  71.                                       },
  72.                                   new[,]
  73.                                       {
  74.                                           {true},
  75.                                           {true},
  76.                                           {true},
  77.                                           {true}
  78.                                       },
  79.                                   //Square Block
  80.                                   new[,]
  81.                                       {
  82.                                           {true, true},
  83.                                           {true, true},
  84.                                       },
  85.                                   new[,]
  86.                                       {
  87.                                           {true, true},
  88.                                           {true, true},
  89.                                       },
  90.                                   new[,]
  91.                                       {
  92.                                           {true, true},
  93.                                           {true, true},
  94.                                       },
  95.                                   new[,]
  96.                                       {
  97.                                           {true, true},
  98.                                           {true, true},
  99.                                       },
  100.                                   //S Block
  101.                                   new[,]
  102.                                       {
  103.                                           {false, true, true},
  104.                                           {true, true, false}
  105.                                       },
  106.                                   new[,]
  107.                                       {
  108.                                           {true, false},
  109.                                           {true, true},
  110.                                           {false, true}
  111.                                       },
  112.                                   new[,]
  113.                                       {
  114.                                           {false, true, true},
  115.                                           {true, true, false}
  116.                                       },
  117.                                   new[,]
  118.                                       {
  119.                                           {true, false},
  120.                                           {true, true},
  121.                                           {false, true}
  122.                                       },
  123.                                   //T Block
  124.                                   new[,]
  125.                                       {
  126.                                           {false, true, false},
  127.                                           {true, true, true}
  128.                                       },
  129.                                   new[,]
  130.                                       {
  131.                                           {true, false},
  132.                                           {true, true},
  133.                                           {true, false}
  134.                                       },
  135.                                   new[,]
  136.                                       {
  137.                                           {true, true, true},
  138.                                           {false, true, false}
  139.                                       },
  140.                                   new[,]
  141.                                       {
  142.                                           {false, true},
  143.                                           {true, true},
  144.                                           {false, true}
  145.                                       },
  146.                                   //Z Block
  147.                                   new[,]
  148.                                       {
  149.                                           {true, true, false},
  150.                                           {false, true, true}
  151.                                       },
  152.                                   new[,]
  153.                                       {
  154.                                           {false, true},
  155.                                           {true, true},
  156.                                           {true, false}
  157.                                       },
  158.                                   new[,]
  159.                                       {
  160.                                           {true, true, false},
  161.                                           {false, true, true}
  162.                                       },
  163.                                   new[,]
  164.                                       {
  165.                                           {false, true},
  166.                                           {true, true},
  167.                                           {true, false}
  168.                                       },
  169.                                   //Backwards L Block
  170.                                   new[,]
  171.                                       {
  172.                                           {true, false, false},
  173.                                           {true, true, true}
  174.                                       },
  175.                                   new[,]
  176.                                       {
  177.                                           {true, true},
  178.                                           {true, false},
  179.                                           {true, false}
  180.                                       },
  181.                                   new[,]
  182.                                       {
  183.                                           {true, true, true},
  184.                                           {false, false, true}
  185.                                       },
  186.                                   new[,]
  187.                                       {
  188.                                           {false, true},
  189.                                           {false, true},
  190.                                           {true, true}
  191.                                       },
  192.                                   //L Block
  193.                                   new[,]
  194.                                       {
  195.                                           {false, false, true},
  196.                                           {true, true, true}
  197.                                       },
  198.                                   new[,]
  199.                                       {
  200.                                           {true, false},
  201.                                           {true, false},
  202.                                           {true, true}
  203.                                       },
  204.                                   new[,]
  205.                                       {
  206.                                           {true, true, true},
  207.                                           {true, false, false}
  208.                                       },
  209.                                   new[,]
  210.                                       {
  211.                                           {true, true},
  212.                                           {false, true},
  213.                                           {false, true}
  214.                                       }
  215.                               };
  216.            
  217.             base.Initialize();
  218.         }
  219.  
  220.         /// <summary>
  221.         /// LoadContent will be called once per game and is the place to load
  222.         /// all of your content.
  223.         /// </summary>
  224.         protected override void LoadContent()
  225.         {
  226.             // Create a new SpriteBatch, which can be used to draw textures.
  227.             _texture = new Texture2D(_graphics.GraphicsDevice, 1, 1);
  228.             _texture.SetData(new[] { Color.White });
  229.             _spriteBatch = new SpriteBatch(GraphicsDevice);
  230.             var floor = new bool[1,_Width];
  231.             for (int i = 0; i < _Width; ++i)
  232.                 floor[0,i] = true;
  233.             _blockList.Add(new Block(new Vector2(_HorizOffset,_Height + _VertOffset), floor, Color.Red, _texture ));
  234.             _blockList.Add(new Block(new Vector2(_HorizOffset, _VertOffset - 1), floor, Color.Red, _texture));
  235.  
  236.             var side = new bool[_Height, 1];
  237.             for (int i = 0; i < _Height; ++i)
  238.                 side[i, 0] = true;
  239.             _blockList.Add(new Block(new Vector2(_HorizOffset - 1, _VertOffset), side, Color.Red, _texture));
  240.             _blockList.Add(new Block(new Vector2(_HorizOffset + _Width, _VertOffset), side, Color.Red, _texture));
  241.  
  242.             DropBlock();
  243.            
  244.  
  245.             // TODO: use this.Content to load your game content here
  246.         }
  247.  
  248.         /// <summary>
  249.         /// UnloadContent will be called once per game and is the place to unload
  250.         /// all content.
  251.         /// </summary>
  252.         protected override void UnloadContent()
  253.         {
  254.             // TODO: Unload any non ContentManager content here
  255.         }
  256.  
  257.         /// <summary>
  258.         /// Allows the game to run logic such as updating the world,
  259.         /// checking for collisions, gathering input, and playing audio.
  260.         /// </summary>
  261.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  262.         protected override void Update(GameTime gameTime)
  263.         {
  264.             if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) && _moveTimer > new TimeSpan(0,0,0,0,80))
  265.             {
  266.                 _block.Move(Direction.Left, _blockList);
  267.                 _moveTimer = new TimeSpan(0);
  268.             }
  269.             if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) && _moveTimer > new TimeSpan(0,0,0,0,80))
  270.             {
  271.                 _block.Move(Direction.Right, _blockList);
  272.                 _moveTimer = new TimeSpan(0);
  273.             }
  274.             if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down))
  275.             {
  276.                 _block.Drop(_blockList);
  277.             }
  278.             if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up) && _moveTimer > new TimeSpan(0, 0, 0, 0, 150))
  279.             {
  280.                 _block.Turn(_blockList, _blockArray);
  281.                 _moveTimer = new TimeSpan(0);
  282.             }
  283.    
  284.             _elapsedTime += gameTime.ElapsedGameTime;
  285.             _moveTimer += gameTime.ElapsedGameTime;
  286.             if (_elapsedTime.Milliseconds > 500)
  287.             {
  288.                 _block.Drop(_blockList);
  289.                 _elapsedTime = new TimeSpan(0);
  290.             }
  291.  
  292.             foreach (Block block in _blockList)
  293.                 if (block.IsEmpty())
  294.                     _blockList.Remove(block);
  295.  
  296.             // TODO: Add your update logic here);
  297.  
  298.             base.Update(gameTime);
  299.         }
  300.  
  301.         /// <summary>
  302.         /// This is called when the game should draw itself.
  303.         /// </summary>
  304.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  305.         protected override void Draw(GameTime gameTime)
  306.         {
  307.             GraphicsDevice.Clear(Color.CornflowerBlue);
  308.             _spriteBatch.Begin();
  309.  
  310.             // TODO: Add your drawing code here
  311.             _block.Draw(_spriteBatch);
  312.             foreach (Block shape in _blockList)
  313.                 shape.Draw(_spriteBatch);
  314.             _spriteBatch.End();
  315.  
  316.             base.Draw(gameTime);
  317.         }
  318.  
  319.         void DropBlock(object sender = null, EventArgs e = null)
  320.         {
  321.            
  322.             _block = new Block(new Vector2(_HorizOffset, _VertOffset), _texture, _blockArray);
  323.             _block.Froze += CheckRows;
  324.         }
  325.  
  326.         void CheckRows(object sender = null, EventArgs e = null)
  327.         {
  328.             if (_block != null)
  329.                 _blockList.Add(_block);
  330.             for(int i = _VertOffset; i < _Height + _VertOffset; ++i)
  331.             {
  332.                 bool rowFull = true;
  333.                 for ( int j = _HorizOffset; j < _Width + _HorizOffset; ++j)
  334.                 {
  335.                     bool contains = false;
  336.                     foreach (Block block in _blockList)
  337.                     {
  338.                         if (block.Contains(new Vector2(j,i)))
  339.                         {
  340.                             contains = true;
  341.                         }
  342.                     }
  343.                     if (!contains)
  344.                         rowFull = false;
  345.                 }
  346.                 if (rowFull)
  347.                     DeleteRow(i);
  348.             }
  349.             DropBlock();
  350.         }
  351.  
  352.         void DeleteRow(int row)
  353.         {
  354.             for (int i = _HorizOffset; i < _HorizOffset + _Width; ++i)
  355.             {
  356.                 foreach (Block block in _blockList.Where(block => block.Contains(new Vector2(i, row))))
  357.                 {
  358.                     block.Delete(new Vector2(i, row));
  359.                    
  360.                 }
  361.                 MoveAllAboveDown(row);
  362.             }
  363.         }
  364.  
  365.         void MoveAllAboveDown(int row)
  366.         {
  367.             List<Block> newBlockList = new List<Block>();
  368.             for (int rowCheck = row; rowCheck > _VertOffset; --rowCheck)
  369.             {
  370.                 for (int i = _HorizOffset; i < _HorizOffset + _Width; ++i)
  371.                 {
  372.                     //If the spot above this is filled
  373.                     if (_blockList.Where(block => block.Contains(new Vector2(i, rowCheck - 1))).ToList().Count > 0)
  374.                     {
  375.                         //insert block here
  376.                         newBlockList.Add(new Block(new Vector2(i, rowCheck), new[,] { { true } }, Color.Black, _texture));
  377.                     }
  378.                     else
  379.                     {
  380.                         var list = _blockList.Where(tempBlock => tempBlock.Contains(new Vector2(i, rowCheck - 1))).ToList();
  381.                         if (list.Count > 0)
  382.                             list.ElementAt(0).Delete(new Vector2(i, rowCheck - 1));
  383.                     }
  384.                 }
  385.             }
  386.             for (int rowToDelete = row; rowToDelete > _VertOffset; --rowToDelete)
  387.             {
  388.                 DeleteRow(row);
  389.             }
  390.             foreach (Block block in newBlockList)
  391.             {
  392.                 _blockList.Add(block);
  393.             }
  394.         }
  395.     }
  396. }
  397.  
  398.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement