C#贪吃蛇

新建一个窗体C#应用




Block.cs里面的代码:

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Timers;

namespace WindowsFormsApplication1
{
    class Block
    {
        private Color _color;  //颜色
        private int _size;   //单位大小
        private Point _point; //坐标

        public Block(Color color, int size, Point p)
        {
            this._color = color;
            this._size = size;
            this._point = p;
        }

        public Point Point
        {
            get { return this._point; }
        }

        //绘制自身到画布
        public virtual void Paint(Graphics g)
        {
            SolidBrush sb = new SolidBrush(_color);
            lock (g)
            {
                try
                {
                    g.FillRectangle(sb,
                        this.Point.X * this._size,
                        this.Point.Y * this._size,
                        this._size - 1,
                        this._size - 1);
                }
                catch
                { }
            }
        }
    }
}



Palette.cs:

using System;
using System.Collections;
using System.Drawing;
using System.Timers;

namespace WindowsFormsApplication1
{
    class Palette
    {
        private int _width = 20;   //宽度
        private int _height = 20;  //高度
        private Color _bgColor;  //背景色
        private Graphics _gpPalette; //画布
        private ArrayList _blocks;   //蛇块列表
        private Direction _direction;  //前进的方向
        private Timer timerBlock;   //更新器
        private Block _food;   //当前食物
        private int _size = 20;   //单位大小
        private int _level = 1;    //游戏等级
        private bool _isGameOver = false;
        private int[] _speed = new int[] { 500, 450, 400, 350, 300, 250, 200, 150, 100, 50 };//游戏速度


        //构造函数很简单就是设定类属性值,还有初始化蛇块列表ArrayList 
        public Palette(int width, int height, int size, Color bgColor, Graphics g, int lvl)  
        {
            this._width = width;
            this._height = height;
            this._size = size; 
            this._bgColor = bgColor;
            this._level = lvl;
            this._gpPalette = g;
            this._blocks = new ArrayList();
            this._blocks.Insert(0, (new Block(Color.Red, this._size, new Point(width / 2, height / 2))));   //这句话就是在画布的正中央产生一个只有一节的贪吃蛇
            this._direction = Direction.Right;
        }

        public Direction Direction
        {
            get 
            {
                return _direction;
            }
            set
            {
                _direction = value;
            }
        }

        //开始游戏
        public void Start()  
        {
            this._food = GetFood();//生成一个食物
            //初始化计时器
            timerBlock = new System.Timers.Timer(_speed[this._level]);
            timerBlock.Elapsed+=new ElapsedEventHandler(OnBlockTimedEvent);
            timerBlock.AutoReset = true;
            timerBlock.Start();
        }


        //定时更新
        private void OnBlockTimedEvent(object source, ElapsedEventArgs e)
        {
            this.Move();//前进一个单位
            if (this.CheckDead()) //检测是否游戏结束
            {
                this.timerBlock.Stop();
                this.timerBlock.Dispose();
                System.Windows.Forms.MessageBox.Show("Score:"+this._blocks.Count,"Game Over");
            }
        }

        //检查游戏是否有戏结束
        private bool CheckDead()
        { 
            Block head = (Block)(this._blocks[0]);// 取蛇块列表的第一个,即蛇头
            //检查是否超出画布范围
            if (head.Point.X < 0 || head.Point.Y<0||head.Point.X >= this._width || head.Point.Y >= this._height)
                return true;
            for (int i = 1; i < this._blocks.Count; i++)   //检查是否撞上自己
            { 
                Block b=(Block)this._blocks[i];
                if (head.Point.X == b.Point.X && head.Point.Y == b.Point.Y)  
                {
                    this._isGameOver = true;
                    return true;
                }
            }
            this._isGameOver = false;
            return false;
        }

        //前进一节
        private void Move()
        {
            Point p;  //下一个坐标位置
            Block head = (Block)(this._blocks[0]);
            if (this._direction == Direction.Up)
                p = new Point(head.Point.X, head.Point.Y - 1);
            else if (this._direction == Direction.Down)
                p = new Point(head.Point.X, head.Point.Y + 1);
            else if (this._direction == Direction.Left)
                p = new Point(head.Point.X - 1, head.Point.Y);
            else
                p = new Point(head.Point.X+1,head.Point.Y);

            //生成新坐标,将来成为蛇头
            Block b = new Block(Color.Blue,this._size,p);

            //如果下一个坐标不是当前食物坐标,那么从蛇块信息列表中删除最后一哥蛇块
            if (this._food.Point!= p)
                this._blocks.RemoveAt(this._blocks.Count - 1);
            //如果下一个坐标和食物坐标重合,那么就成一个新食物
            else
                this._food = this.GetFood();

            //把下一个坐标插入到蛇块信息列表的第一个,使其成为蛇头
            this._blocks.Insert(0,b);

            this.PaintPalette(this._gpPalette); //更新画板

        }

        //生成下一个食物,也就是下一节蛇块
        public Block GetFood()
        {
            Block food = null;
            Random r = new Random();
            bool redo = false;
            while (true)
            {
                redo = false;
                int x = r.Next(this._width);
                int y = r.Next(this._height);
                for (int i = 0; i < this._blocks.Count; i++)  //检查食物所在所标是否和贪吃蛇冲突
                {
                    Block b = (Block)(this._blocks[i]);
                    if (b.Point.X == x && b.Point.Y == y) //有冲突时,再随机找一个坐标
                    {
                        redo = true;
                    }
                }
                if (redo == false)
                {
                    food = new Block(Color.Black,this._size,new Point(x,y));
                    break;
                }
            }
            return food;
        }

        //更新画板PaintPalette函数需要一个参数,也就是绘图句柄,然后在这个画布上画图也就是我们看到的游戏效果
        public void PaintPalette(Graphics gp) 
        {
            gp.Clear(this._bgColor);
            this._food.Paint(gp);
            foreach (Block b in this._blocks)
                b.Paint(gp);
        }
    }

    //枚举四个方向
    public enum Direction
    {
        Left,
        Right,
        Up,
        Down
    }
}

Program.cs  里面自动生成的代码。


  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值