Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- namespace Tetris
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- private const int _HorizOffset = 10;
- private const int _VertOffset = 2;
- private const int _Height = 40;
- private const int _Width = 10;
- readonly GraphicsDeviceManager _graphics;
- SpriteBatch _spriteBatch;
- Block _block;
- private TimeSpan _elapsedTime;
- private TimeSpan _moveTimer;
- private Texture2D _texture;
- private List<Block> _blockList;
- private List<bool[,]> _blockArray;
- public Game1()
- {
- _graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- /// <summary>
- /// Allows the game to perform any initialization it needs to before starting to run.
- /// This is where it can query for any required services and load any non-graphic
- /// related content. Calling base.Initialize will enumerate through any components
- /// and initialize them as well.
- /// </summary>
- protected override void Initialize()
- {
- // TODO: Add your initialization logic here
- _elapsedTime = new TimeSpan(0);
- _blockList = new List<Block>();
- _blockArray = new List<bool[,]>
- {
- //Straight Block
- new[,]
- {
- {true, true, true, true}
- },
- new[,]
- {
- {true},
- {true},
- {true},
- {true}
- },
- new[,]
- {
- {true, true, true, true}
- },
- new[,]
- {
- {true},
- {true},
- {true},
- {true}
- },
- //Square Block
- new[,]
- {
- {true, true},
- {true, true},
- },
- new[,]
- {
- {true, true},
- {true, true},
- },
- new[,]
- {
- {true, true},
- {true, true},
- },
- new[,]
- {
- {true, true},
- {true, true},
- },
- //S Block
- new[,]
- {
- {false, true, true},
- {true, true, false}
- },
- new[,]
- {
- {true, false},
- {true, true},
- {false, true}
- },
- new[,]
- {
- {false, true, true},
- {true, true, false}
- },
- new[,]
- {
- {true, false},
- {true, true},
- {false, true}
- },
- //T Block
- new[,]
- {
- {false, true, false},
- {true, true, true}
- },
- new[,]
- {
- {true, false},
- {true, true},
- {true, false}
- },
- new[,]
- {
- {true, true, true},
- {false, true, false}
- },
- new[,]
- {
- {false, true},
- {true, true},
- {false, true}
- },
- //Z Block
- new[,]
- {
- {true, true, false},
- {false, true, true}
- },
- new[,]
- {
- {false, true},
- {true, true},
- {true, false}
- },
- new[,]
- {
- {true, true, false},
- {false, true, true}
- },
- new[,]
- {
- {false, true},
- {true, true},
- {true, false}
- },
- //Backwards L Block
- new[,]
- {
- {true, false, false},
- {true, true, true}
- },
- new[,]
- {
- {true, true},
- {true, false},
- {true, false}
- },
- new[,]
- {
- {true, true, true},
- {false, false, true}
- },
- new[,]
- {
- {false, true},
- {false, true},
- {true, true}
- },
- //L Block
- new[,]
- {
- {false, false, true},
- {true, true, true}
- },
- new[,]
- {
- {true, false},
- {true, false},
- {true, true}
- },
- new[,]
- {
- {true, true, true},
- {true, false, false}
- },
- new[,]
- {
- {true, true},
- {false, true},
- {false, true}
- }
- };
- base.Initialize();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- _texture = new Texture2D(_graphics.GraphicsDevice, 1, 1);
- _texture.SetData(new[] { Color.White });
- _spriteBatch = new SpriteBatch(GraphicsDevice);
- var floor = new bool[1,_Width];
- for (int i = 0; i < _Width; ++i)
- floor[0,i] = true;
- _blockList.Add(new Block(new Vector2(_HorizOffset,_Height + _VertOffset), floor, Color.Red, _texture ));
- _blockList.Add(new Block(new Vector2(_HorizOffset, _VertOffset - 1), floor, Color.Red, _texture));
- var side = new bool[_Height, 1];
- for (int i = 0; i < _Height; ++i)
- side[i, 0] = true;
- _blockList.Add(new Block(new Vector2(_HorizOffset - 1, _VertOffset), side, Color.Red, _texture));
- _blockList.Add(new Block(new Vector2(_HorizOffset + _Width, _VertOffset), side, Color.Red, _texture));
- DropBlock();
- // TODO: use this.Content to load your game content here
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input, and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Update(GameTime gameTime)
- {
- if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) && _moveTimer > new TimeSpan(0,0,0,0,80))
- {
- _block.Move(Direction.Left, _blockList);
- _moveTimer = new TimeSpan(0);
- }
- if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) && _moveTimer > new TimeSpan(0,0,0,0,80))
- {
- _block.Move(Direction.Right, _blockList);
- _moveTimer = new TimeSpan(0);
- }
- if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down))
- {
- _block.Drop(_blockList);
- }
- if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up) && _moveTimer > new TimeSpan(0, 0, 0, 0, 150))
- {
- _block.Turn(_blockList, _blockArray);
- _moveTimer = new TimeSpan(0);
- }
- _elapsedTime += gameTime.ElapsedGameTime;
- _moveTimer += gameTime.ElapsedGameTime;
- if (_elapsedTime.Milliseconds > 500)
- {
- _block.Drop(_blockList);
- _elapsedTime = new TimeSpan(0);
- }
- foreach (Block block in _blockList)
- if (block.IsEmpty())
- _blockList.Remove(block);
- // TODO: Add your update logic here);
- base.Update(gameTime);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- _spriteBatch.Begin();
- // TODO: Add your drawing code here
- _block.Draw(_spriteBatch);
- foreach (Block shape in _blockList)
- shape.Draw(_spriteBatch);
- _spriteBatch.End();
- base.Draw(gameTime);
- }
- void DropBlock(object sender = null, EventArgs e = null)
- {
- _block = new Block(new Vector2(_HorizOffset, _VertOffset), _texture, _blockArray);
- _block.Froze += CheckRows;
- }
- void CheckRows(object sender = null, EventArgs e = null)
- {
- if (_block != null)
- _blockList.Add(_block);
- for(int i = _VertOffset; i < _Height + _VertOffset; ++i)
- {
- bool rowFull = true;
- for ( int j = _HorizOffset; j < _Width + _HorizOffset; ++j)
- {
- bool contains = false;
- foreach (Block block in _blockList)
- {
- if (block.Contains(new Vector2(j,i)))
- {
- contains = true;
- }
- }
- if (!contains)
- rowFull = false;
- }
- if (rowFull)
- DeleteRow(i);
- }
- DropBlock();
- }
- void DeleteRow(int row)
- {
- for (int i = _HorizOffset; i < _HorizOffset + _Width; ++i)
- {
- foreach (Block block in _blockList.Where(block => block.Contains(new Vector2(i, row))))
- {
- block.Delete(new Vector2(i, row));
- }
- MoveAllAboveDown(row);
- }
- }
- void MoveAllAboveDown(int row)
- {
- List<Block> newBlockList = new List<Block>();
- for (int rowCheck = row; rowCheck > _VertOffset; --rowCheck)
- {
- for (int i = _HorizOffset; i < _HorizOffset + _Width; ++i)
- {
- //If the spot above this is filled
- if (_blockList.Where(block => block.Contains(new Vector2(i, rowCheck - 1))).ToList().Count > 0)
- {
- //insert block here
- newBlockList.Add(new Block(new Vector2(i, rowCheck), new[,] { { true } }, Color.Black, _texture));
- }
- else
- {
- var list = _blockList.Where(tempBlock => tempBlock.Contains(new Vector2(i, rowCheck - 1))).ToList();
- if (list.Count > 0)
- list.ElementAt(0).Delete(new Vector2(i, rowCheck - 1));
- }
- }
- }
- for (int rowToDelete = row; rowToDelete > _VertOffset; --rowToDelete)
- {
- DeleteRow(row);
- }
- foreach (Block block in newBlockList)
- {
- _blockList.Add(block);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement