在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
------------恢复内容开始------------ 新手学习c# 在博客园上看到后自己模仿打出来的第一个程序 开心,纪念一下 bean :食物类 block :蛇块类 snake :蛇类 Map :地图 图形类 食物类: class bean { private Point _origin;//用于画食物顶端坐标 public Point Origin//封装字段 { get { return _origin; } set { _origin = value; } } //显示食物方法 public void showBean(Graphics g) { //定义红色画笔 SolidBrush brush = new SolidBrush(Color.Red); //用实心矩形画食物 g.FillRectangle(brush, _origin.X, _origin.Y, 15, 15); } //食物消失方法 public void unShowBean(Graphics g) { //定义背景色画笔 SolidBrush brush = new SolidBrush(Color.Red); //用实心矩形画食物 g.FillRectangle(brush, _origin.X, _origin.Y, 15, 15); } } 蛇块类:2020-02-16 public class block { //是否是蛇头 private bool _isHead; public bool IShead { get { return _isHead; } set { _isHead = value; } } //蛇块编号 private int _blockNumber; public int BlockNumber { get { return _blockNumber; } set { _blockNumber = value; } } //蛇块在左上角的位置 private Point _origin; public Point Origin { get { return _origin; } set { _origin = value; } } //根据指定位置画蛇块 public void showBlock(Graphics g) { Bitmap bitMap; if (IShead) { bitMap = new Bitmap(Image.FromFile("blue.gif")); } else { bitMap = new Bitmap(Image.FromFile("Yellow.gif")); } g.DrawImage(bitMap, Origin.X, Origin.Y, 15, 15); } //蛇块消除 public void UnShowBlock(Graphics g) { SolidBrush brush = new SolidBrush(Color.Silver); g.FillRectangle(brush,Origin.X,Origin.Y,15,15); } }
蛇类: public class Snake { //用于存放蛇的集合 private List<block> blockList; //0-1-2-3 上-右-下-左 private int direction = 1; //蛇运动方向属性 public int Direction { get { return direction; } set { direction = value; } } //蛇右的编号 蛇头的长度 private int headNumber; //蛇头左上角坐标 private Point headPoint; //蛇头坐标只读属性 public Point HeadPoint { get { return headPoint; } } private Point mapLeft; //游戏开始时初始的蛇 public Snake(Point map, int count)//填充Blocklist { mapLeft = map; block blockSnake; //定义蛇的起始位置 Point p = new Point(map.X + 15, map.Y + 15); blockList = new List<block>(); //循环画蛇 用于填充集合 for (int i = 0; i < count; i++) { //X坐标加15 p.X += 15; //实例化蛇块 blockSnake = new block(); //定义蛇块左上角的位置 blockSnake.Origin = p; //定义蛇块编号1,2,3,4 blockSnake.BlockNumber = i + 1; if (i == count - 1) { //蛇头 headPoint = blockSnake.Origin; //给蛇头赋值 blockSnake.IShead = true; } blockList.Add(blockSnake); } } //蛇边长函数 public void snakeGrowth() { //找到蛇头坐标 Point head = blockList[blockList.Count - 1].Origin; int x = head.X; int y = head.Y; switch (direction) { case 0: y = y - 15; break; case 1: x = x + 15; break; case 2: y = y + 15; break; case 3: x = x - 15; break; } //先把原先蛇块变为普通蛇块 blockList[blockList.Count - 1].IShead = false; //实例化新蛇头 block headNew = new block(); headNew.IShead = true; headNew.BlockNumber = blockList.Count + 1; headNew.Origin = new Point(x, y); blockList.Add(headNew); headNumber++; headPoint = headNew.Origin; } //蛇先前运动 没有吃到食物 public void Go(Graphics g) { block snakeTail = blockList[0]; //消除蛇块 snakeTail.UnShowBlock(g); //集合中移除尾巴 blockList.RemoveAt(0); //序号全部减1 foreach (var item in blockList) { item.BlockNumber--; } headNumber--; snakeGrowth(); } //蛇的方向转换 public void turnDriction(int pDriction) { switch (direction) { //原来向上运动 case 0: { //希望想做运动 if (pDriction == 3) direction = 3; else if (pDriction == 1) direction = 1; } break; case 1: { if (pDriction == 2) direction = 2; else if (pDriction == 0) direction = 0; break; } case 2: { if (pDriction == 1) direction = 1; else if (pDriction == 3) direction = 3; break; } case 3: { if (pDriction == 0) direction = 0; else if (pDriction == 2) direction = 2; break; } } } //蛇显示函数 public void ShowSnake(Graphics g) { foreach (var item in blockList) { item.showBlock(g); } } //隐藏蛇 public void unShowSnake(Graphics g) { foreach (var item in blockList) { item.UnShowBlock(g); } } //自己是否会碰到自己 public bool IsTouchMyself() { bool isTouch = false; for (int i = 0; i < blockList.Count - 1; i++) { if (headPoint == blockList[i].Origin) { isTouch = true; break; } } return isTouch; } //重置蛇 public void Reset(Point Map, int count) { block blockSnake; //定义蛇的起始位置 Point p = new Point(mapLeft.X + 15, mapLeft.Y + 15); blockList = new List<block>(); //循环画蛇块用于填充集合 for (int i = 0; i < count; i++) { //x坐标加15 p.X += 15; //实例化蛇块 blockSnake = new block(); //定义蛇块左上角位置 blockSnake.Origin = p; //定义蛇块编号 blockSnake.BlockNumber = i + 1; if (i == count - 1) { //蛇头 //给蛇头位置赋值 headPoint = blockSnake.Origin; blockSnake.IShead = true; } blockList.Add(blockSnake); } } }
地图类: public class Map { private Point mapLeft; private static int unit = 15; //定义地图长度为28个蛇块 private readonly int length = 30 * unit; //定义地图宽度为20个蛇块 private readonly int width = 25 * unit; //定义分数 public int score = 0; //定义蛇 private readonly Snake snake; public bool victory = false; public Snake Snake { get { return snake; } } bean food; public Map(Point start) { //把地图左上角的点赋值给全局变量 mapLeft = start; //实例化蛇 snake = new Snake(start, 5); //实例化食物 food = new bean(); food.Origin = new Point(start.X + 30, start.Y + 30); } //产生一个新食物 private bean foodRandom() { Random d = new Random(); int x = d.Next(0, length / unit); int y = d.Next(0, width / unit); Point Origin = new Point(mapLeft.X+x*15,mapLeft.Y+y*15); bean food = new bean(); food.Origin = Origin; return food; } //显示新食物方法 public void showNewFood(Graphics g) { //消除原先食物 food.unShowBean(g); //产生随机位置的食物 food = foodRandom(); //显示新食物 food.showBean(g); } //画地图 public void showMap(Graphics g) { //创建一支红笔 Pen pen = new Pen(Color.Blue); //画地图框 g.DrawRectangle(pen, mapLeft.X, mapLeft.Y, length, width); //显示食物 food.showBean(g); if (cheakBean()) { //吃到了食物 //显示新食物 showNewFood(g); //蛇变长 snake.snakeGrowth(); //加分 score += 10; //显示蛇 snake.ShowSnake(g); } else { snake.Go(g); snake.ShowSnake(g); } } //判断是否吃到食物 public bool cheakBean() { return food.Origin.Equals(snake.HeadPoint); } //检查蛇是否撞墙 public bool checkSnake() { return !(Snake.HeadPoint.X > mapLeft.X - 5 && Snake.HeadPoint.X < (mapLeft.X + length - 5) && Snake.HeadPoint.Y > mapLeft.Y - 5 && Snake.HeadPoint.Y < (mapLeft.Y + width - 5)); } //重新开始 public void Resect(Graphics g) { Snake.unShowSnake(g); Snake.Reset(mapLeft,5); } }
窗体代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); //定义地图 map = new Map(new Point(50, 50)); this.BackColor = Color.Silver; } private readonly Map map; private int gradeNum = 100; private int pink; private void timer1_Tick(object sender, EventArgs e) { this.开始游戏ToolStripMenuItem.Text = "重新开始"; this.label1.Text = map.score.ToString(); if (map.score > 500) { this.timer1.Enabled = false; MessageBox.Show("通关"); } Bitmap bmp =new Bitmap(this .Width,this.Height); BackgroundImage = bmp; Graphics g = Graphics.FromImage(bmp); map.showMap(g); if (map.checkSnake() || map.Snake.IsTouchMyself()) { this.timer1.Enabled = false; MessageBox.Show("失败"); } } private void Form1_Load(object sender, EventArgs e) { SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true); UpdateStyles(); } private void Form1_KeyDown(object sender, KeyEventArgs e) { int k, d = 0; k = e.KeyValue; if (k == 37) { d = 3; } if (k == 40) { d = 2; } if (k == 38) { d = 0; } if (k == 39) { d = 1; } map.Snake.turnDriction(d); } private void 开始游戏ToolStripMenuItem_Click(object sender, EventArgs e) { if (this.label2.Text == "") { MessageBox.Show("请选择级别"); return; } if(this .开始游戏ToolStripMenuItem.Text =="开始") { this.开始游戏ToolStripMenuItem.Text = "重新开始"; timer1.Enabled = true; } else if(this .开始游戏ToolStripMenuItem.Text=="重新开始") { this.开始游戏ToolStripMenuItem.Text = "开始"; this.继续ToolStripMenuItem.Text = "暂停"; this.timer1.Enabled = true; map.Resect(CreateGraphics()); map.score = 0; } } private void 继续ToolStripMenuItem_Click(object sender, EventArgs e) { if (this.继续ToolStripMenuItem.Text == "暂停") { this.继续ToolStripMenuItem.Text = "继续"; this.timer1.Enabled = true; } else if (this.继续ToolStripMenuItem.Text == "继续") { this.继续ToolStripMenuItem.Text = "暂停"; this.timer1.Enabled = false; } } private void 低级ToolStripMenuItem_Click(object sender, EventArgs e) { gradeNum = 200; this.label2.Text = "菜鸟"; this.timer1.Interval = gradeNum; } private void 中级ToolStripMenuItem_Click(object sender, EventArgs e) { gradeNum = 150; this.label2.Text = "平民"; this.timer1.Interval = gradeNum; } private void 高级ToolStripMenuItem_Click(object sender, EventArgs e) { gradeNum = 100; this.label2.Text = "高手"; this.timer1.Interval = gradeNum; } private void 大神ToolStripMenuItem_Click(object sender, EventArgs e) { gradeNum = 50; this.label2.Text = "大神"; this.timer1.Interval = gradeNum; } }
不是自己想的 但是我弄懂了
------------恢复内容结束------------ |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论