Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace Tetris
- {
- public enum Direction
- {
- None,
- Up,
- Down,
- Right,
- Left
- }
- internal class Block
- {
- private readonly Texture2D _texture;
- public EventHandler Froze;
- private int _blockNumber;
- private Vector2 _location;
- private readonly Color _color;
- private bool[,] _shape;
- private bool _stopped;
- public Block(Vector2 location, Texture2D texture, List<bool[,]> blockArray)
- {
- _stopped = false;
- _location = location;
- _color = Color.RoyalBlue;
- _blockNumber = new Random().Next(0, blockArray.Count);
- _blockNumber = 0;
- _shape = blockArray[_blockNumber];
- _texture = texture;
- }
- public Block(Vector2 location, bool[,] shape, Color color, Texture2D texture)
- {
- _stopped = false;
- _location = location;
- _color = color;
- _shape = shape;
- _texture = texture;
- }
- public virtual void Turn(IEnumerable<Block> blocks, List<bool[,]> blockArray)
- {
- int nextBlock = _blockNumber;
- if ((_blockNumber + 1)%4 != 0)
- ++nextBlock;
- else
- nextBlock -= 3;
- _shape = blockArray[nextBlock];
- if (NoCollisions(Direction.None, blocks))
- _blockNumber = nextBlock;
- _shape = blockArray[_blockNumber];
- }
- public void Drop(List<Block> blocks)
- {
- if (NoCollisions(Direction.Down, blocks))
- _location.Y += 1;
- else if (_stopped == false)
- _stopped = true;
- else
- {
- Froze(this, null);
- }
- }
- private bool NoCollisions(Direction direction, IEnumerable<Block> blocks)
- {
- int changeInX;
- if (direction == Direction.Left)
- changeInX = -1;
- else if (direction == Direction.Right)
- changeInX = 1;
- else
- changeInX = 0;
- int changeInY = direction == Direction.Down ? 1 : 0;
- bool allowDrop = true;
- for (int i = 0; i < _shape.GetLength(0); ++i)
- {
- for (int j = 0; j < _shape.GetLength(1); ++j)
- {
- if (_shape[i, j])
- {
- foreach (Block block in blocks)
- if (block.Contains(new Vector2(j + _location.X + changeInX, i + _location.Y + changeInY)))
- {
- allowDrop = false;
- }
- }
- }
- }
- return allowDrop;
- }
- public void Move(Direction direction, IEnumerable<Block> blockList)
- {
- switch (direction)
- {
- case Direction.None:
- break;
- case Direction.Left:
- if (NoCollisions(Direction.Left, blockList))
- _location.X -= 1;
- break;
- case Direction.Right:
- if (NoCollisions(Direction.Right, blockList))
- _location.X += 1;
- break;
- }
- _stopped = false;
- }
- public void Draw(SpriteBatch batch)
- {
- for (int i = 0; i < _shape.GetLength(0); ++i)
- {
- for (int j = 0; j < _shape.GetLength(1); ++j)
- {
- if (_shape[i, j])
- batch.Draw(_texture,
- new Rectangle((int) (_location.X + j)*10, (int) (_location.Y + i)*10, 10, 10),
- _color);
- }
- }
- }
- public bool Contains(Vector2 point)
- {
- if (point.Y < _location.Y)
- return false;
- if (point.X < _location.X)
- return false;
- if (point.Y >= _location.Y + _shape.GetLength(0))
- return false;
- if (point.X >= _location.X + _shape.GetLength(1))
- return false;
- return _shape[(int) (point.Y - _location.Y), (int) (point.X - _location.X)];
- }
- public bool IsEmpty()
- {
- bool empty = true;
- foreach (bool location in _shape)
- if (location)
- empty = false;
- return empty;
- }
- /// <summary>
- /// Deletes a square in the block
- /// </summary>
- /// <param name="point">The location of the point to delete</param>
- /// <returns>True if a point was deleted</returns>
- public bool Delete(Vector2 point)
- {
- if (point.Y < _location.Y)
- return false;
- if (point.X < _location.X)
- return false;
- if (point.Y >= _location.Y + _shape.GetLength(0))
- return false;
- if (point.X >= _location.X + _shape.GetLength(1))
- return false;
- _shape[(int) (point.Y - _location.Y), (int) (point.X - _location.X)] = false;
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement