📄 nlsfk.cs
字号:
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(30, 22);
this.label7.TabIndex = 16;
this.label7.Text = "右移";
this.label7.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
//
// label8
//
this.label8.ForeColor = System.Drawing.Color.Fuchsia;
this.label8.Location = new System.Drawing.Point(235, 252);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(30, 22);
this.label8.TabIndex = 17;
this.label8.Text = "下移";
this.label8.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
//
// label9
//
this.label9.ForeColor = System.Drawing.Color.Fuchsia;
this.label9.Location = new System.Drawing.Point(235, 274);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(30, 22);
this.label9.TabIndex = 18;
this.label9.Text = "改变";
this.label9.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
//
// nlsfk
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(386, 305);
this.Controls.Add(this.label9);
this.Controls.Add(this.label8);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.pictureBox5);
this.Controls.Add(this.pictureBox4);
this.Controls.Add(this.pictureBox3);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.label5);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.speedLabel);
this.Controls.Add(this.linesLabel);
this.Controls.Add(this.scoreLabel);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.nextPanel);
this.Controls.Add(this.screenPanel);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Menu = this.mainMenu;
this.Name = "nlsfk";
this.Text = "员工查询系统 娱乐中心 俄罗斯方块";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.nlsfk_KeyDown);
this.Load += new System.EventHandler(this.nlsfk_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.nlsfkPaint);
this.ResumeLayout(false);
}
#endregion
//初始化游戏区
public void DrawScreen()
{
if (gameStatus == GAME_STATUS.GAME_RUN || gameStatus == GAME_STATUS.GAME_OVER)
{
ReDrawNextShape();
Graphics grMain = screenPanel.CreateGraphics();
grMain.FillRectangle(new SolidBrush(Color.White), 0, 0, screenPanel.Width, screenPanel.Height);
mainBody.Draw(grMain);
}
if (gameStatus == GAME_STATUS.GAME_STOP || gameStatus == GAME_STATUS.GAME_OVER)
{
Graphics grMain = screenPanel.CreateGraphics();
string logo = "DAYE2T";
DrawText(logo, grMain, new Point(20, (int)(screenPanel.Height*0.28)), 30);
}
if (gameStatus == GAME_STATUS.GAME_OVER)
{
Graphics grMain = screenPanel.CreateGraphics();
string logo = "GAME OVER";
DrawText(logo, grMain, new Point(20, (int)(screenPanel.Height*0.42)), 21);
}
}
private void nlsfkPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
DrawScreen();
}
//控制游戏速度
private void OnTimer(object sender, System.EventArgs e)
{
if (gameStatus == GAME_STATUS.GAME_RUN)
{
Graphics grMain = screenPanel.CreateGraphics();
if (mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_DOWN))
{
DisposeShapeDown();
}
}
}
private void startMenu_Click(object sender, System.EventArgs e)
{
StartGame();
}
private void stopMenu_Click(object sender, System.EventArgs e)
{
GameOver();
}
private void exitMenu_Click(object sender, System.EventArgs e)
{
this.Close();
}
//开始函数
public void StartGame()
{
score = 0;
speed = 0;
lines = 0;
ChangeLines(0);
timer.Interval = SpeedToTime(speed);
timer.Enabled = true;
startMenu.Enabled = false;
stopMenu.Enabled = true;
gameStatus = GAME_STATUS.GAME_RUN;
mainBody.Reset();
GetNextShape(true);
DrawScreen();
}
//输出下一个方块框
public bool GetNextShape()
{
return GetNextShape(false);
}
//控制键设置
private void nlsfk_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
int key = e.KeyValue;
bool ret;
Graphics grMain = screenPanel.CreateGraphics();
if (gameStatus == GAME_STATUS.GAME_RUN)
{
switch (key)
{
case 38: // 方向键上控制改变
ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_ROATE);
break;
case 37: // 方向键左控制向左移
ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_LEFT);
break;
case 39: // 方向键右控制向右移
ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_RIGHT);
break;
case 40: // 方向键下控制向左下移
ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_FALL);
break;
default:
ret = false;
break;
}
if (ret && key == 40)
{
DisposeShapeDown();
}
}
}
private void nlsfk_Load(object sender, System.EventArgs e)
{
}
public bool GetNextShape(bool initGame)
{
int shapeCount = 7;
if (initGame)
{
int indexShape = rndShape.Next(shapeCount);
nextShape = new Shape(indexShape);
}
bool ret = mainBody.SetNextShape(nextShape);
int indNextShape = rndShape.Next(shapeCount);
nextShape = new Shape(indNextShape);
return ret;
}
public void DisposeShapeDown()
{
int count = mainBody.ClearLines();
if (GetNextShape())
{
GameOver();
}
if (count > 0)
{
ChangeLines(count);
DrawScreen();
}
else
{
ReDrawNextShape();
}
}
public void ReDrawNextShape()
{
Graphics grNext = nextPanel.CreateGraphics();
grNext.FillRectangle(new SolidBrush(Color.White), 0, 0, nextPanel.Width, nextPanel.Height);
nextShape.Draw(grNext, nextPanel.Size);
Graphics grMain = screenPanel.CreateGraphics();
mainBody.DrawNextShape(grMain);
}
public void GameOver()
{
gameStatus = GAME_STATUS.GAME_OVER;
timer.Enabled = false;
startMenu.Enabled = true;
stopMenu.Enabled = false;
DrawScreen();
}
public void DrawText(string text, Graphics g, Point pt, int font)
{
Font drawFont = new Font("Courier new", font, FontStyle.Bold);
for (int i=0; i<text.Length; i++)
{
int corIndex = i;
if (i >= 7)
corIndex = i % 7;
SolidBrush drawBrush = new SolidBrush(Block.GetColor(corIndex));
string drawText = new String(' ', i);
drawText += text.Substring(i, 1);
g.DrawString(drawText, drawFont, drawBrush, pt);
}
}
public void ChangeLines(int count)
{
switch (count)
{
case 1:
score += 100;
break;
case 2:
score += 300;
break;
case 3:
score += 500;
break;
case 4:
score += 1000;
break;
default:
break;
}
if ((lines+count) / 30 > lines / 30)
{
speed++;
timer.Interval = SpeedToTime(speed);
}
lines += count;
scoreLabel.Text = score.ToString();
speedLabel.Text = speed.ToString();
linesLabel.Text = lines.ToString();
}
public int SpeedToTime(int nSpeed)
{
switch (nSpeed)
{
case 0:
return(1000);
case 1:
return(900);
case 2:
return(800);
case 3:
return(700);
case 4:
return(600);
case 5:
return(500);
case 6:
return(400);
case 7:
return(300);
case 8:
return(200);
case 9:
return(150);
default:
return(150);
}
}
private void aboutMenu_Click(object sender, System.EventArgs e)
{
about aboutform = new about();
aboutform.ShowDialog();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -