C#编程 [“贪吃蛇”小游戏]_第1页
C#编程 [“贪吃蛇”小游戏]_第2页
C#编程 [“贪吃蛇”小游戏]_第3页
C#编程 [“贪吃蛇”小游戏]_第4页
C#编程 [“贪吃蛇”小游戏]_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

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

3、t;);其行为成员包括定时刷新豆的集合以及移除某个豆。三、界面及运行过程四、关键代码(代码要求注释完整,例如:变量的作用,语句的作用,方法的功能等都要加注释说明)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

4、; /蛇初始速度 score = 0; Point body = new Point1000; /蛇最大长度 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

5、: 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.ProcessDialog

6、Key(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

7、.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 = &q

8、uot;继续" 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

9、(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)

10、 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 = fa

11、lse;/timer1不可用 if (snake.Body0.X = 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); else food.Position = food.createdFood();/重新创造绘制食物 food.Exist = true; food.Draw(g); using Syst

12、em;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

13、; public const int side = 10;/蛇身每个节点长度 Point body = new Point600;/蛇身最大长度 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 dir

14、ection = 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); body0 = node1; body1 = node2; number = 2; direction = Direction.right; live = true; public

15、 void Move()/蛇的移动 for (int i = this.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

16、 (body0.X < 10 | body0.X > 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)/蛇吃食物 Label label2 = new Label(); bodynumber = food.Position; t

17、his.number+;/长度加 score += 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+)

18、g.DrawRectangle(Pens.Red, bodyi.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 node2 = new Point(90, 320); body1 = node1; body0 = node2; number = 2; direction = D

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论