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

上传人:scccc 文档编号:12365885 上传时间:2021-12-03 格式:DOCX 页数:10 大小:77.43KB
返回 下载 相关 举报
C#编程[“贪吃蛇”小游戏].docx_第1页
第1页 / 共10页
C#编程[“贪吃蛇”小游戏].docx_第2页
第2页 / 共10页
C#编程[“贪吃蛇”小游戏].docx_第3页
第3页 / 共10页
C#编程[“贪吃蛇”小游戏].docx_第4页
第4页 / 共10页
C#编程[“贪吃蛇”小游戏].docx_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《C#编程[“贪吃蛇”小游戏].docx》由会员分享,可在线阅读,更多相关《C#编程[“贪吃蛇”小游戏].docx(10页珍藏版)》请在三一文库上搜索。

1、C#编程“贪吃蛇”小游戏一、项目内容及要求编程实现一个 贪吃蛇”小游戏,具体要求如下:1)程序启动后,蛇身默认向右移动,直到用户按下方向键改变运动方向。2)玩家通过键盘上的上、下、左、右四个方向键控制蛇在地图上寻找豆, 蛇吃下豆后,蛇身加长一节。3)界面上任意时刻同时会有三个豆,豆的位置随机生成,定时刷新。4)在游戏过程中,若蛇头碰到场地边界或自己的身体,则游戏失败。二、算法分析一、控件设计1)主菜单控件MenuStrip2)面板控件 Panel:背景色为黄色3)标签控件 Labell、Label2二、类设计1) Gam兼,入口主窗体、游戏界面类。Gam啖的主要功能包括控制游戏的 开始与暂停、

2、选择游戏关卡、声明并创建蛇的行为及豆的行为线程,启动线程。2) Block类:代表蛇和豆一个节点的块类,游戏中需要绘制的块分为以下 三种情况:(1)蛇头:红色椭圆(2)蛇身:红色矩形(3)豆:绿色椭圆添加类的属性:块的颜色、是否蛇头、是否豆,横坐标X、纵坐标Y添加类的行为:绘制块draw ()方法,构造函数(判断是否豆),重载构造 函数3) Snake类:定义游戏中的蛇。其属性包括蛇移动的方向以及蛇身的集合 (泛型集合List<> );其行为成员包括蛇吃豆、吃过豆之后蛇身增长、蛇身移动 以及判断蛇是否死亡等。4) Beans类:定义游戏中的豆。其属性包括 3个豆组成的列表(List

3、<> ); 其行为成员包括定时刷新豆的集合以及移除某个豆。、界面及运行过程四、关键代码(代码要求注释完整,例如:变量的作用,语句的作用,方法的功能等都要加注释说明)namespace 项目 5public partial class Forml : Form Food food = new Food();Snake snake = new Snake();private int score; public Form1()InitializeComponent();timer1.Enabled =false ; /timer 是否可用timer1.Interval = snake.S

4、peed;/ 蛇初始速度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 !=snake.Direction =break;case Keys.Down:if (snake.Direction !=snake.Dire

5、ction =break;case Keys.Left:if (snake.Direction !=snake.Direction =break;case Keys.Right:if (snake.Direction !=snake.Direction =break;Direction .down)Direction .up;Direction .up)Direction .down;Direction .right)Direction .left;Direction .left)Direction .right;return base.ProcessDialogKey(keyData);pr

6、ivate void menuStrip1_ItemClicked( object sender,ToolStripItemClickedEventArgs e) private void 开始游戏 ToolStripMenuItem_Click( object sender,EventArgs e)label2.Text ="0"score = 0;timerl.Enabled =true ;if (snake.Live = false )snake =new Snake();snake.resetSnake();label2.Text ="0"sco

7、re = 0;timelEnabled =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 ="继续"; elsetimer1.Enabled

8、=true ;暂停游戏 ToolStripMenuItem.Text ="暂停游戏"private void 退出游戏 ToolStripMenuItem_Click(object sender,EventArgs e)this .Close();private void 第一关 ToolStripMenuItem_Click( object sender,EventArgs e)timerl.Interval = 200;private void 第二关 ToolStripMenuItem_Click( object sender, EventArgs e)timer1.

9、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 ti

10、mer1_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.Body0.X =

11、 food.Position.X &&snake.Body0.Y = food.Position.Y)snake.Eat(food);food.Exist =false ;score += 10;label2.Text = score.ToString();if (food.Exist)food.Draw(g);elsefood.Position = food.createdFood();/ 重新创造绘制食物food.Exist =true ;food.Draw(g); using System;using System.Collections.Generic;using Sy

12、stem.Linq;using System.Text;using System.ComponentModel;using System.Data;using System.Drawing;using System.Windows.Forms;using System.Media;using System.IO;namespace 项目 5class Block public enum Directionup, down, left, right, class Snakepublic int score;public const int side = 10; 蛇身每个节点长度Point 口 b

13、ody = 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 /Liv

14、e 的属性 get return live;setlive =value ;public Point 口 Body /Body 属性 get return body; public Snake() /蛇类的构造器初始值Point node1 = new Point (100, 50);Point node2 = new Point (90, 50);body0 = node1;body1 = node2;number = 2;direction =Direction .right;live =true ; public void Move() / 蛇的移动 for ( int i = this

15、 .number - 1; i > 0; i-)蛇移动的方式 bodyi = bodyi - 1; switch ( this .direction) / 蛇移动的方向 case Direction .up: body0.Y -= side; break; case Direction .down: body0.Y += side; break;case Direction .left:body0.X -= side; break; case Direction .right:body0.X += side; break; if (body0.X < 10 | body0.X &g

16、t; 537 | body0.Y < 10 | body0.Y > 346) / 蛇撞四壁挂this .live = false ;for ( int i = 1; i <= number - 1; i+)if (body0.X = bodyi.X && body0.Y = bodyi.Y) this .live = false ;public void Eat( Food food) / 蛇吃食物Labe11abel2 =new Label ();bodynumber = food.Position;this .number+; / 长度加score +=

17、2;label2.Text =Convert .ToString(score);public void Draw( Graphics g) / 绘制蛇SolidBrush RedBrush = new SolidBrush (Color .Red);g.DrawEllipse(Pens.Red, body0.X, body0.Y, side, side);g.FillEllipse(RedBrush, body0.X, body0.Y, side, side);for ( int i = 1; i <= number - 1; i+)g.DrawRectangle(Pens.Red, b

18、odyi.X, bodyi.Y, side,side);g.FillRectangle(RedBrush, bodyi.X, bodyi.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 nod

19、e2 = new Point (90, 320);body1 = node1;body0 = node2;number = 2;direction = live =Direction .right;true ;class Food Point position; / 食物位置Boolean exist; /判断失误是否存在 public Food() position =this .createdFood();exist =true ;public Point Position /Position 的属性get return position; setposition =value ; pub

20、lic Boolean Exist /Exist 属性get return exist; setexist =value ; public Point createdFood() / 随机产生食物 Point position = new Point ();Randomran = new Rando®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);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1