⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 old_block.cs

📁 Beginning C# Game Programming 的源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace PocketNettrix {
	public class Block {

		public enum BlockTypes {
			Undefined = 0,
			Square = 1,
			Line = 2,
			J = 3,
			L = 4,
			T = 5,
			Z = 6,
			S = 7
		};
		private BlockTypes blockType;

		public BlockTypes BlockType {
			get { return blockType; }
			set { blockType = value; }
		}

		public enum RotationDirections {
			North = 1,
			East = 2,
			South = 3,
			West = 4
		};

		private RotationDirections statusRotation = RotationDirections.North;		public RotationDirections StatusRotation {			get { return statusRotation; }			set { statusRotation = value; }		}

		// The colors of each block type 
		private Color[] backColors = {Color.Empty, Color.Red, Color.Blue, Color.Red, Color.Yellow, Color.Green, Color.White, Color.Black};
		private Color[] foreColors = {Color.Empty, Color.Purple, Color.LightBlue, Color.Yellow, Color.Red, Color.LightGreen, Color.Black, Color.White};


		// The 4 squares that compose the block
		private Square square1;
		private Square square2;
		private Square square3;
		private Square square4;

		public Square Square1 {
			get { return square1; }
			set { square1 = value; }
		}

		public Square Square2 {
			get { return square2; }
			set { square2 = value; }
		}

		public Square Square3 {
			get { return square3; }
			set { square3 = value; }
		}

		public Square Square4 {
			get { return square4; }
			set { square4 = value; }
		}

		private const int squareSize = GameField.SquareSize;
		private static Random random = new Random();

		public int Top() {
			return Math.Min(square1.Location.Y, Math.Min(square2.Location.Y, 
				Math.Min(square3.Location.Y, square4.Location.Y)));
		}

		public Block(Point location,  BlockTypes newBlockType) {
			// Create the new block
			if (newBlockType==BlockTypes.Undefined) {
				//BlockType = (BlockTypes)(random.Next(7)) + 1;
				BlockType = BlockTypes.Line; //force line block for testing
			}
			else {
				BlockType = newBlockType;
			}
			// Create each of the squares of the block
			// Set the square colors, based on the block type
			square1 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);
			square2 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);
			square3 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);
			square4 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);

			// Set the squares positions based on the block type
			switch(BlockType) {
				case BlockTypes.Square:
					square1.Location = new Point(location.X, location.Y);
					square2.Location = new Point(location.X+squareSize, location.Y);
					square3.Location = new Point(location.X, location.Y+squareSize);
					square4.Location = new Point(location.X+squareSize, location.Y+squareSize);
					break;
				case BlockTypes.Line:
					square1.Location = new Point(location.X, location.Y);
					square2.Location = new Point(location.X, location.Y+squareSize);
					square3.Location = new Point(location.X, location.Y+2*squareSize);
					square4.Location = new Point(location.X, location.Y+3*squareSize);
					break;
				case BlockTypes.J:
					square1.Location = new Point(location.X+squareSize, location.Y);
					square2.Location = new Point(location.X+squareSize, location.Y+squareSize);
					square3.Location = new Point(location.X+squareSize, location.Y+2*squareSize);
					square4.Location = new Point(location.X, location.Y+2*squareSize);
					break;
				case BlockTypes.L:
					square1.Location = new Point(location.X, location.Y);
					square2.Location = new Point(location.X, location.Y+squareSize);
					square3.Location = new Point(location.X, location.Y+2*squareSize);
					square4.Location = new Point(location.X+squareSize, location.Y+2*squareSize);
					break;
				case BlockTypes.T:
					square1.Location = new Point(location.X, location.Y);
					square2.Location = new Point(location.X+squareSize, location.Y);
					square3.Location = new Point(location.X+2*squareSize, location.Y);
					square4.Location = new Point(location.X+squareSize, location.Y+squareSize);
					break;
				case BlockTypes.Z:
					square1.Location = new Point(location.X, location.Y);
					square2.Location = new Point(location.X+squareSize, location.Y);
					square3.Location = new Point(location.X+squareSize, location.Y+squareSize);
					square4.Location = new Point(location.X+2*squareSize, location.Y+squareSize);
					break;
				case BlockTypes.S:
					square1.Location = new Point(location.X, location.Y+squareSize);
					square2.Location = new Point(location.X+squareSize, location.Y+squareSize);
					square3.Location = new Point(location.X+squareSize, location.Y);
					square4.Location = new Point(location.X+2*squareSize, location.Y);
					break;
			}
		}

		public void Rotate() {
			// Store the current block position
			Point OldPosition1 = square1.Location;
			Point OldPosition2 = square2.Location;
			Point OldPosition3 = square3.Location;
			Point OldPosition4 = square4.Location;
			RotationDirections OldStatusRotation = StatusRotation;
			Hide(GameField.GraphBackground);

			// Rotate the blocks
			switch(BlockType) {
				case BlockTypes.Square:
					// Do nothing. Squares don't rotate.
					break;
				case BlockTypes.Line:
					// Rotate all squares around square 2
				switch(StatusRotation) {
					case RotationDirections.North:
						StatusRotation = RotationDirections.East;
						square1.Location = new Point(square2.Location.X-squareSize, square2.Location.Y);
						square3.Location = new Point(square2.Location.X+squareSize, square2.Location.Y);
						square4.Location = new Point(square2.Location.X+2*squareSize, square2.Location.Y);
						break;
					case RotationDirections.East:
						StatusRotation = RotationDirections.North;
						square1.Location = new Point(square2.Location.X, square2.Location.Y-squareSize);
						square3.Location = new Point(square2.Location.X, square2.Location.Y+squareSize);
						square4.Location = new Point(square2.Location.X, square2.Location.Y+2*squareSize);
						break;
				}
					break;
				case BlockTypes.J:
					// Rotate all squares around square 3  
				switch(StatusRotation) {
					case RotationDirections.North:
						StatusRotation = RotationDirections.East;
						square1.Location = new Point(square3.Location.X, square3.Location.Y-squareSize);
						square2.Location = new Point(square3.Location.X+squareSize, square3.Location.Y);
						square4.Location = new Point(square3.Location.X+2*squareSize, square3.Location.Y);
						break;
					case RotationDirections.East:
						StatusRotation = RotationDirections.South;
						square1.Location = new Point(square3.Location.X+squareSize, square3.Location.Y);
						square2.Location = new Point(square3.Location.X, square3.Location.Y+squareSize);
						square4.Location = new Point(square3.Location.X, square3.Location.Y+2*squareSize);
						break;
					case RotationDirections.South:
						StatusRotation = RotationDirections.West;
						square1.Location = new Point(square3.Location.X, square3.Location.Y+squareSize);
						square2.Location = new Point(square3.Location.X-squareSize, square3.Location.Y);
						square4.Location = new Point(square3.Location.X-2*squareSize, square3.Location.Y);
						break;
					case RotationDirections.West:
						StatusRotation = RotationDirections.North;
						square1.Location = new Point(square3.Location.X-squareSize, square3.Location.Y);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -