百变方块V1.0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading; //线程
using System.Drawing.Drawing2D; //含GraphicsPath类
using System.Reflection;
namespace VarietyBox //百变方块
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//在类class Form1中声明私有的数据成员变量
private const int CHIP_COUNT=8;
private const int MAX_POINTS = 8;
private const int CHIP_WIDTH = 40;
private const int DELTA = 240;
CChip []m_chipList=new CChip[CHIP_COUNT];
private int m_nCurrIndex; //用户选中的拼块
private int oldx, oldy; //记录鼠标原位置
private bool Drag_PictBox = false;
private int n = 0; //图案序号
private int max = 3; //图案的数量
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics gp = e.Graphics;
Pen p = new Pen(Color.Brown, 1);
for (int i = 1; i < 7; i++)
for (int j = 1; j < 7; j++)
{
gp.DrawLine(p, 0, j * CHIP_WIDTH, CHIP_WIDTH*6, j * CHIP_WIDTH);
gp.DrawLine(p, i * CHIP_WIDTH, 0, i * CHIP_WIDTH, CHIP_WIDTH * 6);
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
label1.Text = p.ToString();
//if (Drag_PictBox == true)
//{
// for (int i = 0; i < CHIP_COUNT; i++)
// if (m_chipList[i].PtInChip(p) == true)
// {
// m_chipList[i].Move2(e.X - oldx, e.Y - oldy); //移动
// Draw_AllChip();//画出所有拼块
// }
//}
if (Drag_PictBox == true)
{
m_chipList[m_nCurrIndex].Move2(e.X - oldx, e.Y - oldy); //移动
Draw_AllChip(); //画出所有拼块
//Thread.Sleep(10); //线程sleep0.01秒,明显减少屏幕闪烁
}
oldx = e.X;
oldy = e.Y;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Point p = new Point(e.X, e.Y);
for (int i = 0; i < CHIP_COUNT; i++)
if (m_chipList[i].PtInChip(p) == true)
{
m_nCurrIndex = i; //记录用户选中的拼块
break;
}
if (e.Button == MouseButtons.Right) //右键旋转
{
Cursor.Current = Cursors.Hand;
m_chipList[m_nCurrIndex].Rotation2(); //旋转顺时针45度
m_chipList[m_nCurrIndex].Rotation2(); //旋转顺时针45度
Draw_AllChip();//画出所有拼块
}
oldx = e.X;
oldy = e.Y;
Drag_PictBox = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
Drag_PictBox = false;
//对用户选中的m_nCurrIndex拼块坐标进行修正,放置到正确位置
m_chipList[m_nCurrIndex].Verity(CHIP_WIDTH);
Draw_AllChip(); //画出所有拼块
}
private void Reset()
{
for (int i = 0; i < CHIP_COUNT; i++)
m_chipList[i] = new CChip();
Point[] pointList = new Point[MAX_POINTS];
pointList[0].X = DELTA;
pointList[0].Y = DELTA;
pointList[1].X = DELTA + CHIP_WIDTH * 2;
pointList[1].Y = DELTA;
pointList[2].X = DELTA + CHIP_WIDTH * 2;
pointList[2].Y = DELTA + CHIP_WIDTH * 2;
pointList[3].X = DELTA;
pointList[3].Y = DELTA + CHIP_WIDTH * 2;
//m_chipList[0]为一个2W*2W的正方形
m_chipList[0].SetChip(1, pointList, 4);
pointList[0].X = DELTA + CHIP_WIDTH * 2;
pointList[0].Y = DELTA;
pointList[1].X = DELTA + CHIP_WIDTH * 6;
pointList[1].Y = DELTA;
pointList[2].X = DELTA + CHIP_WIDTH * 6;
pointList[2].Y = DELTA + CHIP_WIDTH;
pointList[3].X = DELTA + CHIP_WIDTH * 2;
pointList[3].Y = DELTA + CHIP_WIDTH;
//m_chipList[1]为一个4W*1W的长方形
m_chipList[1].SetChip(2, pointList, 4);
pointList[0].X = DELTA;
pointList[0].Y = DELTA + CHIP_WIDTH * 2; ;
pointList[1].X = DELTA + CHIP_WIDTH;
pointList[1].Y = DELTA + CHIP_WIDTH * 2;
pointList[2].X = DELTA + CHIP_WIDTH;
pointList[2].Y = DELTA + CHIP_WIDTH * 3;
pointList[3].X = DELTA + CHIP_WIDTH * 2;
pointList[3].Y = DELTA + CHIP_WIDTH * 3;
pointList[4].X = DELTA + CHIP_WIDTH * 2;
pointList[4].Y = DELTA + CHIP_WIDTH * 4;
pointList[5].X = DELTA + CHIP_WIDTH;
pointList[5].Y = DELTA + CHIP_WIDTH * 4;
pointList[6].X = DELTA + CHIP_WIDTH;
pointList[6].Y = DELTA + CHIP_WIDTH * 5;
pointList[7].X = DELTA;
pointList[7].Y = DELTA + CHIP_WIDTH * 5;
//m_chipList[2]为一个头朝右的┣形
m_chipList[2].SetChip(3, pointList, 8);
pointList[0].X = DELTA + CHIP_WIDTH;
pointList[0].Y = DELTA + CHIP_WIDTH * 4;
pointList[1].X = DELTA + CHIP_WIDTH * 2;
pointList[1].Y = DELTA + CHIP_WIDTH * 4;
pointList[2].X = DELTA + CHIP_WIDTH * 2;
pointList[2].Y = DELTA + CHIP_WIDTH * 6;