收藏 分销(赏)

C#编程-[“贪吃蛇”小游戏].docx

上传人:仙人****88 文档编号:12012613 上传时间:2025-08-27 格式:DOCX 页数:16 大小:26.15KB 下载积分:10 金币
下载 相关 举报
C#编程-[“贪吃蛇”小游戏].docx_第1页
第1页 / 共16页
C#编程-[“贪吃蛇”小游戏].docx_第2页
第2页 / 共16页


点击查看更多>>
资源描述
C#编程 [“贪吃蛇”小游戏] 一、项目内容及要求 编程实现一个“贪吃蛇”小游戏,具体要求如下: 1)程序启动后,蛇身默认向右移动,直到用户按下方向键改变运动方向。 2)玩家通过键盘上的上、下、左、右四个方向键控制蛇在地图上寻找豆,蛇吃下豆后,蛇身加长一节。 3)界面上任意时刻同时会有三个豆,豆的位置随机生成,定时刷新。 4)在游戏过程中,若蛇头碰到场地边界或自己的身体,则游戏失败。 二、算法分析 一、控件设计 1)主菜单控件 MenuStrip 2)面板控件 Panel:背景色为黄色 3)标签控件 Label1、Label2 二、类设计 1)Game类,入口主窗体、游戏界面类。Game类的主要功能包括控制游戏的开始与暂停、选择游戏关卡、声明并创建蛇的行为及豆的行为线程,启动线程。 2)Block类:代表蛇和豆一个节点的块类,游戏中需要绘制的块分为以下三种情况:(1)蛇头:红色椭圆(2)蛇身:红色矩形 (3)豆:绿色椭圆 添加类的属性:块的颜色、是否蛇头、是否豆,横坐标X、纵坐标Y 添加类的行为:绘制块draw()方法,构造函数(判断是否豆),重载构造函数 3)Snake类:定义游戏中的蛇。其属性包括蛇移动的方向以及蛇身的集合(泛型集合List<>);其行为成员包括蛇吃豆、吃过豆之后蛇身增长、蛇身移动以及判断蛇是否死亡等。 4)Beans类:定义游戏中的豆。其属性包括3个豆组成的列表(List<>);其行为成员包括定时刷新豆的集合以及移除某个豆。 三、界面及运行过程 四、关键代码 (代码要求注释完整,例如:变量的作用,语句的作用,方法的功能等都要加注释说明) namespace 项目5 { public partial class Form1 : Form { Food food = new Food(); Snake snake = new Snake(); private int score; public Form1() { InitializeComponent(); timer1.Enabled = false;//timer是否可用 timer1.Interval = snake.Speed; //蛇初始速度 score = 0; } Point[] body = new Point[1000]; //蛇最大长度 private void 操作ToolStripMenuItem_Click(object sender, EventArgs e) { } protected override bool ProcessDialogKey(Keys keyData) { switch (keyData) { case Keys.Up: if (snake.Direction != Direction.down) { snake.Direction = Direction.up; } break; case Keys.Down: if (snake.Direction != Direction.up) { snake.Direction = Direction.down; } break; case Keys.Left: if (snake.Direction != Direction.right) { snake.Direction = Direction.left; } break; case Keys.Right: if (snake.Direction != Direction.left) { snake.Direction = Direction.right; } break; } return base.ProcessDialogKey(keyData); } private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { } private void 开始游戏ToolStripMenuItem_Click(object sender, EventArgs e) { label2.Text = "0"; score = 0; timer1.Enabled = true; if (snake.Live == false) { snake = new Snake(); snake.resetSnake(); label2.Text = "0"; score = 0; timer1.Enabled = true; } } private void 关卡设定ToolStripMenuItem_Click(object sender, EventArgs e) { } private void 暂停游戏ToolStripMenuItem_Click(object sender, EventArgs e) { if (暂停游戏ToolStripMenuItem.Text == "暂停游戏") { timer1.Enabled = false; 暂停游戏ToolStripMenuItem.Text = "继续"; } else { timer1.Enabled = true; 暂停游戏ToolStripMenuItem.Text = "暂停游戏"; } } private void 退出游戏ToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void 第一关ToolStripMenuItem_Click(object sender, EventArgs e) { timer1.Interval = 200; } private void 第二关ToolStripMenuItem_Click(object sender, EventArgs e) { timer1.Interval = 150; } private void 第三关ToolStripMenuItem_Click(object sender, EventArgs e) { timer1.Interval = 100; } private void 第四关ToolStripMenuItem_Click(object sender, EventArgs e) { timer1.Interval = 50; } private void 第五关ToolStripMenuItem_Click(object sender, EventArgs e) { timer1.Interval = 30; } private void timer1_Tick(object sender, EventArgs e) { snake.Move(); panel1.Invalidate();//蛇运动后,把picturebox1当前界面设为无效,并引发paint事件,重新绘制界面 } private void panel1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; snake.Draw(g);//画蛇 if (snake.live == false) { timer1.Enabled = false;//timer1不可用 } if (snake.Body[0].X == food.Position.X && snake.Body[0].Y == food.Position.Y) { snake.Eat(food); food.Exist = false; score += 10; label2.Text = score.ToString(); } if (food.Exist) { food.Draw(g); } else { food.Position = food.createdFood();//重新创造绘制食物 food.Exist = true; food.Draw(g); } } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using System.Media; using System.IO; namespace 项目5 { class Block { } public enum Direction { up, down, left, right, } class Snake { public int score; public const int side = 10;//蛇身每个节点长度 Point[] body = new Point[600];//蛇身最大长度 Direction direction;//蛇初始移动方向 public bool live;//蛇存活状态 int number;//蛇身节点个数 int speed = 120;//蛇的初始速度 public int Speed//Speed属性 { get { return speed; } set { speed = value; } } public Direction Direction//Direction的属性 { get { return direction; } set { direction = value; } } public bool Live//Live的属性 { get { return live; } set { live = value; } } public Point[] Body//Body属性 { get { return body; } } public Snake()//蛇类的构造器初始值 { Point node1 = new Point(100, 50); Point node2 = new Point(90, 50); body[0] = node1; body[1] = node2; number = 2; direction = Direction.right; live = true; } public void Move()//蛇的移动 { for (int i = this.number - 1; i > 0; i--)//蛇移动的方式 { body[i] = body[i - 1]; } switch (this.direction)//蛇移动的方向 { case Direction.up: body[0].Y -= side; break; case Direction.down: body[0].Y += side; break; case Direction.left: body[0].X -= side; break; case Direction.right: body[0].X += side; break; } if (body[0].X < 10 || body[0].X > 537 || body[0].Y < 10 || body[0].Y > 346)//蛇撞四壁挂 { this.live = false; } for (int i = 1; i <= number - 1; i++) { if (body[0].X == body[i].X && body[0].Y == body[i].Y) { this.live = false; } } } public void Eat(Food food)//蛇吃食物 { Label label2 = new Label(); body[number] = food.Position; this.number++;//长度加 score += 2; label2.Text = Convert.ToString(score); } public void Draw(Graphics g)//绘制蛇 { SolidBrush RedBrush = new SolidBrush(Color.Red); g.DrawEllipse(Pens.Red, body[0].X, body[0].Y, side, side); g.FillEllipse(RedBrush, body[0].X, body[0].Y, side, side); for (int i = 1; i <= number - 1; i++) { g.DrawRectangle(Pens.Red, body[i].X, body[i].Y, side, side); g.FillRectangle(RedBrush, body[i].X, body[i].Y, side, side); } if (this.live == false) { g.DrawString("Game Over!", new Font("宋体", 36, FontStyle.Bold), RedBrush, new Point(100, 150)); } } public void resetSnake()//重新设置蛇 { Point node1 = new Point(100, 320); Point node2 = new Point(90, 320); body[1] = node1; body[0] = node2; number = 2; direction = Direction.right; live = true; } } class Food { Point position;//食物位置 Boolean exist;//判断失误是否存在 public Food() { position = this.createdFood(); exist = true; } public Point Position//Position的属性 { get { return position; } set { position = value; } } public Boolean Exist//Exist属性 { get { return exist; } set { exist = value; } } public Point createdFood()//随机产生食物 { Point position = new Point(); Random ran = new Random(); position.X = ran.Next(1, 30) * 10; position.Y = ran.Next(1, 30) * 10; return position; } public void Draw(Graphics g)//绘制食物 { SolidBrush greenBrush = new SolidBrush(Color.Green); g.FillEllipse(greenBrush, position.X, position.Y, 10, 10); } } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服