zoukankan      html  css  js  c++  java
  • 随手写 --- 贪吃蛇

    public class BodyCell : PictureBox
        {
            public Direction direct
            { get; set; }
    
            public BodyCell()
                : base()
            {
                this.Width = 10;
                this.Height = 10;
                this.BackColor = Color.Black;
                direct = Direction.Right;
            }
        }
    
    
    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 Snake
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public int bodyCount = 5;
            public Direction direct = Direction.Right;
    
            //public BodyCell[] cells;
            public List<BodyCell> cells;
            public PictureBox food;
    
            private void Form1_Load(object sender, EventArgs e)
            {
                cells = new List<BodyCell>();
    
                for (int i = 0; i < bodyCount; i++)
                {
    
                    BodyCell cell = new BodyCell();
                    cell.Left = i * 10;
                    cell.Top = panel1.Height / 2;
                    cells.Add(cell);
                    panel1.Controls.Add(cell);
                }
    
                food = new PictureBox();
                food.BackColor = Color.Black;
                food.Height = 10;
                food.Width = 10;
                Random rY = new Random();
                food.Top = rY.Next(0, 30) * 10;
                Random rX = new Random();
                food.Left = rX.Next(0, 30) * 10;
                panel1.Controls.Add(food);
            }
    
            private void Timer_Process_Tick(object sender, EventArgs e)
            {
                SnakeProcess();
    
                if (GameOver())
                {
                    Timer_Process.Enabled = false;
                    MessageBox.Show("Game Over");
                }
            }
    
            private void SnakeProcess()
            {
                #region  移动
                for (int i = bodyCount - 1; i >= 0; i--)
                {
                    if (cells[i].direct == Direction.Right)
                    {
                        cells[i].Left += 10;
                    }
                    else if (cells[i].direct == Direction.Left)
                    {
                        cells[i].Left -= 10;
                    }
                    else if (cells[i].direct == Direction.Up)
                    {
                        cells[i].Top -= 10;
                    }
                    else if (cells[i].direct == Direction.Down)
                    {
                        cells[i].Top += 10;
                    }
                }
    
                for (int i = 0; i < bodyCount - 1; i++)
                {
                    cells[i].direct = cells[i + 1].direct;
                }
                #endregion
    
                #region 吃食物
                EatFood();
                #endregion
            }
    
            private bool GameOver()
            {
                bool isOver = false;
                //撞墙
                if (cells[bodyCount - 1].Top == 0 && cells[bodyCount - 1].direct == Direction.Up)
                {
                    isOver = true;
                }
                if (cells[bodyCount - 1].Top == 300 && cells[bodyCount - 1].direct == Direction.Down)
                {
                    isOver = true;
                }
                if (cells[bodyCount - 1].Left == 0 && cells[bodyCount - 1].direct == Direction.Left)
                {
                    isOver = true;
                }
                if (cells[bodyCount - 1].Left == 300 && cells[bodyCount - 1].direct == Direction.Right)
                {
                    isOver = true;
                }
    
                //撞自己
                for (int i = bodyCount - 2; i >= 0; i--)
                {
                    if (cells[i].Top == cells[bodyCount - 1].Top && cells[i].Left == cells[bodyCount - 1].Left)
                    {
                        isOver = true;
                        break;
                    }
                }
                return isOver;
            }
    
            private void EatFood()
            {
                BodyCell head = cells[bodyCount - 1];
                if (cells[bodyCount - 1].Top == food.Top && cells[bodyCount - 1].Left == food.Left)
                {
                    BodyCell cell = new BodyCell();
                    panel1.Controls.Add(cell);
                    cell.direct = head.direct;
                    if (head.direct == Direction.Up)
                    {
                        cell.Top = head.Top - 10;
                        cell.Left = head.Left;
                    }
                    else if (head.direct == Direction.Down)
                    {
                        cell.Top = head.Top + 10;
                        cell.Left = head.Left;
    
                    }
                    else if (head.direct == Direction.Left)
                    {
                        cell.Top = head.Top;
                        cell.Left = head.Left - 10;
                    }
                    else if (head.direct == Direction.Right)
                    {
                        cell.Top = head.Top;
                        cell.Left = head.Left + 10;
                    }
                    cells.Add(cell);
    
                    bodyCount++;
    
                    panel1.Controls.Remove(food);
    
                    Random rY = new Random();
                    food.Top = rY.Next(0, 30) * 10;
                    Random rX = new Random();
                    food.Left = rX.Next(0, 30) * 10;
                    panel1.Controls.Add(food);
                }
            }
    
            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Up && cells[bodyCount - 1].direct != Direction.Down)
                {
                    //direct = Direction.Up;
                    cells[bodyCount - 1].direct = Direction.Up;
                }
                else if (e.KeyCode == Keys.Left && cells[bodyCount - 1].direct != Direction.Right)
                {
                    //direct = Direction.Left;
                    cells[bodyCount - 1].direct = Direction.Left;
                }
                else if (e.KeyCode == Keys.Down && cells[bodyCount - 1].direct != Direction.Up)
                {
                    //direct = Direction.Down;
                    cells[bodyCount - 1].direct = Direction.Down;
                }
                else if (e.KeyCode == Keys.Right && cells[bodyCount - 1].direct != Direction.Left)
                {
                    //direct = Direction.Right;
                    cells[bodyCount - 1].direct = Direction.Right;
                }
            }
        }
    
        public enum Direction
        {
            Up,
            Down,
            Left,
            Right
        }
    }
    

      

  • 相关阅读:
    身份证验证(c#和js)
    获取焦点问题
    关于加载设计器遇到一个或多个错误问题的解决方案
    关于如何使用自定义的结束消息循环的方式 (转载)
    多种重要源码下载
    关于线程同步(转载)
    ArrayList的使用技巧
    一些所谓有利于家庭生活的优点
    080801 30℃
    080731 31℃
  • 原文地址:https://www.cnblogs.com/september-bk/p/4043661.html
Copyright © 2011-2022 走看看