源代碼下載鏈接
:[1]C#貪吃蛇遊戲#
工具/原料
Visual Studio 2013
方法/步驟
新建項目,選擇c#語言,Windows程序
:[1]C#貪吃蛇遊戲#
右邊的窗口中有 form1.cs ,它有兩種查看方式,可以使用 設計的方式,也可以使用代碼的方式,代碼的方式查看如下。
:[1]C#貪吃蛇遊戲#
設置主窗體的屬性,屬性工具欄也在右邊,設置的屬性如圖所示。
:[1]C#貪吃蛇遊戲#
切換到設計模式下面 打開工具箱,拖一個 pictureBox控件到主窗口。同時設置pictureBox的屬性,把屬性名稱設置為pBox
:[1]C#貪吃蛇遊戲#
向主窗口添加菜單,拖動菜單控件到主窗體,並設置三個子菜單item,並設置三個子菜單的屬性名稱。
:[1]C#貪吃蛇遊戲#
向主窗體添加定時器,並設置定時器的間隔時間等屬性
:[1]C#貪吃蛇遊戲#
到目前為止界面基本上就畫完了,下面的工作就是要實現 Snake 類,這個類的目的就是完成貪吃蛇的核心功能。首先創建一個Snake類。並完成相應的函數。添加新類可以使用Shift + Alt +c 添加。代碼如下,(看不清的話我會將源代碼放到網上,下載鏈接在下面)
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Snake{ class Point { int x, y; public int Y { get { return y; } set { y = value; } } public int X { get { return x; } set { x = value; } } public Point() { x = y = 0; } public Point(int _x, int _y) { x = _x; y = _y; } } class Snake { private List
:[1]C#貪吃蛇遊戲#
Snake的基本功能在Snake類中都實現了,下面的工作就是在界面類中調用類的函數,完成程序的最後步驟。先添加響應函數,總共有下面的響應函數
3個menuitem的響應函數,前面已經添加;
1個定時器的回調函數;
2個鍵盤響應函數;keyup和keydown
如圖所示:
:[1]C#貪吃蛇遊戲#
添加完成響應函數之後是沒有實現的,需要自己實現函數體,代碼都在 Form1.cs中修改。
下面就是我實現的代碼:格式很亂!!沒辦法(看不清的話我會將源代碼放到網上,下載鏈接在下面)
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Snake{ public partial class Form1 : Form { Snake snake; Point food; int[,] data; int unitWidth, unitHeight; int xNum, yNum; Pen bgPen; Pen greenPen; Pen whitePen; Pen yellowPen; Pen redPen; public Form1() { InitializeComponent(); food = new Point(); xNum = 10; yNum = 10; data = new int[xNum, yNum]; bgPen = new Pen(Color.Black); whitePen = new Pen(Color.White); greenPen = new Pen(Color.Green); yellowPen = new Pen(Color.Yellow); redPen = new Pen(Color.Red); snake = new Snake() ; this.pBox.Image = new System.Drawing.Bitmap(this.pBox.Size.Width, this.pBox.Size.Height); this.unitWidth = this.pBox.Image.Width / this.yNum - 1; this.unitHeight = this.pBox.Image.Height / this.xNum - 1; reDrawPBox(); this.timer1.Enabled = false; initFace(); } void initFace() { snake.addHead(); } void gameOver() { Graphics g = Graphics.FromImage(this.pBox.Image); System.Drawing.Font font = new System.Drawing.Font("微軟雅黑", 20); g.DrawString("GAME OVER", font, this.redPen.Brush, this.pBox.Image.Width / 2 - 80, this.pBox.Image.Height / 3); this.pBox.Refresh(); font.Dispose(); g.Dispose(); } void drawMemory() { snake.initArr(data, xNum, yNum); for (int i = 0; i < snake.SQ.Count; i++) { data[snake.SQ.ElementAt(i).X, snake.SQ.ElementAt(i).Y] = 2; } data[snake.Head.X, snake.Head.Y] = 1 ; data[snake.Food.X, snake.Food.Y] = 3; // 食物 } void reDrawPBox() { Graphics g = Graphics.FromImage(pBox.Image); Pen otPen = null; g.FillRectangle(bgPen.Brush, 0, 0, this.pBox.Image.Width, this.pBox.Image.Height); for (int i = 0; i < this.xNum; i++) { for (int j = 0; j < this.yNum; j++) { if (data[i, j] == 0) otPen = whitePen; else if(data[i, j] == 1)otPen = redPen ; else if (data[i, j] == 2) otPen = greenPen; else if (data[i, j] == 3) otPen = yellowPen; g.FillRectangle(otPen.Brush,j * (unitWidth + 1), i * (unitHeight + 1), unitWidth, unitHeight); } } pBox.Refresh(); g.Dispose(); } private void onStartMenuClick(object sender, EventArgs e) { snake.SQ.Clear(); snake.addHead(); snake.initArr(data, xNum, yNum); this.timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { if (snake.isOver()) { gameOver(); this.timer1.Enabled = false; return; } snake.next(); drawMemory(); reDrawPBox(); } private void onKeyUp(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Up){ snake.up(); }else if(e.KeyCode == Keys.Left){ snake.left(); }else if(e.KeyCode == Keys.Down){ snake.down(); } else if (e.KeyCode == Keys.Right) { snake.right(); } } private void onStopMenuClick(object sender, EventArgs e) { if (this.stopToolStripMenuItem.Text == "繼續") { this.timer1.Enabled = true; this.stopToolStripMenuItem.Text = "暫停"; } else { this.timer1.Enabled = false; this.stopToolStripMenuItem.Text = "繼續"; } } private void onExitClick(object sender, EventArgs e) { Environment.Exit(0); } private void onKeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Up) { snake.up(); } else if (e.KeyCode == Keys.Left) { snake.left(); } else if (e.KeyCode == Keys.Down) { snake.down(); } else if (e.KeyCode == Keys.Right) { snake.right(); } if (snake.isOver()) { gameOver(); this.timer1.Enabled = false; return; } snake.next(); drawMemory(); reDrawPBox(); } } }
然後就是調試,運行了,這樣就能出現最簡單的貪吃蛇遊戲了!
:[1]C#貪吃蛇遊戲#
注意事項
先調試,後運行
注意程序的鍵盤響應部分
源代碼可以去CSDN中找 C#貪吃蛇