贪吃蛇大战游戏设计游戏C#主窗口设计

&>&c#窗体实现贪吃蛇游戏
c#窗体实现贪吃蛇游戏
上传大小:63KB
C#窗体实现贪吃蛇,程序简单易懂,开发平台vs2010
综合评分:0
{%username%}回复{%com_username%}{%time%}\
/*点击出现回复框*/
$(".respond_btn").on("click", function (e) {
$(this).parents(".rightLi").children(".respond_box").show();
e.stopPropagation();
$(".cancel_res").on("click", function (e) {
$(this).parents(".res_b").siblings(".res_area").val("");
$(this).parents(".respond_box").hide();
e.stopPropagation();
/*删除评论*/
$(".del_comment_c").on("click", function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_invalid/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parents(".conLi").remove();
alert(data.msg);
$(".res_btn").click(function (e) {
var parentWrap = $(this).parents(".respond_box"),
q = parentWrap.find(".form1").serializeArray(),
resStr = $.trim(parentWrap.find(".res_area_r").val());
console.log(q);
//var res_area_r = $.trim($(".res_area_r").val());
if (resStr == '') {
$(".res_text").css({color: "red"});
$.post("/index.php/comment/do_comment_reply/", q,
function (data) {
if (data.succ == 1) {
var $target,
evt = e || window.
$target = $(evt.target || evt.srcElement);
var $dd = $target.parents('dd');
var $wrapReply = $dd.find('.respond_box');
console.log($wrapReply);
//var mess = $(".res_area_r").val();
var mess = resS
var str = str.replace(/{%header%}/g, data.header)
.replace(/{%href%}/g, 'http://' + window.location.host + '/user/' + data.username)
.replace(/{%username%}/g, data.username)
.replace(/{%com_username%}/g, data.com_username)
.replace(/{%time%}/g, data.time)
.replace(/{%id%}/g, data.id)
.replace(/{%mess%}/g, mess);
$dd.after(str);
$(".respond_box").hide();
$(".res_area_r").val("");
$(".res_area").val("");
$wrapReply.hide();
alert(data.msg);
}, "json");
/*删除回复*/
$(".rightLi").on("click", '.del_comment_r', function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_comment_del/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parent().parent().parent().parent().parent().remove();
$(e.target).parents('.res_list').remove()
alert(data.msg);
//填充回复
function KeyP(v) {
var parentWrap = $(v).parents(".respond_box");
parentWrap.find(".res_area_r").val($.trim(parentWrap.find(".res_area").val()));
评论共有0条
综合评分:
积分/C币:5
综合评分:
积分/C币:3
VIP会员动态
CSDN下载频道资源及相关规则调整公告V11.10
下载频道用户反馈专区
下载频道积分规则调整V1710.18
spring mvc+mybatis+mysql+maven+bootstrap 整合实现增删查改简单实例.zip
资源所需积分/C币
当前拥有积分
当前拥有C币
输入下载码
为了良好体验,不建议使用迅雷下载
c#窗体实现贪吃蛇游戏
会员到期时间:
剩余下载个数:
剩余积分:0
为了良好体验,不建议使用迅雷下载
积分不足!
资源所需积分/C币
当前拥有积分
您可以选择
程序员的必选
绿色安全资源
资源所需积分/C币
当前拥有积分
当前拥有C币
为了良好体验,不建议使用迅雷下载
资源所需积分/C币
当前拥有积分
当前拥有C币
为了良好体验,不建议使用迅雷下载
资源所需积分/C币
当前拥有积分
当前拥有C币
您的积分不足,将扣除 10 C币
为了良好体验,不建议使用迅雷下载
无法举报自己的资源
你当前的下载分为234。
你还不是VIP会员
开通VIP会员权限,免积分下载
你下载资源过于频繁,请输入验证码
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
若举报审核通过,可返还被扣除的积分
被举报人:
举报的资源分:
请选择类型
资源无法下载 ( 404页面、下载失败、资源本身问题)
资源无法使用 (文件损坏、内容缺失、题文不符)
侵犯版权资源 (侵犯公司或个人版权)
虚假资源 (恶意欺诈、刷分资源)
含色情、危害国家安全内容
含广告、木马病毒资源
*详细原因:
c#窗体实现贪吃蛇游戏C#贪吃蛇游戏课程设计实验报告_图文_百度文库
您的浏览器Javascript被禁用,需开启后体验完整功能,
赠送免券下载特权
10W篇文档免费专享
部分付费文档8折起
每天抽奖多种福利
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
C#贪吃蛇游戏课程设计实验报告
阅读已结束,下载本文需要
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,同时保存到云知识,更方便管理
加入VIP
还剩22页未读,
定制HR最喜欢的简历
你可能喜欢 上传我的文档
 下载
 收藏
粉丝量:80
该文档贡献者很忙,什么也没留下。
 下载此文档
用C#做的贪吃蛇游戏的设计论文
下载积分:1500
内容提示:用C#做的贪吃蛇游戏的设计论文
文档格式:DOC|
浏览次数:29|
上传日期: 23:26:54|
文档星级:
全文阅读已结束,如果下载本文需要使用
 1500 积分
下载此文档
该用户还上传了这些文档
用C#做的贪吃蛇游戏的设计论文
关注微信公众号下次自动登录
现在的位置:
& 综合 & 正文
C#贪吃蛇游戏之–界面层(五)
界面层调用逻辑层定义好的方法即可,相对比较简单,直接帖代码就好了!
界面暂且设计成上面的样子,不怎么美观,根据个人爱好设置了!
MySnakeUI.cs
public partial class MySnakeUI : Form
//游戏逻辑类
GameLogic gameL
//游戏信息存取类
SaveGameInfo saveI
int windowW = 250;
int windowH = 350;
bool IsStop =
//最高积分
private int largeS
//存放角色和积分信息
private string[]
private Timer gameT
public MySnakeUI()
InitializeComponent();
panGameInfo.Visible =
saveInfo = new SaveGameInfo();
LoadGameInfo();
gameTimer = new Timer();
gameTimer.Interval = (int)SnakeState.N
gameTimer.Tick+=new EventHandler(gameTimer_Tick);
private void gameTimer_Tick(object sender, EventArgs e)
gameLogic.SnakeMove();
lbScore.Text = gameLogic.GetFenShu();
if (gameLogic.GameOver)
gameTimer.Enabled =
lbGOver.Text = "Game Over!";
lbGOver.Location = new Point(panel1.Width / 2 - 35, panel1.Height / 2 - 40);
lbGOver.Visible =
cbBoxBackW.Enabled =
cbBoxBackH.Enabled =
cbBocSpeed.Enabled =
btnOK.Enabled =
btnStart.Enabled =
if (Convert.ToInt16(lbScore.Text) & largeScore)
lbGOver.Visible =
panGameInfo.Visible =
txtNewScore.Text = lbScore.T
btnSave.Enabled =
LoadGameHeros();
//开始游戏
public void GameStart()
lbGOver.Text = "";
lbGOver.Location = new Point(180, 1);
cbBoxBackW.Enabled =
cbBoxBackH.Enabled =
cbBocSpeed.Enabled =
btnOK.Enabled =
if (gameLogic != null)
gameLogic.CrearAll();
gameLogic = new GameLogic(panel1, windowW, windowH);
gameLogic.InitGame();
gameTimer.Start();
/// &summary&
/// 菜单选项
/// &/summary&
private void MenuBegin_Click(object sender, EventArgs e)
GameStart();
private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
this.Dispose();
Application.Exit();
private void 排行榜PToolStripMenuItem_Click(object sender, EventArgs e)
panGameInfo.Visible =
btnSave.Enabled =
LoadGameHeros();
lbGOver.Visible =
private void 最高分HToolStripMenuItem_Click(object sender, EventArgs e)
MessageBox.Show("最高分:" + "" + largeScore.ToString() + "/n/n" + "玩家:" + "" + lbPlayer.Text);
private void 关于游戏GToolStripMenuItem_Click(object sender, EventArgs e)
AboutGame form = new AboutGame();
form.ShowDialog();
private void MySnakeUI_KeyDown(object sender, KeyEventArgs e)
switch (e.KeyCode)
case Keys.Up:
if (gameLogic.SnakeDirection != SnakeDirection.Down)
gameLogic.SnakeDirection = SnakeDirection.Up;
case Keys.Down:
if (gameLogic.SnakeDirection != SnakeDirection.Up)
gameLogic.SnakeDirection = SnakeDirection.D
case Keys.Left:
if (gameLogic.SnakeDirection != SnakeDirection.Right)
gameLogic.SnakeDirection = SnakeDirection.L
case Keys.Right:
if (gameLogic.SnakeDirection != SnakeDirection.Left)
gameLogic.SnakeDirection = SnakeDirection.R
//配置速度
private void cbBocSpeed_SelectedIndexChanged(object sender, EventArgs e)
switch (cbBocSpeed.SelectedItem.ToString())
case "非常慢":
gameTimer.Interval = 150;
case "比较慢":
gameTimer.Interval = 100;
case "快":
gameTimer.Interval = 70;
case "很快":
gameTimer.Interval = 50;
case "超快":
gameTimer.Interval = 20;
//配置游戏场景
private void btnOK_Click(object sender, EventArgs e)
if (cbBoxBackW.SelectedIndex==-1)
if (cbBoxBackH.SelectedIndex==-1)
windowW = Convert.ToInt16(cbBoxBackW.SelectedItem.ToString());
windowH = Convert.ToInt16(cbBoxBackH.SelectedItem.ToString());
//稍微调整了大小,有点勉强
panel1.Width = (windowW == 250 || windowW == 300) ? windowW + 7 : (windowW == 350) ? windowW + 15 : windowW + 22;
this.Height = (windowH == 300) ? windowH + 51 : (windowH == 350) ? windowH + 57 : windowH + 62;
this.Width = panel1.Width + 118;
//信息保存
private void btnSave_Click(object sender, EventArgs e)
saveInfo = new SaveGameInfo();
if (txtPlayer.Text == "")
txtPlayer.Text = "隐姓埋名";
char[] ch = (txtPlayer.Text).ToCharArray();
string str="";
foreach (char c in ch)
if (c == '-')
if (saveInfo.SaveInfo(txtNewScore.Text, str))
MessageBox.Show("保存成功!");
btnSave.Enabled =
LoadGameHeros();
//关闭信息保存
private void btnClose_Click(object sender, EventArgs e)
panGameInfo.Visible =
lbGOver.Visible =
LoadGameInfo();
btnOK.Focus();
//加载游戏信息
public void LoadGameInfo()
scores = saveInfo.ReadInfo();
if (scores != null)
if (scores.Length & 1)
string[] scoreAndplayer = new string[] { };
foreach (string s in scores)
if (s == "")
scoreAndplayer = s.Split(';');
lbTScore.Text = scoreAndplayer[0];
lbPlayer.Text = scoreAndplayer[1];
largeScore = Convert.ToInt16(lbTScore.Text);
lbTScore.Text = "加载中…";
lbPlayer.Text = "加载中…";
lbTScore.Text = "加载中…";
lbPlayer.Text = "加载中…";
//加载英雄榜
public void LoadGameHeros()
scores = saveInfo.ReadInfo();
if (scores != null)
if (scores.Length & 1)
if (listHeros.Items.Count & 0)
listHeros.Items.Clear();
for (int i = scores.Length - 2; i &= 0; i--)
listHeros.Items.Add("No." + (scores.Length - i - 1) + "
" + scores[i]);
listHeros.Items.Add("英雄,去创造奇迹吧!");
listHeros.Items.Add("英雄,去创造奇迹吧!");
/// &summary&
/// 按方向键时焦点会转移到Enabel=true的控件上,导致无法改变方向,
/// 所以干脆用图片替换Button控件了,下面是图片按钮的一些样式.
/// &/summary&
private void btnStart_Click(object sender, EventArgs e)
if (IsStop == true)
gameTimer.Enabled =
btnStop.Enabled =
btnStart.Enabled =
GameStart();
btnStart.Enabled =
private void btnStart_MouseMove(object sender, MouseEventArgs e)
g = btnStart.CreateGraphics();
Pen p = new Pen(Color.Orange,3);
g.DrawRectangle(p,0,0,btnStart.Width,btnStart.Height);
private void btnStart_MouseLeave(object sender, EventArgs e)
btnStart.Invalidate();
private void btnStop_Click(object sender, EventArgs e)
gameTimer.Enabled =
btnStop.Enabled =
btnStart.Enabled =
private void btnStop_MouseMove(object sender, MouseEventArgs e)
g = btnStop.CreateGraphics();
Pen p = new Pen(Color.Orange, 2);
g.DrawRectangle(p, 1, 1, btnStart.Width-2, btnStart.Height-1);
private void btnStop_MouseLeave(object sender, EventArgs e)
btnStop.Invalidate();
运行界面:
至此,一款贪吃蛇小游戏就完成了,目前只有一个关卡,界面也较为普通,这些以后会改善之。
编译环境:VS2008,c#
源代码下载:
【上篇】【下篇】C#实现贪吃蛇游戏的示例代码分析
原创
474
这篇文章主要为大家分析了贪吃蛇游戏的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下今天无聊突发奇想做个贪吃蛇,虽然网上很多这东西了,不过自己写的感觉还行吧贪吃蛇分析游戏规则:1、蛇起始长度5,每吃一个食物增加1,最大15过关2、蛇用蓝色表示,食物用绿色,障碍物用黑色3、当蛇碰到自己、墙壁、障碍物则游戏失败4、方向键控制蛇的移动方向,蛇不可反方向移动,如正在向上移动,不能马上向下,只能向左、右、上运动5、每过关一次速度提升一次大概思路:1、用网格的形式表示,蛇由方格组成,保存在中2、1中提到了方格,方格保存的内容有,颜色,坐标,是否可以通过,是否是食物3、向前移动一次,将前面方格添加进蛇列表中,将列表最后一个移除,若为前方格子为食物,则不移除最后一个4、使用死来做整个移动5、空格键为加速键,通过修改sleep时间来实现加速包括了3个类一个主窗体,分别是Node(用来表示方格)、(用来表示地图)、Serpent(用来表示蛇),另外一个主窗体。下面依次把代码贴上,基本上每个方法都有代码1:using S
using System.Collections.G
using System.T
using System.D
namespace EngorgeSerpent
/// &summary&
/// &/summary&
class Node
#region 字段
private int width = 10;
private bool isFood =
private bool isPass =//是否可通过
private Color bgColor = Color.FromArgb(224, 224, 224);
private Color foodColor = Color.G
private Color hinderColor = Color.B
private Color thisC
private Color serpentColor = Color.C
#endregion
/// &summary&
/// 设置食物参数
/// &/summary&
/// &param name=&_isFood&&&/param&
public void SetFood(bool _isFood)
IsFood = _isF
if (_isFood)
ThisColor = FoodC
ThisColor = BgC
/// &summary&
/// 设置障碍物参数
/// &/summary&
/// &param name=&_isHinder&&是否为障碍物&/param&
public void SetHinder(bool _isHinder)
IsPass =! _isH
if (_isHinder)
ThisColor = HinderC
ThisColor = BgC
/// &summary&
/// 设置蛇颜色
/// &/summary&
/// &param name=&_isSerpent&&&/param&
public void SetSerpent(bool _isSerpent)
IsPass = !_isS
if (_isSerpent)
ThisColor = SerpentC
ThisColor = BgC
#region 构造函数
public Node()
thisColor = bgC
/// &summary&
/// 有参构造方法
/// &/summary&
/// &param name=&_x&&相对x坐标&/param&
/// &param name=&_y&&相对y坐标&/param&
/// &param name=&_width&&边长&/param&
/// &param name=&_isFood&&是否是食物&/param&
/// &param name=&_isPass&&是否可通过&/param&
public Node(int _x, int _y, int _width, bool _isFood, bool _isPass)
thisColor = bgC
IsFood = _isF
IsPass = _isP
/// &summary&
/// 有参构造方法
/// &/summary&
/// &param name=&_x&&相对x坐标&/param&
/// &param name=&_y&&相对y坐标&/param&
/// &param name=&_width&&边长&/param&
public Node(int _x, int _y, int _width)
/// &summary&
/// 有参构造方法
/// &/summary&
/// &param name=&_x&&相对x坐标&/param&
/// &param name=&_y&&相对y坐标&/param&
public Node(int _x, int _y)
#endregion
#region 属性
/// &summary&
/// 蛇颜色
/// &/summary&
public Color SerpentColor
get { return serpentC }
/// &summary&
/// 背景色
/// &/summary&
public Color BgColor
get { return bgC }
/// &summary&
/// 食物颜色
/// &/summary&
public Color FoodColor
get { return foodC }
/// &summary&
/// 障碍物颜色
/// &/summary&
public Color HinderColor
get { return hinderC }
/// &summary&
/// 当前颜色
/// &/summary&
public Color ThisColor
get { return thisC }
set { thisColor = }
/// &summary&
/// 获取或设置相对横坐标
/// &/summary&
public int X
set { x = }
/// &summary&
/// 获取或设置相对纵坐标
/// &/summary&
public int Y
set { y = }
/// &summary&
/// 获取或设置节点边长
/// &/summary&
public int Width
set { width = }
/// &summary&
/// 获取或设置是否为食物
/// &/summary&
public bool IsFood
get { return isF }
set { isFood = }
/// &summary&
/// 获取或设置是否可以通过
/// &/summary&
public bool IsPass
get { return isP }
set { isPass = }
#endregion
}代码2:using S
using System.Collections.G
using System.T
using System.D
namespace EngorgeSerpent
/// &summary&
/// &/summary&
/// &summary&
/// 节点数组
/// &/summary&
private List&List&Node&& _
private int RowC
private int ComsC
private Color bgColor = Color.FromArgb(224, 224, 224);
private System.Windows.Forms.Control MapP
/// &summary&
/// 地图背景色 和node中背景色一致
/// &/summary&
public Color BgColor
get { return bgC }
/// &summary&
/// 构造方法
/// &/summary&
/// &param name=&rows&&行数&/param&
/// &param name=&coms&&列数&/param&
public Map(int rows, int coms, System.Windows.Forms.Control c)
RowCount =
ComsCount =
MapPanel =
g = c.CreateGraphics();
_nodes = new List&List&Node&&();
for (int i = 0; i & i++)//行
List&Node& index = new List&Node&();
for (int j = 0; j & j++)
Node node = new Node(j, i);
index.Add(node);
_nodes.Add(index);
/// &summary&
/// 构造方法
/// &/summary&
/// &param name=&rows&&行数&/param&
/// &param name=&coms&&列数&/param&
/// &param name=&width&&节点宽度&/param&
public Map(int rows, int coms, int width, System.Windows.Forms.Control c)
RowCount =
ComsCount =
MapPanel =
g = c.CreateGraphics();
_nodes = new List&List&Node&&();
for (int i = 0; i & i++)//行
List&Node& index = new List&Node&();
for (int j = 0; j & j++)
Node node = new Node(j, i, width);
index.Add(node);
_nodes.Add(index);
/// &summary&
/// 重新加载地图
/// &/summary&
public void ResetMap()
for (int i = 0; i & ComsC i++)//行
for (int j = 0; j & RowC j++)
Node node = GetNode(i, j);
node.IsPass =
node.IsFood =
/// &summary&
/// 获得节点
/// &/summary&
/// &param name=&x&&&/param&
/// &param name=&y&&&/param&
/// &returns&&/returns&
public Node GetNode(int x, int y)
return _nodes[y][x];
/// &summary&
/// 设置食物
/// &/summary&
public void SetFood()
SolidBrush brush =
int _x, _y;
Random r = new Random();
while (true)
_x = r.Next(0, RowCount);
_y = r.Next(0, ComsCount);
if (_nodes[_x][_y].IsPass)
Node nodeindex = _nodes[_x][_y];
nodeindex.SetFood(true);
brush = new SolidBrush(nodeindex.FoodColor);
RectangleF[] rects = { new RectangleF(nodeindex.X * nodeindex.Width, nodeindex.Y * nodeindex.Width, nodeindex.Width, nodeindex.Width) };
g.FillRectangles(brush, rects);
/// &summary&
/// 设置障碍物
/// &/summary&
/// &param name=&list&&&/param&
public void SetHinder(List&Node& list)
SolidBrush brush =
RectangleF[] rects = new RectangleF[list.Count];
for (int i = 0; i & list.C i++)
Node _node = list[i];
_node.SetHinder(true);
_node.IsPass =
if (brush == null)
brush = new SolidBrush(_node.HinderColor);
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[i] =
g.FillRectangles(brush, rects);
/// &summary&
/// 设置边界
/// &/summary&
public void SetBorder()
//通过计算得出边界的个数是2(x+y-2)个方格
SolidBrush brush =
int borders = 2 * (ComsCount + RowCount - 2);
RectangleF[] rects = new RectangleF[borders];
int indexcount = 0;
//添加顶部方格进rects列表中
for (int i = 0; i & RowC i++)
Node _node = _nodes[i][0];
_node.SetHinder(true);
if (brush == null)
brush = new SolidBrush(_node.HinderColor);
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[indexcount] =
indexcount++;
//添加底部方格进rects列表中
for (int i = 0; i & RowC i++)
Node _node = _nodes[i][ComsCount - 1];
_node.SetHinder(true);
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[indexcount] =
indexcount++;
//添加左侧方格进列表,因为左侧最上面以及最下面的两个方格已经添加进去,这里不需要重复添加
for (int i = 1; i & ComsCount - 1; i++)
Node _node = _nodes[0][i];
_node.SetHinder(true);
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[indexcount] =
indexcount++;
//添加右侧方格进列表,因为右侧最上面以及最下面两个方格已经添加进去,这里不需要重复添加
for (int i = 1; i & ComsCount - 1; i++)
Node _node = _nodes[RowCount - 1][i];
_node.SetHinder(true);
RectangleF r = new RectangleF(_node.X * _node.Width, _node.Y * _node.Width, _node.Width, _node.Width);
rects[indexcount] =
indexcount++;
g.FillRectangles(brush, rects);
}代码3:using S
using System.Collections.G
using System.T
using System.D
namespace EngorgeSerpent
/// &summary&
/// &/summary&
class Serpent
private List&Node& serpentList = new List&Node&();
private Direction direction = Direction.R//运动方向
private int maxCount = 15;
private int minCount = 5;
private System.Windows.Forms.Control MapP
/// &summary&
/// 设置蛇长度数据
/// &/summary&
/// &param name=&maxLength&&最大长度&/param&
/// &param name=&minLength&&最小长度&/param&
public Serpent(int maxLength, int minLength, System.Windows.Forms.Control c)
maxCount = maxL
minCount = minL
MapPanel =
g = MapPanel.CreateGraphics();
/// &summary&
/// 初始化蛇
/// &/summary&
public void InitializeSerpent()
SolidBrush brush =
RectangleF[] rects = new RectangleF[minCount];
for (int i = 1; i & minC i++)
Node indexnode = new Node(i, 1);
indexnode.SetSerpent(true);//设置蛇颜色
serpentList.Insert(0, indexnode);
if (brush == null)
brush = new SolidBrush(indexnode.SerpentColor);
rects[i] = new RectangleF(indexnode.X * indexnode.Width, indexnode.Y * indexnode.Width, indexnode.Width, indexnode.Width);
g.FillRectangles(brush, rects);
/// &summary&
/// 插入一个
/// &/summary&
/// &param name=&node&&&/param&
public void InsertNode(Node node)
serpentList.Insert(0, node);
node.SetSerpent(true);
SolidBrush brush = new SolidBrush(node.SerpentColor);
RectangleF rect = new RectangleF(node.X * node.Width, node.Y * node.Width, node.Width, node.Width);
g.FillRectangle(brush, rect);
/// &summary&
/// 移除尾巴
/// &/summary&
/// &param name=&node&&&/param&
public void RemoveNode()
Node node = serpentList[serpentList.Count - 1];
serpentList.Remove(node);
node.SetSerpent(false);
SolidBrush brush = new SolidBrush(node.BgColor);
RectangleF rect = new RectangleF(node.X * node.Width, node.Y * node.Width, node.Width, node.Width);
g.FillRectangle(brush, rect);
/// &summary&
/// 获取蛇头
/// &/summary&
/// &returns&蛇头方格&/returns&
public Node GetSerpentHead()
return serpentList[0];
/// &summary&
/// 蛇是否最长
/// &/summary&
/// &returns&&/returns&
public bool IsMax()
if (serpentList.Count == maxCount)
/// &summary&
/// 蛇运动方向
/// &/summary&
public Direction Direction
set { direction = }
/// &summary&
/// 运动方向
/// &/summary&
public enum Direction
}代码4:using S
using System.Collections.G
using System.ComponentM
using System.D
using System.D
using System.T
using System.Windows.F
using System.T
namespace EngorgeSerpent
public partial class MainForm : Form
public MainForm()
InitializeComponent();
List&List&Node&& maplist = new List&List&Node&&();
int level = 1;
/// &summary&
/// 运行线程
/// &/summary&
Thread Work_Thread =
/// &summary&
/// 运行线程监控值
/// &/summary&
bool IsWork =
int sleepTime = 1000;
private void MainForm_Load(object sender, EventArgs e)
g = this.panel1.CreateGraphics();
map = new Map(40, 30, this.panel1);//这里可以对画布进行下大小设置 此处偷懒省略
LoadMapList();//加载障碍物列表
/// &summary&
/// 默认将地图设置为30*40
/// &/summary&
private void SetMap()
map.ResetMap();
g.Clear(map.BgColor);
map.SetBorder();//设置边界
List&Node& hiderList = GetHider();//获取障碍物列表
map.SetHinder(hiderList);//设置障碍物
SetSerpent();//初始化蛇
/// &summary&
/// 设置蛇
/// &/summary&
private void SetSerpent()
serpent = new Serpent(15, 5, this.panel1);
serpent.InitializeSerpent();//初始化蛇
/// &summary&
/// 获取地图障碍物列表 以增加不同级别难度
/// &/summary&
private void LoadMapList()
//目前分为5个级别
//第一级别
List&Node& hiderList1 = new List&Node&();
for (int i = 15; i & 25; i++)
hiderList1.Add(map.GetNode(i, 15));
hiderList1.Add(map.GetNode(15, i));
maplist.Add(hiderList1);
//第二级别
List&Node& hiderList2 = new List&Node&();
for (int i = 7; i & 25; i++)
hiderList2.Add(map.GetNode(i, 15));
hiderList2.Add(map.GetNode(15, i));
maplist.Add(hiderList2);
//第三级别
List&Node& hiderList3 = new List&Node&();
for (int i = 7; i & 25; i++)
hiderList3.Add(map.GetNode(i, 15));
hiderList3.Add(map.GetNode(15, i));
hiderList3.Add(map.GetNode(i, 25));
maplist.Add(hiderList3);
//第四级别
List&Node& hiderList4 = new List&Node&();
for (int i = 7; i & 25; i++)
hiderList4.Add(map.GetNode(i, 25));
hiderList4.Add(map.GetNode(i, 15));
hiderList4.Add(map.GetNode(15, i));
hiderList4.Add(map.GetNode(i, 7));
maplist.Add(hiderList4);
//第五级别
List&Node& hiderList5 = new List&Node&();
for (int i = 7; i & 25; i++)
hiderList5.Add(map.GetNode(i, 25));
hiderList5.Add(map.GetNode(i, 15));
hiderList5.Add(map.GetNode(15, i));
hiderList5.Add(map.GetNode(i, 7));
hiderList5.Add(map.GetNode(i, 35));
for (int i = 12; i & 20; i++)
hiderList5.Add(map.GetNode(7, i));
hiderList5.Add(map.GetNode(25, i));
maplist.Add(hiderList5);
/// &summary&
/// 获取障碍物列表
/// &/summary&
/// &returns&&/returns&
private List&Node& GetHider()
//这里可以添加多个地图,当级别改变时需要重新加载
return maplist[level - 1];
/// &summary&
/// 重置地图
/// &/summary&
/// &param name=&sender&&&/param&
/// &param name=&e&&&/param&
private void btnResetMap_Click(object sender, EventArgs e)
btnStop.Enabled =
button3.Enabled =
button2.Enabled =
//map.ResetMap();
/// &summary&
/// &/summary&
private void Work()
map.SetFood();//设置食物
while (IsWork)
Node node_
Node serpentHead = serpent.GetSerpentHead();
switch (serpent.Direction)
case Direction.Left:
node_index = map.GetNode(serpentHead.X - 1, serpentHead.Y);
case Direction.Right:
node_index = map.GetNode(serpentHead.X + 1, serpentHead.Y);
case Direction.Up:
node_index = map.GetNode(serpentHead.X, serpentHead.Y - 1);
node_index = map.GetNode(serpentHead.X, serpentHead.Y + 1);
SerpentState index_move = SerpentMove(node_index);
if (index_move == SerpentState.Error)//游戏结束
//map.ResetMap();
MessageBox.Show(&游戏结束!&, &提示&, MessageBoxButtons.OK, MessageBoxIcon.Information);
sleepTime = 1000;
level = 1;
thissleeptime = sleepT
lblLevel.BeginInvoke(new MethodInvoker(delegate()
btnStop.Enabled =
button3.Enabled =
button2.Enabled =
lblLevel.Text = &1&;
lblCount.Text = &5&;
else if (index_move == SerpentState.NextLevel)
this.lblCount.BeginInvoke(new MethodInvoker(delegate()
level += 1;
lblLevel.Text = level.ToString();
lblCount.Text = &5&;
sleepTime = sleepTime / 2;
thissleeptime = sleepT
SetMap();//重置地图
Thread.Sleep(thissleeptime);
map.ResetMap();
/// &summary&
/// &/summary&
/// &param name=&sender&&&/param&
/// &param name=&e&&&/param&
private void button2_Click(object sender, EventArgs e)
btnStop.Enabled =
button3.Enabled =
button2.Enabled =
//map.ResetMap();
thissleeptime = sleepT
this.panel1.Focus();
this.btnStop.Enabled =
this.button3.Enabled =
button2.Enabled =
Work_Thread = new Thread(new ThreadStart(Work));
Work_Thread.IsBackground =
Work_Thread.Start();
private void MainForm_KeyDown(object sender, KeyEventArgs e)
if (e.KeyCode == Keys.Right)
if (serpent.Direction != Direction.Left)
serpent.Direction = Direction.R
else if (e.KeyCode == Keys.Left)
if (serpent.Direction != Direction.Right)
serpent.Direction = Direction.L
else if (e.KeyCode == Keys.Up)
if (serpent.Direction != Direction.Down)
serpent.Direction = Direction.Up;
else if (e.KeyCode == Keys.Down)
if (serpent.Direction != Direction.Up)
serpent.Direction = Direction.D
else if (e.KeyCode == Keys.Space)
thissleeptime = sleepTime / 2;
else if (e.KeyCode == Keys.Escape)
if (IsWork)
this.button3.Text = &继续&;
/// &summary&
/// &/summary&
/// &param name=&sender&&&/param&
/// &param name=&e&&&/param&
private void button3_Click(object sender, EventArgs e)
if (!IsWork)
this.button3.Text = &暂停&;
Work_Thread = new Thread(new ThreadStart(Work));
Work_Thread.IsBackground =
Work_Thread.Start();
this.button3.Text = &继续&;
/// &summary&
/// &/summary&
/// &param name=&sender&&&/param&
/// &param name=&e&&&/param&
private void button4_Click(object sender, EventArgs e)
this.Close();
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
Application.Exit();
System.Diagnostics.Process.GetCurrentProcess().Kill();
private void btnStop_Click(object sender, EventArgs e)
// map.ResetMap();
btnStop.Enabled =
button3.Enabled =
button2.Enabled =
Work_Thread.Abort();
/// &summary&
/// &/summary&
/// &param name=&node&&将要移动到的节点&/param&
/// &returns&返回状态&/returns&
private SerpentState SerpentMove(Node node)
if (!node.IsPass)
return SerpentState.E
serpent.InsertNode(node);
if (!node.IsFood)
//不是食物,则移除最后一个节点
serpent.RemoveNode();
lblCount.BeginInvoke(new MethodInvoker(delegate()
this.lblCount.Text = (Convert.ToInt32(this.lblCount.Text.Trim()) + 1).ToString();
map.SetFood();//设置食物
if (serpent.IsMax())
return SerpentState.NextL
return SerpentState.M
private void MainForm_KeyUp(object sender, KeyEventArgs e)
if (e.KeyCode == Keys.Space)
thissleeptime = sleepT
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
int index = 1;
int index_count = Convert.ToInt32(comboBox1.Text);
for (int i = 1; i & index_ i++)
index = index * 2;
level = index_
sleepTime = 1000 /
thissleeptime = sleepT
btnStop.Enabled =
button3.Enabled =
button2.Enabled =
lblCount.Text = &5&;
lblLevel.Text = index_count.ToString();
serpent.Direction = Direction.R
private void checkBox1_Click(object sender, EventArgs e)
comboBox1.Enabled = this.checkBox1.C
public enum SerpentState
NextLevel,
}主界面以上就是C#实现贪吃蛇游戏的示例代码分析的详细内容,更多请关注php中文网其它相关文章!
江湖传言:PHP是世界上最好的编程语言。真的是这样吗?这个梗究竟是从哪来的?学会本课程,你就会明白了。
PHP中文网出品的PHP入门系统教学视频,完全从初学者的角度出发,绝不玩虚的,一切以实用、有用...
ThinkPHP是国内最流行的中文PHP开发框架,也是您Web项目的最佳选择。《php.cn独孤九贱(5)-ThinkPHP5视频教程》课程以ThinkPHP5最新版本为例,从最基本的框架常识开始,将...
本套教程,以一个真实的学校教学管理系统为案例,手把手教会您如何在一张白纸上,从零开始,一步一步的用ThinkPHP5框架快速开发出一个商业项目。
所有计算机语言的学习都要从基础开始,《PHP入门视频教程之一周学会PHP》不仅是PHP的基础部分更主要的是PHP语言的核心技术,是学习PHP必须掌握的内容,任何PHP项目的实现都离不开这部分的内容,通...
《php.cn原创html5视频教程》课程特色:php中文网原创幽默段子系列课程,以恶搞,段子为主题风格的php视频教程!轻松的教学风格,简短的教学模式,让同学们在不知不觉中,学会了HTML知识。
本课以最新版ThinkPHP5.0.10为基础进行开发,全程实录一个完整企业点,从后台到前台,从控制器到路由的全套完整教程,不论是你是新人,还是有一定开发经验的程序员,都可以从中学到实用的知识~~
ThinkPHP是一个快速、开源的轻量级国产PHP开发框架,是业内最流行的PHP框架之一。本课程以博客系统为例,讲述如何使用TP实战开发,从中学习Thinkphp的实践应用。模版下载地址:http:/...
本课程是php实战开发课程,以爱奇艺电影网站为蓝本从零开发一个自己的网站。目的是让大家了解真实项目的架构及开发过程
本课以一个极简的PHP开发框架为案例,向您展示了一个PHP框架应该具有的基本功能,以及具体的实现方法,让您快速对PHP开发框架的底层实现有一个清楚的认识,为以后学习其实的开发框架打下坚实的基础。
javascript是运行在浏览器上的脚本语言,连续多年,被评为全球最受欢迎的编程语言。是前端开发必备三大法器中,最具杀伤力。如果前端开发是降龙十八掌,好么javascript就是第18掌:亢龙有悔。...
本站9月直播课已经结束,本套教程是直播实录,没有报上名或者漏听学员福利来了,赶紧看看吧,说不定这里就有你的菜
轻松明快,简洁生动,让你快速走入HTML5的世界,体会语义化开发的魅力
JavaScript能够称得上是史上使用最广泛的编程语言,也是前端开发必须掌握的三技能之一:描述网页内容的HTML、描述网页样式的CSS以及描述网页行为的JavaScript。本章节将帮助大家迅速掌握...
《php用户注册登录系统》主要介绍网站的登录注册功能,我们会从最简单的实现登录注册功能开始,增加验证码,cookie验证等,丰富网站的登录注册功能
Bootstrap 是最受欢迎的 HTML、CSS 和 JS 框架,用于开发响应式布局、移动设备优先的 WEB 项目。为所有开发者、所有应用场景而设计,它让前端开发更快速、简单,所有开发者都能快速上手...
《PHP学生管理系统视频教程》主要给大家讲解了HTML,PHP,MySQL之间的相互协作,实现动态的网页显示和获取数据.
《php.cn独孤九贱(2)-css视频教程》课程特色:php中文网原创幽默段子系列课程,以恶搞,段子为主题风格的php视频教程!轻松的教学风格,简短的教学模式,让同学们在不知不觉中,学会了CSS知识...
《弹指间学会HTML视频教程》从最基本的概念开始讲起,步步深入,带领大家学习HTML,了解各种常用标签的意义以及基本用法,学习HTML知识为以后的学习打下基础
jQuery是一个快速、简洁的JavaScript框架。设计的宗旨是“write Less,Do More”,即倡导写更少的代码,做更多的事情。它封装JavaScript常用的功能代码,提供一种简便的...
《最新微信小程序开发视频教程》本节课程是由微趋道录制,讲述了如何申请一个微信小程序,以及开发中需要使用哪些工具,和需要注意哪些等。
文章总浏览数

我要回帖

更多关于 贪吃蛇大战游戏设计 的文章

 

随机推荐