using System;
using System.Windows.Forms;
using System.Drawing;
using System.Media;
class me : Form
{
int w, h, dw, dh, xsize, ysize;
Graphics g;
Timer t = new Timer();
Bitmap[] bitmaps = new Bitmap[8];
me()
{
createBitmaps();
Text = "俄罗斯方块-press f1 for help";
BackColor = Color.Black;
xsize = 10; ysize = 20;
ClientSize = new Size(xsize * 30, ysize * 30);
DesktopLocation = new Point(0, 0);
MaximumSize = MinimumSize = Size;
t.Interval = slow;
t.Tick += go;
KeyUp += keyup;
KeyDown += press;
screen = new int[xsize, ysize];
backup = new int[xsize, ysize];
body = new int[xsize * ysize, 2];
bodysize = 0;
init();
t.Start();
w = ClientSize.Width / xsize;
h = ClientSize.Height / ysize;
dw = 1;
dh = 1;
g = CreateGraphics();
}
void createBitmaps()
{
Color[] colors = new Color[] { Color.White, Color.Yellow, Color.Brown, Color.Green, Color.Blue, Color.Purple, Color.Red,Color.AliceBlue };
for (int i = 0; i < bitmaps.Length; i++)
{
bitmaps[i] = new Bitmap(10, 10);
Graphics.FromImage(bitmaps[i]).FillRectangle(new SolidBrush(colors[i]), 0, 0, bitmaps[i].Width, bitmaps[i].Height);
}
}
void draw()
{
drawBody();
int i, j;
for (i = 0; i < xsize; i++)
for (j = 0; j < ysize; j++)
if (screen[i, j] != backup[i, j])
{
screen[i, j] = backup[i, j];
g.DrawImage(bitmaps[backup[i, j]], new Rectangle(new Point(i * w, j * h), new Size(w - dw, h - dh)));
}
cover();//drawBody和cover一唱一和,配合默契
}
void keyup(object o, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Down: t.Interval = slow; break;
}
}
void press(object o, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up: turn(); draw(); break;
case Keys.Down: t.Interval = fast; break;
case Keys.Left: left(); draw(); break;
case Keys.Right: right(); draw(); break;
case Keys.F1: help(); break;
}
}
void help()
{
t.Stop();
MessageBox.Show("up-rotate;\ndown-accelerate;\nleft-left;\nright-right;\nf1-help\n\t\t--made by weidiao.neu");
t.Start();
}
static void Main()
{
me haha = new me();
Application.Run(haha);
}
int[,] screen;
int[,] backup;
int[,] body;
int bodysize;
const int fast = 70;
const int slow = 400;
Random r = new Random();
int nowColor;//现在块的颜色
int[,] dir = new int[4, 2] { { -1, 0 }, { 0, 1 }, { 0, -1 }, { 1, 0 } };
void init()
{
int i, j;
for (i = 0; i < xsize; i++)
for (j = 0; j < ysize; j++)
{
backup[i, j] = 0;
screen[i, j] = -1;
}
}
void turn()
{
int i;
int[,] a = new int[bodysize, 2];
for (i = 0; i < bodysize; i++)
{
a[i, 0] = body[i, 1] - body[0, 1] + body[0, 0];
a[i, 1] = -(body[i, 0] - body[0, 0]) + body[0, 1];
if (!legal(a[i, 0], a[i, 1])) return;
}
copy(a);
}
void left()
{
int i;
int[,] a = new int[bodysize, 2];
for (i = 0; i < bodysize; i++)
{
a[i, 0] = body[i, 0] - 1;
a[i, 1] = body[i, 1];
if (!legal(a[i, 0], a[i, 1])) return;
}
copy(a);
}
void right()
{
int i;
int[,] a = new int[bodysize, 2];
for (i = 0; i < bodysize; i++)
{
a[i, 0] = body[i, 0] + 1;
a[i, 1] = body[i, 1];
if (!legal(a[i, 0], a[i, 1])) return;
}
copy(a);
}
bool move()
{
if (bodysize == 0) return false;
int i;
int[,] a = new int[bodysize, 2];
for (i = 0; i < bodysize; i++)
{
a[i, 0] = body[i, 0];
a[i, 1] = body[i, 1] + 1;
if (!legal(a[i, 0], a[i, 1])) return false;
}
copy(a);
return true;
}
void copy(int[,] a)
{
int i;
for (i = 0; i < bodysize; i++)
{
body[i, 0] = a[i, 0];
body[i, 1] = a[i, 1];
}
}
void drawBody()
{
int i;
for (i = 0; i < bodysize; i++)
backup[body[i, 0], body[i, 1]] = nowColor;
}
void cover()//把backup中的物体清空
{
int i;
for (i = 0; i < bodysize; i++)
backup[body[i, 0], body[i, 1]] = 0;
}
void lose()
{
t.Stop();
DialogResult result = MessageBox.Show("你弱爆了", "lose", MessageBoxButtons.RetryCancel);
if (result == DialogResult.Retry)
{
init();
t.Start();
}
else Application.Exit();
}
void stay()
{
int i;
for (i = 0; i < bodysize; i++)
backup[body[i, 0], body[i, 1]] = nowColor;
}
/*新块产生方式有很多种,是决定游戏好玩和难度的主要因素
*/
void produce(int x, int y)//产生新块
{
if (bodysize == 4) return;
body[bodysize, 0] = x;
body[bodysize, 1] = y;
bodysize++;
int i;
int m, n, l;
for (i = 0; i < 5; i++)
{
l = r.Next() % 4;
m = x + dir[l, 0];
n = y + dir[l, 1];
if (legal(m, n))
produce(m, n);
}
}
bool newbody()
{
bodysize = 0;
nowColor = r.Next() % 7 + 1;
body[0, 0] = xsize / 2;
body[0, 1] = 0;
if (!legal(body[0, 0], body[0, 1])) return false;
else
{
produce(body[0, 0], body[0, 1]);
return true;
}
}
void go(object o, EventArgs e)
{
if (!move())
{
stay();
vanish();
if (newbody() == false)
{
lose();
return;
}
}
draw();
}
void vanish()
{
int i, j;
for (j = 0; j < ysize; j++)
{
for (i = 0; i < xsize; i++)
if (backup[i, j] == 0) break;
if (i == xsize) fall(j);
}
}
void fall(int floor)
{
if (floor == 0) return;
for (int i = 0; i < xsize; i++)
{
backup[i, floor] = backup[i, floor - 1];
backup[i, floor - 1] = 0;
}
fall(floor - 1);
}
bool legal(int i, int j)
{
if (i >= 0 && j >= 0 && i < xsize && j < ysize) return backup[i, j] == 0;
else return false;
}
}