📄 mainform.cs
字号:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Xml;
using System.IO;
namespace MyTetris
{
public partial class MainForm : Form
{
private Keys downKey;
private Keys leftMoveKey;
private Keys rightMoveKey;
private Keys dropKey;
private Keys deasilRotateKey;//顺时
private Keys disecRotateeKey;//逆时
private int paletteWidth;
private int paletteHeight;
private int rectPix;
private Color backColor;
#region
public Keys DownKey
{
get { return downKey; }
set { downKey = value; }
}
public Keys LeftMoveKey
{
get { return leftMoveKey; }
set { leftMoveKey = value; }
}
public Keys RightMoveKey
{
get { return rightMoveKey; }
set { rightMoveKey = value; }
}
public Keys DropKey
{
get { return dropKey; }
set { dropKey = value; }
}
public Keys DeasilRotateKey
{
get { return deasilRotateKey; }
set { deasilRotateKey = value; }
}
public Keys DisecRotateeKey
{
get { return disecRotateeKey; }
set { disecRotateeKey = value; }
}
public int PaletteWidth
{
get { return paletteWidth; }
set { paletteWidth = value; }
}
public int PaletteHeight
{
get { return paletteHeight; }
set { paletteHeight = value; }
}
public int RectPix
{
get { return rectPix; }
set { rectPix = value; }
}
public Color BackColor1
{
get { return backColor; }
set { backColor = value; }
}
#endregion //属性
Palette p;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, System.EventArgs e)
{
try
{
Config config = new Config();
config.LoadFromXml();
downKey = config.DownKey;
dropKey = config.DropKey;
leftMoveKey = config.LeftKey;
rightMoveKey = config.RightKey;
deasilRotateKey = config.DeasilKey;
disecRotateeKey = config.DiseconomicsKey;
paletteHeight = config.CoorHeight;
paletteWidth = config.CoorWidth;
backColor = config.BackColor;
rectPix = config.RectPix;
//确定窗体大小
this.Width = paletteWidth * rectPix + 145;
this.Height = paletteHeight * rectPix + 80;
//确定画布大小
pbRun.Width = paletteWidth * rectPix;
pbRun.Height = paletteHeight * rectPix;
//设置下一个方块显示控件的位置
panel4.Left = pbRun.Location.X + pbRun.Width + 5;
panel4.Top = pbRun.Location.Y;
//设置状态显示控件的大小
palShowStatus.Left = pbRun.Location.X + pbRun.Width + 5;
palShowStatus.Top = panel4.Height + 20;
palShowStatus.Height = pbRun.Height - panel4.Height - 20;
palShowStatus.Width = panel4.Width;
}
catch(Exception ex)
{
Config config = new Config();
config.LoadFromXml();
downKey =Keys.Down;
dropKey =Keys.T;
leftMoveKey = Keys.Left;
rightMoveKey = Keys.Right;
deasilRotateKey =Keys.Up;
disecRotateeKey = Keys.R;
paletteHeight =15;
paletteWidth =25;
backColor = Color.Black;
rectPix = 15;
//确定窗体大小
this.Width = paletteWidth * rectPix + 145;
this.Height = paletteHeight * rectPix + 80;
//确定画布大小
pbRun.Width = paletteWidth * rectPix;
pbRun.Height = paletteHeight * rectPix;
//设置下一个方块显示控件的位置
panel4.Left = pbRun.Location.X + pbRun.Width + 5;
panel4.Top = pbRun.Location.Y;
//设置状态显示控件的大小
palShowStatus.Left = pbRun.Location.X + pbRun.Width + 5;
palShowStatus.Top = panel4.Height + 20;
palShowStatus.Height = pbRun.Height - panel4.Height - 20;
palShowStatus.Width = panel4.Width;
}
}
private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (p != null)
p.Close() ;
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == downKey)
p.Down();
else if (e.KeyCode == dropKey)
p.Drop();
else if (e.KeyCode == leftMoveKey)
p.MoveLeft();
else if (e.KeyCode == rightMoveKey)
p.MoveRight();
else if (e.KeyCode == deasilRotateKey)
p.DeasilRotate();
else if (e.KeyCode == disecRotateeKey)
p.DisecRotate();
}
private void menuItemStart_Click(object sender, EventArgs e)
{
if (p == null)
{
p = new Palette(paletteWidth, paletteHeight, rectPix, backColor, pbRun.CreateGraphics(), labReady.CreateGraphics(), palShowStatus.CreateGraphics());
p.PaintPlatte(pbRun.CreateGraphics());
p.Start();
}
else
{
p.Close();
p.PaintPlatte(pbRun.CreateGraphics());
p = new Palette(paletteWidth, paletteHeight, rectPix, backColor, pbRun.CreateGraphics(), labReady.CreateGraphics(), palShowStatus.CreateGraphics());
p.Start();
}
}
private void menuItemExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void pbRun_Paint(object sender, PaintEventArgs e)
{
if(p!=null)
{
p.PaintPlatte(e.Graphics);
}
}
private void labReady_Paint(object sender, PaintEventArgs e)
{
if (p != null)
{
p.PaintReady(e.Graphics);
}
}
private void menuItemSetOption_Click(object sender, EventArgs e)
{
if (this.menuItemPause.Text.Equals("暂 停"))
{
this.menuItemPause.PerformClick();
}
SettingForm sf = new SettingForm(this);
sf.ShowDialog();
}
private void menuItemPause_Click(object sender, EventArgs e)
{
if (p == null)
return;
if (this.menuItemPause.Text == "暂 停")
{
p.Pause();
this.menuItemPause.Text = "继 续";
}
else
{
p.GoOn();
this.menuItemPause.Text = "暂 停";
}
}
private void menuItemReStart_Click(object sender, EventArgs e)
{
if (p != null)
p.Close();
p = new Palette(paletteWidth, paletteHeight, rectPix, backColor, pbRun.CreateGraphics(), labReady.CreateGraphics(), palShowStatus.CreateGraphics());
p.PaintPlatte(pbRun.CreateGraphics());
p.Start();
}
private void palShowStatus_Paint(object sender, PaintEventArgs e)
{
if (p != null)
{
p.ShowStatus();
}
}
private void menuItemContactUs_Click(object sender, EventArgs e)
{
FrmAboutUs fau = new FrmAboutUs();
fau.ShowDialog();
}
private void menuItemOption_Click(object sender, EventArgs e)
{
if (this.menuItemPause.Text.Equals("暂 停"))
{
this.menuItemPause.PerformClick();
}
SettingForm sf = new SettingForm(this);
sf.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
of1.ShowDialog();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -