これまで回転するときは右周り(時計回り)しかできませんでしたが、今回は左周り(反時計回り)にも対応させます。
これまでスペースキーで回転させましたが、左右の両方の回転をするのであればキーは近いほうが便利なはずです。そこでこれまで同様、スペースキーで右回転させますが、Zキーで左回転、Xキーで右回転できるようにします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public partial class Form1 : Form { private void Form1_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Left) MoveLeftTetoro(); else if(e.KeyCode == Keys.Right) MoveRightTetoro(); else if(e.KeyCode == Keys.Space) RotateTetoro(); else if(e.KeyCode == Keys.Z) RotateTetoro2(); else if(e.KeyCode == Keys.X) RotateTetoro(); else if(e.KeyCode == Keys.Down) Drop(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public partial class Form1 : Form { // 左回転させる void RotateTetoro2() { // Oミノは回転処理の必要なし if(tetoroTypes == TetoroTypes.O) return; // Iミノは特殊 if(tetoroTypes == TetoroTypes.I) { RotateLeftTetoroI(); return; } // それ以外 RotateLeftTetoro(); } } |
以下は実際に左回転させるためのメソッドです。多少、値が入れ替わっていますが、やっていることは右周りと同じです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
public partial class Form1 : Form { void RotateLeftTetoro() { int[] xs; int[] ys; if(tetoroAngle == TetoroAngle.Angle0) { xs = new int[] { 0, 1, 1, 0, 1, }; ys = new int[] { 0, 0, -1, 2, 2, }; } else if(tetoroAngle == TetoroAngle.Angle90) { xs = new int[] { 0, 1, 1, 0, 1, }; ys = new int[] { 0, 0, 1, -2, -2, }; } else if(tetoroAngle == TetoroAngle.Angle180) { xs = new int[] { 0, -1, -1, 0, -1, }; ys = new int[] { 0, 0, -1, 2, 2, }; } else // tetoroAngle == TetoroAngle.Angle270 { xs = new int[] { 0, 1, 1, 0, 1, }; ys = new int[] { 0, 0, -1, 2, 2, }; } List<BlockPosition> newBlockPositions = GetLeftRotatedPositions(MovingTetoros); List<Block> newTetoro = new List<Block>(); Text = newBlockPositions.Count.ToString(); for(int i = 0; i < 5; i++) { List<BlockPosition> newBlockPositions1 = new List<BlockPosition>(); foreach(BlockPosition pos in newBlockPositions) newBlockPositions1.Add(new BlockPosition(pos.Colum + xs[i], pos.Row + ys[i])); if(CanRotate(newBlockPositions1)) { newTetoro = GetBlocksFromBlockPositions(newBlockPositions1); break; } } if(newTetoro.Count == 0) return; if(tetoroAngle == TetoroAngle.Angle0) tetoroAngle = TetoroAngle.Angle270; else if(tetoroAngle == TetoroAngle.Angle90) tetoroAngle = TetoroAngle.Angle0; else if(tetoroAngle == TetoroAngle.Angle180) tetoroAngle = TetoroAngle.Angle90; else if(tetoroAngle == TetoroAngle.Angle270) tetoroAngle = TetoroAngle.Angle180; ClearOldTetoro(); foreach(Block block in newTetoro) { block.BackColor = GetTetoroColor(tetoroTypes); } MovingTetoros = newTetoro; } } |
左に回転したときにブロックが移動する位置を取得するためのメソッドです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public partial class Form1 : Form { List<BlockPosition> GetLeftRotatedPositions(List<Block> blocks) { int centerX = blocks[0].Colum; int centerY = blocks[0].Row; List<BlockPosition> blockPositions = new List<BlockPosition>(); foreach(Block block in blocks) { if(blocks[0] == block) blockPositions.Add(new BlockPosition(block.Colum, block.Row)); else if(block.Colum < centerX && block.Row < centerY) blockPositions.Add(new BlockPosition(block.Colum, block.Row + 2)); else if(block.Colum == centerX && block.Row < centerY) blockPositions.Add(new BlockPosition(block.Colum - 1, block.Row + 1)); else if(block.Colum > centerX && block.Row < centerY) blockPositions.Add(new BlockPosition(block.Colum-2, block.Row)); else if(block.Colum < centerX && block.Row == centerY) blockPositions.Add(new BlockPosition(block.Colum + 1, block.Row + 1)); else if(block.Colum == centerX && block.Row == centerY) blockPositions.Add(new BlockPosition(block.Colum, block.Row)); else if(block.Colum > centerX && block.Row == centerY) blockPositions.Add(new BlockPosition(block.Colum - 1, block.Row - 1)); else if(block.Colum < centerX && block.Row > centerY) blockPositions.Add(new BlockPosition(block.Colum+2, block.Row)); else if(block.Colum == centerX && block.Row > centerY) blockPositions.Add(new BlockPosition(block.Colum + 1, block.Row - 1)); else if(block.Colum > centerX && block.Row > centerY) blockPositions.Add(new BlockPosition(block.Colum, block.Row-2)); } return blockPositions; } } |