private void Restart() { level = 1; time = 1000; score = 0; upScore = 500; x = 3; y = 0; game = true; Clear(Field); Clear(Next); Tetris.NullArray(ref current); Tetris.NullArray(ref next); Tetris.NullArray(ref coordinates); Tetris.NullArray(ref array); Tetris.NullArray(ref moving); labelNext.Text = Convert.ToString(upScore); labelScore.Text = "0"; timer1.Interval = time; timer1.Enabled = true; figure = (Tetris.Figure)figures.GetValue(r.Next(figures.Length)); Tetris.SetNewFigure(ref current, figure); Tetris.Place(current, ref moving, array, ref x, ref y, ref coordinates); Update(true); nextFigure = (Tetris.Figure)figures.GetValue(r.Next(figures.Length)); Tetris.SetNewFigure(ref next, nextFigure); Draw(next, nextFigure); }
private void NewGame() { timer1.Interval = time; CreatePanel(10, 20); CreatePanel(4, 4); labelNext.Text = Convert.ToString(upScore); figure = (Tetris.Figure)figures.GetValue(r.Next(figures.Length)); Tetris.SetNewFigure(ref current, figure); Tetris.Place(current, ref moving, array, ref x, ref y, ref coordinates); Update(true); nextFigure = (Tetris.Figure)figures.GetValue(r.Next(figures.Length)); Tetris.SetNewFigure(ref next, nextFigure); Draw(next, nextFigure); }
private void FormTetris_KeyDown(object sender, KeyEventArgs e) { if (game == true) { switch (e.KeyCode) { case Keys.Left: case Keys.A: if (coordinates[0, 0] != 0 && coordinates[0, 1] != 0 && coordinates[0, 2] != 0 && coordinates[0, 3] != 0 && array[coordinates[0, 0] - 1, coordinates[1, 0]] == 0 && array[coordinates[0, 1] - 1, coordinates[1, 1]] == 0 && array[coordinates[0, 2] - 1, coordinates[1, 2]] == 0 && array[coordinates[0, 3] - 1, coordinates[1, 3]] == 0) { MoveSound.Play(); ChangePosition(Tetris.Direction.Left); } break; case Keys.Right: case Keys.D: if (coordinates[0, 0] != 9 && coordinates[0, 1] != 9 && coordinates[0, 2] != 9 && coordinates[0, 3] != 9 && array[coordinates[0, 0] + 1, coordinates[1, 0]] == 0 && array[coordinates[0, 1] + 1, coordinates[1, 1]] == 0 && array[coordinates[0, 2] + 1, coordinates[1, 2]] == 0 && array[coordinates[0, 3] + 1, coordinates[1, 3]] == 0) { MoveSound.Play(); ChangePosition(Tetris.Direction.Right); } break; case Keys.Down: case Keys.S: while (coordinates[1, 0] != 19 && coordinates[1, 1] != 19 && coordinates[1, 2] != 19 && coordinates[1, 3] != 19 && array[coordinates[0, 0], coordinates[1, 0] + 1] == 0 && array[coordinates[0, 1], coordinates[1, 1] + 1] == 0 && array[coordinates[0, 2], coordinates[1, 2] + 1] == 0 && array[coordinates[0, 3], coordinates[1, 3] + 1] == 0) { ChangePosition(Tetris.Direction.Down); } if (coordinates[1, 0] == 19 || coordinates[1, 1] == 19 || coordinates[1, 2] == 19 || coordinates[1, 3] == 19 || array[coordinates[0, 0], coordinates[1, 0] + 1] != 0 || array[coordinates[0, 1], coordinates[1, 1] + 1] != 0 || array[coordinates[0, 2], coordinates[1, 2] + 1] != 0 || array[coordinates[0, 3], coordinates[1, 3] + 1] != 0) { Down(); } break; case Keys.Up: case Keys.W: ChangePosition(Tetris.Direction.Rotate); if (Tetris.Place(current, ref moving, array, ref x, ref y, ref coordinates) == true) { Rotate.Play(); Update(true); } else { for (int i = 0; i < 3; i++) { Tetris.Rotate(ref current); } Tetris.Place(current, ref moving, array, ref x, ref y, ref coordinates); Update(true); } break; } } switch (e.KeyCode) { case Keys.Escape: { timer1.Enabled = false; FormPause Pause = new FormPause(this); Pause.ShowDialog(); } break; } }
public void Down() { if (coordinates[1, 0] != 19 && coordinates[1, 1] != 19 && coordinates[1, 2] != 19 && coordinates[1, 3] != 19 && array[coordinates[0, 0], coordinates[1, 0] + 1] == 0 && array[coordinates[0, 1], coordinates[1, 1] + 1] == 0 && array[coordinates[0, 2], coordinates[1, 2] + 1] == 0 && array[coordinates[0, 3], coordinates[1, 3] + 1] == 0) { ChangePosition(Tetris.Direction.Down); } else { DownSound.Play(); AddScore(0); for (int i = 0; i < 4; i++) { moving[coordinates[0, i], coordinates[1, i]] = 0; } for (int i = 0; i < 4; i++) { array[coordinates[0, i], coordinates[1, i]] = Convert.ToInt32(figure); } int linesCount = Tetris.FullLine(ref array); if (linesCount > 0) { Win.Play(); AddScore(linesCount); Draw(array, figure); } if (score >= upScore) { Tetris.NextLevel(ref level, out time, out upScore); labelNext.Text = Convert.ToString(upScore); timer1.Interval = time; } x = 3; y = 0; Tetris.NullArray(ref current); Tetris.NullArray(ref coordinates); figure = nextFigure; Tetris.SetNewFigure(ref current, figure); Clear(Next); nextFigure = (Tetris.Figure)figures.GetValue(r.Next(figures.Length)); Tetris.SetNewFigure(ref next, nextFigure); Draw(next, nextFigure); if (Tetris.Place(current, ref moving, array, ref x, ref y, ref coordinates) == true) { Update(true); } else { Lose.Play(); timer1.Enabled = false; game = false; DialogResult result = MessageBox.Show("Continue", "You Lose", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { Restart(); } } } }