📄 tcsform.cs
字号:
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing.Drawing2D;
namespace 自娱自乐
{
/// <summary>
/// TCS 的摘要说明。
/// </summary>
public class TCSForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer GameTime;
private System.Windows.Forms.MainMenu TCSMenu;
private System.Windows.Forms.MenuItem menuGame;
private System.Windows.Forms.MenuItem menuGameLev1;
private System.Windows.Forms.MenuItem menuGameLev2;
private System.Windows.Forms.MenuItem menuGameLev3;
private System.Windows.Forms.MenuItem menuGamePause;
private System.Windows.Forms.MenuItem menuGameCompart;
private System.Windows.Forms.MenuItem menuGameExit;
private System.Windows.Forms.Panel Lawn;
private System.ComponentModel.IContainer components;
//定义全局参数
private struct SnakeType
{
public int Direction;
public PictureBox Pic;
}
//蛇身长度(个),单节蛇身长度(象素),方向
private int LenSnake,LenNode;
//拐点坐标表
private DataTable Inflexion;
//声明蛇体
private SnakeType[] Snake ;
//声明蛋
private PictureBox Egg ;
public TCSForm()
{
//
// 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(TCSForm));
this.GameTime = new System.Windows.Forms.Timer(this.components);
this.TCSMenu = new System.Windows.Forms.MainMenu();
this.menuGame = new System.Windows.Forms.MenuItem();
this.menuGameLev1 = new System.Windows.Forms.MenuItem();
this.menuGameLev2 = new System.Windows.Forms.MenuItem();
this.menuGameLev3 = new System.Windows.Forms.MenuItem();
this.menuGameCompart = new System.Windows.Forms.MenuItem();
this.menuGameExit = new System.Windows.Forms.MenuItem();
this.Lawn = new System.Windows.Forms.Panel();
this.menuGamePause = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// GameTime
//
this.GameTime.Tick += new System.EventHandler(this.GameTime_Tick);
//
// TCSMenu
//
this.TCSMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuGame});
//
// menuGame
//
this.menuGame.Index = 0;
this.menuGame.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuGameLev1,
this.menuGameLev2,
this.menuGameLev3,
this.menuGamePause,
this.menuGameCompart,
this.menuGameExit});
this.menuGame.Text = "游戏";
//
// menuGameLev1
//
this.menuGameLev1.Index = 0;
this.menuGameLev1.Shortcut = System.Windows.Forms.Shortcut.F1;
this.menuGameLev1.Text = "初级";
this.menuGameLev1.Click += new System.EventHandler(this.menuGameLev1_Click);
//
// menuGameLev2
//
this.menuGameLev2.Index = 1;
this.menuGameLev2.Shortcut = System.Windows.Forms.Shortcut.F2;
this.menuGameLev2.Text = "中级";
this.menuGameLev2.Click += new System.EventHandler(this.menuGameLev2_Click);
//
// menuGameLev3
//
this.menuGameLev3.Index = 2;
this.menuGameLev3.Shortcut = System.Windows.Forms.Shortcut.F3;
this.menuGameLev3.Text = "高级";
this.menuGameLev3.Click += new System.EventHandler(this.menuGameLev3_Click);
//
// menuGameCompart
//
this.menuGameCompart.Index = 4;
this.menuGameCompart.Text = "-";
//
// menuGameExit
//
this.menuGameExit.Index = 5;
this.menuGameExit.Shortcut = System.Windows.Forms.Shortcut.F12;
this.menuGameExit.Text = "退出";
this.menuGameExit.Click += new System.EventHandler(this.menuGameExit_Click);
//
// Lawn
//
this.Lawn.BackColor = System.Drawing.Color.DarkSeaGreen;
this.Lawn.Location = new System.Drawing.Point(20, 20);
this.Lawn.Name = "Lawn";
this.Lawn.Size = new System.Drawing.Size(600, 450);
this.Lawn.TabIndex = 0;
//
// menuGamePause
//
this.menuGamePause.Index = 3;
this.menuGamePause.Shortcut = System.Windows.Forms.Shortcut.F4;
this.menuGamePause.Text = "暂停";
this.menuGamePause.Click += new System.EventHandler(this.menuGamePause_Click);
//
// TCSForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.Gray;
this.ClientSize = new System.Drawing.Size(642, 488);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.Lawn});
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Menu = this.TCSMenu;
this.Name = "TCSForm";
this.Text = "贪吃蛇";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TCSForm_KeyDown);
this.Load += new System.EventHandler(this.TCSForm_Load);
this.ResumeLayout(false);
}
#endregion
private void TCSForm_Load(object sender, System.EventArgs e)
{
//程序初始化
//声明拐点坐标表
Inflexion = new DataTable("Inflexion") ;
Inflexion.Columns.Add("X", typeof(Int32));
Inflexion.Columns.Add("Y", typeof(Int32));
Inflexion.Columns.Add("Direction", typeof(Int32));
}
private void menuGameLev1_Click(object sender, System.EventArgs e)
{
//开始初级游戏
Initialize();
GameTime.Interval = 200 ;
GameTime.Start();
}
private void menuGameLev2_Click(object sender, System.EventArgs e)
{
//开始中级游戏
Initialize();
GameTime.Interval = 100 ;
GameTime.Start();
}
private void menuGameLev3_Click(object sender, System.EventArgs e)
{
//开始高级游戏
Initialize();
GameTime.Interval = 50 ;
GameTime.Start();
}
private void menuGamePause_Click(object sender, System.EventArgs e)
{
//暂停游戏
bool GTime = true ;
//确定显示坐标
if(GameTime.Enabled == true)
{
GTime = false ;
GameTime.Stop();
}
if(GTime)
{ GameTime.Start();}
}
private void menuGameExit_Click(object sender, System.EventArgs e)
{
//退出游戏
this.Close();
}
private void GameTime_Tick(object sender, System.EventArgs e)
{
//移动蛇
int i,j ;
for(i=0;i<=LenSnake;i++)
{
for(j=0;j<Inflexion.Rows.Count;j++)
{
if((Snake[i].Pic.Left == (int)Inflexion.Rows[j]["X"])&&(Snake[i].Pic.Top == (int)Inflexion.Rows[j]["Y"]))
{
Snake[i].Direction = (int)Inflexion.Rows[j]["Direction"] ;
if(i == 0)
{
MoveSnake(1,Snake[0].Direction,Snake[0].Pic);
}
if(i == LenSnake)
{
MoveSnake(3,Snake[LenSnake].Direction,Snake[LenSnake].Pic);
Inflexion.Rows[j].Delete();
}
if((i!=0)&&(i!=LenSnake))
{
MoveSnake(2,Snake[i].Direction,Snake[i].Pic);
}
break;
}
}
switch(Snake[i].Direction)
{
case 1 :
//向上
Snake[i].Pic.Top = Snake[i].Pic.Top - Snake[i].Pic.Height ;
break;
case 2 :
//向右
Snake[i].Pic.Left = Snake[i].Pic.Left + Snake[i].Pic.Width ;
break;
case 3 :
//向下
Snake[i].Pic.Top = Snake[i].Pic.Top + Snake[i].Pic.Height ;
break;
case 4 :
//向左
Snake[i].Pic.Left = Snake[i].Pic.Left - Snake[i].Pic.Width ;
break;
}
//判断是否与自身重合
if(i != 0)
{
if((Snake[i].Pic.Top == Snake[0].Pic.Top)&&(Snake[i].Pic.Left == Snake[0].Pic.Left))
{
GameStop();
}
}
}
//判断是否吃蛋
if((Snake[0].Pic.Top == Egg.Top )&&(Snake[0].Pic.Left == Egg.Left ))
{
SnakeAdd();
MoveEgg();
}
//判断是否超出边界
if((Snake[0].Pic.Top < 0)||(Snake[0].Pic.Top >= Lawn.Height)||(Snake[0].Pic.Left < 0)||(Snake[0].Pic.Left >= Lawn.Width))
{
GameStop();
}
}
private void Egg_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//将蛋外观改为圆形
PictureBox Egg = (PictureBox)sender ;
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(0, 0, 10, 15);
Region r = new Region(gp);
Egg.Region = r;
}
private void SnakeHead_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//画蛇头的外观
PictureBox SnakeHead = (PictureBox)sender ;
Point[] myArray = new Point[4] ;
switch(Snake[0].Direction)
{
case 1:
//向上
myArray[0] = new Point(0 , 15) ;
myArray[1] = new Point(7 , 0) ;
myArray[2] = new Point(8 , 0) ;
myArray[3] = new Point(15 ,15) ;
break;
case 2:
//向右
myArray[0] = new Point(0 , 0) ;
myArray[1] = new Point(15, 7) ;
myArray[2] = new Point(15, 8) ;
myArray[3] = new Point(0 , 15) ;
break;
case 3:
//向下
myArray[0] = new Point(0 , 0) ;
myArray[1] = new Point(7 , 15) ;
myArray[2] = new Point(8 , 15) ;
myArray[3] = new Point(15 , 0) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -