c# 貪吃蛇小游戲


------------恢復內容開始------------

新手學習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;
        }
    }

 

不是自己想的 但是我弄懂了

 

------------恢復內容結束------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM