x01.Tetris: 俄罗斯方块

时间:2022-07-30 19:47:32

最强大脑有个小孩玩俄罗斯方块游戏神乎其技,那么,就写一个吧,玩玩而已。

由于逻辑简单,又作了一些简化,所以代码并不多。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading; namespace x01.Tetris
{
public enum BlockType
{
Straight, T, Square, Bent
} public struct Square
{
public int Row, Col;
} public partial class MainWindow : Window
{
const int MaxRow = ;
const int MaxCol = ;
double size = ;
static bool isStarted = false;
int top = , down = ;
Random rand = new Random();
DispatcherTimer timer = new DispatcherTimer();
Rectangle[,] rects = new Rectangle[MaxRow, MaxCol];
Square[] current = new Square[];
List<Square> backup = new List<Square>();
BlockType blockType = BlockType.T; public MainWindow()
{
InitializeComponent();
Init(); timer.Tick += Timer_Tick;
timer.Interval = TimeSpan.FromSeconds(0.5);
timer.Start();
} bool isPressing = false;
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e); if (e.Key == Key.Escape) {
for (int r = ; r < MaxRow; r++) {
for (int c = ; c < MaxCol; c++) {
rects[r, c].Visibility = Visibility.Hidden;
}
}
isStarted = false;
isStarting = false;
} if (!isStarted) return; isPressing = true; if (e.Key == Key.Left) {
for (int i = ; i < ; i++) {
var c = current[i];
if (HasSquare(c.Row, c.Col - ) || !InRange(c.Row, c.Col - )) {
isPressing = false;
return;
}
}
backup.AddRange(current);
for (int i = ; i < ; i++) {
current[i].Col--;
}
} else if (e.Key == Key.Right) {
for (int i = ; i < ; i++) {
var c = current[i];
if (HasSquare(c.Row, c.Col + ) || !InRange(c.Row, c.Col + )) {
isPressing = false;
return;
}
}
backup.AddRange(current);
for (int i = ; i < ; i++) {
current[i].Col++;
}
} else if (e.Key == Key.Up) {
Rotate();
} else if (e.Key == Key.Down) {
for (int i = ; i < ; i++) {
Down();
ReDraw();
}
} isPressing = false;
} bool HasSquare(int row, int col)
{
return InRange(row, col) && !current.Any(s => s.Row == row && s.Col == col)
&& rects[row, col].Visibility == Visibility.Visible;
} int rotateCount = ;
Square[] rotateBack = null;
private void Rotate()
{
rotateBack = (Square[])current.Clone(); switch (blockType) {
case BlockType.Straight:
if (rotateCount % == || rotateCount % == ) {
for (int i = ; i < ; i++) {
rotateBack[i].Row = top;
rotateBack[i].Col += i;
}
} else if (rotateCount % == || rotateCount % == ) {
for (int i = ; i < ; i++) {
rotateBack[i].Row += i;
rotateBack[i].Col = rotateBack[].Col;
}
}
break;
case BlockType.T:
if (rotateCount % == ) {
rotateBack[].Row--;
rotateBack[].Col++;
rotateBack[].Row++;
rotateBack[].Col++;
rotateBack[].Row--;
rotateBack[].Col--;
} else if (rotateCount % == ) {
rotateBack[].Row--;
rotateBack[].Col--;
rotateBack[].Row--;
rotateBack[].Col++;
rotateBack[].Row++;
rotateBack[].Col--;
} else if (rotateCount % == ) {
rotateBack[].Row++;
rotateBack[].Col--;
rotateBack[].Row--;
rotateBack[].Col--;
rotateBack[].Row++;
rotateBack[].Col++;
} else if (rotateCount % == ) {
rotateBack[].Row++;
rotateBack[].Col++;
rotateBack[].Row++;
rotateBack[].Col--;
rotateBack[].Row--;
rotateBack[].Col++;
}
break;
case BlockType.Square:
break;
case BlockType.Bent:
if (rotateCount % == || rotateCount % == ) {
rotateBack[].Col += ;
rotateBack[].Row -= ;
} else if (rotateCount % == || rotateCount % == ) {
rotateBack[].Col -= ;
rotateBack[].Row += ;
}
break;
default:
break;
} for (int i = ; i < ; i++) {
var r = rotateBack[i];
if (HasSquare(r.Row, r.Col) || !InRange(r.Row, r.Col)) {
return;
}
} current = (Square[])rotateBack.Clone();
backup.AddRange(current);
rotateCount++;
if (rotateCount == ) rotateCount = ;
} private void Timer_Tick(object sender, EventArgs e)
{
if (isPressing) return;
if (isStarting) return; if (isStarted == false)
Start(); Down();
ReDraw();
} private void Down()
{
if (isStarted == false) return;
if (current.Any(s => s.Row + == MaxRow)) return; foreach (var b in backup) {
if (InRange(b.Row, b.Col))
rects[b.Row, b.Col].Visibility = Visibility.Hidden;
} backup.Clear();
top = down = current[].Row;
for (int i = ; i < ; i++) {
int row = ++current[i].Row;
int col = current[i].Col;
if (InRange(row, col)) {
rects[row, col].Visibility = Visibility.Visible;
} if (top > row) top = row;
if (down < row) down = row;
} backup.AddRange(current);
} bool InRange(int row, int col)
{
return row >= && row < MaxRow && col >= && col < MaxCol;
} bool isStarting = false;
private void Start()
{
isStarting = true; if (isStarted) return;
isStarted = true; for (int i = ; i < ; i++) {
current[i].Row = current[i].Col = ;
}
rotateCount = ; blockType = (BlockType)rand.Next();
switch (blockType) {
case BlockType.Straight:
for (int i = ; i < ; i++) {
current[i].Col = (MaxCol - ) / ;
current[i].Row = -i;
}
break;
case BlockType.T:
for (int i = ; i < ; i++) {
current[].Row = ;
current[].Col = (MaxCol - ) / ;
if (i > ) {
current[i].Row = -;
current[i].Col = (MaxCol - ) / + (i - );
}
}
break;
case BlockType.Square:
for (int i = ; i < ; i++) {
if (i <= ) {
current[i].Row = ;
current[i].Col = (MaxCol - ) / + i;
} else {
current[i].Row = -;
current[i].Col = (MaxCol - ) / + (i - );
}
}
break;
case BlockType.Bent:
for (int i = ; i < ; i++) {
if (i <= ) {
current[i].Row = ;
current[i].Col = (MaxCol - ) / + i;
} else {
current[i].Row = -;
current[i].Col = (MaxCol - ) / + (i - );
}
}
break;
} isStarting = false;
} private void Init()
{
size = (Height - ) / MaxRow;
canvas.Width = size * MaxCol;
canvas.Height = size * MaxRow; for (int r = ; r < MaxRow; r++) {
for (int c = ; c < MaxCol; c++) {
rects[r, c] = new Rectangle();
rects[r, c].Width = rects[r, c].Height = size;
rects[r, c].Fill = Brushes.Gray;
rects[r, c].Stroke = Brushes.LightGray;
rects[r, c].Visibility = Visibility.Hidden;
canvas.Children.Add(rects[r, c]);
Canvas.SetLeft(rects[r, c], c * size);
Canvas.SetTop(rects[r, c], r * size);
}
} for (int i = ; i < ; i++) {
current[i].Col = current[i].Row = ;
}
} protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext); size = (ActualHeight - ) / MaxRow;
canvas.Width = size * MaxCol;
canvas.Height = size * MaxRow;
ReDraw();
} private void ReDraw()
{
if (isStarted == false) return; for (int r = ; r < MaxRow; r++) {
for (int c = ; c < MaxCol; c++) {
rects[r, c].Width = rects[r, c].Height = size;
Canvas.SetLeft(rects[r, c], c * size);
Canvas.SetTop(rects[r, c], r * size); bool hasSquare = current.Any(s => s.Col == c && s.Row + == r
&& rects[r, c].Visibility == Visibility.Visible)
&& !current.Any(s => s.Row == r && s.Col == c);
if (down == MaxRow - || hasSquare) {
top = down = ;
isStarted = false;
backup.Clear();
ClearLines();
return;
}
}
}
} List<int> cols = new List<int>();
List<int> rows = new List<int>();
void ClearLines()
{
cols.Clear();
rows.Clear(); bool isClear;
for (int r = ; r < MaxRow; r++) {
for (int c = ; c < MaxCol; c++) {
cols.Add(c);
if (c == MaxCol - ) {
isClear = true;
foreach (var col in cols) {
if (rects[r, col].Visibility != Visibility.Visible) {
isClear = false;
break;
}
}
cols.Clear();
if (isClear) rows.Add(r);
}
}
} foreach (var r in rows) {
for (int c = ; c < MaxCol; c++) {
rects[r, c].Visibility = Visibility.Hidden;
}
} foreach (var r in rows) {
for (int i = r - ; i >= ; i--) {
for (int j = ; j < MaxCol; j++) {
rects[i + , j].Visibility = rects[i, j].Visibility;
}
}
}
}
}
}

运行效果图如下:

x01.Tetris: 俄罗斯方块

源代码:https://github.com/chinax01/x01.Tetris