这是写了三天的小游戏,大家下载玩玩,自己暂未发现任何bug
源码下载:
贪吃蛇.zip
(229.18 KB, 下载次数: 4)
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using 贪吃蛇.Util;
- namespace 贪吃蛇.Entities
- {
- /// <summary>
- /// 身体单元类 用来表示组成蛇的每一个身体单元
- /// </summary>
- class SnakeUnit
- {
- #region 身体单元的颜色 默认为红色
- /// <summary>
- /// 颜色
- /// </summary>
- private Color color = Color.Red;
- public Color Color
- {
- get { return color; }
- set { color = value; }
- }
- #endregion
- #region 身体单元的坐标
- /// <summary>
- /// 坐标
- /// </summary>
- private Position pos;
- internal Position Pos
- {
- get { return pos; }
- set { pos = value; }
- }
- #endregion
- #region 构造函数 用于各种方式创建对象
- public SnakeUnit(int rowIndex, int colIndex)
- {
- this.pos.rowIndex = rowIndex;
- this.pos.colIndex = colIndex;
- }
- public SnakeUnit(int rowIndex, int colIndex, Color color)
- : this(rowIndex, colIndex)
- {
- this.color = color;
- }
- public SnakeUnit(Position pos)
- {
- this.pos = pos;
- }
- public SnakeUnit(Position pos, Color color)
- : this(pos)
- {
- this.color = color;
- }
- #endregion
- #region 将当前身体单元所在单元格的颜色填充为当前身体单元对象的颜色
- /// <summary>
- /// 当前身体单元根据自己的坐标和颜色将自己绘制出来
- /// </summary>
- /// <param name="g"></param>
- public void DrawMe(Graphics g)
- {
- //将当前身体单元所在单元格的颜色填充为当前身体单元对象的颜色
- Brush brush = new SolidBrush(this.color);
- g.FillRectangle(brush, new Rectangle(
- this.pos.colIndex * CommonHelper.SquLength + 1,
- this.pos.rowIndex * CommonHelper.SquLength + 1,
- CommonHelper.SquLength - 1,
- CommonHelper.SquLength - 1
- ));
- }
- #endregion
- #region 身体但单元的前进方向 默认向左
- private Direction dir = Direction.Left;
- internal Direction Dir
- {
- get { return dir; }
- set { dir = value; }
- }
- #endregion
- #region 身体单元向左移动
- /// <summary>
- /// 身体单元方向向走移动 其实就是身体单元的列坐标-1
- /// </summary>
- public void MoveToLeft()
- {
- this.pos.colIndex--;
- }
- #endregion
- #region 身体单元向上移动
- /// <summary>
- /// 身体单元方向向上移动 其实就是身体单元的行坐标-1
- /// </summary>
- public void MoveToUp()
- {
- this.pos.rowIndex--;
- }
- #endregion
- #region 身体单元向右移动
- /// <summary>
- /// 身体单元方向向右移动的方法 其实就是身体单元的列坐标+1
- /// </summary>
- public void MoveToRight()
- {
- this.pos.colIndex++;
- }
- #endregion
- #region 身体单元向下移动
- /// <summary>
- /// 身体单元方向向下移动的方向 其实就是身体单元的行坐标+1
- /// </summary>
- public void MovetoDown()
- {
- this.pos.rowIndex++;
- }
- #endregion
- #region 身体单元擦除自己的方法
- /// <summary>
- /// 身体单元自己擦除自己方法
- /// </summary>
- /// <param name="g"></param>
- public void WipeMe(Graphics g)
- {
- //将当前身体单元所在的单元格的颜色填充为背景色
- Brush brush = new SolidBrush(CommonHelper.GroundColor);
- g.FillRectangle(brush, new Rectangle(
- this.pos.colIndex * CommonHelper.SquLength + 1,
- this.pos.rowIndex * CommonHelper.SquLength + 1,
- CommonHelper.SquLength - 1,
- CommonHelper.SquLength - 1
- ));
- }
- #endregion
- #region 身体单元移动的旧方向
- private Direction oldDir = Direction.Left;
- internal Direction OldDir
- {
- get { return oldDir; }
- set { oldDir = value; }
- }
- #endregion
- }
- }
复制代码
|