📄 elsform.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace 自娱自乐
{
/// <summary>
/// ELSForm 的摘要说明。
/// </summary>
public class ELSForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel PicBox;
private System.Windows.Forms.Panel PicNext;
private System.Windows.Forms.Label labelSuccessShow;
private System.Windows.Forms.Label labelSuccess;
private System.Windows.Forms.Label labelNext;
private System.Windows.Forms.Label labelVelocityUnit;
private System.Windows.Forms.Label labelVelocityShow;
private System.Windows.Forms.Label labelVelocity;
private System.Windows.Forms.Label ButBegin;
private System.Windows.Forms.Label ButPause;
private System.Windows.Forms.Label ButQuick;
private System.Windows.Forms.Label ButSlower;
private System.Windows.Forms.Label ButEnd;
private System.Windows.Forms.Timer GameTime;
private System.ComponentModel.IContainer components;
//定义全局变量
//配置参数:游戏速度,游戏成绩,游戏状态
private int Speed,Success,Status;
//配置参数:图片ID,当前对象方向,当前对象类型,下一对象类型
// 对象类型: 1:方块 2:长条 3:左Z 4:右Z 5:左L 6:右L
// 对象方向: 1:向上 2:向右 3:向下 4:向左
private int PicID,PicOrientation,CurrentType,NextType;
//配置参数:当前对象组,下一对象组
private PictureBox[] CurrentPic = new PictureBox[4];
private PictureBox[] NextPic = new PictureBox[4];
//配置参数:
private int[,] Free = new int[20,10];
public ELSForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ELSForm));
this.PicBox = new System.Windows.Forms.Panel();
this.PicNext = new System.Windows.Forms.Panel();
this.labelSuccessShow = new System.Windows.Forms.Label();
this.labelSuccess = new System.Windows.Forms.Label();
this.labelNext = new System.Windows.Forms.Label();
this.labelVelocityShow = new System.Windows.Forms.Label();
this.labelVelocity = new System.Windows.Forms.Label();
this.GameTime = new System.Windows.Forms.Timer(this.components);
this.labelVelocityUnit = new System.Windows.Forms.Label();
this.ButBegin = new System.Windows.Forms.Label();
this.ButPause = new System.Windows.Forms.Label();
this.ButQuick = new System.Windows.Forms.Label();
this.ButSlower = new System.Windows.Forms.Label();
this.ButEnd = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// PicBox
//
this.PicBox.BackColor = System.Drawing.Color.Black;
this.PicBox.Location = new System.Drawing.Point(100, 0);
this.PicBox.Name = "PicBox";
this.PicBox.Size = new System.Drawing.Size(250, 500);
this.PicBox.TabIndex = 12;
//
// PicNext
//
this.PicNext.BackColor = System.Drawing.Color.Black;
this.PicNext.Location = new System.Drawing.Point(5, 150);
this.PicNext.Name = "PicNext";
this.PicNext.Size = new System.Drawing.Size(90, 150);
this.PicNext.TabIndex = 11;
//
// labelSuccessShow
//
this.labelSuccessShow.BackColor = System.Drawing.Color.Transparent;
this.labelSuccessShow.ForeColor = System.Drawing.Color.Lime;
this.labelSuccessShow.Location = new System.Drawing.Point(10, 350);
this.labelSuccessShow.Name = "labelSuccessShow";
this.labelSuccessShow.Size = new System.Drawing.Size(80, 20);
this.labelSuccessShow.TabIndex = 6;
this.labelSuccessShow.Text = "成 绩:";
this.labelSuccessShow.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// labelSuccess
//
this.labelSuccess.BackColor = System.Drawing.Color.Transparent;
this.labelSuccess.Font = new System.Drawing.Font("新宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.labelSuccess.ForeColor = System.Drawing.Color.White;
this.labelSuccess.Location = new System.Drawing.Point(10, 400);
this.labelSuccess.Name = "labelSuccess";
this.labelSuccess.Size = new System.Drawing.Size(80, 20);
this.labelSuccess.TabIndex = 7;
this.labelSuccess.Text = "0";
this.labelSuccess.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// labelNext
//
this.labelNext.BackColor = System.Drawing.Color.Transparent;
this.labelNext.ForeColor = System.Drawing.Color.Lime;
this.labelNext.Location = new System.Drawing.Point(10, 110);
this.labelNext.Name = "labelNext";
this.labelNext.Size = new System.Drawing.Size(80, 20);
this.labelNext.TabIndex = 5;
this.labelNext.Text = "下 一 个:";
this.labelNext.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// labelVelocityShow
//
this.labelVelocityShow.BackColor = System.Drawing.Color.Transparent;
this.labelVelocityShow.ForeColor = System.Drawing.Color.Lime;
this.labelVelocityShow.Location = new System.Drawing.Point(360, 32);
this.labelVelocityShow.Name = "labelVelocityShow";
this.labelVelocityShow.Size = new System.Drawing.Size(70, 20);
this.labelVelocityShow.TabIndex = 8;
this.labelVelocityShow.Text = "当前速度:";
this.labelVelocityShow.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// labelVelocity
//
this.labelVelocity.BackColor = System.Drawing.Color.Transparent;
this.labelVelocity.Font = new System.Drawing.Font("新宋体", 12F, System.Drawing.FontStyle.Bold);
this.labelVelocity.ForeColor = System.Drawing.Color.White;
this.labelVelocity.Location = new System.Drawing.Point(360, 70);
this.labelVelocity.Name = "labelVelocity";
this.labelVelocity.Size = new System.Drawing.Size(40, 20);
this.labelVelocity.TabIndex = 9;
this.labelVelocity.Text = "1000";
this.labelVelocity.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// GameTime
//
this.GameTime.Interval = 1000;
this.GameTime.Tick += new System.EventHandler(this.GameTime_Tick);
//
// labelVelocityUnit
//
this.labelVelocityUnit.BackColor = System.Drawing.Color.Transparent;
this.labelVelocityUnit.ForeColor = System.Drawing.Color.Lime;
this.labelVelocityUnit.Location = new System.Drawing.Point(400, 70);
this.labelVelocityUnit.Name = "labelVelocityUnit";
this.labelVelocityUnit.Size = new System.Drawing.Size(30, 20);
this.labelVelocityUnit.TabIndex = 10;
this.labelVelocityUnit.Text = "毫秒";
this.labelVelocityUnit.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// ButBegin
//
this.ButBegin.BackColor = System.Drawing.Color.Transparent;
this.ButBegin.Font = new System.Drawing.Font("新宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.ButBegin.ForeColor = System.Drawing.Color.Lime;
this.ButBegin.Location = new System.Drawing.Point(10, 20);
this.ButBegin.Name = "ButBegin";
this.ButBegin.Size = new System.Drawing.Size(80, 25);
this.ButBegin.TabIndex = 13;
this.ButBegin.Text = "开 始";
this.ButBegin.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.ButBegin.Click += new System.EventHandler(this.ButBegin_Click);
//
// ButPause
//
this.ButPause.BackColor = System.Drawing.Color.Transparent;
this.ButPause.Font = new System.Drawing.Font("新宋体", 10.5F, System.Drawing.FontStyle.Bold);
this.ButPause.ForeColor = System.Drawing.Color.Lime;
this.ButPause.Location = new System.Drawing.Point(10, 60);
this.ButPause.Name = "ButPause";
this.ButPause.Size = new System.Drawing.Size(80, 25);
this.ButPause.TabIndex = 14;
this.ButPause.Text = "暂 停";
this.ButPause.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.ButPause.Click += new System.EventHandler(this.ButPause_Click);
//
// ButQuick
//
this.ButQuick.BackColor = System.Drawing.Color.Transparent;
this.ButQuick.Font = new System.Drawing.Font("新宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.ButQuick.ForeColor = System.Drawing.Color.Lime;
this.ButQuick.Location = new System.Drawing.Point(360, 130);
this.ButQuick.Name = "ButQuick";
this.ButQuick.Size = new System.Drawing.Size(70, 25);
this.ButQuick.TabIndex = 15;
this.ButQuick.Text = "加 快";
this.ButQuick.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.ButQuick.Click += new System.EventHandler(this.ButQuick_Click);
//
// ButSlower
//
this.ButSlower.BackColor = System.Drawing.Color.Transparent;
this.ButSlower.Font = new System.Drawing.Font("新宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.ButSlower.ForeColor = System.Drawing.Color.Lime;
this.ButSlower.Location = new System.Drawing.Point(360, 180);
this.ButSlower.Name = "ButSlower";
this.ButSlower.Size = new System.Drawing.Size(70, 25);
this.ButSlower.TabIndex = 16;
this.ButSlower.Text = "减 慢";
this.ButSlower.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.ButSlower.Click += new System.EventHandler(this.ButSlower_Click);
//
// ButEnd
//
this.ButEnd.BackColor = System.Drawing.Color.Transparent;
this.ButEnd.Font = new System.Drawing.Font("新宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.ButEnd.ForeColor = System.Drawing.Color.Lime;
this.ButEnd.Location = new System.Drawing.Point(360, 460);
this.ButEnd.Name = "ButEnd";
this.ButEnd.Size = new System.Drawing.Size(70, 25);
this.ButEnd.TabIndex = 17;
this.ButEnd.Text = "退 出";
this.ButEnd.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.ButEnd.Click += new System.EventHandler(this.ButEnd_Click);
//
// ELSForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.HotTrack;
this.ClientSize = new System.Drawing.Size(442, 501);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.ButEnd,
this.ButSlower,
this.ButQuick,
this.ButPause,
this.ButBegin,
this.labelVelocityUnit,
this.labelVelocity,
this.labelVelocityShow,
this.labelNext,
this.labelSuccess,
this.labelSuccessShow,
this.PicNext,
this.PicBox});
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "ELSForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "俄罗斯方块";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ELSForm_KeyDown);
this.Load += new System.EventHandler(this.ELSForm_Load);
this.ResumeLayout(false);
}
#endregion
private void ELSForm_Load(object sender, System.EventArgs e)
{
//初始化参数
Speed = 1000 ;
Success = 0 ;
Status = 0 ;
PicID = 0 ;
PicOrientation = 1 ;
}
private void ButQuick_Click(object sender, System.EventArgs e)
{
//加速
if(Speed > 100)
{ Speed = Speed - 100 ; }
labelVelocity.Text = Speed.ToString() ;
GameTime.Interval = Speed ;
}
private void ButSlower_Click(object sender, System.EventArgs e)
{
//减速
if(Speed < 10000)
{ Speed = Speed + 100 ; }
labelVelocity.Text = Speed.ToString() ;
GameTime.Interval = Speed ;
}
private void ButEnd_Click(object sender, System.EventArgs e)
{
//退出
this.Close();
}
private void ButPause_Click(object sender, System.EventArgs e)
{
//暂停
int Opt = 0 ;
if(Status == 0)
{
Status = 1 ;
Opt = 1 ;
GameTime.Start();
}
if((Opt == 0)&&(Status == 1))
{
Status = 0 ;
GameTime.Stop();
}
}
private void ButBegin_Click(object sender, System.EventArgs e)
{
//开始游戏
int i,j;
for(i=0;i<20;i++)
for(j=0;j<10;j++)
{
Free[i,j] = 0 ;
}
while(PicBox.Controls.Count > 0)
{
PicBox.Controls[0].Dispose();
}
while(PicNext.Controls.Count > 0)
{
PicNext.Controls[0].Dispose();
}
PicID = 0 ;
PicOrientation = 1 ;
//生成一个PicNext的对象
CreatePic();
//将PicNext的对象转到PicBox上
TransferPic();
//再生成下一个PicNext的对象
CreatePic();
Status = 1 ;
GameTime.Start();
}
private void GameTime_Tick(object sender, System.EventArgs e)
{
//完成对象下移操作,并进行后序处理工作
int i ;
bool accept ;
GameTime.Stop() ;
//可下性判断
accept = true ;
if(CurrentPic[0].Top >= (PicBox.Height - CurrentPic[0].Height)) { accept = false ; }
if(CurrentPic[1].Top >= (PicBox.Height - CurrentPic[1].Height)) { accept = false ; }
if(CurrentPic[2].Top >= (PicBox.Height - CurrentPic[2].Height)) { accept = false ; }
if(CurrentPic[3].Top >= (PicBox.Height - CurrentPic[3].Height)) { accept = false ; }
if(accept)
{
if(Free[CurrentPic[0].Top/CurrentPic[0].Height+1,CurrentPic[0].Left/CurrentPic[0].Width] == 1) { accept = false ; }
if(Free[CurrentPic[1].Top/CurrentPic[1].Height+1,CurrentPic[1].Left/CurrentPic[1].Width] == 1) { accept = false ; }
if(Free[CurrentPic[2].Top/CurrentPic[2].Height+1,CurrentPic[2].Left/CurrentPic[2].Width] == 1) { accept = false ; }
if(Free[CurrentPic[3].Top/CurrentPic[3].Height+1,CurrentPic[3].Left/CurrentPic[3].Width] == 1) { accept = false ; }
}
if(accept)
{
//下移对象
for(i=0;i<4;i++)
{
CurrentPic[i].Top = CurrentPic[i].Top + CurrentPic[i].Height ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -