C#坦克大战

上学时候练习画图和随机数做的坦克大战,分享出来给winform学习使用。

下载链接:
https://download.csdn.net/download/zhanglianzhu_91/11055687
在这里插入图片描述
示例代码

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


namespace Tank
{
    public enum Drection {up,low,left,right};
    public partial class Main : Form
    {
        Gride MyGride = new Gride(new Rectangle(20, 20, 500, 600));
        Random random = new Random(DateTime.Now.Second);
        List<Tank> TankList;
        List<Bullet> BulletList;
        Tank human;
        Keys drect = Keys .Shift;
        Keys fire = Keys.Shift ;
        public Main()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Main_Load(object sender, EventArgs e)
        {
            TankList = new List<Tank>();
            BulletList = new List<Bullet>();
            human = new Tank();
            human.RowIndex = 28;
            human.ColIndex = 13;
            human.Drect = Drection.up;
            human.Cold = 2;
            human.TankLife = 3;
            human.IsHuman = true;
            TankList.Add(human);
            for (int i = 0; i < GetRandom(8) + 1; i++)
            {
                TankList.Add(ProduceTank(false));
            }
            MyGride.InitGrid();
            Draw(this.CreateGraphics());
            
            
        }

        /// <summary>
        /// Paint函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Main_Paint(object sender, PaintEventArgs e)
        {
            try
            {
                Draw(this.CreateGraphics());
            }
            catch (Exception)
            {
             
            }
        }

        /// <summary>
        /// 绘图
        /// </summary>
        /// <param name="pDC"></param>
        private void Draw(Graphics pDC)
        {
            MyGride.InitGrid();
            foreach (Tank tank in TankList)
            {
                tank.DrawTank(MyGride);
            }
            foreach (Bullet bullet in BulletList)
            {
                bullet.Draw(MyGride);
            }
            Graphics graphics;
            Bitmap bit=new Bitmap(MyGride.rect.Width+MyGride.rect.X+2,MyGride.rect.Height+MyGride.rect.Y+2);
            graphics = Graphics.FromImage(bit);
            graphics.Clear(this.BackColor);
            MyGride.DrawGrid(graphics);
            pDC.DrawImage(bit,0 ,0);
            bit.Dispose();
            graphics.Dispose();

        }

        /// <summary>
        /// 制造坦克
        /// </summary>
        /// <returns></returns>
        public Tank ProduceTank(bool isHuman)
        {
            l:
            Point point = new Point(GetRandom(MyGride.rowCount-2)+1, GetRandom(MyGride.colCount-2)+1);
            bool canPro = true;
            foreach (Tank tank in TankList)
            {
                if(Math.Abs(tank.RowIndex-point.X)<3||Math.Abs(tank.ColIndex-point.Y)<3)
                {
                    canPro = false;
                    goto l;
                }
            }
            if (canPro)
            {
                
                Tank tank = new Tank();
                tank.RowIndex = point.X;
                tank.ColIndex = point.Y;
                tank.IsHuman = isHuman;
                tank.TankLife = GetRandom(2);
                tank.Cold = 2;
                tank.Drect = GetDrection();
                return tank;
            }
            else
            {
                return null;
            }

        }

        /// <summary>
        /// 获得随机方向
        /// </summary>
        /// <returns></returns>
        public Drection GetDrection()
        {
            int drect = GetRandom(4);
            Drection myDrect;
            if (drect == 0)
            {
                myDrect = Drection.up;
            }
            else if (drect == 1)
            {
                myDrect = Drection.low;
            }
            else if (drect == 2)
            {
                myDrect = Drection.left;
            }
            else
            {
                myDrect = Drection.right;
            }
            return myDrect;
        }
        /// <summary>
        /// 返回一个小于该数的随机数
        /// </summary>
        /// <param name="max"></param>
        /// <returns></returns>
        public int GetRandom(int max)
        {
            return random.Next(max);
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (TankList.Count < 5)
            {
                for (int i = 0; i < GetRandom(2) + 1; i++)
                {
                    TankList.Add(ProduceTank(false));
                }
            }
            foreach (Tank tank in TankList)
            {
                if (tank.IsHuman == false)//电脑坦克
                {

                    if (GetRandom(10) > 7)
                    {
                        if (tank.Cold == 2)
                        {
                            Bullet bullet = tank.Fire(MyGride);
                            BulletList.Add(bullet);
                            tank.Cold = tank.Cold - 2;
                        }
                    }
                    if (GetRandom(20) > 6)
                    {
                        if (tank.Cold == 2)
                        {
                            tank.Move(MyGride);
                        }

                    }
                    if (GetRandom(20) > 2)
                    {
                        if (!tank.CanMove())
                        {
                            tank.Drect = GetDrection();
                        }
                        if (GetRandom(20) > 18)
                        {
                            tank.Drect = GetDrection();
                        }

                    }
                    if (tank.Cold < 2)
                    {
                        tank.Cold++;
                    }
                }
                else//人坦克
                {
                    HumanMove();
                }
            }
           
            foreach(Bullet bullet in BulletList)
            {
                bullet.Move(MyGride);
                if (MyGride.grid[bullet.RowIndex, bullet.ColIndex].Foxed)
                {
                    Tank tank=IsInTank(bullet.RowIndex, bullet.ColIndex);
                    if(bullet.IsHuman!=tank.IsHuman)
                    {
                        tank.TankLife--;
                    }
                }
            }

            for (int i = 0; i < BulletList.Count; i++)
            {
                if (MyGride.grid[BulletList[i].RowIndex, BulletList[i].ColIndex].Foxed)
                {
                    BulletList.RemoveAt(i);
                    continue;
                }
                if (BulletList[i].ColIndex == 0 || BulletList[i].ColIndex == MyGride.colCount - 1 || BulletList[i].RowIndex == 0 || BulletList[i].RowIndex == MyGride.rowCount - 1)
                {
                    MyGride.grid[BulletList[i].RowIndex, BulletList[i].ColIndex].BeUseed = false;
                    BulletList.RemoveAt(i);
                }
                
            }
            for (int i = 0; i < TankList.Count; i++)
            {
                if (TankList[i].TankLife <= 0)
                {
                    if (!TankList[i].IsHuman)
                    {
                        TankList.RemoveAt(i);
                    }
                }
            }
            if (human.TankLife <= 0)
            {
                timer1.Enabled = false;
                MessageBox.Show("你阵亡了","结束提示");
                
            }
            Draw(this.CreateGraphics());

        }

        /// <summary>
        /// 人移动
        /// </summary>
        private void HumanMove()
        {
            if (drect != Keys.Shift)
            {
                human.Move(MyGride);
            }
            if (fire != Keys.Shift)
            {
                if (human.Cold == 2)
                {
                    BulletList.Add(human.Fire(MyGride));
                    human.Cold = human.Cold - 2;
                }
            }
            if (human.Cold < 2)
            {
                human.Cold++;
            }
        }

        /// <summary>
        /// 获得子弹所在的坦克
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        public Tank IsInTank(int row,int col)
        {
            foreach (Tank tank in TankList)
            {
                if (tank.Drect == Drection.left)
                {
                    if (col == tank.ColIndex)
                    {
                        if (Math.Abs(row - tank.RowIndex) <= 1)
                        {
                            return tank;
                        }
                    }
                    if (col == tank.ColIndex - 1)
                    {
                        if (row == tank.RowIndex)
                        {
                            return tank;
                        }
                    }
                    if (col == tank.ColIndex + 1)
                    {
                        if (Math.Abs(row - tank.RowIndex) == 1)
                        {
                            return tank;
                        }
                    }
                }
                if (tank.Drect == Drection.right)
                {
                    if (col == tank.ColIndex)
                    {
                        if (Math.Abs(row - tank.RowIndex) <= 1)
                        {
                            return tank;
                        }
                    }
                    if (col == tank.ColIndex - 1)
                    { 
                        if (Math.Abs(row - tank.RowIndex) == 1)
                        {
                            return tank;
                        }        
                    }
                    if (col == tank.ColIndex + 1)
                    {
                       if (row == tank.RowIndex)
                        {
                            return tank;
                        }
                    }
                }
                if (tank.Drect == Drection.up)
                {
                    if (row == tank.RowIndex)
                    {
                        if (Math.Abs(col - tank.ColIndex) <= 1)
                        {
                            return tank;
                        }
                    }
                    if (row == tank.RowIndex + 1)
                    {
                        if (Math.Abs(col - tank.ColIndex) == 1)
                        {
                            return tank;
                        }
                    }
                    if (row == tank.RowIndex - 1)
                    {
                        if (col == tank.ColIndex)
                        {
                            return tank;
                        }
                    }
                }
                if (tank.Drect == Drection.low)
                {
                    if (row == tank.RowIndex)
                    {
                        if (Math.Abs(col - tank.ColIndex) <= 1)
                        {
                            return tank;
                        }
                    }
                    if (row == tank.RowIndex + 1)
                    { 
                        if (col == tank.ColIndex)
                        {
                            return tank;
                        }
                       
                    }
                    if (row == tank.RowIndex - 1)
                    {
                        if (Math.Abs(col - tank.ColIndex) == 1)
                        {
                            return tank;
                        }
                    }
                }                
            }
            return null;
        }

        private void Main_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                fire = Keys.Space;
                //BulletList.Add(human.Fire(MyGride));
            }
            if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.Down)
            {
                drect = e.KeyCode;
                if(e.KeyCode==Keys.Up)
                {
                    human.Drect = Drection.up;
                }
                if (e.KeyCode == Keys.Down)
                {
                    human.Drect = Drection.low;
                }
                if (e.KeyCode == Keys.Left)
                {
                    human.Drect = Drection.left;
                }
                if (e.KeyCode == Keys.Right)
                {
                    human.Drect = Drection.right;
                }
                //human.Move(MyGride);
            }
            HumanMove();
            Draw(this.CreateGraphics());
        }

        private void Main_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                fire = Keys.Shift;
            }
            if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right || e.KeyCode == Keys.Down)
            {
                drect = Keys.Shift;
            }
            HumanMove();
            Draw(this.CreateGraphics());
        }

        private void btnRestart_Click(object sender, EventArgs e)
        {
            human.TankLife = 3;
            timer1.Enabled = true;
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

        private void btnContiniu_Click(object sender, EventArgs e)
        {
            if (human.TankLife > 0)
            {
                timer1.Enabled = true;
            }
        }

       
    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小乌鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值