C#游戲開(kāi)發(fā)之實(shí)現(xiàn)貪吃蛇游戲
實(shí)踐過(guò)程
效果
代碼
public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static bool ifStart = false;//判斷是否開(kāi)始 public static int career = 400;//移動(dòng)的速度 Snake snake = new Snake();//實(shí)例化Snake類(lèi) int snake_W = 20;//骨節(jié)的寬度 int snake_H = 20;//骨節(jié)的高度 public static bool pause = false;//是否暫停游戲 /// <summary> /// 繪制游戲場(chǎng)景 /// </summary> /// <param g="Graphics">封裝一個(gè)GDI+繪圖圖面</param> public void ProtractTable(Graphics g) { for (int i = 0; i <= panel1.Width / snake_W; i++)//繪制單元格的縱向線 { g.DrawLine(new Pen(Color.Black, 1), new Point(i * snake_W, 0), new Point(i * snake_W, panel1.Height)); } for (int i = 0; i <= panel1.Height / snake_H; i++)//繪制單元格的橫向線 { g.DrawLine(new Pen(Color.Black, 1), new Point(0, i * snake_H), new Point(panel1.Width, i * snake_H)); } } private void panel1_Paint(object sender, PaintEventArgs e) { Graphics g = panel1.CreateGraphics();//創(chuàng)建panel1控件的Graphics類(lèi) ProtractTable(g);//繪制游戲場(chǎng)景 if (!ifStart)//如是沒(méi)有開(kāi)始游戲 { Snake.timer = timer1; Snake.label = label2; snake.Ophidian(panel1, snake_W);//初始化場(chǎng)地及貪吃蛇信息 } else { for (int i = 0; i < Snake.List.Count; i++)//繪制蛇身 { e.Graphics.FillRectangle(Snake.SolidB, ((Point)Snake.List[i]).X + 1, ((Point)Snake.List[i]).Y + 1, snake_W - 1, snake_H - 1); } e.Graphics.FillRectangle(Snake.SolidF, Snake.Food.X + 1, Snake.Food.Y + 1, snake_W - 1, snake_H - 1);//繪制食物 if (Snake.ifGame)//如果游戲結(jié)束 //繪制提示文本 e.Graphics.DrawString("Game Over", new Font("宋體", 30, FontStyle.Bold), new SolidBrush(Color.DarkSlateGray), new PointF(150, 130)); } } private void 開(kāi)始ToolStripMenuItem_Click(object sender, EventArgs e) { NoviceCortrol(Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString())); snake.BuildFood(); textBox1.Focus(); } /// <summary> /// 控制游戲的開(kāi)始、暫停和結(jié)束 /// </summary> /// <param n="int">標(biāo)識(shí)</param> public void NoviceCortrol(int n) { switch (n) { case 1://開(kāi)始游戲 { ifStart = false; Graphics g = panel1.CreateGraphics();//創(chuàng)建panel1控件的Graphics類(lèi) g.FillRectangle(Snake.SolidD, 0, 0, panel1.Width, panel1.Height);//刷新游戲場(chǎng)地 ProtractTable(g);//繪制游戲場(chǎng)地 ifStart = true;//開(kāi)始游戲 snake.Ophidian(panel1, snake_W);//初始化場(chǎng)地及貪吃蛇信息 timer1.Interval = career;//設(shè)置貪吃蛇移動(dòng)的速度 timer1.Start();//啟動(dòng)計(jì)時(shí)器 pause = true;//是否暫停游戲 label2.Text = "0";//顯示當(dāng)前分?jǐn)?shù) break; } case 2://暫停游戲 { if (pause)//如果游戲正在運(yùn)行 { ifStart = true;//游戲正在開(kāi)始 timer1.Stop();//停止計(jì)時(shí)器 pause = false;//當(dāng)前已暫停游戲 } else { ifStart = true;//游戲正在開(kāi)始 timer1.Start();//啟動(dòng)計(jì)時(shí)器 pause = true;//開(kāi)始游戲 } break; } case 3://退出游戲 { timer1.Stop();//停止計(jì)時(shí)器 Application.Exit();//半閉工程 break; } } } private void 初級(jí)ToolStripMenuItem_Click(object sender, EventArgs e) { if (timer1.Enabled == false)//如果游戲沒(méi)有開(kāi)始 { 初級(jí)ToolStripMenuItem.Checked = false;//設(shè)置該項(xiàng)沒(méi)有被選中 中級(jí)ToolStripMenuItem.Checked = false;//設(shè)置該項(xiàng)沒(méi)有被選中 高級(jí)ToolStripMenuItem.Checked = false;//設(shè)置該項(xiàng)沒(méi)有被選中 ((ToolStripMenuItem)sender).Checked = true;//設(shè)置當(dāng)前項(xiàng)被選中 switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString())) { case 1://高級(jí) { career = 400;//設(shè)置移動(dòng)的速度 break; } case 2://中級(jí) { career = 200; break; } case 3://初 { career = 50; break; } } } textBox1.Focus();//獲得焦點(diǎn) } private void Form1_KeyDown(object sender, KeyEventArgs e) { int tem_n = -1;//記錄移動(dòng)鍵值 if (e.KeyCode == Keys.Right)//如果按→鍵 tem_n = 0;//向右移 if (e.KeyCode == Keys.Left)//如果按←鍵 tem_n = 1;//向左移 if (e.KeyCode == Keys.Up)//如果按↑鍵 tem_n = 2;//向上移 if (e.KeyCode == Keys.Down)//如果按↓鍵 tem_n = 3;//向下移 if (tem_n != -1 && tem_n != Snake.Aspect)//如果移動(dòng)的方向不是相同方向 { if (Snake.ifGame == false) { //如果移動(dòng)的方向不是相反的方向 if (!((tem_n == 0 && Snake.Aspect == 1 || tem_n == 1 && Snake.Aspect == 0) || (tem_n == 2 && Snake.Aspect == 3 || tem_n == 3 && Snake.Aspect == 2))) { Snake.Aspect = tem_n;//記錄移動(dòng)的方向 snake.SnakeMove(tem_n);//移動(dòng)貪吃蛇 } } } int tem_p = -1;//記錄控制鍵值 if (e.KeyCode == Keys.F2)//如果按F2鍵 tem_p = 1;//開(kāi)始游戲 if (e.KeyCode == Keys.F3)//如果按F3鍵 tem_p = 2;//暫?;蚶^續(xù)游戲 if (e.KeyCode == Keys.F4)//如果按F4鍵 tem_p = 3;//關(guān)閉游戲 if (tem_p != -1)//如果當(dāng)前是操作標(biāo)識(shí) NoviceCortrol(tem_p);//控制游戲的開(kāi)始、暫止和關(guān)閉 } private void timer1_Tick(object sender, EventArgs e) { snake.SnakeMove(Snake.Aspect);//移動(dòng)貪吃蛇 } }
partial class Form1 { /// <summary> /// 必需的設(shè)計(jì)器變量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設(shè)計(jì)器生成的代碼 /// <summary> /// 設(shè)計(jì)器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內(nèi)容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.設(shè)置ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.初級(jí)ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.中級(jí)ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.高級(jí)ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.控制ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.開(kāi)始ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.暫停F3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.退出F4ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.panel1 = new System.Windows.Forms.Panel(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.textBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.設(shè)置ToolStripMenuItem, this.控制ToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(525, 24); this.menuStrip1.TabIndex = 0; this.menuStrip1.Text = "menuStrip1"; // // 設(shè)置ToolStripMenuItem // this.設(shè)置ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.初級(jí)ToolStripMenuItem, this.中級(jí)ToolStripMenuItem, this.高級(jí)ToolStripMenuItem}); this.設(shè)置ToolStripMenuItem.Name = "設(shè)置ToolStripMenuItem"; this.設(shè)置ToolStripMenuItem.Size = new System.Drawing.Size(41, 20); this.設(shè)置ToolStripMenuItem.Text = "設(shè)置"; // // 初級(jí)ToolStripMenuItem // this.初級(jí)ToolStripMenuItem.Checked = true; this.初級(jí)ToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked; this.初級(jí)ToolStripMenuItem.Name = "初級(jí)ToolStripMenuItem"; this.初級(jí)ToolStripMenuItem.Size = new System.Drawing.Size(94, 22); this.初級(jí)ToolStripMenuItem.Tag = "1"; this.初級(jí)ToolStripMenuItem.Text = "初級(jí)"; this.初級(jí)ToolStripMenuItem.Click += new System.EventHandler(this.初級(jí)ToolStripMenuItem_Click); // // 中級(jí)ToolStripMenuItem // this.中級(jí)ToolStripMenuItem.Name = "中級(jí)ToolStripMenuItem"; this.中級(jí)ToolStripMenuItem.Size = new System.Drawing.Size(94, 22); this.中級(jí)ToolStripMenuItem.Tag = "2"; this.中級(jí)ToolStripMenuItem.Text = "中級(jí)"; this.中級(jí)ToolStripMenuItem.Click += new System.EventHandler(this.初級(jí)ToolStripMenuItem_Click); // // 高級(jí)ToolStripMenuItem // this.高級(jí)ToolStripMenuItem.Name = "高級(jí)ToolStripMenuItem"; this.高級(jí)ToolStripMenuItem.Size = new System.Drawing.Size(94, 22); this.高級(jí)ToolStripMenuItem.Tag = "3"; this.高級(jí)ToolStripMenuItem.Text = "高級(jí)"; this.高級(jí)ToolStripMenuItem.Click += new System.EventHandler(this.初級(jí)ToolStripMenuItem_Click); // // 控制ToolStripMenuItem // this.控制ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.開(kāi)始ToolStripMenuItem, this.暫停F3ToolStripMenuItem, this.退出F4ToolStripMenuItem}); this.控制ToolStripMenuItem.Name = "控制ToolStripMenuItem"; this.控制ToolStripMenuItem.Size = new System.Drawing.Size(41, 20); this.控制ToolStripMenuItem.Text = "控制"; // // 開(kāi)始ToolStripMenuItem // this.開(kāi)始ToolStripMenuItem.Name = "開(kāi)始ToolStripMenuItem"; this.開(kāi)始ToolStripMenuItem.Size = new System.Drawing.Size(130, 22); this.開(kāi)始ToolStripMenuItem.Tag = "1"; this.開(kāi)始ToolStripMenuItem.Text = "開(kāi)始 &F2"; this.開(kāi)始ToolStripMenuItem.Click += new System.EventHandler(this.開(kāi)始ToolStripMenuItem_Click); // // 暫停F3ToolStripMenuItem // this.暫停F3ToolStripMenuItem.Name = "暫停F3ToolStripMenuItem"; this.暫停F3ToolStripMenuItem.Size = new System.Drawing.Size(130, 22); this.暫停F3ToolStripMenuItem.Tag = "2"; this.暫停F3ToolStripMenuItem.Text = "暫停 &F3"; this.暫停F3ToolStripMenuItem.Click += new System.EventHandler(this.開(kāi)始ToolStripMenuItem_Click); // // 退出F4ToolStripMenuItem // this.退出F4ToolStripMenuItem.Name = "退出F4ToolStripMenuItem"; this.退出F4ToolStripMenuItem.Size = new System.Drawing.Size(130, 22); this.退出F4ToolStripMenuItem.Tag = "3"; this.退出F4ToolStripMenuItem.Text = "退出 &F4"; this.退出F4ToolStripMenuItem.Click += new System.EventHandler(this.開(kāi)始ToolStripMenuItem_Click); // // panel1 // this.panel1.Location = new System.Drawing.Point(12, 37); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(501, 401); this.panel1.TabIndex = 1; this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint); // // timer1 // this.timer1.Interval = 400; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(245, 513); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 21); this.textBox1.TabIndex = 2; this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 448); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(41, 12); this.label1.TabIndex = 3; this.label1.Text = "分?jǐn)?shù):"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(59, 448); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(11, 12); this.label2.TabIndex = 4; this.label2.Text = "0"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(525, 469); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.textBox1); this.Controls.Add(this.panel1); this.Controls.Add(this.menuStrip1); this.MainMenuStrip = this.menuStrip1; this.Name = "Form1"; this.Text = "貪吃蛇"; this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.ToolStripMenuItem 設(shè)置ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 初級(jí)ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 中級(jí)ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 高級(jí)ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 控制ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 開(kāi)始ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 暫停F3ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 退出F4ToolStripMenuItem; private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; }
/** * https://zhima.blog.csdn.net/ */ class Snake { public static int Condyle = 0;//設(shè)置骨節(jié)的大小 public static int Aspect = 0;//設(shè)置方向 public static Point[] Place = { new Point(-1, -1), new Point(-1, -1), new Point(-1, -1), new Point(-1, -1), new Point(-1, -1), new Point(-1, -1) };//設(shè)置個(gè)骨節(jié)的位置 public static Point Food = new Point(-1, -1);//設(shè)置食物的所在點(diǎn) public static bool ifFood = false;//是否有食物 public static bool ifGame = false;//游戲是否結(jié)束 public static int Field_width = 0;//場(chǎng)地的寬度 public static int Field_Height = 0;//場(chǎng)地的高度 public static Control control;//記錄繪制貪吃蛇的控件 public static Timer timer;//記錄Timer組件 public static SolidBrush SolidB = new SolidBrush(Color.Red);//設(shè)置貪吃蛇身體的顏色 public static SolidBrush SolidD = new SolidBrush(Color.LightCoral);//設(shè)置背景顏色 public static SolidBrush SolidF = new SolidBrush(Color.Blue);//設(shè)置食物的顏色 public static Label label;//記錄Label控件 public static ArrayList List = new ArrayList();//實(shí)例化ArrayList數(shù)組 Graphics g;//實(shí)例化Graphics類(lèi) /// <summary> /// 初始化場(chǎng)地及貪吃蛇的信息 /// </summary> /// <param Con="Control">控件</param> /// <param condyle="int">骨節(jié)大小</param> public void Ophidian(Control Con, int condyle) { Field_width = Con.Width;//獲取場(chǎng)地的寬度 Field_Height = Con.Height;//獲取場(chǎng)地的高度 Condyle = condyle;//記錄骨節(jié)的大小 control = Con;//記錄背景控件 g = control.CreateGraphics();//創(chuàng)建背景控件的Graphics類(lèi) SolidD = new SolidBrush(Con.BackColor);//設(shè)置畫(huà)刷顏色 for (int i = 0; i < Place.Length; i++)//繪制貪吃蛇 { Place[i].X = (Place.Length - i - 1) * Condyle;//設(shè)置骨節(jié)的橫坐標(biāo)位置 Place[i].Y = (Field_Height / 2) - Condyle;//設(shè)置骨節(jié)的縱坐標(biāo)位置 g.FillRectangle(SolidB, Place[i].X + 1, Place[i].Y + 1, Condyle - 1, Condyle - 1);//繪制骨節(jié) } List = new ArrayList(Place);//記錄每個(gè)骨節(jié)的位置 ifGame = false;//停止游戲 Aspect = 0;//設(shè)置方向?yàn)橛? } /// <summary> /// 移動(dòng)貪吃蛇 /// </summary> /// <param n="int">標(biāo)識(shí),判斷的移動(dòng)的方向</param> public void SnakeMove(int n) { Point tem_Point = new Point(-1, -1);//定義坐標(biāo)結(jié)構(gòu) switch (n) { case 0://右移 { tem_Point.X = ((Point)List[0]).X + Condyle;//蛇頭向右移 tem_Point.Y = ((Point)List[0]).Y; break; } case 1://左移 { tem_Point.X = ((Point)List[0]).X - Condyle;//蛇頭向左移 tem_Point.Y = ((Point)List[0]).Y; break; } case 2://上移 { tem_Point.Y = ((Point)List[0]).Y - Condyle;//蛇頭向上移 tem_Point.X = ((Point)List[0]).X; break; } case 3://下移 { tem_Point.Y = ((Point)List[0]).Y + Condyle;//蛇頭向下移 tem_Point.X = ((Point)List[0]).X; break; } } BuildFood();//生成食物 if (!EstimateMove(tem_Point))//如果沒(méi)有向相反的方向移動(dòng) { Aspect = n;//改變貪吃蛇的移動(dòng)方向 if (!GameAborted(tem_Point))//如果游戲沒(méi)有結(jié)束 { ProtractSnake(tem_Point);//重新繪制蛇身 EatFood();//吃食 } g.FillRectangle(SolidF, Food.X + 1, Food.Y + 1, Condyle - 1, Condyle - 1);//繪制食物 } } /// <summary> /// 吃食 /// </summary> public void EatFood() { if (((Point)List[0]) == Food)//如果蛇頭吃到了食物 { List.Add(List[List.Count - 1]);//在蛇的尾部添加蛇身 ifFood = false;//沒(méi)有食物 BuildFood();//生成食物 label.Text = Convert.ToString(Convert.ToInt32(label.Text) + 5);//顯示當(dāng)前分?jǐn)?shù) } } /// <summary> /// 游戲是否失敗 /// </summary> /// <param GameP="Point">設(shè)置文本的顯示位置</param> public bool GameAborted(Point GameP) { bool tem_b = false;//游戲是否結(jié)束 bool tem_body = false;//記錄蛇身是否重疊 for (int i = 1; i < List.Count; i++)//遍歷所有骨節(jié) { if (((Point)List[0]) == ((Point)List[i]))//如是骨節(jié)重疊 tem_body = true;//游戲失敗 } //判斷蛇頭是否超出游戲場(chǎng)地 if (GameP.X <= -20 || GameP.X >= control.Width - 1 || GameP.Y <= -20 || GameP.Y >= control.Height - 1 || tem_body) { //繪制游戲結(jié)束的提示文本 g.DrawString("Game Over", new Font("宋體", 30, FontStyle.Bold), new SolidBrush(Color.DarkSlateGray), new PointF(150, 130)); ifGame = true;//游戲結(jié)束 timer.Stop();//停止記時(shí)器 tem_b = true; } return tem_b; } /// <summary> /// 判斷蛇是否向相反的方向移動(dòng) /// </summary> /// <param Ep="Point">移動(dòng)的下一步位置</param> public bool EstimateMove(Point Ep) { bool tem_bool = false;//記錄蛇頭是否向相反的方向移動(dòng) if (Ep.X == ((Point)List[0]).X && Ep.Y == ((Point)List[0]).Y)//如果蛇頭向相反的方向移動(dòng) tem_bool = true; return tem_bool; } /// <summary> /// 重新繪制蛇身 /// </summary> public void ProtractSnake(Point Ep) { bool tem_bool = false;//是否清除移動(dòng)后的蛇身 List.Insert(0, Ep);//根據(jù)蛇頭的移動(dòng)方向,設(shè)置蛇頭的位置 Point tem_point = ((Point)List[List.Count-1]);//記錄蛇尾的位置 List.RemoveAt(List.Count - 1);//移除蛇的尾部 //使骨節(jié)向前移動(dòng)一位 for (int i = 0; i < List.Count - 1; i++) { if (tem_point == ((Point)List[i])) tem_bool = true; } if (!tem_bool)//清除貪吃蛇移動(dòng)前的蛇尾部份 g.FillRectangle(SolidD, tem_point.X + 1, tem_point.Y + 1, Condyle - 1, Condyle - 1); for (int i = 0; i < List.Count; i++)//重新繪制蛇身 { g.FillRectangle(SolidB, ((Point)List[0]).X + 1, ((Point)List[0]).Y + 1, Condyle - 1, Condyle - 1); } } /// <summary> /// 生成食物 /// </summary> public void BuildFood() { if (ifFood == false)//如果沒(méi)有食物 { Point tem_p = new Point(-1, -1);//定義坐標(biāo)結(jié)構(gòu) bool tem_bool = true;//是否計(jì)算出食物的合位置 bool tem_b = false;//判斷食物是否和蛇身重疊 while (tem_bool)//計(jì)算食物的顯示位置 { tem_b = false; tem_p = RectFood();//隨機(jī)生成食物的位置 for (int i = 0; i < List.Count; i++)//遍歷整個(gè)蛇身的位置 { if (((Point)List[i]) == tem_p)//如果食物是否和蛇身重疊 { tem_b = true;//記錄重疊 break; } } if (tem_b == false)//如果沒(méi)有重疊 tem_bool = false;//退出循環(huán) } Food = tem_p;//記錄食物的顯示位置 } ifFood = true;//有食物 } /// <summary> /// 隨機(jī)生成食物的節(jié)點(diǎn) /// </summary> public Point RectFood() { int tem_W = Field_width / 20;//獲取場(chǎng)地的行數(shù) int tem_H = Field_Height / 20;//獲取場(chǎng)地的列數(shù) Random RandW = new Random();//實(shí)例化Random類(lèi) tem_W=RandW.Next(0, tem_W - 1);//生成食物的橫向坐標(biāo) Random RandH = new Random();//實(shí)例化Random類(lèi) tem_H = RandH.Next(0, tem_H - 1);//生成食物的縱向坐標(biāo) Point tme_P = new Point(tem_W * Condyle, tem_H * Condyle);//生成食物的顯示位置 return tme_P; } }
到此這篇關(guān)于C#游戲開(kāi)發(fā)之實(shí)現(xiàn)貪吃蛇游戲的文章就介紹到這了,更多相關(guān)C#貪吃蛇游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity3D實(shí)現(xiàn)扭動(dòng)擠壓瀏覽效果
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)扭動(dòng)擠壓瀏覽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02HighCharts圖表控件在ASP.NET WebForm中的使用總結(jié)(全)
這篇文章主要介紹了HighCharts圖表控件在ASP.NET WebForm中的使用總結(jié)(全),需要的朋友可以參考下2015-08-08c# DevExpress gridcontrol日期行的顯示格式設(shè)置
這篇文章主要介紹了c# DevExpress gridcontrol日期行的顯示格式設(shè)置,需要的朋友可以參考下2017-02-02C#使用foreach遍歷哈希表(hashtable)的方法
這篇文章主要介紹了C#使用foreach遍歷哈希表(hashtable)的方法,是C#中foreach語(yǔ)句遍歷散列表的典型應(yīng)用,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04基于WPF實(shí)現(xiàn)一個(gè)簡(jiǎn)單的音頻播放動(dòng)畫(huà)控件
這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)一個(gè)簡(jiǎn)單的音頻播放動(dòng)畫(huà)控件,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-07-07C# 常用協(xié)議實(shí)現(xiàn)模版及FixedSizeReceiveFilter示例(SuperSocket入門(mén))
本文主要介紹了常用協(xié)議實(shí)現(xiàn)模版及FixedSizeReceiveFilter示例。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01