贪吃蛇-源码(待完善)

粗略的雏形:

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

namespace MineSweeperSharp
{
    public partial class FrmSnakeGame : Form
    {
        public FrmSnakeGame()
        {
            InitializeComponent();
            NewGame();
        }

        //贪吃蛇类
        public class Snake : Button
        { 
            //属性,移动规则
            public int No;

            public Snake(int in_No)
            {
                No = in_No;
                Size = new Size(20,20);
            }

            //增长身体
            public void Growth()
            { 

            }
        }

        Snake[] snakes=new Snake[10];

        //果子类
        public class Fruit : Label
        { 
            
        }

        #region 新游戏
        public Snake snakeHead;
        Timer timer;
        public void NewGame()
        {

            SetFrom();//设置主窗体
            snakeHead = new Snake(0);
            snakeHead.KeyUp += snakeHead_KeyUp;
            snakeHead.Location = new Point(this.Width/2,this.Height/2);
            //设置蛇头的坐标
            snake_x = this.Width / 2;
            snake_y= this.Height / 2;
            timer = new Timer();
            timer.Tick += timer_Tick;
            timer.Enabled = true;
            //移动速度
            timer.Interval = 20;
            Controls.Add(snakeHead);
            SetFruit();
        }
        #endregion

        #region 设置主窗体
        //设置主窗体
        public void SetFrom()
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Width = 500;
            this.Height = 500;
        }
        #endregion

        #region 随机生成的果子
        //随机生成的果子
        Fruit fruit;
        private void SetFruit()
        {
            Random rd = new Random();
            int f_x = rd.Next(0,this.Width-0);
            int f_y = rd.Next(0,this.Height-0);

            fruit = new Fruit();
            fruit.Size = new Size(10, 10);
            fruit.Location = new Point(f_x, f_y);
            fruit.BackColor = Color.Red;
            Controls.Add(fruit);
        }
        #endregion

        #region 键盘“上下左右”事件
        //响应键盘“上下左右”事件
        void snakeHead_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            { 
                case Keys.Up:
                    if (keys.Equals("下"))
                        break;
                    keys = "上";
                    break;
                case Keys.Down:
                    if (keys.Equals("上"))
                        break;
                     keys = "下";
                    break;
                case Keys.Left:
                    if (keys.Equals("右"))
                        break;
                    keys = "左";
                    break;
                case Keys.Right:
                    if (keys.Equals("左"))
                        break;
                    keys = "右";
                    break;
            }
        }
        #endregion

        //蛇头的坐标
         int snake_x = 0;
         int snake_y = 0;
         int hou_x = 0;
         int hou_y = 0;

         int hou_x2 = 0;
         int hou_y2 = 0;
        //控制方向
        string keys = "下";
        int sudu = 0;
        int sudu2 = 0;
        void timer_Tick(object sender, EventArgs e)
        {
           
            switch (keys)
            {
                case "上":
                    snake_y -= 5;
                    break;
                case "下":
                    snake_y += 5;
                    break;
                case "左":
                    snake_x -= 5;
                    break;
                case "右":
                    snake_x += 5;
                    break;
            }

            if (sudu == 0)
            {
                hou_x = snake_x;
                hou_y = snake_y;
 
            }
            if (sudu2 == 5)
            {
                hou_x2 = hou_x;
                hou_y2 = hou_y;
            }
            sudu2++;
            sudu++;
            snakeHead.Location = new Point(snake_x, snake_y);
            if (sudu == 5)
            {
                for (int i = 0; i < snakes.Length; i++)
                {
                    if (snakes[i] != null)
                    {
                        if (i == 0)
                        {
                            snakes[i].Location = new Point(hou_x, hou_y);
                        }
                    }
                }
                sudu = 0;
            }
            
            if (sudu2 == 10)
            {
                for (int i = 0; i < snakes.Length; i++)
                {
                    if (snakes[i] != null)
                    {
                        if (i == 1)
                        {
                            snakes[i].Location = new Point(hou_x2, hou_y2);
                        }
                    }
                }
                sudu2 = 0;
            }


            int f_L_T_x = fruit.Location.X;     //果子的左上角点的x轴
            int f_L_T_y = fruit.Location.Y;     //果子的左上角点的y轴

            int f_R_T_x = fruit.Location.X + fruit.Width;//果子右上角点的x轴
            int f_L_B_y = fruit.Location.Y + fruit.Height;//果子左下角点的y轴

            int s_L_T_x = snake_x; //蛇头左上角点的x轴
            int s_L_T_y = snake_y;//蛇头左上角点的y轴

            int s_R_T_x = snake_x + snakeHead.Width;
            int s_L_B_y = snake_y + snakeHead.Height;

            //艹!这两行代码想了我2个钟!!!
            if (s_L_T_x <= f_R_T_x && s_L_T_x >= f_R_T_x - (snakeHead.Width + fruit.Width) &&
               s_L_T_y <= f_L_B_y && s_L_T_y >= f_L_B_y - (snakeHead.Height + fruit.Height))
            {
                this.Controls.Remove(fruit);
                SetFruit();
                EatedFruit();
            }
            //蛇头撞墙
            SetFangXiang();
        }

        //吃到果子后
        private void EatedFruit()
        {
            for (int i = 0; i < 2; i++)
            {
                if (snakes[i] == null)
                {
                    snakes[i] = new Snake(i);
                    snakes[i].Location = new Point(snake_x , snake_y);
                    snakes[i].Enabled = false;
                    snakes[i].BackColor = Color.Green;
                    Controls.Add(snakes[i]);
                    return;
                }
            }

        }

       

        private void SetFangXiang()
        {
            //控制方向
            if (snakeHead.Location.X + snakeHead.Width < 0)
            {
                snake_x = this.Width - snakeHead.Width;
            }
            else if (snakeHead.Location.Y < 0)
            {
                snake_y = this.Height - snakeHead.Height;
            }
            else if (snakeHead.Location.X > this.Width)
            {
                snake_x = 0;
            }
            else if (snakeHead.Location.Y + snakeHead.Height > this.Height)
            {
                snake_y = 0;
            }
        }

    }
}

你可能感兴趣的:(源码,winform,C#)