public GamePiece createGhostPiece(List <GameBlock> grid) { List <GameBlock> ghostBlocks = new List <GameBlock>(); foreach (GameBlock block in _blocks) { /*ghostBlocks.Add(new GameBlock(new Rectangle(block.bounds.X, block.bounds.Y, block.bounds.Width, block.bounds.Height), * new Point(block.location.X, block.location.Y)));*/ GameBlock ghostSubBlock = new GameBlock(new Rectangle(block.bounds.X, block.bounds.Y, block.bounds.Width, block.bounds.Height), new Point(block.location.X, block.location.Y)); ghostSubBlock.Color = block.Color; ghostBlocks.Add(ghostSubBlock); } GamePiece ghost = new GamePiece(ghostBlocks.ToArray(), ghostBlocks[0]); while (ghost.canMoveDown(grid)) { ghost.moveDown(); } return(ghost); }
private Point rotateBlockAroundPivot(GameBlock block) { var pivotLoc = _pivot.location; var blockLoc = block.location; var x = blockLoc.X - pivotLoc.X; var y = blockLoc.Y - pivotLoc.Y; blockLoc.X = -y + pivotLoc.X; blockLoc.Y = x + pivotLoc.Y; return(blockLoc); }
/// <summary> /// 在top, left位置插入一个block /// </summary> public void Insert(GameBlock block, int top, int left) { lock (block) { for (int i = 0; i < block.size; i++) { for (int j = 0; j < block.size; j++) { if (block.block[i, j] == 1) { board[top + i, left + j] = 1; } } } block.Remove(mainWindow.gameCanvas); } }
private void Timer_Elapsed(object sender, ElapsedEventArgs e) { mainWindow.Dispatcher.Invoke(new Action(() => { if (MoveDown()) { return; } else { //下落完成 Insert(nowBlock, top, left); nowBlock = nextBlock; top = 0; left = column / 2 - nowBlock.size / 2; nextBlock.Remove(mainWindow.nextBlockCanvas); nextBlock = GameBlock.NewBlock(); nextBlock.Draw(mainWindow.nextBlockCanvas, 6, 6, fillBrush1, edgeBrush1, 1, 1); UpdateBoard(); if (!CanInsert(nowBlock, top, left)) { timer.Stop(); MessageBox.Show("游戏结束"); nowBlock = null; File.WriteAllText("score", highScore.ToString()); Clear(); } else { nowBlock.Draw(mainWindow.gameCanvas, row, column, fillBrush1, edgeBrush1, top, left); //更新interval timer.Stop(); timer.Interval = Math.Max(minInterval, initInterval - score * gapInterval); timer.Start(); } } })); }
/// <summary> /// 开始游戏 /// </summary> public void Start() { timer.Stop(); if (nowBlock != null) { nowBlock.Remove(mainWindow.gameCanvas); } if (nextBlock != null) { nextBlock.Remove(mainWindow.nextBlockCanvas); } random = new Random(); Clear(); nowBlock = GameBlock.NewBlock(); nextBlock = GameBlock.NewBlock(); top = 0; left = column / 2 - nowBlock.size / 2; nowBlock.Draw(mainWindow.gameCanvas, row, column, fillBrush1, edgeBrush1, top, left); nextBlock.Draw(mainWindow.nextBlockCanvas, 6, 6, fillBrush1, edgeBrush1, 1, 1); score = 0; if (!File.Exists("score")) { highScore = 0; } else { Int32.TryParse(File.ReadAllText("score"), out highScore); } mainWindow.highScore.Text = "最高分:\n" + highScore.ToString(); mainWindow.score.Text = "得分:\n" + score.ToString(); timer.Interval = initInterval; timer.Start(); }
/// <summary> /// 判断是否能在top, left位置插入一个block /// </summary> public bool CanInsert(GameBlock block, int top, int left) { for (int i = 0; i < block.size; i++) { for (int j = 0; j < block.size; j++) { if (block.block[i, j] == 0) { continue; } if (top + i < 0 || top + i >= row || left + j < 0 || left + j >= column) { return(false); } if (board[top + i, left + j] == 1) { return(false); } } } return(true); }
public GamePiece(GameBlock[] blocks, GameBlock pivot) { _blocks = new List <GameBlock>(blocks); _pivot = pivot; }
public GamePiece createGamePiece(GamePieces piece) { int centerBlock = Constants.GRID_WIDITH / 2; GameBlock b1; GameBlock b2; GameBlock b3; GameBlock b4; switch (piece) { case GamePieces.L_RIGHT: b1 = new GameBlock(location: new Point(centerBlock, -3)); b1.Color = Constants.L_RIGHT_PIECE_COLOR; b2 = new GameBlock(location: new Point(centerBlock, -2)); b2.Color = Constants.L_RIGHT_PIECE_COLOR; b3 = new GameBlock(location: new Point(centerBlock, -1)); b3.Color = Constants.L_RIGHT_PIECE_COLOR; b4 = new GameBlock(location: new Point(centerBlock + 1, -1)); b4.Color = Constants.L_RIGHT_PIECE_COLOR; return(new GamePiece(new GameBlock[] { b1, b2, b3, b4 }, b2)); case GamePieces.L_LEFT: b1 = new GameBlock(location: new Point(centerBlock, -3)); b1.Color = Constants.L_LEFT_PIECE_COLOR; b2 = new GameBlock(location: new Point(centerBlock, -2)); b2.Color = Constants.L_LEFT_PIECE_COLOR; b3 = new GameBlock(location: new Point(centerBlock, -1)); b3.Color = Constants.L_LEFT_PIECE_COLOR; b4 = new GameBlock(location: new Point(centerBlock + 1, -3)); b4.Color = Constants.L_LEFT_PIECE_COLOR; return(new GamePiece(new GameBlock[] { b1, b2, b3, b4 }, b2)); case GamePieces.BLOCK: b1 = new GameBlock(location: new Point(centerBlock, -1)); b1.Color = Constants.BLOCK_PIECE_COLOR; b2 = new GameBlock(location: new Point(centerBlock, -2)); b2.Color = Constants.BLOCK_PIECE_COLOR; b3 = new GameBlock(location: new Point(centerBlock + 1, -2)); b3.Color = Constants.BLOCK_PIECE_COLOR; b4 = new GameBlock(location: new Point(centerBlock + 1, -1)); b4.Color = Constants.BLOCK_PIECE_COLOR; return(new GamePiece(new GameBlock[] { b1, b2, b3, b4 }, null)); case GamePieces.T: b1 = new GameBlock(location: new Point(centerBlock, -1)); b1.Color = Constants.T_PIECE_COLOR; b2 = new GameBlock(location: new Point(centerBlock - 1, -1)); b2.Color = Constants.T_PIECE_COLOR; b3 = new GameBlock(location: new Point(centerBlock + 1, -1)); b3.Color = Constants.T_PIECE_COLOR; b4 = new GameBlock(location: new Point(centerBlock, -2)); b4.Color = Constants.T_PIECE_COLOR; return(new GamePiece(new GameBlock[] { b1, b2, b3, b4 }, b1)); case GamePieces.LINE: b1 = new GameBlock(location: new Point(centerBlock, -4)); b1.Color = Constants.LINE_GAME_PIECE_COLOR; b2 = new GameBlock(location: new Point(centerBlock, -3)); b2.Color = Constants.LINE_GAME_PIECE_COLOR; b3 = new GameBlock(location: new Point(centerBlock, + -2)); b3.Color = Constants.LINE_GAME_PIECE_COLOR; b4 = new GameBlock(location: new Point(centerBlock, + -1)); b4.Color = Constants.LINE_GAME_PIECE_COLOR; return(new GamePiece(new GameBlock[] { b1, b2, b3, b4 }, b2)); case GamePieces.CURVE_LEFT: b1 = new GameBlock(location: new Point(centerBlock, -3)); b1.Color = Constants.CURVE_LEFT_PIECE_COLOR; b2 = new GameBlock(location: new Point(centerBlock, -2)); b2.Color = Constants.CURVE_LEFT_PIECE_COLOR; b3 = new GameBlock(location: new Point(centerBlock - 1, -2)); b3.Color = Constants.CURVE_LEFT_PIECE_COLOR; b4 = new GameBlock(location: new Point(centerBlock - 1, -1)); b4.Color = Constants.CURVE_LEFT_PIECE_COLOR; return(new GamePiece(new GameBlock[] { b1, b2, b3, b4 }, b2)); case GamePieces.CURVE_RIGHT: b1 = new GameBlock(location: new Point(centerBlock, -3)); b1.Color = Constants.CURVE_RIGHT_PEICE_COLOR; b2 = new GameBlock(location: new Point(centerBlock, -2)); b2.Color = Constants.CURVE_RIGHT_PEICE_COLOR; b3 = new GameBlock(location: new Point(centerBlock + 1, -2)); b3.Color = Constants.CURVE_RIGHT_PEICE_COLOR; b4 = new GameBlock(location: new Point(centerBlock + 1, -1)); b4.Color = Constants.CURVE_RIGHT_PEICE_COLOR; return(new GamePiece(new GameBlock[] { b1, b2, b3, b4 }, b2)); } return(null); }