C#—贪吃蛇

闲着晚上,写个贪吃蛇吧,冗余的界面功能没时间做啦,就写个主面板的内容吧。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static MyGreedySnake.Direction;

namespace MyGreedySnake
{
    public partial class Form1 : Form
    {
        private int difficultLevel =0;
        static MDirection direction = Direction.MDirection.right;
        static bool gameover = false;
        private BasicBlock food;
        private BasicBlock head;
        private List<BasicBlock> bodies = new List<BasicBlock>();
        BasicPane pane;
        List<Image> headImgs = new List<Image>();
        ThreadStart threadStart = null;
        Thread thread = null;
        public Form1()
        {
            InitializeComponent();
            pane = new BasicPane();
            this.Controls.Add(pane);
            pane.Dock = DockStyle.Fill;

            this.FormBorderStyle = FormBorderStyle.None;
            this.Size = new Size(1000, 800);
            this.Opacity = 0.5;
            this.ControlBox = false;
            this.BackgroundImage = Image.FromFile(@"Image\背景.png");
            Image image = Image.FromFile(@"Image\红心.jpg");
            food = new BasicBlock(100, 100, image);
            pane.Controls.Add(food);

            image = Image.FromFile(@"Image\蛇头1.png");
            headImgs.Add(image);
            head = new BasicBlock(500, 400, image);
            image = Image.FromFile(@"Image\蛇头2.png");
            headImgs.Add(image);
            image = Image.FromFile(@"Image\蛇头3.png");
            headImgs.Add(image);
            image = Image.FromFile(@"Image\蛇头4.png");
            headImgs.Add(image);
            bodies.Add(head);
            pane.Controls.Add(head);

            ReFreshFoodPos();

            //创建线程和委托
            threadStart = new ThreadStart(Move);
            thread = new Thread(threadStart);
            thread.IsBackground = true;

            thread.Start();
        }

       private bool CheckGameOver()
        {
            bool gameover = false;
            if (head.Location.X < 0 || head.Location.X > 990 || head.Location.Y < 0 || head.Location.Y > 790)
                gameover = true;
            if (bodies.Count > 1)
            {
                for(int i = 1; i <bodies.Count;i++)
                {
                    if (head.Location == bodies[i].Location)
                        gameover = true;
                }
            }
            return gameover;
        }
         private void ReFreshFoodPos()
        {
            Random random = new Random();
            bool validPosition = false;
            int x = random.Next(10, 990);
            int y = random.Next(10, 790);
            while (!validPosition)
            {
                validPosition = true;
                foreach(var item in bodies)
                {
                    if(x == item.Location.X && y == item.Location.Y)
                    {
                        validPosition = false;
                        break;
                    }
                }
            }
            food.Location = new Point(x, y);
        }

        public void Move()
        {
            while(!gameover)
            {
                int x = head.Location.X;
                int y = head.Location.Y;    
                head.move();
                if (!CheckEaten(x,y))
                {
                    if (bodies.Count > 1)
                    {
                        for (int i = bodies.Count; i > 0; i--)
                        {
                            if (i != 1)
                            {
                                bodies[i - 1].Location = bodies[i - 2].Location;
                                bodies[i - 1].direction = bodies[i - 2].direction;
                            }
                            else
                            {
                                bodies[1].Location = new Point(x, y);
                                bodies[1].direction = head.direction;
                            }

                        }
                    }
                }
                int time = 0;
                if (difficultLevel == 0)
                    time = 200;
                Thread.Sleep(time);
                gameover = CheckGameOver();
            }
            if (gameover)
            {
                DialogResult resault = MessageBox.Show("You Failed!","Information",MessageBoxButtons.OK);
                if(resault == DialogResult.OK)
                    this.Close();
            }
        }
        public bool CheckEaten(int x, int y)
        {
            bool eaten = false;
            if(Math.Abs(head.Location.X - food.Location.X) < 20 && Math.Abs(head.Location.Y - food.Location.Y) < 20)
            {
                Image image = Image.FromFile(@"Image\方块.jpg");
                BasicBlock body = new BasicBlock(x, y, image);
                body.direction = head.direction;
                this.pane.Invoke((MethodInvoker)delegate { AddObjectToForm(body); });
                eaten = true;
            }
            return eaten;
        }

        private void AddObjectToForm(object myObject)
        {
            // 将对象添加到UI控件中
            this.pane.Controls.Add(myObject as BasicBlock);
            bodies.Insert(1,myObject as BasicBlock);
            ReFreshFoodPos();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {

            BasicBlock item = null;
            if (bodies.Count > 1)
                item = bodies[1];
            switch(e.KeyCode)
            {
                case Keys.Right:
                    if (item == null || item.direction != MDirection.left)
                    {
                        head.direction = MDirection.right;
                        head.BackgroundImage = headImgs[0];
                    }
                    break;
                case Keys.Left:
                    if (item == null || item.direction != MDirection.right)
                    {
                        head.direction = MDirection.left;
                        head.BackgroundImage = headImgs[2];
                    }
                    break;
                case Keys.Up:
                    if (item == null || item.direction != MDirection.down)
                    {
                        head.direction = MDirection.up;
                        head.BackgroundImage = headImgs[3];
                    }
                    break;
                case Keys.Down:
                    if (item == null || item.direction != MDirection.up)
                    {
                        head.direction = MDirection.down;
                        head.BackgroundImage = headImgs[1];
                    }
                    break;
                default:
                    break;
            }
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值